[pypy-commit] pypy py3.5: isinstance(X, Y) is of course completely different than issubclass(type(X), Y)

arigo pypy.commits at gmail.com
Wed Nov 30 10:43:03 EST 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: py3.5
Changeset: r88777:0ffbab13b5cd
Date: 2016-11-30 16:42 +0100
http://bitbucket.org/pypy/pypy/changeset/0ffbab13b5cd/

Log:	isinstance(X, Y) is of course completely different than
	issubclass(type(X), Y)

diff --git a/pypy/module/sys/app.py b/pypy/module/sys/app.py
--- a/pypy/module/sys/app.py
+++ b/pypy/module/sys/app.py
@@ -112,24 +112,30 @@
 
 
 class SimpleNamespace:
+    """A simple attribute-based namespace.
+
+SimpleNamespace(**kwargs)"""
+
     def __init__(self, **kwargs):
         self.__dict__.update(kwargs)
 
-    def __repr__(self, recurse=set()):
+    def __repr__(self):
         ident = id(self)
-        if ident in recurse:
+        if ident in sns_recurse:
             return "namespace(...)"
-        recurse.add(ident)
+        sns_recurse.add(ident)
         try:
             pairs = ('%s=%r' % item for item in sorted(self.__dict__.items()))
             return "namespace(%s)" % ', '.join(pairs)
         finally:
-            recurse.remove(ident)
+            sns_recurse.discard(ident)
 
     def __eq__(self, other):
-        return (isinstance(other, SimpleNamespace) and
+        return (issubclass(type(other), SimpleNamespace) and
                 self.__dict__ == other.__dict__)
 
+sns_recurse = set()
+
 # This class is not exposed in sys, but by the types module.
 SimpleNamespace.__module__ = 'types'
 
diff --git a/pypy/module/sys/test/test_sysmodule.py b/pypy/module/sys/test/test_sysmodule.py
--- a/pypy/module/sys/test/test_sysmodule.py
+++ b/pypy/module/sys/test/test_sysmodule.py
@@ -611,6 +611,13 @@
         assert ns != Other(z=4, x=1, w=3)
         assert (Foo(z=4, x=1, w=3) == Other(z=4, x=1, w=3)) is False
         assert Foo(z=4, x=1, w=3) != Other(z=4, x=1, w=3)
+        #
+        class Fake:
+            __class__ = SimpleNamespace
+        assert isinstance(Fake(), SimpleNamespace)
+        assert not issubclass(Fake, SimpleNamespace)
+        assert (Fake() == SimpleNamespace()) is False
+        assert SimpleNamespace() != Fake()
 
     def test_pickle_simplenamespace(self):
         import pickle, sys


More information about the pypy-commit mailing list