diff -r cf564121f9f0 Lib/test/test_dict.py --- a/Lib/test/test_dict.py Mon Dec 19 21:01:33 2016 +0800 +++ b/Lib/test/test_dict.py Tue Dec 20 02:51:24 2016 +0900 @@ -833,12 +833,30 @@ def test_track_subtypes(self): # Dict subtypes are always tracked class MyDict(dict): pass self._tracked(MyDict()) + @support.cpython_only + def test_splittable_setattr_after_pop(self): + """setattr must not convert combined table into split table""" + # Issue 28147 + import _testcapi + + class C: + pass + a = C() + a.a = 2 + self.assertTrue(_testcapi.dict_hassplittable(a.__dict__)) + # dict.popitem() convert it to combined table + a.__dict__.popitem() + self.assertFalse(_testcapi.dict_hassplittable(a.__dict__)) + # But C should not convert a.__dict__ to split table again. + a.a = 3 + self.assertFalse(_testcapi.dict_hassplittable(a.__dict__)) + def test_iterator_pickling(self): for proto in range(pickle.HIGHEST_PROTOCOL + 1): data = {1:"a", 2:"b", 3:"c"} it = iter(data) d = pickle.dumps(it, proto) it = pickle.loads(d) diff -r cf564121f9f0 Modules/_testcapimodule.c --- a/Modules/_testcapimodule.c Mon Dec 19 21:01:33 2016 +0800 +++ b/Modules/_testcapimodule.c Tue Dec 20 02:51:24 2016 +0900 @@ -246,12 +246,21 @@ Py_INCREF(Py_None); return Py_None; } +static PyObject* +dict_hassplittable(PyObject *self, PyObject *arg) +{ + if (!PyArg_Parse(arg, "O!:dict_hassplittable", &PyDict_Type, &arg)) { + return NULL; + } + return PyBool_FromLong(_PyDict_HasSplitTable((PyDictObject*)arg)); +} + /* Issue #4701: Check that PyObject_Hash implicitly calls * PyType_Ready if it hasn't already been called */ static PyTypeObject _HashInheritanceTester_Type = { PyVarObject_HEAD_INIT(NULL, 0) "hashinheritancetester", /* Name of this type */ @@ -3855,12 +3864,13 @@ {"set_errno", set_errno, METH_VARARGS}, {"test_config", (PyCFunction)test_config, METH_NOARGS}, {"test_sizeof_c_types", (PyCFunction)test_sizeof_c_types, METH_NOARGS}, {"test_datetime_capi", test_datetime_capi, METH_NOARGS}, {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS}, {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, + {"dict_hassplittable", dict_hassplittable, METH_O}, {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS}, {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS}, {"test_xincref_doesnt_leak",(PyCFunction)test_xincref_doesnt_leak, METH_NOARGS}, {"test_incref_doesnt_leak", (PyCFunction)test_incref_doesnt_leak, METH_NOARGS}, {"test_xdecref_doesnt_leak",(PyCFunction)test_xdecref_doesnt_leak, METH_NOARGS}, {"test_decref_doesnt_leak", (PyCFunction)test_decref_doesnt_leak, METH_NOARGS}, diff -r cf564121f9f0 Objects/dictobject.c --- a/Objects/dictobject.c Mon Dec 19 21:01:33 2016 +0800 +++ b/Objects/dictobject.c Tue Dec 20 02:51:24 2016 +0900 @@ -982,14 +982,16 @@ PyObject **values; assert(mp->ma_keys->dk_refcnt == 1); if (mp->ma_keys->dk_lookup == lookdict) { return NULL; } else if (mp->ma_keys->dk_lookup == lookdict_unicode) { - /* Remove dummy keys */ - if (dictresize(mp, DK_SIZE(mp->ma_keys))) + /* Remove dummy keys + * -1 is required since dictresize() uses key size > minused + */ + if (dictresize(mp, DK_SIZE(mp->ma_keys) - 1)) return NULL; } assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy); /* Copy values into a new array */ ep0 = &mp->ma_keys->dk_entries[0]; size = DK_SIZE(mp->ma_keys); @@ -2470,13 +2472,14 @@ PyErr_SetString(PyExc_KeyError, "popitem(): dictionary is empty"); return NULL; } /* Convert split table to combined table */ if (mp->ma_keys->dk_lookup == lookdict_split) { - if (dictresize(mp, DK_SIZE(mp->ma_keys))) { + /* -1 is required since dictresize() uses key size > minused */ + if (dictresize(mp, DK_SIZE(mp->ma_keys) - 1)) { Py_DECREF(res); return NULL; } } ENSURE_ALLOWS_DELETIONS(mp); /* Set ep to "the first" dict entry with a value. We abuse the hash @@ -3845,16 +3848,22 @@ if (value == NULL) { res = PyDict_DelItem(dict, key); if (cached != ((PyDictObject *)dict)->ma_keys) { CACHED_KEYS(tp) = NULL; DK_DECREF(cached); } - } else { + } + else { + int was_shared = cached == ((PyDictObject *)dict)->ma_keys; res = PyDict_SetItem(dict, key, value); - if (cached != ((PyDictObject *)dict)->ma_keys) { - /* Either update tp->ht_cached_keys or delete it */ + /* PyDict_SetItem() may call dictresize() and convert split table + * into combined table. In such case, convert it to split + * table again and update type's shared key only when this is + * the only dict sharing key with the type. + */ + if (was_shared && cached != ((PyDictObject *)dict)->ma_keys) { if (cached->dk_refcnt == 1) { CACHED_KEYS(tp) = make_keys_shared(dict); } else { CACHED_KEYS(tp) = NULL; } DK_DECREF(cached);