summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2018-09-26 06:38:36 (GMT)
committerGitHub <noreply@github.com>2018-09-26 06:38:36 (GMT)
commitd45a9613402b686f8afd3dd5b6acf8141f14d711 (patch)
treeec99aa32aba5b44f7200e409679dd547cca1b3d6
parentdc335ae77dfc1fb6a500eb1cd0baf23fcda45434 (diff)
downloadcpython-d45a9613402b686f8afd3dd5b6acf8141f14d711.zip
cpython-d45a9613402b686f8afd3dd5b6acf8141f14d711.tar.gz
cpython-d45a9613402b686f8afd3dd5b6acf8141f14d711.tar.bz2
[3.6] bpo-34320: Fix dict(o) didn't copy order of dict subclass (GH-8624) (GH-9583)
When dict subclass overrides order (`__iter__()`, `keys()`, and `items()`), `dict(o)` should use it instead of dict ordering. https://bugs.python.org/issue34320 (cherry picked from commit 2aaf98c16ae3070378de523a173e29644037d8bd) Co-authored-by: INADA Naoki <methane@users.noreply.github.com> https://bugs.python.org/issue34320
-rw-r--r--Lib/test/test_builtin.py9
-rw-r--r--Lib/test/test_call.py18
-rw-r--r--Lib/test/test_dict.py31
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2018-08-02-22-34-59.bpo-34320.hNshAA.rst1
-rw-r--r--Objects/dictobject.c4
5 files changed, 62 insertions, 1 deletions
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index e0dbe78..e885e6d 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -1841,6 +1841,15 @@ class TestType(unittest.TestCase):
with self.assertRaises(TypeError):
type('A', (B,), {'__slots__': '__weakref__'})
+ def test_namespace_order(self):
+ # bpo-34320: namespace should preserve order
+ od = collections.OrderedDict([('a', 1), ('b', 2)])
+ od.move_to_end('a')
+ expected = list(od.items())
+
+ C = type('C', (), od)
+ self.assertEqual(list(C.__dict__.items())[:2], [('b', 2), ('a', 1)])
+
def load_tests(loader, tests, pattern):
from doctest import DocTestSuite
diff --git a/Lib/test/test_call.py b/Lib/test/test_call.py
index 2e8819b..e71ede2 100644
--- a/Lib/test/test_call.py
+++ b/Lib/test/test_call.py
@@ -1,3 +1,4 @@
+import collections
import datetime
import unittest
from test.support import cpython_only
@@ -6,6 +7,23 @@ try:
except ImportError:
_testcapi = None
+
+class FunctionCalls(unittest.TestCase):
+
+ def test_kwargs_order(self):
+ # bpo-34320: **kwargs should preserve order of passed OrderedDict
+ od = collections.OrderedDict([('a', 1), ('b', 2)])
+ od.move_to_end('a')
+ expected = list(od.items())
+
+ def fn(**kw):
+ return kw
+
+ res = fn(**od)
+ self.assertIsInstance(res, dict)
+ self.assertEqual(list(res.items()), expected)
+
+
# The test cases here cover several paths through the function calling
# code. They depend on the METH_XXX flag that is used to define a C
# function, which can't be verified from Python. If the METH_XXX decl
diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py
index 639e05f..7e94ad2 100644
--- a/Lib/test/test_dict.py
+++ b/Lib/test/test_dict.py
@@ -1176,6 +1176,37 @@ class DictTest(unittest.TestCase):
self.assertRaises(RuntimeError, iter_and_mutate)
+ @support.cpython_only
+ def test_dict_copy_order(self):
+ # bpo-34320
+ od = collections.OrderedDict([('a', 1), ('b', 2)])
+ od.move_to_end('a')
+ expected = list(od.items())
+
+ copy = dict(od)
+ self.assertEqual(list(copy.items()), expected)
+
+ # dict subclass doesn't override __iter__
+ class CustomDict(dict):
+ pass
+
+ pairs = [('a', 1), ('b', 2), ('c', 3)]
+
+ d = CustomDict(pairs)
+ self.assertEqual(pairs, list(dict(d).items()))
+
+ class CustomReversedDict(dict):
+ def keys(self):
+ return reversed(list(dict.keys(self)))
+
+ __iter__ = keys
+
+ def items(self):
+ return reversed(dict.items(self))
+
+ d = CustomReversedDict(pairs)
+ self.assertEqual(pairs[::-1], list(dict(d).items()))
+
class CAPITest(unittest.TestCase):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-08-02-22-34-59.bpo-34320.hNshAA.rst b/Misc/NEWS.d/next/Core and Builtins/2018-08-02-22-34-59.bpo-34320.hNshAA.rst
new file mode 100644
index 0000000..ce5b339
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2018-08-02-22-34-59.bpo-34320.hNshAA.rst
@@ -0,0 +1 @@
+Fix ``dict(od)`` didn't copy iteration order of OrderedDict.
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index 0768d11..b55e49c 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -237,6 +237,8 @@ static Py_ssize_t lookdict_split(PyDictObject *mp, PyObject *key,
static int dictresize(PyDictObject *mp, Py_ssize_t minused);
+static PyObject* dict_iter(PyDictObject *dict);
+
/*Global counter used to set ma_version_tag field of dictionary.
* It is incremented each time that a dictionary is created and each
* time that a dictionary is modified. */
@@ -2482,7 +2484,7 @@ dict_merge(PyObject *a, PyObject *b, int override)
return -1;
}
mp = (PyDictObject*)a;
- if (PyDict_Check(b)) {
+ if (PyDict_Check(b) && (Py_TYPE(b)->tp_iter == (getiterfunc)dict_iter)) {
other = (PyDictObject*)b;
if (other == mp || other->ma_used == 0)
/* a.update(a) or a.update({}); nothing to do */