Skip to content

Commit 2aaf98c

Browse files
methanemiss-islington
authored andcommitted
bpo-34320: Fix dict(o) didn't copy order of dict subclass (GH-8624)
When dict subclass overrides order (`__iter__()`, `keys()`, and `items()`), `dict(o)` should use it instead of dict ordering. https://bugs.python.org/issue34320
1 parent d345bb4 commit 2aaf98c

File tree

5 files changed

+60
-1
lines changed

5 files changed

+60
-1
lines changed

Lib/test/test_builtin.py

+9
Original file line numberDiff line numberDiff line change
@@ -1968,6 +1968,15 @@ class B:
19681968
with self.assertRaises(TypeError):
19691969
type('A', (B,), {'__slots__': '__weakref__'})
19701970

1971+
def test_namespace_order(self):
1972+
# bpo-34320: namespace should preserve order
1973+
od = collections.OrderedDict([('a', 1), ('b', 2)])
1974+
od.move_to_end('a')
1975+
expected = list(od.items())
1976+
1977+
C = type('C', (), od)
1978+
self.assertEqual(list(C.__dict__.items())[:2], [('b', 2), ('a', 1)])
1979+
19711980

19721981
def load_tests(loader, tests, pattern):
19731982
from doctest import DocTestSuite

Lib/test/test_call.py

+17
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,23 @@
99
import collections
1010
import itertools
1111

12+
13+
class FunctionCalls(unittest.TestCase):
14+
15+
def test_kwargs_order(self):
16+
# bpo-34320: **kwargs should preserve order of passed OrderedDict
17+
od = collections.OrderedDict([('a', 1), ('b', 2)])
18+
od.move_to_end('a')
19+
expected = list(od.items())
20+
21+
def fn(**kw):
22+
return kw
23+
24+
res = fn(**od)
25+
self.assertIsInstance(res, dict)
26+
self.assertEqual(list(res.items()), expected)
27+
28+
1229
# The test cases here cover several paths through the function calling
1330
# code. They depend on the METH_XXX flag that is used to define a C
1431
# function, which can't be verified from Python. If the METH_XXX decl

Lib/test/test_dict.py

+30
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,36 @@ def iter_and_mutate():
12221222

12231223
self.assertRaises(RuntimeError, iter_and_mutate)
12241224

1225+
def test_dict_copy_order(self):
1226+
# bpo-34320
1227+
od = collections.OrderedDict([('a', 1), ('b', 2)])
1228+
od.move_to_end('a')
1229+
expected = list(od.items())
1230+
1231+
copy = dict(od)
1232+
self.assertEqual(list(copy.items()), expected)
1233+
1234+
# dict subclass doesn't override __iter__
1235+
class CustomDict(dict):
1236+
pass
1237+
1238+
pairs = [('a', 1), ('b', 2), ('c', 3)]
1239+
1240+
d = CustomDict(pairs)
1241+
self.assertEqual(pairs, list(dict(d).items()))
1242+
1243+
class CustomReversedDict(dict):
1244+
def keys(self):
1245+
return reversed(list(dict.keys(self)))
1246+
1247+
__iter__ = keys
1248+
1249+
def items(self):
1250+
return reversed(dict.items(self))
1251+
1252+
d = CustomReversedDict(pairs)
1253+
self.assertEqual(pairs[::-1], list(dict(d).items()))
1254+
12251255

12261256
class CAPITest(unittest.TestCase):
12271257

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix ``dict(od)`` didn't copy iteration order of OrderedDict.

Objects/dictobject.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ static Py_ssize_t lookdict_split(PyDictObject *mp, PyObject *key,
235235

236236
static int dictresize(PyDictObject *mp, Py_ssize_t minused);
237237

238+
static PyObject* dict_iter(PyDictObject *dict);
239+
238240
/*Global counter used to set ma_version_tag field of dictionary.
239241
* It is incremented each time that a dictionary is created and each
240242
* time that a dictionary is modified. */
@@ -2379,7 +2381,7 @@ dict_merge(PyObject *a, PyObject *b, int override)
23792381
return -1;
23802382
}
23812383
mp = (PyDictObject*)a;
2382-
if (PyDict_Check(b)) {
2384+
if (PyDict_Check(b) && (Py_TYPE(b)->tp_iter == (getiterfunc)dict_iter)) {
23832385
other = (PyDictObject*)b;
23842386
if (other == mp || other->ma_used == 0)
23852387
/* a.update(a) or a.update({}); nothing to do */

0 commit comments

Comments
 (0)