[Pkg-libvirt-commits] [libguestfs] 19/72: python: Call PyErr_Clear() on non-error paths out of the Python bindings.

Hilko Bengen bengen at moszumanska.debian.org
Sun Apr 5 15:19:48 UTC 2015


This is an automated email from the git hooks/post-receive script.

bengen pushed a commit to branch master
in repository libguestfs.

commit 69125dac04948c02510bc6407985385df885e78b
Author: Richard W.M. Jones <rjones at redhat.com>
Date:   Sat Feb 14 17:21:48 2015 +0000

    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.
    
    (cherry picked from commit c3566da409ab686330be3be51964183b2b510f15)
---
 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 07e87d2..4d53c0b 100644
--- a/generator/python.ml
+++ b/generator/python.ml
@@ -454,7 +454,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";
@@ -465,14 +466,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___free_string_list (r);\n"
@@ -491,10 +494,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 (

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-libvirt/libguestfs.git



More information about the Pkg-libvirt-commits mailing list