Skip to content

Commit

Permalink
Use __static_attributes__ to initialize shared keys
Browse files Browse the repository at this point in the history
  • Loading branch information
markshannon committed May 1, 2024
1 parent 21c09d9 commit dedffa7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Include/internal/pycore_dict.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ typedef struct {
PyObject *me_value; /* This field is only meaningful for combined tables */
} PyDictUnicodeEntry;

extern PyDictKeysObject *_PyDict_NewKeysForClass(void);
extern PyDictKeysObject *_PyDict_NewKeysForClass(PyHeapTypeObject *);
extern PyObject *_PyDict_FromKeys(PyObject *, PyObject *, PyObject *);

/* Gets a version number unique to the current state of the keys of dict, if possible.
Expand Down
15 changes: 14 additions & 1 deletion Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -6586,9 +6586,10 @@ dictvalues_reversed(PyObject *self, PyObject *Py_UNUSED(ignored))
/* Returns NULL if cannot allocate a new PyDictKeysObject,
but does not set an error */
PyDictKeysObject *
_PyDict_NewKeysForClass(void)
_PyDict_NewKeysForClass(PyHeapTypeObject *cls)
{
PyInterpreterState *interp = _PyInterpreterState_GET();

PyDictKeysObject *keys = new_keys_object(
interp, NEXT_LOG2_SHARED_KEYS_MAX_SIZE, 1);
if (keys == NULL) {
Expand All @@ -6600,6 +6601,18 @@ _PyDict_NewKeysForClass(void)
keys->dk_usable = SHARED_KEYS_MAX_SIZE;
keys->dk_kind = DICT_KEYS_SPLIT;
}
if (cls->ht_type.tp_dict) {
PyObject *attrs = PyDict_GetItem(cls->ht_type.tp_dict, &_Py_ID(__static_attributes__));
if (attrs != NULL && PyTuple_Check(attrs)) {
for (Py_ssize_t i; i < PyTuple_GET_SIZE(attrs); i++) {

Check warning on line 6607 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Address sanitizer

‘i’ may be used uninitialized in this function [-Wmaybe-uninitialized]

Check warning on line 6607 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (1.1.1w)

‘i’ may be used uninitialized in this function [-Wmaybe-uninitialized]

Check warning on line 6607 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.0.13)

‘i’ may be used uninitialized in this function [-Wmaybe-uninitialized]

Check warning on line 6607 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.1.5)

‘i’ may be used uninitialized in this function [-Wmaybe-uninitialized]

Check warning on line 6607 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

‘i’ may be used uninitialized in this function [-Wmaybe-uninitialized]

Check warning on line 6607 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.2.1)

‘i’ may be used uninitialized in this function [-Wmaybe-uninitialized]

Check warning on line 6607 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

uninitialized local variable 'i' used [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 6607 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

uninitialized local variable 'i' used [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 6607 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Windows / build (arm64)

uninitialized local variable 'i' used [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 6607 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Windows / build (arm64)

uninitialized local variable 'i' used [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 6607 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test

‘i’ may be used uninitialized in this function [-Wmaybe-uninitialized]

Check warning on line 6607 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (x64)

uninitialized local variable 'i' used [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 6607 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (x64)

uninitialized local variable 'i' used [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 6607 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build (arm64)

uninitialized local variable 'i' used [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 6607 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build (arm64)

uninitialized local variable 'i' used [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 6607 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test

‘i’ may be used uninitialized in this function [-Wmaybe-uninitialized]
PyObject *key = PyTuple_GET_ITEM(attrs, i);
Py_hash_t hash;
if (PyUnicode_CheckExact(key) && (hash = unicode_get_hash(key)) != -1) {
insert_split_key(keys, key, hash);
}
}
}
}
return keys;
}

Expand Down
2 changes: 1 addition & 1 deletion Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -7883,7 +7883,7 @@ type_ready_managed_dict(PyTypeObject *type)
}
PyHeapTypeObject* et = (PyHeapTypeObject*)type;
if (et->ht_cached_keys == NULL) {
et->ht_cached_keys = _PyDict_NewKeysForClass();
et->ht_cached_keys = _PyDict_NewKeysForClass(et);
if (et->ht_cached_keys == NULL) {
PyErr_NoMemory();
return -1;
Expand Down

0 comments on commit dedffa7

Please sign in to comment.