summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorDino Viehland <dinoviehland@meta.com>2024-05-06 17:50:35 (GMT)
committerGitHub <noreply@github.com>2024-05-06 17:50:35 (GMT)
commit5a1618a2c8c108b8c73aa9459b63f0dbd66b60f6 (patch)
treea9b79e4cb8e0a9d1d648e21619382a03363c1f46 /Objects
parente6b213ee3ffb05f067d30cb8bb45681887212444 (diff)
downloadcpython-5a1618a2c8c108b8c73aa9459b63f0dbd66b60f6.zip
cpython-5a1618a2c8c108b8c73aa9459b63f0dbd66b60f6.tar.gz
cpython-5a1618a2c8c108b8c73aa9459b63f0dbd66b60f6.tar.bz2
gh-118362: Fix thread safety around lookups from the type cache in the face of concurrent mutators (#118454)
Add _PyType_LookupRef and use incref before setting attribute on type Makes setting an attribute on a class and signaling type modified atomic Avoid adding re-entrancy exposing the type cache in an inconsistent state by decrefing after type is updated
Diffstat (limited to 'Objects')
-rw-r--r--Objects/classobject.c22
-rw-r--r--Objects/dictobject.c50
-rw-r--r--Objects/object.c40
-rw-r--r--Objects/typeobject.c269
4 files changed, 263 insertions, 118 deletions
diff --git a/Objects/classobject.c b/Objects/classobject.c
index 9cbb944..69a7d5f 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -188,15 +188,18 @@ method_getattro(PyObject *obj, PyObject *name)
if (PyType_Ready(tp) < 0)
return NULL;
}
- descr = _PyType_Lookup(tp, name);
+ descr = _PyType_LookupRef(tp, name);
}
if (descr != NULL) {
descrgetfunc f = TP_DESCR_GET(Py_TYPE(descr));
- if (f != NULL)
- return f(descr, obj, (PyObject *)Py_TYPE(obj));
+ if (f != NULL) {
+ PyObject *res = f(descr, obj, (PyObject *)Py_TYPE(obj));
+ Py_DECREF(descr);
+ return res;
+ }
else {
- return Py_NewRef(descr);
+ return descr;
}
}
@@ -410,14 +413,17 @@ instancemethod_getattro(PyObject *self, PyObject *name)
if (PyType_Ready(tp) < 0)
return NULL;
}
- descr = _PyType_Lookup(tp, name);
+ descr = _PyType_LookupRef(tp, name);
if (descr != NULL) {
descrgetfunc f = TP_DESCR_GET(Py_TYPE(descr));
- if (f != NULL)
- return f(descr, self, (PyObject *)Py_TYPE(self));
+ if (f != NULL) {
+ PyObject *res = f(descr, self, (PyObject *)Py_TYPE(self));
+ Py_DECREF(descr);
+ return res;
+ }
else {
- return Py_NewRef(descr);
+ return descr;
}
}
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index 1f21f70..3e662e0 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -2268,15 +2268,13 @@ _PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
* exception occurred.
*/
int
-_PyDict_GetItemRef_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash, PyObject **result)
+_PyDict_GetItemRef_KnownHash(PyDictObject *op, PyObject *key, Py_hash_t hash, PyObject **result)
{
- PyDictObject*mp = (PyDictObject *)op;
-
PyObject *value;
#ifdef Py_GIL_DISABLED
- Py_ssize_t ix = _Py_dict_lookup_threadsafe(mp, key, hash, &value);
+ Py_ssize_t ix = _Py_dict_lookup_threadsafe(op, key, hash, &value);
#else
- Py_ssize_t ix = _Py_dict_lookup(mp, key, hash, &value);
+ Py_ssize_t ix = _Py_dict_lookup(op, key, hash, &value);
#endif
assert(ix >= 0 || value == NULL);
if (ix == DKIX_ERROR) {
@@ -2314,9 +2312,38 @@ PyDict_GetItemRef(PyObject *op, PyObject *key, PyObject **result)
}
}
- return _PyDict_GetItemRef_KnownHash(op, key, hash, result);
+ return _PyDict_GetItemRef_KnownHash((PyDictObject *)op, key, hash, result);
}
+int
+_PyDict_GetItemRef_Unicode_LockHeld(PyDictObject *op, PyObject *key, PyObject **result)
+{
+ ASSERT_DICT_LOCKED(op);
+ assert(PyUnicode_CheckExact(key));
+
+ Py_hash_t hash;
+ if ((hash = unicode_get_hash(key)) == -1) {
+ hash = PyObject_Hash(key);
+ if (hash == -1) {
+ *result = NULL;
+ return -1;
+ }
+ }
+
+ PyObject *value;
+ Py_ssize_t ix = _Py_dict_lookup(op, key, hash, &value);
+ assert(ix >= 0 || value == NULL);
+ if (ix == DKIX_ERROR) {
+ *result = NULL;
+ return -1;
+ }
+ if (value == NULL) {
+ *result = NULL;
+ return 0; // missing key
+ }
+ *result = Py_NewRef(value);
+ return 1; // key is present
+}
/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
This returns NULL *with* an exception set if an exception occurred.
@@ -6704,8 +6731,8 @@ exit:
return dict;
}
-static int
-set_or_del_lock_held(PyDictObject *dict, PyObject *name, PyObject *value)
+int
+_PyDict_SetItem_LockHeld(PyDictObject *dict, PyObject *name, PyObject *value)
{
if (value == NULL) {
Py_hash_t hash;
@@ -6767,7 +6794,7 @@ store_instance_attr_lock_held(PyObject *obj, PyDictValues *values,
// so that no one else will see it.
dict = make_dict_from_instance_attributes(PyInterpreterState_Get(), keys, values);
if (dict == NULL ||
- set_or_del_lock_held(dict, name, value) < 0) {
+ _PyDict_SetItem_LockHeld(dict, name, value) < 0) {
Py_XDECREF(dict);
return -1;
}
@@ -6779,7 +6806,7 @@ store_instance_attr_lock_held(PyObject *obj, PyDictValues *values,
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(dict);
- res = set_or_del_lock_held (dict, name, value);
+ res = _PyDict_SetItem_LockHeld(dict, name, value);
return res;
}
@@ -6822,7 +6849,7 @@ store_instance_attr_dict(PyObject *obj, PyDictObject *dict, PyObject *name, PyOb
res = store_instance_attr_lock_held(obj, values, name, value);
}
else {
- res = set_or_del_lock_held(dict, name, value);
+ res = _PyDict_SetItem_LockHeld(dict, name, value);
}
Py_END_CRITICAL_SECTION();
return res;
@@ -7253,6 +7280,7 @@ _PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
res = PyDict_SetItem(dict, key, value);
}
}
+
ASSERT_CONSISTENT(dict);
return res;
}
diff --git a/Objects/object.c b/Objects/object.c
index 1bf0e65..effbd51 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -1132,8 +1132,8 @@ _PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w)
return result;
}
-static inline int
-set_attribute_error_context(PyObject* v, PyObject* name)
+int
+_PyObject_SetAttributeErrorContext(PyObject* v, PyObject* name)
{
assert(PyErr_Occurred());
if (!PyErr_ExceptionMatches(PyExc_AttributeError)){
@@ -1188,7 +1188,7 @@ PyObject_GetAttr(PyObject *v, PyObject *name)
}
if (result == NULL) {
- set_attribute_error_context(v, name);
+ _PyObject_SetAttributeErrorContext(v, name);
}
return result;
}
@@ -1466,13 +1466,13 @@ _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
return 0;
}
- PyObject *descr = _PyType_Lookup(tp, name);
+ PyObject *descr = _PyType_LookupRef(tp, name);
descrgetfunc f = NULL;
if (descr != NULL) {
- Py_INCREF(descr);
if (_PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR)) {
meth_found = 1;
- } else {
+ }
+ else {
f = Py_TYPE(descr)->tp_descr_get;
if (f != NULL && PyDescr_IsData(descr)) {
*method = f(descr, obj, (PyObject *)Py_TYPE(obj));
@@ -1535,7 +1535,7 @@ _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
"'%.100s' object has no attribute '%U'",
tp->tp_name, name);
- set_attribute_error_context(obj, name);
+ _PyObject_SetAttributeErrorContext(obj, name);
return 0;
}
@@ -1569,11 +1569,10 @@ _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
goto done;
}
- descr = _PyType_Lookup(tp, name);
+ descr = _PyType_LookupRef(tp, name);
f = NULL;
if (descr != NULL) {
- Py_INCREF(descr);
f = Py_TYPE(descr)->tp_descr_get;
if (f != NULL && PyDescr_IsData(descr)) {
res = f(descr, obj, (PyObject *)Py_TYPE(obj));
@@ -1647,7 +1646,7 @@ _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
"'%.100s' object has no attribute '%U'",
tp->tp_name, name);
- set_attribute_error_context(obj, name);
+ _PyObject_SetAttributeErrorContext(obj, name);
}
done:
Py_XDECREF(descr);
@@ -1670,6 +1669,7 @@ _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
descrsetfunc f;
int res = -1;
+ assert(!PyType_IsSubtype(tp, &PyType_Type));
if (!PyUnicode_Check(name)){
PyErr_Format(PyExc_TypeError,
"attribute name must be string, not '%.200s'",
@@ -1683,10 +1683,9 @@ _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
Py_INCREF(name);
Py_INCREF(tp);
- descr = _PyType_Lookup(tp, name);
+ descr = _PyType_LookupRef(tp, name);
if (descr != NULL) {
- Py_INCREF(descr);
f = Py_TYPE(descr)->tp_descr_set;
if (f != NULL) {
res = f(descr, obj, value);
@@ -1722,7 +1721,7 @@ _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
"'%.100s' object has no attribute '%U'",
tp->tp_name, name);
}
- set_attribute_error_context(obj, name);
+ _PyObject_SetAttributeErrorContext(obj, name);
}
else {
PyErr_Format(PyExc_AttributeError,
@@ -1745,17 +1744,10 @@ _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
}
error_check:
if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) {
- if (PyType_IsSubtype(tp, &PyType_Type)) {
- PyErr_Format(PyExc_AttributeError,
- "type object '%.50s' has no attribute '%U'",
- ((PyTypeObject*)obj)->tp_name, name);
- }
- else {
- PyErr_Format(PyExc_AttributeError,
- "'%.100s' object has no attribute '%U'",
- tp->tp_name, name);
- }
- set_attribute_error_context(obj, name);
+ PyErr_Format(PyExc_AttributeError,
+ "'%.100s' object has no attribute '%U'",
+ tp->tp_name, name);
+ _PyObject_SetAttributeErrorContext(obj, name);
}
done:
Py_XDECREF(descr);
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index ec19a3d..4b144fa 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -71,6 +71,16 @@ class object "PyObject *" "&PyBaseObject_Type"
_PyCriticalSection_End(&_cs); \
}
+#define BEGIN_TYPE_DICT_LOCK(d) \
+ { \
+ _PyCriticalSection2 _cs; \
+ _PyCriticalSection2_Begin(&_cs, TYPE_LOCK, \
+ &_PyObject_CAST(d)->ob_mutex); \
+
+#define END_TYPE_DICT_LOCK() \
+ _PyCriticalSection2_End(&_cs); \
+ }
+
#define ASSERT_TYPE_LOCK_HELD() \
_Py_CRITICAL_SECTION_ASSERT_MUTEX_LOCKED(TYPE_LOCK)
@@ -78,6 +88,8 @@ class object "PyObject *" "&PyBaseObject_Type"
#define BEGIN_TYPE_LOCK()
#define END_TYPE_LOCK()
+#define BEGIN_TYPE_DICT_LOCK(d)
+#define END_TYPE_DICT_LOCK()
#define ASSERT_TYPE_LOCK_HELD()
#endif
@@ -728,7 +740,7 @@ _PyType_InitCache(PyInterpreterState *interp)
assert(entry->name == NULL);
entry->version = 0;
- // Set to None so _PyType_Lookup() can use Py_SETREF(),
+ // Set to None so _PyType_LookupRef() can use Py_SETREF(),
// rather than using slower Py_XSETREF().
entry->name = Py_None;
entry->value = NULL;
@@ -740,7 +752,7 @@ static unsigned int
_PyType_ClearCache(PyInterpreterState *interp)
{
struct type_cache *cache = &interp->types.type_cache;
- // Set to None, rather than NULL, so _PyType_Lookup() can
+ // Set to None, rather than NULL, so _PyType_LookupRef() can
// use Py_SETREF() rather than using slower Py_XSETREF().
type_cache_clear(cache, Py_None);
@@ -849,6 +861,44 @@ PyType_Unwatch(int watcher_id, PyObject* obj)
return 0;
}
+#ifdef Py_GIL_DISABLED
+
+static void
+type_modification_starting_unlocked(PyTypeObject *type)
+{
+ ASSERT_TYPE_LOCK_HELD();
+
+ /* Clear version tags on all types, but leave the valid
+ version tag intact. This prepares for a modification so that
+ any concurrent readers of the type cache will not see invalid
+ values.
+ */
+ if (!_PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) {
+ return;
+ }
+
+ PyObject *subclasses = lookup_tp_subclasses(type);
+ if (subclasses != NULL) {
+ assert(PyDict_CheckExact(subclasses));
+
+ Py_ssize_t i = 0;
+ PyObject *ref;
+ while (PyDict_Next(subclasses, &i, NULL, &ref)) {
+ PyTypeObject *subclass = type_from_ref(ref);
+ if (subclass == NULL) {
+ continue;
+ }
+ type_modification_starting_unlocked(subclass);
+ Py_DECREF(subclass);
+ }
+ }
+
+ /* 0 is not a valid version tag */
+ _Py_atomic_store_uint32_release(&type->tp_version_tag, 0);
+}
+
+#endif
+
static void
type_modified_unlocked(PyTypeObject *type)
{
@@ -882,7 +932,7 @@ type_modified_unlocked(PyTypeObject *type)
if (subclass == NULL) {
continue;
}
- PyType_Modified(subclass);
+ type_modified_unlocked(subclass);
Py_DECREF(subclass);
}
}
@@ -2352,7 +2402,7 @@ PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
Variants:
- _PyObject_LookupSpecial() returns NULL without raising an exception
- when the _PyType_Lookup() call fails;
+ when the _PyType_LookupRef() call fails;
- lookup_maybe_method() and lookup_method() are internal routines similar
to _PyObject_LookupSpecial(), but can return unbound PyFunction
@@ -2365,13 +2415,12 @@ _PyObject_LookupSpecial(PyObject *self, PyObject *attr)
{
PyObject *res;
- res = _PyType_Lookup(Py_TYPE(self), attr);
+ res = _PyType_LookupRef(Py_TYPE(self), attr);
if (res != NULL) {
descrgetfunc f;
- if ((f = Py_TYPE(res)->tp_descr_get) == NULL)
- Py_INCREF(res);
- else
- res = f(res, self, (PyObject *)(Py_TYPE(self)));
+ if ((f = Py_TYPE(res)->tp_descr_get) != NULL) {
+ Py_SETREF(res, f(res, self, (PyObject *)(Py_TYPE(self))));
+ }
}
return res;
}
@@ -2388,7 +2437,7 @@ _PyObject_LookupSpecialId(PyObject *self, _Py_Identifier *attrid)
static PyObject *
lookup_maybe_method(PyObject *self, PyObject *attr, int *unbound)
{
- PyObject *res = _PyType_Lookup(Py_TYPE(self), attr);
+ PyObject *res = _PyType_LookupRef(Py_TYPE(self), attr);
if (res == NULL) {
return NULL;
}
@@ -2396,16 +2445,12 @@ lookup_maybe_method(PyObject *self, PyObject *attr, int *unbound)
if (_PyType_HasFeature(Py_TYPE(res), Py_TPFLAGS_METHOD_DESCRIPTOR)) {
/* Avoid temporary PyMethodObject */
*unbound = 1;
- Py_INCREF(res);
}
else {
*unbound = 0;
descrgetfunc f = Py_TYPE(res)->tp_descr_get;
- if (f == NULL) {
- Py_INCREF(res);
- }
- else {
- res = f(res, self, (PyObject *)(Py_TYPE(self)));
+ if (f != NULL) {
+ Py_SETREF(res, f(res, self, (PyObject *)(Py_TYPE(self))));
}
}
return res;
@@ -4981,14 +5026,13 @@ find_name_in_mro(PyTypeObject *type, PyObject *name, int *error)
PyObject *base = PyTuple_GET_ITEM(mro, i);
PyObject *dict = lookup_tp_dict(_PyType_CAST(base));
assert(dict && PyDict_Check(dict));
- res = _PyDict_GetItem_KnownHash(dict, name, hash);
- if (res != NULL) {
- break;
- }
- if (PyErr_Occurred()) {
+ if (_PyDict_GetItemRef_KnownHash((PyDictObject *)dict, name, hash, &res) < 0) {
*error = -1;
goto done;
}
+ if (res != NULL) {
+ break;
+ }
}
*error = 0;
done:
@@ -5030,8 +5074,6 @@ update_cache(struct type_cache_entry *entry, PyObject *name, unsigned int versio
#if Py_GIL_DISABLED
-#define TYPE_CACHE_IS_UPDATING(sequence) (sequence & 0x01)
-
static void
update_cache_gil_disabled(struct type_cache_entry *entry, PyObject *name,
unsigned int version_tag, PyObject *value)
@@ -5075,7 +5117,7 @@ _PyTypes_AfterFork(void)
/* Internal API to look for a name through the MRO.
This returns a borrowed reference, and doesn't set an exception! */
PyObject *
-_PyType_Lookup(PyTypeObject *type, PyObject *name)
+_PyType_LookupRef(PyTypeObject *type, PyObject *name)
{
PyObject *res;
int error;
@@ -5089,19 +5131,25 @@ _PyType_Lookup(PyTypeObject *type, PyObject *name)
while (1) {
int sequence = _PySeqLock_BeginRead(&entry->sequence);
uint32_t entry_version = _Py_atomic_load_uint32_relaxed(&entry->version);
- uint32_t type_version = _Py_atomic_load_uint32_relaxed(&type->tp_version_tag);
+ uint32_t type_version = _Py_atomic_load_uint32_acquire(&type->tp_version_tag);
if (entry_version == type_version &&
_Py_atomic_load_ptr_relaxed(&entry->name) == name) {
- assert(_PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG));
OBJECT_STAT_INC_COND(type_cache_hits, !is_dunder_name(name));
OBJECT_STAT_INC_COND(type_cache_dunder_hits, is_dunder_name(name));
PyObject *value = _Py_atomic_load_ptr_relaxed(&entry->value);
-
// If the sequence is still valid then we're done
- if (_PySeqLock_EndRead(&entry->sequence, sequence)) {
- return value;
+ if (value == NULL || _Py_TryIncref(value)) {
+ if (_PySeqLock_EndRead(&entry->sequence, sequence)) {
+ return value;
+ }
+ Py_XDECREF(value);
+ }
+ else {
+ // If we can't incref the object we need to fallback to locking
+ break;
}
- } else {
+ }
+ else {
// cache miss
break;
}
@@ -5112,6 +5160,7 @@ _PyType_Lookup(PyTypeObject *type, PyObject *name)
assert(_PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG));
OBJECT_STAT_INC_COND(type_cache_hits, !is_dunder_name(name));
OBJECT_STAT_INC_COND(type_cache_dunder_hits, is_dunder_name(name));
+ Py_XINCREF(entry->value);
return entry->value;
}
#endif
@@ -5162,6 +5211,14 @@ _PyType_Lookup(PyTypeObject *type, PyObject *name)
}
PyObject *
+_PyType_Lookup(PyTypeObject *type, PyObject *name)
+{
+ PyObject *res = _PyType_LookupRef(type, name);
+ Py_XDECREF(res);
+ return res;
+}
+
+PyObject *
_PyType_LookupId(PyTypeObject *type, _Py_Identifier *name)
{
PyObject *oname;
@@ -5218,7 +5275,7 @@ _PyType_SetFlagsRecursive(PyTypeObject *self, unsigned long mask, unsigned long
}
/* This is similar to PyObject_GenericGetAttr(),
- but uses _PyType_Lookup() instead of just looking in type->tp_dict.
+ but uses _PyType_LookupRef() instead of just looking in type->tp_dict.
The argument suppress_missing_attribute is used to provide a
fast path for hasattr. The possible values are:
@@ -5254,10 +5311,9 @@ _Py_type_getattro_impl(PyTypeObject *type, PyObject *name, int * suppress_missin
meta_get = NULL;
/* Look for the attribute in the metatype */
- meta_attribute = _PyType_Lookup(metatype, name);
+ meta_attribute = _PyType_LookupRef(metatype, name);
if (meta_attribute != NULL) {
- Py_INCREF(meta_attribute);
meta_get = Py_TYPE(meta_attribute)->tp_descr_get;
if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
@@ -5274,10 +5330,9 @@ _Py_type_getattro_impl(PyTypeObject *type, PyObject *name, int * suppress_missin
/* No data descriptor found on metatype. Look in tp_dict of this
* type and its bases */
- attribute = _PyType_Lookup(type, name);
+ attribute = _PyType_LookupRef(type, name);
if (attribute != NULL) {
/* Implement descriptor functionality, if any */
- Py_INCREF(attribute);
descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get;
Py_XDECREF(meta_attribute);
@@ -5322,7 +5377,7 @@ _Py_type_getattro_impl(PyTypeObject *type, PyObject *name, int * suppress_missin
}
/* This is similar to PyObject_GenericGetAttr(),
- but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
+ but uses _PyType_LookupRef() instead of just looking in type->tp_dict. */
PyObject *
_Py_type_getattro(PyObject *type, PyObject *name)
{
@@ -5341,49 +5396,110 @@ type_setattro(PyObject *self, PyObject *name, PyObject *value)
name, type->tp_name);
return -1;
}
- if (PyUnicode_Check(name)) {
- if (PyUnicode_CheckExact(name)) {
- Py_INCREF(name);
+ if (!PyUnicode_Check(name)) {
+ PyErr_Format(PyExc_TypeError,
+ "attribute name must be string, not '%.200s'",
+ Py_TYPE(name)->tp_name);
+ return -1;
+ }
+
+ if (PyUnicode_CheckExact(name)) {
+ Py_INCREF(name);
+ }
+ else {
+ name = _PyUnicode_Copy(name);
+ if (name == NULL)
+ return -1;
+ }
+ /* bpo-40521: Interned strings are shared by all subinterpreters */
+ if (!PyUnicode_CHECK_INTERNED(name)) {
+ PyUnicode_InternInPlace(&name);
+ if (!PyUnicode_CHECK_INTERNED(name)) {
+ PyErr_SetString(PyExc_MemoryError,
+ "Out of memory interning an attribute name");
+ Py_DECREF(name);
+ return -1;
}
- else {
- name = _PyUnicode_Copy(name);
- if (name == NULL)
- return -1;
+ }
+
+ PyTypeObject *metatype = Py_TYPE(type);
+ assert(!_PyType_HasFeature(metatype, Py_TPFLAGS_INLINE_VALUES));
+ assert(!_PyType_HasFeature(metatype, Py_TPFLAGS_MANAGED_DICT));
+
+ PyObject *old_value;
+ PyObject *descr = _PyType_LookupRef(metatype, name);
+ if (descr != NULL) {
+ descrsetfunc f = Py_TYPE(descr)->tp_descr_set;
+ if (f != NULL) {
+ old_value = NULL;
+ res = f(descr, (PyObject *)type, value);
+ goto done;
}
- /* bpo-40521: Interned strings are shared by all subinterpreters */
- if (!PyUnicode_CHECK_INTERNED(name)) {
- PyUnicode_InternInPlace(&name);
- if (!PyUnicode_CHECK_INTERNED(name)) {
- PyErr_SetString(PyExc_MemoryError,
- "Out of memory interning an attribute name");
- Py_DECREF(name);
- return -1;
- }
+ }
+
+ PyObject *dict = type->tp_dict;
+ if (dict == NULL) {
+ // We don't just do PyType_Ready because we could already be readying
+ BEGIN_TYPE_LOCK();
+ dict = type->tp_dict;
+ if (dict == NULL) {
+ dict = type->tp_dict = PyDict_New();
+ }
+ END_TYPE_LOCK();
+ if (dict == NULL) {
+ return -1;
}
}
- else {
- /* Will fail in _PyObject_GenericSetAttrWithDict. */
- Py_INCREF(name);
+
+ // We don't want any re-entrancy between when we update the dict
+ // and call type_modified_unlocked, including running the destructor
+ // of the current value as it can observe the cache in an inconsistent
+ // state. Because we have an exact unicode and our dict has exact
+ // unicodes we know that this will all complete without releasing
+ // the locks.
+ BEGIN_TYPE_DICT_LOCK(dict);
+
+ if (_PyDict_GetItemRef_Unicode_LockHeld((PyDictObject *)dict, name, &old_value) < 0) {
+ return -1;
}
- BEGIN_TYPE_LOCK()
- res = _PyObject_GenericSetAttrWithDict((PyObject *)type, name, value, NULL);
- if (res == 0) {
- /* Clear the VALID_VERSION flag of 'type' and all its
- subclasses. This could possibly be unified with the
- update_subclasses() recursion in update_slot(), but carefully:
- they each have their own conditions on which to stop
- recursing into subclasses. */
- type_modified_unlocked(type);
+#ifdef Py_GIL_DISABLED
+ // In free-threaded builds readers can race with the lock-free portion
+ // of the type cache and the assignment into the dict. We clear all of the
+ // versions initially so no readers will succeed in the lock-free case.
+ // They'll then block on the type lock until the update below is done.
+ type_modification_starting_unlocked(type);
+#endif
+
+ res = _PyDict_SetItem_LockHeld((PyDictObject *)dict, name, value);
+ /* Clear the VALID_VERSION flag of 'type' and all its
+ subclasses. This could possibly be unified with the
+ update_subclasses() recursion in update_slot(), but carefully:
+ they each have their own conditions on which to stop
+ recursing into subclasses. */
+ type_modified_unlocked(type);
+
+ if (res == 0) {
if (is_dunder_name(name)) {
res = update_slot(type, name);
}
- assert(_PyType_CheckConsistency(type));
}
- END_TYPE_LOCK()
+ else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
+ PyErr_Format(PyExc_AttributeError,
+ "type object '%.50s' has no attribute '%U'",
+ ((PyTypeObject*)type)->tp_name, name);
+
+ _PyObject_SetAttributeErrorContext((PyObject *)type, name);
+ }
+
+ assert(_PyType_CheckConsistency(type));
+ END_TYPE_DICT_LOCK();
+done:
Py_DECREF(name);
+ Py_XDECREF(descr);
+ Py_XDECREF(old_value);
return res;
}
@@ -9343,33 +9459,33 @@ _Py_slot_tp_getattr_hook(PyObject *self, PyObject *name)
/* speed hack: we could use lookup_maybe, but that would resolve the
method fully for each attribute lookup for classes with
__getattr__, even when the attribute is present. So we use
- _PyType_Lookup and create the method only when needed, with
+ _PyType_LookupRef and create the method only when needed, with
call_attribute. */
- getattr = _PyType_Lookup(tp, &_Py_ID(__getattr__));
+ getattr = _PyType_LookupRef(tp, &_Py_ID(__getattr__));
if (getattr == NULL) {
/* No __getattr__ hook: use a simpler dispatcher */
tp->tp_getattro = _Py_slot_tp_getattro;
return _Py_slot_tp_getattro(self, name);
}
- Py_INCREF(getattr);
/* speed hack: we could use lookup_maybe, but that would resolve the
method fully for each attribute lookup for classes with
__getattr__, even when self has the default __getattribute__
- method. So we use _PyType_Lookup and create the method only when
+ method. So we use _PyType_LookupRef and create the method only when
needed, with call_attribute. */
- getattribute = _PyType_Lookup(tp, &_Py_ID(__getattribute__));
+ getattribute = _PyType_LookupRef(tp, &_Py_ID(__getattribute__));
if (getattribute == NULL ||
(Py_IS_TYPE(getattribute, &PyWrapperDescr_Type) &&
((PyWrapperDescrObject *)getattribute)->d_wrapped ==
(void *)PyObject_GenericGetAttr)) {
+ Py_XDECREF(getattribute);
res = _PyObject_GenericGetAttrWithDict(self, name, NULL, 1);
/* if res == NULL with no exception set, then it must be an
AttributeError suppressed by us. */
if (res == NULL && !PyErr_Occurred()) {
res = call_attribute(self, getattr, name);
}
- } else {
- Py_INCREF(getattribute);
+ }
+ else {
res = call_attribute(self, getattribute, name);
Py_DECREF(getattribute);
if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
@@ -9476,7 +9592,7 @@ slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
PyTypeObject *tp = Py_TYPE(self);
PyObject *get;
- get = _PyType_Lookup(tp, &_Py_ID(__get__));
+ get = _PyType_LookupRef(tp, &_Py_ID(__get__));
if (get == NULL) {
/* Avoid further slowdowns */
if (tp->tp_descr_get == slot_tp_descr_get)
@@ -9488,7 +9604,9 @@ slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
if (type == NULL)
type = Py_None;
PyObject *stack[3] = {self, obj, type};
- return PyObject_Vectorcall(get, stack, 3, NULL);
+ PyObject *res = PyObject_Vectorcall(get, stack, 3, NULL);
+ Py_DECREF(get);
+ return res;
}
static int
@@ -10383,6 +10501,7 @@ update_one_slot(PyTypeObject *type, pytype_slotdef *p)
type->tp_flags &= ~Py_TPFLAGS_HAVE_VECTORCALL;
}
}
+ Py_DECREF(descr);
} while ((++p)->offset == offset);
if (specific && !use_generic)
*ptr = specific;