Author: Ronan Lamy <ronan.l...@gmail.com>
Branch: py3.5
Changeset: r91498:c13ae2a7e07a
Date: 2017-06-02 18:00 +0100
http://bitbucket.org/pypy/pypy/changeset/c13ae2a7e07a/

Log:    Let OrderedDict.__init__ behave like CPython wrt. subclasses
        overridding __setitem__

diff --git a/lib_pypy/_pypy_collections.py b/lib_pypy/_pypy_collections.py
--- a/lib_pypy/_pypy_collections.py
+++ b/lib_pypy/_pypy_collections.py
@@ -15,6 +15,21 @@
     cases but is nonsensical in other cases.  This is officially
     forbidden by the CPython docs, so we forbid it explicitly for now.
     '''
+    def __init__(*args, **kwds):
+        '''Initialize an ordered dictionary.  The signature is the same as
+        regular dictionaries, but keyword arguments are not recommended because
+        their insertion order is arbitrary.
+
+        '''
+        if not args:
+            raise TypeError("descriptor '__init__' of 'OrderedDict' object "
+                            "needs an argument")
+        self, *args = args
+        if len(args) > 1:
+            raise TypeError('expected at most 1 arguments, got %d' % len(args))
+        self.__update(*args, **kwds)
+
+    update = __update = _collections_abc.MutableMapping.update
 
     def __reversed__(self):
         return reversed_dict(self)
diff --git a/pypy/module/_collections/test/test_ordereddict.py 
b/pypy/module/_collections/test/test_ordereddict.py
--- a/pypy/module/_collections/test/test_ordereddict.py
+++ b/pypy/module/_collections/test/test_ordereddict.py
@@ -12,3 +12,13 @@
         d = OrderedDict()
         d[1] = d
         assert repr(d) == 'OrderedDict([(1, ...)])'
+
+    def test_subclass(self):
+        from _collections import OrderedDict
+        class MyODict(OrderedDict):
+            def __setitem__(self, key, value):
+                super().__setitem__(key, 42)
+        d = MyODict(x=1)
+        assert d['x'] == 42
+        d.update({'y': 2})
+        assert d['y'] == 42
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to