Author: Ronan Lamy <[email protected]>
Branch: release-pypy3.5-5.x
Changeset: r91499:9d7a5438be1d
Date: 2017-06-03 21:31 +0300
http://bitbucket.org/pypy/pypy/changeset/9d7a5438be1d/
Log: Let OrderedDict.__init__ behave like CPython wrt. subclasses
overridding __setitem__ (grafted from
c13ae2a7e07aed0d1756a73bbfc9c389057c35e1)
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
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit