Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 15ee821

Browse files
committed
distiguish between refusing to creating shared keys and error (#13903)
1 parent 17feca0 commit 15ee821

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

Lib/test/test_dict.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,13 @@ class _str(str):
889889
self.assertEqual(f.msg, getattr(f, _str('msg')))
890890
self.assertEqual(f.msg, f.__dict__[_str('msg')])
891891

892+
def test_object_set_item_single_instance_non_str_key(self):
893+
class Foo: pass
894+
f = Foo()
895+
f.__dict__[1] = 1
896+
f.a = 'a'
897+
self.assertEqual(f.__dict__, {1:1, 'a':'a'})
898+
892899
from test import mapping_tests
893900

894901
class GeneralMappingTests(mapping_tests.BasicTestMappingProtocol):

Objects/dictobject.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -966,14 +966,17 @@ dictresize(PyDictObject *mp, Py_ssize_t minused)
966966
return 0;
967967
}
968968

969+
/* Returns NULL if unable to split table.
970+
* A NULL return does not necessarily indicate an error */
969971
static PyDictKeysObject *
970972
make_keys_shared(PyObject *op)
971973
{
972974
Py_ssize_t i;
973975
Py_ssize_t size;
974976
PyDictObject *mp = (PyDictObject *)op;
975977

976-
assert(PyDict_CheckExact(op));
978+
if (!PyDict_CheckExact(op))
979+
return NULL;
977980
if (!_PyDict_HasSplitTable(mp)) {
978981
PyDictKeyEntry *ep0;
979982
PyObject **values;
@@ -3694,14 +3697,14 @@ _PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
36943697
res = PyDict_SetItem(dict, key, value);
36953698
if (cached != ((PyDictObject *)dict)->ma_keys) {
36963699
/* Either update tp->ht_cached_keys or delete it */
3697-
if (cached->dk_refcnt == 1 && PyDict_CheckExact(dict)) {
3700+
if (cached->dk_refcnt == 1) {
36983701
CACHED_KEYS(tp) = make_keys_shared(dict);
3699-
if (CACHED_KEYS(tp) == NULL)
3700-
return -1;
37013702
} else {
37023703
CACHED_KEYS(tp) = NULL;
37033704
}
37043705
DK_DECREF(cached);
3706+
if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
3707+
return -1;
37053708
}
37063709
}
37073710
} else {

0 commit comments

Comments
 (0)