8000 Rename _PyType_Fetch -> _PyType_LookupRef · python/cpython@8074d0d · GitHub
[go: up one dir, main page]

Skip to content

Commit 8074d0d

Browse files
committed
Rename _PyType_Fetch -> _PyType_LookupRef
Fix some formatting Add news blurb Use PyDictObject * more Rename _PyDict_GetItemRef_LockHeld to _PyDict_GetItemRef_Unicode_LockHeld Reduce iterations in test Expose _PyObject_SetAttributeErrorContext for attaching context
1 parent c688d6b commit 8074d0d

File tree

12 files changed

+57
-57
lines changed

12 files changed

+57
-57
lines changed

Include/cpython/object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ typedef struct _heaptypeobject {
275275

276276
PyAPI_FUNC(const char *) _PyType_Name(PyTypeObject *);
277277
PyAPI_FUNC(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *);
278-
PyAPI_FUNC(PyObject *) _PyType_Fetch(PyTypeObject *, PyObject *);
278+
PyAPI_FUNC(PyObject *) _PyType_LookupRef(PyTypeObject *, PyObject *);
279279
PyAPI_FUNC(PyObject *) PyType_GetDict(PyTypeObject *);
280280

281281
PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int);

Include/internal/pycore_dict.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ PyAPI_FUNC(PyObject *)_PyDict_LoadGlobal(PyDictObject *, PyDictObject *, PyObjec
107107
PyAPI_FUNC(int) _PyDict_SetItem_Take2(PyDictObject *op, PyObject *key, PyObject *value);
108108
extern int _PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr, PyObject *name, PyObject *value);
109109
extern int _PyDict_SetItem_LockHeld(PyDictObject *dict, PyObject *name, PyObject *value);
110-
extern int _PyDict_GetItemRef_LockHeld(PyObject *op, PyObject *key, PyObject **result);
111-
extern int _PyDict_GetItemRef_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash, PyObject **result);
110+
extern int _PyDict_GetItemRef_Unicode_LockHeld(PyDictObject *op, PyObject *key, PyObject **result);
111+
extern int _PyDict_GetItemRef_KnownHash(PyDictObject *op, PyObject *key, Py_hash_t hash, PyObject **result);
112112

113113
extern int _PyDict_Pop_KnownHash(
114114
PyDictObject *dict,

Include/internal/pycore_object.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,7 @@ extern PyObject *_PyType_NewManagedObject(PyTypeObject *type);
658658
extern PyTypeObject* _PyType_CalculateMetaclass(PyTypeObject *, PyObject *);
659659
extern PyObject* _PyType_GetDocFromInternalDoc(const char *, const char *);
660660
extern PyObject* _PyType_GetTextSignatureFromInternalDoc(const char *, const char *, int);
661+
extern int _PyObject_SetAttributeErrorContext(PyObject* v, PyObject* name);
661662

662663
void _PyObject_InitInlineValues(PyObject *obj, PyTypeObject *tp);
663664
extern int _PyObject_StoreInstanceAttribute(PyObject *obj,

Lib/test/test_free_threading/test_type.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class C:
4646

4747
DONE = False
4848
def writer_func():
49-
for i in range(10000):
49+
for i in range(3000):
5050
C.x
5151
C.x
5252
C.x += 1
@@ -75,7 +75,7 @@ class D(C):
7575

7676
DONE = False
7777
def writer_func():
78-
for i in range(10000):
78+
for i in range(3000):
7979
D.x
8080
D.x
8181
C.x += 1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix an issue where the type cache can expose a previously accessed attribute
2+
when a finalizer is run.

Modules/_collectionsmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,9 +2511,9 @@ _collections__count_elements_impl(PyObject *module, PyObject *mapping,
25112511
/* Only take the fast path when get() and __setitem__()
25122512
* have not been overridden.
25132513
*/
2514-
mapping_get = _PyType_Fetch(Py_TYPE(mapping), &_Py_ID(get));
2514+
mapping_get = _PyType_LookupRef(Py_TYPE(mapping), &_Py_ID(get));
25152515
dict_get = _PyType_Lookup(&PyDict_Type, &_Py_ID(get));
2516-
mapping_setitem = _PyType_Fetch(Py_TYPE(mapping), &_Py_ID(__setitem__));
2516+
mapping_setitem = _PyType_LookupRef(Py_TYPE(mapping), &_Py_ID(__setitem__));
25172517
dict_setitem = _PyType_Lookup(&PyDict_Type, &_Py_ID(__setitem__));
25182518

25192519
if (mapping_get != NULL && mapping_get == dict_get &&

Modules/_lsprof.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ normalizeUserObj(PyObject *obj)
175175
PyObject *modname = fn->m_module;
176176

177177
if (name != NULL) {
178-
PyObject *mo = _PyType_Fetch(Py_TYPE(self), name);
178+
PyObject *mo = _PyType_LookupRef(Py_TYPE(self), name);
179179
Py_DECREF(name);
180180
if (mo != NULL) {
181181
PyObject *res = PyObject_Repr(mo);

Modules/_testcapi/gc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ slot_tp_del(PyObject *self)
9999
return;
100100
}
101101
/* Execute __del__ method, if any. */
102-
del = _PyType_Fetch(Py_TYPE(self), tp_del);
102+
del = _PyType_LookupRef(Py_TYPE(self), tp_del);
103103
Py_DECREF(tp_del);
104104
if (del != NULL) {
105105
res = PyObject_CallOneArg(del, self);

Objects/classobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ method_getattro(PyObject *obj, PyObject *name)
188188
if (PyType_Ready(tp) < 0)
189189
return NULL;
190190
}
191-
descr = _PyType_Fetch(tp, name);
191+
descr = _PyType_LookupRef(tp, name);
192192
}
193193

194194
if (descr != NULL) {
@@ -413,7 +413,7 @@ instancemethod_getattro(PyObject *self, PyObject *name)
413413
if (PyType_Ready(tp) < 0)
414414
return NULL;
415415
}
416-
descr = _PyType_Fetch(tp, name);
416+
descr = _PyType_LookupRef(tp, name);
417417

418418
if (descr != NULL) {
419419
descrgetfunc f = TP_DESCR_GET(Py_TYPE(descr));

Objects/dictobject.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2268,15 +2268,13 @@ _PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
22682268
* exception occurred.
22692269
*/
22702270
int
2271-
_PyDict_GetItemRef_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash, PyObject **result)
2271+
_PyDict_GetItemRef_KnownHash(PyDictObject *op, PyObject *key, Py_hash_t hash, PyObject **result)
22722272
{
2273-
PyDictObject*mp = (PyDictObject *)op;
2274-
22752273
PyObject *value;
22762274
#ifdef Py_GIL_DISABLED
2277-
Py_ssize_t ix = _Py_dict_lookup_threadsafe(mp, key, hash, &value);
2275+
Py_ssize_t ix = _Py_dict_lookup_threadsafe(op, key, hash, &value);
22782276
#else
2279-
Py_ssize_t ix = _Py_dict_lookup(mp, key, hash, &value);
2277+
Py_ssize_t ix = _Py_dict_lookup(op, key, hash, &value);
22802278
#endif
22812279
assert(ix >= 0 || value == NULL);
22822280
if (ix == DKIX_ERROR) {
@@ -2314,11 +2312,11 @@ PyDict_GetItemRef(PyObject *op, PyObject *key, PyObject **result)
23142312
}
23152313
}
23162314

2317-
return _PyDict_GetItemRef_KnownHash(op, key, hash, result);
2315+
return _PyDict_GetItemRef_KnownHash((PyDictObject *)op, key, hash, result);
23182316
}
23192317

23202318
int
2321-
_PyDict_GetItemRef_LockHeld(PyObject *op, PyObject *key, PyObject **result)
2319+
_PyDict_GetItemRef_Unicode_LockHeld(PyDictObject *op, PyObject *key, PyObject **result)
23222320
{
23232321
ASSERT_DICT_LOCKED(op);
23242322
assert(PyUnicode_CheckExact(key));
@@ -2332,10 +2330,8 @@ _PyDict_GetItemRef_LockHeld(PyObject *op, PyObject *key, PyObject **result)
23322330
}
23332331
}
23342332

2335-
PyDictObject*mp = (PyDictObject *)op;
2336-
23372333
PyObject *value;
2338-
Py_ssize_t ix = _Py_dict_lookup(mp, key, hash, &value);
2334+
Py_ssize_t ix = _Py_dict_lookup(op, key, hash, &value);
23392335
assert(ix >= 0 || value == NULL);
23402336
if (ix == DKIX_ERROR) {
23412337
*result = NULL;

Objects/object.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,8 +1132,8 @@ _PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w)
11321132
return result;
11331133
}
11341134

1135-
static inline int
1136-
set_attribute_error_context(PyObject* v, PyObject* name)
1135+
int
1136+
_PyObject_SetAttributeErrorContext(PyObject* v, PyObject* name)
11371137
{
11381138
assert(PyErr_Occurred());
11391139
if (!PyErr_ExceptionMatches(PyExc_AttributeError)){
@@ -1188,7 +1188,7 @@ PyObject_GetAttr(PyObject *v, PyObject *name)
11881188
}
11891189

11901190
if (result == NULL) {
1191-
set_attribute_error_context(v, name);
1191+
_PyObject_SetAttributeErrorContext(v, name);
11921192
}
11931193
return result;
11941194
}
@@ -1466,7 +1466,7 @@ _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
14661466
return 0;
14671467
}
14681468

