summaryrefslogtreecommitdiff
path: root/Examples/python/multimap/example.i
diff options
context:
space:
mode:
Diffstat (limited to 'Examples/python/multimap/example.i')
-rw-r--r--Examples/python/multimap/example.i47
1 files changed, 37 insertions, 10 deletions
diff --git a/Examples/python/multimap/example.i b/Examples/python/multimap/example.i
index f1c4d9990..3f6fc3db3 100644
--- a/Examples/python/multimap/example.i
+++ b/Examples/python/multimap/example.i
@@ -38,27 +38,44 @@ extern int gcd(int x, int y);
}
%#if PY_VERSION_HEX >= 0x03000000
{
- int l;
- $2[i] = PyUnicode_AsStringAndSize(s, &l);
+ PyObject *utf8str = PyUnicode_AsUTF8String(s);
+ const char *cstr = PyBytes_AsString(utf8str);
+ $2[i] = strdup(cstr);
+ Py_DECREF(utf8str);
}
%#else
$2[i] = PyString_AsString(s);
%#endif
-
}
$2[i] = 0;
}
+%typemap(freearg) (int argc, char *argv[]) {
+%#if PY_VERSION_HEX >= 0x03000000
+ int i;
+ for (i = 0; i < $1; i++) {
+ free($2[i]);
+ }
+%#endif
+}
+
extern int gcdmain(int argc, char *argv[]);
%typemap(in) (char *bytes, int len) {
%#if PY_VERSION_HEX >= 0x03000000
+ char *cstr;
+ Py_ssize_t len;
+ PyObject *utf8str;
if (!PyUnicode_Check($input)) {
PyErr_SetString(PyExc_ValueError,"Expected a string");
return NULL;
}
- $1 = PyUnicode_AsStringAndSize($input, &$2);
+ utf8str = PyUnicode_AsUTF8String($input);
+ PyBytes_AsStringAndSize(utf8str, &cstr, &len);
+ $1 = strndup(cstr, (size_t)len);
+ $2 = (int)len;
+ Py_DECREF(utf8str);
%#else
if (!PyString_Check($input)) {
PyErr_SetString(PyExc_ValueError,"Expected a string");
@@ -69,6 +86,12 @@ extern int gcdmain(int argc, char *argv[]);
%#endif
}
+%typemap(freearg) (char *bytes, int len) {
+%#if PY_VERSION_HEX >= 0x03000000
+ free($1);
+%#endif
+}
+
extern int count(char *bytes, int len, char c);
@@ -79,13 +102,17 @@ extern int count(char *bytes, int len, char c);
%typemap(in) (char *str, int len) {
%#if PY_VERSION_HEX >= 0x03000000
- $2 = PyUnicode_GetSize($input);
- $1 = (char *) malloc($2+1);
- memmove($1,PyUnicode_AsString($input),$2);
+ char *cstr;
+ Py_ssize_t len;
+ PyObject *utf8str = PyUnicode_AsUTF8String($input);
+ PyBytes_AsStringAndSize(utf8str, &cstr, &len);
+ $1 = strndup(cstr, (size_t)len);
+ $2 = (int)len;
+ Py_DECREF(utf8str);
%#else
- $2 = PyString_Size($input);
- $1 = (char *) malloc($2+1);
- memmove($1,PyString_AsString($input),$2);
+ $2 = PyString_Size($input);
+ $1 = (char *) malloc($2+1);
+ memmove($1,PyString_AsString($input),$2);
%#endif
}