Pino Toscano
2017-May-09 13:48 UTC
[Libguestfs] [PATCH v2] python: add simple wrappers for PyObject<->string functions
The current need for #ifdef's based on the presence of PyString_FromString makes both the OCaml code of the generator, and the generated C code a mess to read. Hence, add three simple wrappers to make both the OCaml, and C code more readable, and easier to tweak in the future. --- generator/python.ml | 72 ++++++++++++----------------------------------------- python/handle.c | 65 ++++++++++++++++++++++++++--------------------- 2 files changed, 53 insertions(+), 84 deletions(-) diff --git a/generator/python.ml b/generator/python.ml index 0162733..cf08294 100644 --- a/generator/python.ml +++ b/generator/python.ml @@ -91,6 +91,9 @@ extern PyObject *guestfs_int_py_event_to_string (PyObject *self, PyObject *args) extern char **guestfs_int_py_get_string_list (PyObject *obj); extern PyObject *guestfs_int_py_put_string_list (char * const * const argv); extern PyObject *guestfs_int_py_put_table (char * const * const argv); +extern PyObject *guestfs_int_py_fromstring (const char *str); +extern PyObject *guestfs_int_py_fromstringsize (const char *str, size_t size); +extern char *guestfs_int_py_asstring (PyObject *obj); "; @@ -178,31 +181,16 @@ and generate_python_structs () function | name, FString -> pr " PyDict_SetItemString (dict, \"%s\",\n" name; - pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; - pr " PyString_FromString (%s->%s));\n" - typ name; - pr "#else\n"; - pr " PyUnicode_FromString (%s->%s));\n" - typ name; - pr "#endif\n" + pr " guestfs_int_py_fromstring (%s->%s));\n" + typ name | name, FBuffer -> pr " PyDict_SetItemString (dict, \"%s\",\n" name; - pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; - pr " PyString_FromStringAndSize (%s->%s, %s->%s_len));\n" - typ name typ name; - pr "#else\n"; - pr " PyBytes_FromStringAndSize (%s->%s, %s->%s_len));\n" - typ name typ name; - pr "#endif\n" + pr " guestfs_int_py_fromstringsize (%s->%s, %s->%s_len));\n" + typ name typ name | name, FUUID -> pr " PyDict_SetItemString (dict, \"%s\",\n" name; - pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; - pr " PyString_FromStringAndSize (%s->%s, 32));\n" - typ name; - pr "#else\n"; - pr " PyBytes_FromStringAndSize (%s->%s, 32));\n" - typ name; - pr "#endif\n" + pr " guestfs_int_py_fromstringsize (%s->%s, 32));\n" + typ name | name, (FBytes|FUInt64) -> pr " PyDict_SetItemString (dict, \"%s\",\n" name; pr " PyLong_FromUnsignedLongLong (%s->%s));\n" @@ -229,15 +217,9 @@ and generate_python_structs () pr " PyDict_SetItemString (dict, \"%s\", Py_None);\n" name; pr " }\n" | name, FChar -> - pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; pr " PyDict_SetItemString (dict, \"%s\",\n" name; - pr " PyString_FromStringAndSize (&%s->%s, 1));\n" - typ name; - pr "#else\n"; - pr " PyDict_SetItemString (dict, \"%s\",\n" name; - pr " PyUnicode_FromStringAndSize (&%s->%s, 1));\n" - typ name; - pr "#endif\n" + pr " guestfs_int_py_fromstringsize (&%s->%s, 1));\n" + typ name ) cols; pr " return dict;\n"; pr "};\n"; @@ -419,13 +401,7 @@ and generate_python_actions actions () pr " optargs_s.%s = PyLong_AsLongLong (py_%s);\n" n n; pr " if (PyErr_Occurred ()) goto out;\n" | OString _ -> - pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; - pr " optargs_s.%s = PyString_AsString (py_%s);\n" n n; - pr "#else\n"; - pr " PyObject *bytes;\n"; - pr " bytes = PyUnicode_AsUTF8String (py_%s);\n" n; - pr " optargs_s.%s = PyBytes_AS_STRING (bytes);\n" n; - pr "#endif\n"; + pr " optargs_s.%s = guestfs_int_py_asstring (py_%s);\n" n n | OStringList _ -> pr " optargs_s.%s = guestfs_int_py_get_string_list (py_%s);\n" n n; pr " if (!optargs_s.%s) goto out;\n" n; @@ -480,30 +456,18 @@ and generate_python_actions actions () | RBool _ -> pr " py_r = PyLong_FromLong ((long) r);\n" | RInt64 _ -> pr " py_r = PyLong_FromLongLong (r);\n" | RConstString _ -> - pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; - pr " py_r = PyString_FromString (r);\n"; - pr "#else\n"; - pr " py_r = PyUnicode_FromString (r);\n"; - pr "#endif\n"; + pr " py_r = guestfs_int_py_fromstring (r);\n"; pr " if (py_r == NULL) goto out;\n"; | RConstOptString _ -> pr " if (r) {\n"; - pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; - pr " py_r = PyString_FromString (r);\n"; - pr "#else\n"; - pr " py_r = PyUnicode_FromString (r);\n"; - pr "#endif\n"; + pr " py_r = guestfs_int_py_fromstring (r);\n"; pr " } else {\n"; pr " Py_INCREF (Py_None);\n"; pr " py_r = Py_None;\n"; pr " }\n"; pr " if (py_r == NULL) goto out;\n"; | RString _ -> - pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; - pr " py_r = PyString_FromString (r);\n"; - pr "#else\n"; - pr " py_r = PyUnicode_FromString (r);\n"; - pr "#endif\n"; + pr " py_r = guestfs_int_py_fromstring (r);\n"; pr " free (r);\n"; pr " if (py_r == NULL) goto out;\n"; | RStringList _ -> @@ -519,11 +483,7 @@ and generate_python_actions actions () pr " py_r = guestfs_int_py_put_table (r);\n"; pr " guestfs_int_free_string_list (r);\n" | RBufferOut _ -> - pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; - pr " py_r = PyString_FromStringAndSize (r, size);\n"; - pr "#else\n"; - pr " py_r = PyBytes_FromStringAndSize (r, size);\n"; - pr "#endif\n"; + pr " py_r = guestfs_int_py_fromstringsize (r, size);\n"; pr " free (r);\n"; pr " if (py_r == NULL) goto out;\n"; ); diff --git a/python/handle.c b/python/handle.c index 806408f..f347c00 100644 --- a/python/handle.c +++ b/python/handle.c @@ -241,11 +241,7 @@ guestfs_int_py_event_to_string (PyObject *self, PyObject *args) return NULL; } -#ifdef HAVE_PYSTRING_ASSTRING - py_r = PyString_FromString (str); -#else - py_r = PyUnicode_FromString (str); -#endif + py_r = guestfs_int_py_fromstring (str); free (str); return py_r; @@ -298,9 +294,6 @@ guestfs_int_py_get_string_list (PyObject *obj) { size_t i, len; char **r; -#ifndef HAVE_PYSTRING_ASSTRING - PyObject *bytes; -#endif assert (obj); @@ -321,14 +314,8 @@ guestfs_int_py_get_string_list (PyObject *obj) return NULL; } - for (i = 0; i < len; ++i) { -#ifdef HAVE_PYSTRING_ASSTRING - r[i] = PyString_AsString (PyList_GetItem (obj, i)); -#else - bytes = PyUnicode_AsUTF8String (PyList_GetItem (obj, i)); - r[i] = PyBytes_AS_STRING (bytes); -#endif - } + for (i = 0; i < len; ++i) + r[i] = guestfs_int_py_asstring (PyList_GetItem (obj, i)); r[len] = NULL; return r; @@ -345,11 +332,7 @@ guestfs_int_py_put_string_list (char * const * const argv) list = PyList_New (argc); for (i = 0; i < argc; ++i) { -#ifdef HAVE_PYSTRING_ASSTRING - PyList_SetItem (list, i, PyString_FromString (argv[i])); -#else - PyList_SetItem (list, i, PyUnicode_FromString (argv[i])); -#endif + PyList_SetItem (list, i, guestfs_int_py_fromstring (argv[i])); } return list; @@ -367,15 +350,41 @@ guestfs_int_py_put_table (char * const * const argv) list = PyList_New (argc >> 1); for (i = 0; i < argc; i += 2) { item = PyTuple_New (2); -#ifdef HAVE_PYSTRING_ASSTRING - PyTuple_SetItem (item, 0, PyString_FromString (argv[i])); - PyTuple_SetItem (item, 1, PyString_FromString (argv[i+1])); -#else - PyTuple_SetItem (item, 0, PyUnicode_FromString (argv[i])); - PyTuple_SetItem (item, 1, PyUnicode_FromString (argv[i+1])); -#endif + PyTuple_SetItem (item, 0, guestfs_int_py_fromstring (argv[i])); + PyTuple_SetItem (item, 1, guestfs_int_py_fromstring (argv[i+1])); PyList_SetItem (list, i >> 1, item); } return list; } + +PyObject * +guestfs_int_py_fromstring (const char *str) +{ +#ifdef HAVE_PYSTRING_ASSTRING + return PyString_FromString (str); +#else + return PyUnicode_FromString (str); +#endif +} + +PyObject * +guestfs_int_py_fromstringsize (const char *str, size_t size) +{ +#ifdef HAVE_PYSTRING_ASSTRING + return PyString_FromStringAndSize (str, size); +#else + return PyString_FromStringAndSize (str, size); +#endif +} + +char * +guestfs_int_py_asstring (PyObject *obj) +{ +#ifdef HAVE_PYSTRING_ASSTRING + return PyString_AsString (obj); +#else + PyObject *bytes = PyUnicode_AsUTF8String (obj); + return PyBytes_AS_STRING (bytes); +#endif +} -- 2.9.3
noxdafox
2017-May-09 15:37 UTC
Re: [Libguestfs] [PATCH v2] python: add simple wrappers for PyObject<->string functions
On 09/05/17 16:48, Pino Toscano wrote:> The current need for #ifdef's based on the presence of > PyString_FromString makes both the OCaml code of the generator, and the > generated C code a mess to read. > > Hence, add three simple wrappers to make both the OCaml, and C code more > readable, and easier to tweak in the future. > --- > generator/python.ml | 72 ++++++++++++----------------------------------------- > python/handle.c | 65 ++++++++++++++++++++++++++--------------------- > 2 files changed, 53 insertions(+), 84 deletions(-) > > diff --git a/generator/python.ml b/generator/python.ml > index 0162733..cf08294 100644 > --- a/generator/python.ml > +++ b/generator/python.ml > @@ -91,6 +91,9 @@ extern PyObject *guestfs_int_py_event_to_string (PyObject *self, PyObject *args) > extern char **guestfs_int_py_get_string_list (PyObject *obj); > extern PyObject *guestfs_int_py_put_string_list (char * const * const argv); > extern PyObject *guestfs_int_py_put_table (char * const * const argv); > +extern PyObject *guestfs_int_py_fromstring (const char *str); > +extern PyObject *guestfs_int_py_fromstringsize (const char *str, size_t size); > +extern char *guestfs_int_py_asstring (PyObject *obj); > > "; > > @@ -178,31 +181,16 @@ and generate_python_structs () > function > | name, FString -> > pr " PyDict_SetItemString (dict, \"%s\",\n" name; > - pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; > - pr " PyString_FromString (%s->%s));\n" > - typ name; > - pr "#else\n"; > - pr " PyUnicode_FromString (%s->%s));\n" > - typ name; > - pr "#endif\n" > + pr " guestfs_int_py_fromstring (%s->%s));\n" > + typ name > | name, FBuffer -> > pr " PyDict_SetItemString (dict, \"%s\",\n" name; > - pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; > - pr " PyString_FromStringAndSize (%s->%s, %s->%s_len));\n" > - typ name typ name; > - pr "#else\n"; > - pr " PyBytes_FromStringAndSize (%s->%s, %s->%s_len));\n" > - typ name typ name; > - pr "#endif\n" > + pr " guestfs_int_py_fromstringsize (%s->%s, %s->%s_len));\n" > + typ name typ name > | name, FUUID -> > pr " PyDict_SetItemString (dict, \"%s\",\n" name; > - pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; > - pr " PyString_FromStringAndSize (%s->%s, 32));\n" > - typ name; > - pr "#else\n"; > - pr " PyBytes_FromStringAndSize (%s->%s, 32));\n" > - typ name; > - pr "#endif\n" > + pr " guestfs_int_py_fromstringsize (%s->%s, 32));\n" > + typ name > | name, (FBytes|FUInt64) -> > pr " PyDict_SetItemString (dict, \"%s\",\n" name; > pr " PyLong_FromUnsignedLongLong (%s->%s));\n" > @@ -229,15 +217,9 @@ and generate_python_structs () > pr " PyDict_SetItemString (dict, \"%s\", Py_None);\n" name; > pr " }\n" > | name, FChar -> > - pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; > pr " PyDict_SetItemString (dict, \"%s\",\n" name; > - pr " PyString_FromStringAndSize (&%s->%s, 1));\n" > - typ name; > - pr "#else\n"; > - pr " PyDict_SetItemString (dict, \"%s\",\n" name; > - pr " PyUnicode_FromStringAndSize (&%s->%s, 1));\n" > - typ name; > - pr "#endif\n" > + pr " guestfs_int_py_fromstringsize (&%s->%s, 1));\n" > + typ name > ) cols; > pr " return dict;\n"; > pr "};\n"; > @@ -419,13 +401,7 @@ and generate_python_actions actions () > pr " optargs_s.%s = PyLong_AsLongLong (py_%s);\n" n n; > pr " if (PyErr_Occurred ()) goto out;\n" > | OString _ -> > - pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; > - pr " optargs_s.%s = PyString_AsString (py_%s);\n" n n; > - pr "#else\n"; > - pr " PyObject *bytes;\n"; > - pr " bytes = PyUnicode_AsUTF8String (py_%s);\n" n; > - pr " optargs_s.%s = PyBytes_AS_STRING (bytes);\n" n; > - pr "#endif\n"; > + pr " optargs_s.%s = guestfs_int_py_asstring (py_%s);\n" n n > | OStringList _ -> > pr " optargs_s.%s = guestfs_int_py_get_string_list (py_%s);\n" n n; > pr " if (!optargs_s.%s) goto out;\n" n; > @@ -480,30 +456,18 @@ and generate_python_actions actions () > | RBool _ -> pr " py_r = PyLong_FromLong ((long) r);\n" > | RInt64 _ -> pr " py_r = PyLong_FromLongLong (r);\n" > | RConstString _ -> > - pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; > - pr " py_r = PyString_FromString (r);\n"; > - pr "#else\n"; > - pr " py_r = PyUnicode_FromString (r);\n"; > - pr "#endif\n"; > + pr " py_r = guestfs_int_py_fromstring (r);\n"; > pr " if (py_r == NULL) goto out;\n"; > | RConstOptString _ -> > pr " if (r) {\n"; > - pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; > - pr " py_r = PyString_FromString (r);\n"; > - pr "#else\n"; > - pr " py_r = PyUnicode_FromString (r);\n"; > - pr "#endif\n"; > + pr " py_r = guestfs_int_py_fromstring (r);\n"; > pr " } else {\n"; > pr " Py_INCREF (Py_None);\n"; > pr " py_r = Py_None;\n"; > pr " }\n"; > pr " if (py_r == NULL) goto out;\n"; > | RString _ -> > - pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; > - pr " py_r = PyString_FromString (r);\n"; > - pr "#else\n"; > - pr " py_r = PyUnicode_FromString (r);\n"; > - pr "#endif\n"; > + pr " py_r = guestfs_int_py_fromstring (r);\n"; > pr " free (r);\n"; > pr " if (py_r == NULL) goto out;\n"; > | RStringList _ -> > @@ -519,11 +483,7 @@ and generate_python_actions actions () > pr " py_r = guestfs_int_py_put_table (r);\n"; > pr " guestfs_int_free_string_list (r);\n" > | RBufferOut _ -> > - pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; > - pr " py_r = PyString_FromStringAndSize (r, size);\n"; > - pr "#else\n"; > - pr " py_r = PyBytes_FromStringAndSize (r, size);\n"; > - pr "#endif\n"; > + pr " py_r = guestfs_int_py_fromstringsize (r, size);\n"; > pr " free (r);\n"; > pr " if (py_r == NULL) goto out;\n"; > ); > diff --git a/python/handle.c b/python/handle.c > index 806408f..f347c00 100644 > --- a/python/handle.c > +++ b/python/handle.c > @@ -241,11 +241,7 @@ guestfs_int_py_event_to_string (PyObject *self, PyObject *args) > return NULL; > } > > -#ifdef HAVE_PYSTRING_ASSTRING > - py_r = PyString_FromString (str); > -#else > - py_r = PyUnicode_FromString (str); > -#endif > + py_r = guestfs_int_py_fromstring (str); > free (str); > > return py_r; > @@ -298,9 +294,6 @@ guestfs_int_py_get_string_list (PyObject *obj) > { > size_t i, len; > char **r; > -#ifndef HAVE_PYSTRING_ASSTRING > - PyObject *bytes; > -#endif > > assert (obj); > > @@ -321,14 +314,8 @@ guestfs_int_py_get_string_list (PyObject *obj) > return NULL; > } > > - for (i = 0; i < len; ++i) { > -#ifdef HAVE_PYSTRING_ASSTRING > - r[i] = PyString_AsString (PyList_GetItem (obj, i)); > -#else > - bytes = PyUnicode_AsUTF8String (PyList_GetItem (obj, i)); > - r[i] = PyBytes_AS_STRING (bytes); > -#endif > - } > + for (i = 0; i < len; ++i) > + r[i] = guestfs_int_py_asstring (PyList_GetItem (obj, i)); > r[len] = NULL; > > return r; > @@ -345,11 +332,7 @@ guestfs_int_py_put_string_list (char * const * const argv) > > list = PyList_New (argc); > for (i = 0; i < argc; ++i) { > -#ifdef HAVE_PYSTRING_ASSTRING > - PyList_SetItem (list, i, PyString_FromString (argv[i])); > -#else > - PyList_SetItem (list, i, PyUnicode_FromString (argv[i])); > -#endif > + PyList_SetItem (list, i, guestfs_int_py_fromstring (argv[i])); > } > > return list; > @@ -367,15 +350,41 @@ guestfs_int_py_put_table (char * const * const argv) > list = PyList_New (argc >> 1); > for (i = 0; i < argc; i += 2) { > item = PyTuple_New (2); > -#ifdef HAVE_PYSTRING_ASSTRING > - PyTuple_SetItem (item, 0, PyString_FromString (argv[i])); > - PyTuple_SetItem (item, 1, PyString_FromString (argv[i+1])); > -#else > - PyTuple_SetItem (item, 0, PyUnicode_FromString (argv[i])); > - PyTuple_SetItem (item, 1, PyUnicode_FromString (argv[i+1])); > -#endif > + PyTuple_SetItem (item, 0, guestfs_int_py_fromstring (argv[i])); > + PyTuple_SetItem (item, 1, guestfs_int_py_fromstring (argv[i+1])); > PyList_SetItem (list, i >> 1, item); > } > > return list; > } > + > +PyObject * > +guestfs_int_py_fromstring (const char *str) > +{ > +#ifdef HAVE_PYSTRING_ASSTRING > + return PyString_FromString (str); > +#else > + return PyUnicode_FromString (str); > +#endif > +} > + > +PyObject * > +guestfs_int_py_fromstringsize (const char *str, size_t size) > +{ > +#ifdef HAVE_PYSTRING_ASSTRING > + return PyString_FromStringAndSize (str, size); > +#else > + return PyString_FromStringAndSize (str, size);s/PyString/PyUnicode> +#endif > +} > + > +char * > +guestfs_int_py_asstring (PyObject *obj) > +{ > +#ifdef HAVE_PYSTRING_ASSTRING > + return PyString_AsString (obj); > +#else > + PyObject *bytes = PyUnicode_AsUTF8String (obj); > + return PyBytes_AS_STRING (bytes); > +#endif > +}
Reasonably Related Threads
- [PATCH] python: add simple wrappers for PyObject<->string functions
- [Bug 1406906] [PATCH 0/3] Fix segmentation fault in Python bindings
- [PATCH] RHBZ#1406906: check return value of Python object functions
- [PATCH 0/2] python: improved UTF8 decoding error handling
- [PATCH 0/7] Various Python cleanups.