[pypy-commit] pypy py3.6: fix reduce some more and add setstate of ImportError

cfbolz pypy.commits at gmail.com
Fri Sep 6 05:08:50 EDT 2019


Author: Carl Friedrich Bolz-Tereick <cfbolz at gmx.de>
Branch: py3.6
Changeset: r97384:034134044fdc
Date: 2019-09-06 11:02 +0200
http://bitbucket.org/pypy/pypy/changeset/034134044fdc/

Log:	fix reduce some more and add setstate of ImportError

diff --git a/pypy/module/exceptions/interp_exceptions.py b/pypy/module/exceptions/interp_exceptions.py
--- a/pypy/module/exceptions/interp_exceptions.py
+++ b/pypy/module/exceptions/interp_exceptions.py
@@ -343,13 +343,25 @@
         W_Exception.descr_init(self, space, args_w)
 
     def descr_reduce(self, space):
-        w_dct = space.newdict()
-        space.setitem(w_dct, space.newtext('name'), self.w_name)
-        space.setitem(w_dct, space.newtext('path'), self.w_path)
-        return space.newtuple([space.w_ImportError,
-                    space.newtuple([self.w_msg]),
-                    w_dct,
-                ])
+        lst = [self.getclass(space), space.newtuple(self.args_w)]
+        if self.w_dict is not None and space.is_true(self.w_dict):
+            w_dict = space.call_method(self.w_dict, "copy")
+        else:
+            w_dict = space.newdict()
+        if not space.is_w(self.w_name, space.w_None):
+            space.setitem(w_dict, space.newtext("name"), self.w_name)
+        if not space.is_w(self.w_path, space.w_None):
+            space.setitem(w_dict, space.newtext("path"), self.w_path)
+        if space.is_true(w_dict):
+            lst.append(w_dict)
+        return space.newtuple(lst)
+
+    def descr_setstate(self, space, w_dict):
+        self.w_name = space.call_method(w_dict, "pop", space.newtext("name"), space.w_None)
+        self.w_path = space.call_method(w_dict, "pop", space.newtext("path"), space.w_None)
+        w_olddict = self.getdict(space)
+        space.call_method(w_olddict, 'update', w_dict)
+
 
 W_ImportError.typedef = TypeDef(
     'ImportError',
@@ -358,10 +370,11 @@
     __module__ = 'builtins',
     __new__ = _new(W_ImportError),
     __init__ = interp2app(W_ImportError.descr_init),
+    __reduce__ = interp2app(W_ImportError.descr_reduce),
+    __setstate__ = interp2app(W_ImportError.descr_setstate),
     name = readwrite_attrproperty_w('w_name', W_ImportError),
     path = readwrite_attrproperty_w('w_path', W_ImportError),
     msg = readwrite_attrproperty_w('w_msg', W_ImportError),
-    __reduce__ = interp2app(W_ImportError.descr_reduce),
 )
 
 
diff --git a/pypy/module/exceptions/test/test_exc.py b/pypy/module/exceptions/test/test_exc.py
--- a/pypy/module/exceptions/test/test_exc.py
+++ b/pypy/module/exceptions/test/test_exc.py
@@ -216,6 +216,8 @@
         assert le.__reduce__() == (LookupError, (1, 2, "a"), {"xyz": (1, 2)})
         ee = EnvironmentError(1, 2, "a")
         assert ee.__reduce__() == (PermissionError, (1, 2, "a"))
+        ee = ImportError("a", "b", "c", name="x", path="y")
+        assert ee.__reduce__() == (ImportError, ("a", "b", "c"), {"name": "x", "path": "y"})
 
     def test_setstate(self):
         fw = FutureWarning()
@@ -225,6 +227,14 @@
         assert fw.z == 1
         assert fw.xyz == (1, 2)
 
+        i = ImportError()
+        i.foo = "x"
+        i.__setstate__({"name": "x", "path": "y", "bar": 1})
+        assert i.foo == "x"
+        assert i.name == "x"
+        assert i.path == "y"
+        assert i.bar == 1
+
     def test_unicode_error_uninitialized_str(self):
         assert str(UnicodeEncodeError.__new__(UnicodeEncodeError)) == ""
         assert str(UnicodeDecodeError.__new__(UnicodeDecodeError)) == ""


More information about the pypy-commit mailing list