Richard W.M. Jones
2015-Feb-14 18:46 UTC
[Libguestfs] [PATCH] python: Call PyErr_Clear() on non-error paths out of the Python bindings.
We also need to be more careful about PyString_FromString and similar functions returning NULL on failure. Currently we don't check this every time. This commit adds more checks, but is still not complete. --- TODO | 6 ------ generator/python.ml | 19 +++++++++++++++---- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/TODO b/TODO index 075bdc6..666400e 100644 --- a/TODO +++ b/TODO @@ -563,12 +563,6 @@ Write an extension for mc that would let people browse into filesystems. See http://repo.or.cz/w/midnight-commander.git/tree/HEAD:/misc/ext.d -Python ------- - -It seems as if we should call PyErr_Clear() somewhere in every -Python binding. - Improvements in virt-log ------------------------ diff --git a/generator/python.ml b/generator/python.ml index 4b823e6..637f22d 100644 --- a/generator/python.ml +++ b/generator/python.ml @@ -455,7 +455,8 @@ put_table (char * const * const argv) pr " py_r = PyString_FromString (r);\n"; pr "#else\n"; pr " py_r = PyUnicode_FromString (r);\n"; - pr "#endif\n" + pr "#endif\n"; + pr " if (py_r == NULL) goto out;\n"; | RConstOptString _ -> pr " if (r) {\n"; pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; @@ -466,14 +467,16 @@ put_table (char * const * const argv) pr " } else {\n"; pr " Py_INCREF (Py_None);\n"; pr " py_r = Py_None;\n"; - pr " }\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 " free (r);\n" + pr " free (r);\n"; + pr " if (py_r == NULL) goto out;\n"; | RStringList _ -> pr " py_r = put_string_list (r);\n"; pr " guestfs_int_free_string_list (r);\n" @@ -492,10 +495,18 @@ put_table (char * const * const argv) pr "#else\n"; pr " py_r = PyBytes_FromStringAndSize (r, size);\n"; pr "#endif\n"; - pr " free (r);\n" + pr " free (r);\n"; + pr " if (py_r == NULL) goto out;\n"; ); + (* As this is the non-error path, clear the Python error + * indicator flag in case it was set accidentally somewhere in + * the function. Since we are not returning an error indication + * to the caller, having it set would risk the error popping up + * at random later in the interpreter. + *) pr "\n"; + pr " PyErr_Clear ();\n"; pr " out:\n"; List.iter ( -- 2.1.0
Maybe Matching Threads
- [PATCH] python: add simple wrappers for PyObject<->string functions
- [PATCH v2] python: add simple wrappers for PyObject<->string functions
- [PATCH] python: check return value of Python APIs
- [PATCH 3/4] hivex: python: Produce Unicode strings in get_* methods
- [Bug #1406906] [PATCH] python: fix segmentation fault when setting non UTF-8 strings