[pygobject] Make GLib.Error picklable. Fixes #145



commit e83a48b955331144687b32e01f53f83228ef09a0
Author: Christoph Reiter <reiter christoph gmail com>
Date:   Thu Apr 5 18:12:28 2018 +0200

    Make GLib.Error picklable. Fixes #145
    
    Make __module__ match the module where it gets exposed so Python
    can find the right class.

 gi/_error.py         |  5 +++--
 gi/overrides/GLib.py |  2 +-
 tests/test_error.py  | 17 +++++++++++++++++
 3 files changed, 21 insertions(+), 3 deletions(-)
---
diff --git a/gi/_error.py b/gi/_error.py
index 6b684ce7..6440ef78 100644
--- a/gi/_error.py
+++ b/gi/_error.py
@@ -38,8 +38,9 @@ class GError(RuntimeError):
         return "%s: %s (%d)" % (self.domain, self.message, self.code)
 
     def __repr__(self):
-        return "%s.%s('%s', '%s', %d)" % (GError.__module__, GError.__name__,
-                                          self.message, self.domain, self.code)
+        return "%s.%s('%s', '%s', %d)" % (
+            GError.__module__.rsplit(".", 1)[-1], GError.__name__,
+            self.message, self.domain, self.code)
 
     def copy(self):
         return GError(self.message, self.domain, self.code)
diff --git a/gi/overrides/GLib.py b/gi/overrides/GLib.py
index f00c7793..ed1f5736 100644
--- a/gi/overrides/GLib.py
+++ b/gi/overrides/GLib.py
@@ -73,7 +73,7 @@ def gerror_new_literal(domain, message, code):
 
 # Monkey patch methods that rely on GLib introspection to be loaded at runtime.
 Error.__name__ = 'Error'
-Error.__module__ = 'GLib'
+Error.__module__ = 'gi.repository.GLib'
 Error.__gtype__ = GLib.Error.__gtype__
 Error.matches = gerror_matches
 Error.new_literal = staticmethod(gerror_new_literal)
diff --git a/tests/test_error.py b/tests/test_error.py
index 3f483265..a15c7d33 100644
--- a/tests/test_error.py
+++ b/tests/test_error.py
@@ -25,6 +25,7 @@
 from __future__ import absolute_import
 
 import unittest
+import pickle
 
 from gi.repository import GLib
 from gi.repository import GIMarshallingTests
@@ -65,6 +66,22 @@ class TestType(unittest.TestCase):
     def test_inheritance(self):
         self.assertTrue(issubclass(GLib.Error, RuntimeError))
 
+    def test_pickle(self):
+
+        def check_pickle(e):
+            assert isinstance(e, GLib.Error)
+            new_e = pickle.loads(pickle.dumps(e))
+            assert type(new_e) is type(e)
+            assert repr(e) == repr(new_e)
+
+        e = GLib.Error('test message', 'mydomain', 42)
+        check_pickle(e)
+
+        try:
+            GLib.file_get_contents("")
+        except Exception as e:
+            check_pickle(e)
+
 
 class ObjectWithVFuncException(GIMarshallingTests.Object):
     def do_vfunc_meth_with_err(self, x):


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