python: Make the code compatibly with 3.2

The previous change used some 3.3-specific functions. We still support 3.2 so revert to 3.2-compatible calls. Reported-by: 's avatarS.Çağlar Onur <caglar@10ur.org> Signed-off-by: 's avatarStéphane Graber <stgraber@ubuntu.com> Acked-by: 's avatarSerge E. Hallyn <serge.hallyn@ubuntu.com>
parent 6516ad8b
...@@ -49,18 +49,27 @@ convert_tuple_to_char_pointer_array(PyObject *argv) { ...@@ -49,18 +49,27 @@ convert_tuple_to_char_pointer_array(PyObject *argv) {
assert(pyobj != NULL); assert(pyobj != NULL);
char *str = NULL; char *str = NULL;
PyObject *pystr = NULL;
if (!PyUnicode_Check(pyobj)) { if (!PyUnicode_Check(pyobj)) {
PyErr_SetString(PyExc_ValueError, "Expected a string"); PyErr_SetString(PyExc_ValueError, "Expected a string");
goto error; goto error;
} }
str = PyUnicode_AsUTF8(pyobj); pystr = PyUnicode_AsUTF8String(pyobj);
if (!str) { if (!pystr) {
/* Maybe it wasn't UTF-8 encoded. An exception is already set. */ /* Maybe it wasn't UTF-8 encoded. An exception is already set. */
goto error; goto error;
} }
str = PyBytes_AsString(pystr);
if (!str) {
/* Maybe pystr wasn't a valid object. An exception is already set.
*/
Py_DECREF(pystr);
goto error;
}
/* We must make a copy of str, because it points into internal memory /* We must make a copy of str, because it points into internal memory
* which we do not own. Assume it's NULL terminated, otherwise we'd * which we do not own. Assume it's NULL terminated, otherwise we'd
* have to use PyUnicode_AsUTF8AndSize() and be explicit about copying * have to use PyUnicode_AsUTF8AndSize() and be explicit about copying
...@@ -71,6 +80,7 @@ convert_tuple_to_char_pointer_array(PyObject *argv) { ...@@ -71,6 +80,7 @@ convert_tuple_to_char_pointer_array(PyObject *argv) {
/* Do not decref pyobj since we stole a reference by using /* Do not decref pyobj since we stole a reference by using
* PyTuple_GET_ITEM(). * PyTuple_GET_ITEM().
*/ */
Py_DECREF(pystr);
if (result[i] == NULL) { if (result[i] == NULL) {
PyErr_SetNone(PyExc_MemoryError); PyErr_SetNone(PyExc_MemoryError);
goto error; goto error;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment