-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-121459: Deferred LOAD_ATTR (methods) #124101
Changes from all commits
be5fa22
f93588a
1050673
f0afe98
c3fe777
c3448d9
ba19770
d383437
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -623,6 +623,24 @@ _Py_TryXGetRef(PyObject **ptr) | |
return NULL; | ||
} | ||
|
||
// This belongs here rather than pycore_stackref.h because including pycore_object.h | ||
// there causes a circular include. | ||
static inline _PyStackRef | ||
_Py_TryXGetStackRef(PyObject **ptr) | ||
{ | ||
PyObject *value = _Py_atomic_load_ptr(ptr); | ||
if (value == NULL) { | ||
return PyStackRef_NULL; | ||
} | ||
if (_Py_IsImmortal(value) || _PyObject_HasDeferredRefcount(value)) { | ||
return (_PyStackRef){ .bits = (uintptr_t)value | Py_TAG_DEFERRED }; | ||
} | ||
if (_Py_TryIncrefCompare(ptr, value)) { | ||
return PyStackRef_FromPyObjectSteal(value); | ||
} | ||
return PyStackRef_NULL; | ||
} | ||
|
||
/* Like Py_NewRef but also optimistically sets _Py_REF_MAYBE_WEAKREF | ||
on objects owned by a different thread. */ | ||
static inline PyObject * | ||
|
@@ -816,7 +834,8 @@ extern int _PyObject_StoreInstanceAttribute(PyObject *obj, | |
PyObject *name, PyObject *value); | ||
extern bool _PyObject_TryGetInstanceAttribute(PyObject *obj, PyObject *name, | ||
PyObject **attr); | ||
|
||
extern bool _PyObject_TryGetInstanceAttributeStackRef(PyObject *obj, PyObject *name, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Likewise. Anything with "try" in the name needs to be carefully documented. |
||
_PyStackRef *attr); | ||
#ifdef Py_GIL_DISABLED | ||
# define MANAGED_DICT_OFFSET (((Py_ssize_t)sizeof(PyObject *))*-1) | ||
# define MANAGED_WEAKREF_OFFSET (((Py_ssize_t)sizeof(PyObject *))*-2) | ||
|
@@ -863,6 +882,7 @@ PyAPI_FUNC(PyObject*) _PyObject_LookupSpecialMethod(PyObject *self, PyObject *at | |
extern int _PyObject_IsAbstract(PyObject *); | ||
|
||
PyAPI_FUNC(int) _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method); | ||
PyAPI_FUNC(int) _PyObject_GetMethodStackRef(PyObject *obj, PyObject *name, _PyStackRef *method, _PyStackRef *spare); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does this have a "spare" parameter? |
||
extern PyObject* _PyObject_NextNotImplemented(PyObject *); | ||
|
||
// Pickle support. | ||
|
@@ -900,6 +920,8 @@ PyAPI_DATA(int) _Py_SwappedOp[]; | |
|
||
extern void _Py_GetConstant_Init(void); | ||
|
||
extern void _PyType_LookupStackRef(PyTypeObject *type, PyObject *name, _PyStackRef *result); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't this fail? It should probably return an |
||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1620,6 +1620,129 @@ _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method) | |
return 0; | ||
} | ||
|
||
int | ||
_PyObject_GetMethodStackRef(PyObject *obj, PyObject *name, _PyStackRef *method, _PyStackRef *spare) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't have to be in this PR, but we should replace all usages of |
||
#ifdef Py_GIL_DISABLED | ||
int meth_found = 0; | ||
|
||
assert(PyStackRef_IsNull(*method)); | ||
|
||
PyTypeObject *tp = Py_TYPE(obj); | ||
if (!_PyType_IsReady(tp)) { | ||
if (PyType_Ready(tp) < 0) { | ||
return 0; | ||
} | ||
} | ||
|
||
if (tp->tp_getattro != PyObject_GenericGetAttr || !PyUnicode_CheckExact(name)) { | ||
PyObject *attr_o = PyObject_GetAttr(obj, name); | ||
if (attr_o != NULL) { | ||
*method = PyStackRef_FromPyObjectSteal(attr_o); | ||
} | ||
else { | ||
*method = PyStackRef_NULL; | ||
} | ||
return 0; | ||
} | ||
|
||
_PyType_LookupStackRef(tp, name, method); | ||
*spare = *method; | ||
_PyStackRef descr_st = *method; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's confusing that |
||
descrgetfunc f = NULL; | ||
if (!PyStackRef_IsNull(descr_st)) { | ||
if (_PyType_HasFeature(PyStackRef_TYPE(descr_st), Py_TPFLAGS_METHOD_DESCRIPTOR)) { | ||
meth_found = 1; | ||
} else { | ||
f = PyStackRef_TYPE(descr_st)->tp_descr_get; | ||
if (f != NULL && PyDescr_IsData(PyStackRef_AsPyObjectBorrow(descr_st))) { | ||
PyObject *call_res_o = f(PyStackRef_AsPyObjectBorrow(descr_st), obj, (PyObject *)Py_TYPE(obj)); | ||
if (call_res_o != NULL) { | ||
*method = PyStackRef_FromPyObjectSteal(call_res_o); | ||
} | ||
else { | ||
*method = PyStackRef_NULL; | ||
} | ||
PyStackRef_CLOSE(descr_st); | ||
return 0; | ||
} | ||
} | ||
} | ||
PyObject *dict; | ||
if ((tp->tp_flags & Py_TPFLAGS_INLINE_VALUES) && | ||
_PyObject_TryGetInstanceAttributeStackRef(obj, name, method)) { | ||
if (!PyStackRef_IsNull(*method)) { | ||
PyStackRef_XCLOSE(descr_st); | ||
Comment on lines
+1673
to
+1675
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This overwrites There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We want to restore *method to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you need both Alternatively take in a second There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the second approach needs to wait for @mpage 's PR on adding an extra stack slot? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh we don't, we can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok nevermind, this does indeed need Matt's fix. Because we unconditionally write to self_or_null now. |
||
return 0; | ||
} | ||
dict = NULL; | ||
} | ||
else if ((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT)) { | ||
dict = (PyObject *)_PyObject_GetManagedDict(obj); | ||
} | ||
else { | ||
PyObject **dictptr = _PyObject_ComputedDictPointer(obj); | ||
if (dictptr != NULL) { | ||
dict = *dictptr; | ||
} | ||
else { | ||
dict = NULL; | ||
} | ||
} | ||
if (dict != NULL) { | ||
Py_INCREF(dict); | ||
if (_PyDict_GetItemStackRef(dict, name, method) != 0) { | ||
// found or error | ||
Py_DECREF(dict); | ||
PyStackRef_CLOSE(descr_st); | ||
return 0; | ||
} | ||
// not found | ||
Py_DECREF(dict); | ||
} | ||
|
||
if (meth_found) { | ||
*method = descr_st; | ||
return 1; | ||
} | ||
|
||
if (f != NULL) { | ||
PyObject *call_res_o = f(PyStackRef_AsPyObjectBorrow(descr_st), obj, (PyObject *)Py_TYPE(obj)); | ||
if (call_res_o != NULL) { | ||
*method = PyStackRef_FromPyObjectSteal(call_res_o); | ||
} | ||
else { | ||
*method = PyStackRef_NULL; | ||
} | ||
PyStackRef_CLOSE(descr_st); | ||
return 0; | ||
} | ||
|
||
if (!PyStackRef_IsNull(descr_st)) { | ||
*method = descr_st; | ||
return 0; | ||
} | ||
|
||
*method = PyStackRef_NULL; | ||
PyErr_Format(PyExc_AttributeError, | ||
"'%.100s' object has no attribute '%U'", | ||
tp->tp_name, name); | ||
|
||
_PyObject_SetAttributeErrorContext(obj, name); | ||
return 0; | ||
#else | ||
PyObject *res = NULL; | ||
int err = _PyObject_GetMethod(obj, name, &res); | ||
if (res == NULL) { | ||
*method = PyStackRef_NULL; | ||
} | ||
else { | ||
*method = PyStackRef_FromPyObjectSteal(res); | ||
} | ||
return err; | ||
#endif | ||
} | ||
|
||
/* Generic GetAttr functions - put these in your tp_[gs]etattro slot. */ | ||
|
||
PyObject * | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add clear comments as to what this does.
What is it "trying" to do? What happens if it fails? etc.