mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
bpo-44821: Eagerly assign __dict__ for new objects. (GH-27589)
This commit is contained in:
parent
c83919bd63
commit
cee67fa661
6 changed files with 49 additions and 8 deletions
|
@ -4866,19 +4866,44 @@ _PyDict_NewKeysForClass(void)
|
|||
|
||||
#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
|
||||
|
||||
int
|
||||
_PyObject_InitializeDict(PyObject *obj)
|
||||
{
|
||||
PyObject **dictptr = _PyObject_GetDictPtr(obj);
|
||||
if (dictptr == NULL) {
|
||||
return 0;
|
||||
}
|
||||
assert(*dictptr == NULL);
|
||||
PyTypeObject *tp = Py_TYPE(obj);
|
||||
PyObject *dict;
|
||||
if (_PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
|
||||
dictkeys_incref(CACHED_KEYS(tp));
|
||||
dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
|
||||
}
|
||||
else {
|
||||
dict = PyDict_New();
|
||||
}
|
||||
if (dict == NULL) {
|
||||
return -1;
|
||||
}
|
||||
*dictptr = dict;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
PyObject *
|
||||
PyObject_GenericGetDict(PyObject *obj, void *context)
|
||||
{
|
||||
PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
|
||||
PyObject **dictptr = _PyObject_GetDictPtr(obj);
|
||||
if (dictptr == NULL) {
|
||||
PyErr_SetString(PyExc_AttributeError,
|
||||
"This object has no __dict__");
|
||||
return NULL;
|
||||
}
|
||||
dict = *dictptr;
|
||||
PyObject *dict = *dictptr;
|
||||
if (dict == NULL) {
|
||||
PyTypeObject *tp = Py_TYPE(obj);
|
||||
if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
|
||||
if (_PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
|
||||
dictkeys_incref(CACHED_KEYS(tp));
|
||||
*dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue