diff -r 514571268743 Objects/dictobject.c --- a/Objects/dictobject.c Tue Feb 07 11:27:09 2017 +0300 +++ b/Objects/dictobject.c Sat Feb 11 03:15:04 2017 +0900 @@ -3890,26 +3890,24 @@ if (dict == NULL) return -1; *dictptr = dict; } if (value == NULL) { res = PyDict_DelItem(dict, key); - if (cached != ((PyDictObject *)dict)->ma_keys) { - CACHED_KEYS(tp) = NULL; - DK_DECREF(cached); - } } else { - int was_shared = cached == ((PyDictObject *)dict)->ma_keys; + int was_shared = (cached == ((PyDictObject *)dict)->ma_keys); res = PyDict_SetItem(dict, key, value); /* 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 (was_shared && + (cached = CACHED_KEYS(tp)) != NULL && + 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);