On 6/25/19 3:47 PM, Eric Blake wrote:
On 6/25/19 12:11 PM, Richard W.M. Jones wrote:
> Mutable (Int n) => int *n
>
> This can currently only be used for callback arguments of type int
> (not for other types, nor for any ordinary function arguments), but it
> could be implemented more generally in future.
> ---
> generator/generator | 75 +++++++++++++++++++++++++++++++++++++--------
> 1 file changed, 63 insertions(+), 12 deletions(-)
>
> @@ -3261,14 +3265,20 @@ let print_python_binding name { args; ret } =
> pr " for (size_t i = 0; i < %s; ++i)\n" len;
> pr " PyList_SET_ITEM (py_%s, i, PyLong_FromUnsignedLong
(%s[i]));\n" n n
> | BytesIn _ -> ()
> + | Mutable (Int n) ->
> + pr " PyObject *py_%s_dict = PyImport_GetModuleDict ();\n" n;
> + pr " PyObject *py_%s_mod = PyMapping_GetItemString (py_%s_dict,
\"ctypes\");\n" n n;
> + pr " PyObject *py_%s = PyObject_CallMethod (py_%s_mod,
\"c_int\", \"i\", *%s);\n" n n n
PyMapping_GetItemString() is returning NULL, then the program is
segfaulting on PyObject_CallMethod. This code needs some error checking
to be safe, as well as a tweak to more properly call into Python (did we
forget an earlier global import of the ctypes module?).
Here's what I squashed in to make it work in Python 3 (if we want to
support Python 2, we also need a configure check for
PyString_From_String and use that instead - but I'm of the opinion that
python 2 is close enough to death that libnbd need not worry about it,
even if nbdkit still supports it).
diff --git i/generator/generator w/generator/generator
index fadbdfc..b5c4e9a 100755
--- i/generator/generator
+++ w/generator/generator
@@ -3380,9 +3380,13 @@ let print_python_binding name { args; ret } =
| BytesIn _
| Int _ -> ()
| Mutable (Int n) ->
- pr " PyObject *py_%s_dict = PyImport_GetModuleDict ();\n" n;
- pr " PyObject *py_%s_mod = PyMapping_GetItemString
(py_%s_dict, \"ctypes\");\n" n n;
- pr " PyObject *py_%s = PyObject_CallMethod (py_%s_mod,
\"c_int\", \"i\", *%s);\n" n n n
+ pr " PyObject *py_%s_modname = PyUnicode_FromString
(\"ctypes\");\n" n;
+ pr " if (!py_%s_modname) { PyErr_PrintEx (0); return -1;
}\n" n;
+ pr " PyObject *py_%s_mod = PyImport_Import
(py_%s_modname);\n" n n;
+ pr " Py_DECREF (py_%s_modname);\n" n;
+ pr " if (!py_%s_mod) { PyErr_PrintEx (0); return -1; }\n" n;
+ pr " PyObject *py_%s = PyObject_CallMethod (py_%s_mod,
\"c_int\", \"i\", *%s);\n" n n n;
+ pr " if (!py_%s) { PyErr_PrintEx (0); return -1; }\n" n;
| Opaque n ->
pr " struct %s_%s_data *_data = %s;\n" name cb_name n
| String n
@@ -3460,7 +3464,7 @@ let print_python_binding name { args; ret } =
| ArrayAndLen (UInt32 n, _) ->
pr " Py_DECREF (py_%s);\n" n
| Mutable (Int n) ->
- pr " PyObject *py_%s_ret = PyObject_CallMethod (py_%s,
\"value\", \"\");\n" n n;
+ pr " PyObject *py_%s_ret = PyObject_GetAttrString (py_%s,
\"value\");\n" n n;
pr " *%s = PyLong_AsLong (py_%s_ret);\n" n n;
pr " Py_DECREF (py_%s_ret);\n" n;
pr " Py_DECREF (py_%s);\n" n
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3226
Virtualization:
qemu.org |
libvirt.org