[pygobject] Handle exception unreffing Variant at exit



commit 8694e4dd42565f07b6f9ba1221fb03146be333a0
Author: Dan Nicholson <nicholson endlessm com>
Date:   Wed Dec 21 12:02:14 2016 -0600

    Handle exception unreffing Variant at exit
    
    Calling unref will cause gi and gi.repository.GLib to be imported.
    However, if the program is exiting, then these modules have likely been
    removed from sys.modules and will raise an exception. Assume that's the
    case for ImportError and ignore the exception since everything will be
    cleaned up, anyways.
    
    This can be triggered with the following trivial program:
    
    $ python3 -c 'from gi.repository import GLib; v = GLib.Variant("s", "foo")'
    Exception ignored in:
    
    Adding some debug code to show the full exception revealed this:
    
    Traceback (most recent call last):
      File "/home/dan/src/pygobject/build3/gi/overrides/GLib.py", line 265, in __del__
        self.unref()
    ImportError: import of 'gi.repository.GLib' halted; None in sys.modules
    
    https://bugzilla.gnome.org/show_bug.cgi?id=776092

 gi/overrides/GLib.py |   11 ++++++++++-
 1 files changed, 10 insertions(+), 1 deletions(-)
---
diff --git a/gi/overrides/GLib.py b/gi/overrides/GLib.py
index 5a7792d..82010c0 100644
--- a/gi/overrides/GLib.py
+++ b/gi/overrides/GLib.py
@@ -251,7 +251,16 @@ class Variant(GLib.Variant):
         return GLib.Variant.new_tuple(elements)
 
     def __del__(self):
-        self.unref()
+        try:
+            self.unref()
+        except ImportError:
+            # Calling unref will cause gi and gi.repository.GLib to be
+            # imported. However, if the program is exiting, then these
+            # modules have likely been removed from sys.modules and will
+            # raise an exception. Assume that's the case for ImportError
+            # and ignore the exception since everything will be cleaned
+            # up, anyways.
+            pass
 
     def __str__(self):
         return self.print_(True)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]