diff -r 3275d4b584a2 Objects/dictobject.c --- a/Objects/dictobject.c Wed Jan 25 23:35:40 2017 -0800 +++ b/Objects/dictobject.c Tue Feb 07 17:29:56 2017 +0900 @@ -4376,7 +4376,8 @@ int } if (value == NULL) { res = PyDict_DelItem(dict, key); - if (cached != ((PyDictObject *)dict)->ma_keys) { + // key sharing dict doesn't allow deletion. + if (cached == CACHED_KEYS(tp)) { CACHED_KEYS(tp) = NULL; DK_DECREF(cached); } @@ -4384,7 +4385,9 @@ int else { int was_shared = cached == ((PyDictObject *)dict)->ma_keys; res = PyDict_SetItem(dict, key, value); - if (was_shared && cached != ((PyDictObject *)dict)->ma_keys) { + if (res == 0 && was_shared && + cached != ((PyDictObject *)dict)->ma_keys && + cached == CACHED_KEYS(tp)) { /* 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