From a82f77fb00ca3bd3eb27a6a5d77a19eadcf0ba31 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Sat, 4 Jul 2015 19:55:16 -0500 Subject: protect against mutation of the dict during insertion (closes #24407) --- Lib/test/test_dict.py | 15 +++++++++++++++ Misc/NEWS | 2 ++ Objects/dictobject.c | 26 +++++++++++++++++++------- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py index a388959..bd3040a 100644 --- a/Lib/test/test_dict.py +++ b/Lib/test/test_dict.py @@ -906,6 +906,21 @@ class DictTest(unittest.TestCase): f.a = 'a' self.assertEqual(f.__dict__, {1:1, 'a':'a'}) + def test_merge_and_mutate(self): + class X: + def __hash__(self): + return 0 + + def __eq__(self, o): + other.clear() + return False + + l = [(i,0) for i in range(1, 1337)] + other = dict(l) + other[X()] = 0 + d = {X(): 0, 1: 1} + self.assertRaises(RuntimeError, d.update, other) + from test import mapping_tests class GeneralMappingTests(mapping_tests.BasicTestMappingProtocol): diff --git a/Misc/NEWS b/Misc/NEWS index 43a9f37..c5d7e65 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,8 @@ What's New in Python 3.3.7? Core and Builtins ----------------- +- Issue #24407: Fix crash when dict is mutated while being updated. + - Issue #24096: Make warnings.warn_explicit more robust against mutation of the warnings.filters list. diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 7aa5ea8..953484c 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -1941,20 +1941,32 @@ PyDict_Merge(PyObject *a, PyObject *b, int override) if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0) return -1; for (i = 0, n = DK_SIZE(other->ma_keys); i < n; i++) { - PyObject *value; + PyObject *key, *value; + Py_hash_t hash; entry = &other->ma_keys->dk_entries[i]; + key = entry->me_key; + hash = entry->me_hash; if (other->ma_values) value = other->ma_values[i]; else value = entry->me_value; - if (value != NULL && - (override || - PyDict_GetItem(a, entry->me_key) == NULL)) { - if (insertdict(mp, entry->me_key, - entry->me_hash, - value) != 0) + if (value != NULL) { + int err = 0; + Py_INCREF(key); + Py_INCREF(value); + if (override || PyDict_GetItem(a, key) == NULL) + err = insertdict(mp, key, hash, value); + Py_DECREF(value); + Py_DECREF(key); + if (err != 0) + return -1; + + if (n != DK_SIZE(other->ma_keys)) { + PyErr_SetString(PyExc_RuntimeError, + "dict mutated during update"); return -1; + } } } } -- cgit v0.12 From d423396394c49a339b78f2026a1fdab42a1cffb0 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Sun, 5 Jul 2015 07:24:17 -0700 Subject: Fixes rebuild of strings for Windows installer. --- Tools/msi/msi.targets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tools/msi/msi.targets b/Tools/msi/msi.targets index 34b8747..2431dc2 100644 --- a/Tools/msi/msi.targets +++ b/Tools/msi/msi.targets @@ -24,7 +24,7 @@ - + <_Content>$([System.IO.File]::ReadAllText(%(WxlTemplate.FullPath)).Replace(`{{ShortVersion}}`, `$(MajorVersionNumber).$(MinorVersionNumber)`).Replace(`{{LongVersion}}`, `$(PythonVersion)`).Replace(`{{Bitness}}`, `$(Bitness)`)) <_ExistingContent Condition="Exists('$(IntermediateOutputPath)%(WxlTemplate.Filename).wxl')">$([System.IO.File]::ReadAllText($(IntermediateOutputPath)%(WxlTemplate.Filename).wxl)) -- cgit v0.12 From 1554b17856148596bbec6a22de910cdf6e20a22f Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Sun, 5 Jul 2015 10:37:00 -0500 Subject: add news section for next beta --- Misc/NEWS | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Misc/NEWS b/Misc/NEWS index 8599353..b9cf2bc 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -2,6 +2,18 @@ Python News +++++++++++ +What's New in Python 3.5.0 beta 4? +================================== + +*Release date: XXXX-XX-XX* + +Core and Builtins +----------------- + +Library +------- + + What's New in Python 3.5.0 beta 3? ================================== -- cgit v0.12 From d5d77aac60453694445d1509b51d7514871dbff8 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Sun, 5 Jul 2015 10:37:25 -0500 Subject: set items in dict displays from left to right (closes #24569) --- Lib/test/test_unpack_ex.py | 3 +++ Misc/NEWS | 2 ++ Python/ceval.c | 15 +++++++++------ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_unpack_ex.py b/Lib/test/test_unpack_ex.py index 01f57b9..d27eef0 100644 --- a/Lib/test/test_unpack_ex.py +++ b/Lib/test/test_unpack_ex.py @@ -128,6 +128,9 @@ Dict display element unpacking ... for i in range(1000)) + "}")) 1000 + >>> {0:1, **{0:2}, 0:3, 0:4} + {0: 4} + List comprehension element unpacking >>> a, b, c = [0, 1, 2], 3, 4 diff --git a/Misc/NEWS b/Misc/NEWS index b9cf2bc..46881c3 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,8 @@ What's New in Python 3.5.0 beta 4? Core and Builtins ----------------- +- Issue #24569: Make PEP 448 dictionary evaluation more consistent. + Library ------- diff --git a/Python/ceval.c b/Python/ceval.c index e68ae33..ac52ad9 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -2561,22 +2561,25 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) } TARGET(BUILD_MAP) { + int i; PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg); if (map == NULL) goto error; - while (--oparg >= 0) { + for (i = oparg; i > 0; i--) { int err; - PyObject *value = TOP(); - PyObject *key = SECOND(); - STACKADJ(-2); + PyObject *key = PEEK(2*i); + PyObject *value = PEEK(2*i - 1); err = PyDict_SetItem(map, key, value); - Py_DECREF(value); - Py_DECREF(key); if (err != 0) { Py_DECREF(map); goto error; } } + + while (oparg--) { + Py_DECREF(POP()); + Py_DECREF(POP()); + } PUSH(map); DISPATCH(); } -- cgit v0.12