GeezerVison is showing github.com — GH-115775: Use `__static_attributes__` to initialize shared keys (GH-… · 2union/ircpython@89328f7
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: Include/internal/pycore_dict.h +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -83,7 +83,7 @@ typedef struct { |
83 | 83 | PyObject *me_value; /* This field is only meaningful for combined tables */ |
84 | 84 | } PyDictUnicodeEntry; |
85 | 85 |
|
86 | | -extern PyDictKeysObject *_PyDict_NewKeysForClass(void); |
| 86 | +extern PyDictKeysObject *_PyDict_NewKeysForClass(PyHeapTypeObject *); |
87 | 87 | extern PyObject *_PyDict_FromKeys(PyObject *, PyObject *, PyObject *); |
88 | 88 |
|
89 | 89 | /* Gets a version number unique to the current state of the keys of dict, if possible. |
|
Collapse file
Copy file name to clipboardExpand all lines: Objects/dictobject.c +16-1Lines changed: 16 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -6559,9 +6559,10 @@ dictvalues_reversed(PyObject *self, PyObject *Py_UNUSED(ignored)) |
6559 | 6559 | /* Returns NULL if cannot allocate a new PyDictKeysObject, |
6560 | 6560 | but does not set an error */ |
6561 | 6561 | PyDictKeysObject * |
6562 | | -_PyDict_NewKeysForClass(void) |
| 6562 | +_PyDict_NewKeysForClass(PyHeapTypeObject *cls) |
6563 | 6563 | { |
6564 | 6564 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
| 6565 | + |
6565 | 6566 | PyDictKeysObject *keys = new_keys_object( |
6566 | 6567 | interp, NEXT_LOG2_SHARED_KEYS_MAX_SIZE, 1); |
6567 | 6568 | if (keys == NULL) { |
@@ -6573,6 +6574,20 @@ _PyDict_NewKeysForClass(void) |
6573 | 6574 | keys->dk_usable = SHARED_KEYS_MAX_SIZE; |
6574 | 6575 | keys->dk_kind = DICT_KEYS_SPLIT; |
6575 | 6576 | } |
| 6577 | + if (cls->ht_type.tp_dict) { |
| 6578 | + PyObject *attrs = PyDict_GetItem(cls->ht_type.tp_dict, &_Py_ID(__static_attributes__)); |
| 6579 | + if (attrs != NULL && PyTuple_Check(attrs)) { |
| 6580 | + for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(attrs); i++) { |
| 6581 | + PyObject *key = PyTuple_GET_ITEM(attrs, i); |
| 6582 | + Py_hash_t hash; |
| 6583 | + if (PyUnicode_CheckExact(key) && (hash = unicode_get_hash(key)) != -1) { |
| 6584 | + if (insert_split_key(keys, key, hash) == DKIX_EMPTY) { |
| 6585 | + break; |
| 6586 | + } |
| 6587 | + } |
| 6588 | + } |
| 6589 | + } |
| 6590 | + } |
6576 | 6591 | return keys; |
6577 | 6592 | } |
6578 | 6593 |
|
|
Collapse file
Copy file name to clipboardExpand all lines: Objects/typeobject.c +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -8334,7 +8334,7 @@ type_ready_managed_dict(PyTypeObject *type) |
8334 | 8334 | } |
8335 | 8335 | PyHeapTypeObject* et = (PyHeapTypeObject*)type; |
8336 | 8336 | if (et->ht_cached_keys == NULL) { |
8337 | | - et->ht_cached_keys = _PyDict_NewKeysForClass(); |
| 8337 | + et->ht_cached_keys = _PyDict_NewKeysForClass(et); |
8338 | 8338 | if (et->ht_cached_keys == NULL) { |
8339 | 8339 | PyErr_NoMemory(); |
8340 | 8340 | return -1; |
|