diff -r c4f39b6f3176 Lib/test/test_dict_impl.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lib/test/test_dict_impl.py Thu Dec 08 18:04:54 2016 +0900 @@ -0,0 +1,43 @@ +"""Tests for dict implementation""" + +import struct +import sys +from test import support +import unittest + +_testcapi = support.import_module('_testcapi') + + +@support.cpython_only +class SplitTableTest(unittest.TestCase): + + def test_setattr_after_pop(self): + """setattr must not convert combined table into split table""" + # Issue 28147 + class C: + pass + a = C() + + a.a = 1 + self.assertTrue(_testcapi.dict_hassplittable(a.__dict__)) + + # dict.pop() convert it to combined table + a.__dict__.pop('a') + self.assertFalse(_testcapi.dict_hassplittable(a.__dict__)) + + # But C should not convert a.__dict__ to split table again. + a.a = 1 + self.assertFalse(_testcapi.dict_hassplittable(a.__dict__)) + + # Same for popitem() + a = C() + a.a = 2 + self.assertTrue(_testcapi.dict_hassplittable(a.__dict__)) + a.__dict__.popitem() + self.assertFalse(_testcapi.dict_hassplittable(a.__dict__)) + a.a = 3 + self.assertFalse(_testcapi.dict_hassplittable(a.__dict__)) + + +if __name__ == "__main__": + unittest.main() diff -r c4f39b6f3176 Modules/_testcapimodule.c --- a/Modules/_testcapimodule.c Wed Dec 07 13:31:47 2016 +0200 +++ b/Modules/_testcapimodule.c Thu Dec 08 18:04:54 2016 +0900 @@ -259,6 +259,22 @@ dict_getitem_knownhash(PyObject *self, P return result; } +static PyObject* +dict_hassplittable(PyObject *self, PyObject *arg) +{ + if (!PyDict_Check(arg)) { + PyErr_Format(PyExc_TypeError, + "dict_hassplittable() argument must be dict, not '%s'", + arg->ob_type->tp_name); + return NULL; + } + + if (_PyDict_HasSplitTable((PyDictObject*)arg)) { + Py_RETURN_TRUE; + } + Py_RETURN_FALSE; +} + /* Issue #4701: Check that PyObject_Hash implicitly calls * PyType_Ready if it hasn't already been called */ @@ -4024,6 +4040,7 @@ static PyMethodDef TestMethods[] = { {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS}, {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, {"dict_getitem_knownhash", dict_getitem_knownhash, METH_VARARGS}, + {"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}, diff -r c4f39b6f3176 Objects/dictobject.c --- a/Objects/dictobject.c Wed Dec 07 13:31:47 2016 +0200 +++ b/Objects/dictobject.c Thu Dec 08 18:04:54 2016 +0900 @@ -1245,7 +1245,7 @@ After resizing a table is always combine but can be resplit by make_keys_shared(). */ static int -dictresize(PyDictObject *mp, Py_ssize_t minused) +dictresize(PyDictObject *mp, Py_ssize_t minsize) { Py_ssize_t i, newsize; PyDictKeysObject *oldkeys; @@ -1254,7 +1254,7 @@ dictresize(PyDictObject *mp, Py_ssize_t /* Find the smallest table size > minused. */ for (newsize = PyDict_MINSIZE; - newsize <= minused && newsize > 0; + newsize < minsize && newsize > 0; newsize <<= 1) ; if (newsize <= 0) { @@ -1269,6 +1269,8 @@ dictresize(PyDictObject *mp, Py_ssize_t mp->ma_keys = oldkeys; return -1; } + // New table must be large enough. + assert(mp->ma_keys->dk_usable >= mp->ma_used); if (oldkeys->dk_lookup == lookdict) mp->ma_keys->dk_lookup = lookdict; mp->ma_values = NULL; @@ -4306,9 +4308,11 @@ int CACHED_KEYS(tp) = NULL; DK_DECREF(cached); } - } else { + } + else { + int shared = cached == ((PyDictObject *)dict)->ma_keys; res = PyDict_SetItem(dict, key, value); - if (cached != ((PyDictObject *)dict)->ma_keys) { + if (shared && cached != ((PyDictObject *)dict)->ma_keys) { /* Either update tp->ht_cached_keys or delete it */ if (cached->dk_refcnt == 1) { CACHED_KEYS(tp) = make_keys_shared(dict);