[pypy-commit] pypy py3.5: Allow (and ignore) duplicate names in a single __slots__ specification.

arigo pypy.commits at gmail.com
Tue Aug 16 14:06:39 EDT 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: py3.5
Changeset: r86232:146f531d6c86
Date: 2016-08-16 19:09 +0200
http://bitbucket.org/pypy/pypy/changeset/146f531d6c86/

Log:	Allow (and ignore) duplicate names in a single __slots__
	specification. Believe it or not, the stdlib datetime.py does that.

diff --git a/pypy/objspace/std/test/test_typeobject.py b/pypy/objspace/std/test/test_typeobject.py
--- a/pypy/objspace/std/test/test_typeobject.py
+++ b/pypy/objspace/std/test/test_typeobject.py
@@ -1388,3 +1388,7 @@
         assert not self.compares_by_identity(X)
         del X.__eq__
         assert self.compares_by_identity(X)
+
+    def test_duplicate_slot_name(self):
+        class X:   # does not raise
+            __slots__ = 'a', 'a'
diff --git a/pypy/objspace/std/typeobject.py b/pypy/objspace/std/typeobject.py
--- a/pypy/objspace/std/typeobject.py
+++ b/pypy/objspace/std/typeobject.py
@@ -1076,9 +1076,14 @@
     # create member
     slot_name = mangle(slot_name, w_self.name)
     if slot_name in w_self.dict_w:
-        raise oefmt(space.w_ValueError,
-                    "%R in __slots__ conflicts with class variable",
-                    w_slot_name)
+        w_prev = w_self.dict_w[slot_name]
+        if isinstance(w_prev, Member) and w_prev.w_cls is w_self:
+            pass   # special case: duplicate __slots__ entry, ignored
+                   # (e.g. occurs in datetime.py, fwiw)
+        else:
+            raise oefmt(space.w_ValueError,
+                        "%R in __slots__ conflicts with class variable",
+                        w_slot_name)
     else:
         # Force interning of slot names.
         slot_name = space.str_w(space.new_interned_str(slot_name))


More information about the pypy-commit mailing list