1469-
PyObject *descr = _PyType_Fetch(tp, name);
1469+
PyObject *descr = _PyType_LookupRef(tp, name);
14701470
descrgetfunc f = NULL;
14711471
if (descr != NULL) {
14721472
if (_PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR)) {
@@ -1535,7 +1535,7 @@ _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
15351535
"'%.100s' object has no attribute '%U'",
15361536
tp->tp_name, name);
15371537

1538-
set_attribute_error_context(obj, name);
1538+
_PyObject_SetAttributeErrorContext(obj, name);
15391539
return 0;
15401540
}
15411541

@@ -1569,7 +1569,7 @@ _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
15691569
goto done;
15701570
}
15711571

1572-
descr = _PyType_Fetch(tp, name);
1572+
descr = _PyType_LookupRef(tp, name);
15731573

15741574
f = NULL;
15751575
if (descr != NULL) {
@@ -1646,7 +1646,7 @@ _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
16461646
"'%.100s' object has no attribute '%U'",
16471647
tp->tp_name, name);
16481648

1649-
set_attribute_error_context(obj, name);
1649+
_PyObject_SetAttributeErrorContext(obj, name);
16501650
}
16511651
done:
16521652
Py_XDECREF(descr);
@@ -1669,6 +1669,7 @@ _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
16691669
descrsetfunc f;
16701670
int res = -1;
16711671

1672+
assert(!PyType_IsSubtype(tp, &PyType_Type));
16721673
if (!PyUnicode_Check(name)){
16731674
PyErr_Format(PyExc_TypeError,
16741675
"attribute name must be string, not '%.200s'",
@@ -1682,7 +1683,7 @@ _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
16821683

16831684
Py_INCREF(name);
16841685
Py_INCREF(tp);
1685-
descr = _PyType_Fetch(tp, name);
1686+
descr = _PyType_LookupRef(tp, name);
16861687

16871688
if (descr != NULL) {
16881689
f = Py_TYPE(descr)->tp_descr_set;
@@ -1720,7 +1721,7 @@ _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
17201721
"'%.100s' object has no attribute '%U'",
17211722
tp->tp_name, name);
17221723
}
1723-
set_attribute_error_context(obj, name);
1724+
_PyObject_SetAttributeErrorContext(obj, name);
17241725
}
17251726
else {
17261727
PyErr_Format(PyExc_AttributeError,
@@ -1743,11 +1744,10 @@ _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
17431744
}
17441745
error_check:
17451746
if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) {
1746-
assert(!PyType_IsSubtype(tp, &PyType_Type));
17471747
PyErr_Format(PyExc_AttributeError,
17481748
"'%.100s' object has no attribute '%U'",
17491749
tp->tp_name, name);
1750-
set_attribute_error_context(obj, name);
1750+
_PyObject_SetAttributeErrorContext(obj, name);
17511751
}
17521752
done:
17531753
Py_XDECREF(descr);

0 commit comments

Comments
 (0)
0