-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
GH-94822: Respect metaclasses when caching class attributes #94863
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
Changes from all commits
dbc624c
28d1093
3f1e5ce
4f84a5c
606d460
6864e95
03aa529
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Fix an issue where lookups of metaclass descriptors may be ignored when an | ||
identically-named attribute also exists on the class itself. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -945,24 +945,34 @@ specialize_class_load_attr(PyObject *owner, _Py_CODEUNIT *instr, | |
PyObject *name) | ||
{ | ||
_PyLoadMethodCache *cache = (_PyLoadMethodCache *)(instr + 1); | ||
PyTypeObject *metaclass = Py_TYPE(owner); | ||
if (_PyType_Lookup(metaclass, name)) { | ||
// The metaclass has the named attribute. This breaks LOAD_ATTR_CLASS | ||
// when the attribute *also* exists on the class... but it also means we | ||
// can't specialize for things like Class.__doc__, Class.__name__, etc. | ||
// Perhaps LOAD_ATTR_METACLASS may be worth adding in the future? | ||
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_METACLASS_ATTRIBUTE); | ||
return -1; | ||
} | ||
if (metaclass->tp_getattro != PyType_Type.tp_getattro) { | ||
// The metaclass defines __getattribute__. All bets are off: | ||
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OVERRIDDEN); | ||
return -1; | ||
} | ||
Comment on lines
+957
to
+961
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. Do you think we should just play safe and fail whenever we see a non- 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 have simple ways of checking for all known problematic cases (names present in the metaclass, I'm currently running the benchmarks and gathering stats for this branch, but I worry that disallowing all metaclasses would really hurt benchmarks like 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. Thanks for the quick work @brandtbucher I don't know if this helps but if you want to time SymPy specifically then one way is:
That takes about half an hour to run on this computer. Comparing different CPython versions in CI shows that 3.11.0b4 currently gives a speedup for some jobs and is neutral for others: If you look at for example the
The test2 job does roughly half of what 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. Thanks, I'll take a look. Side note, I just realized that potentially removing support for any metaclasses here would probably (further) trash |
||
PyObject *descr = NULL; | ||
DescriptorClassification kind = 0; | ||
kind = analyze_descriptor((PyTypeObject *)owner, name, &descr, 0); | ||
switch (kind) { | ||
case METHOD: | ||
case NON_DESCRIPTOR: | ||
write_u32(cache->type_version, ((PyTypeObject *)owner)->tp_version_tag); | ||
write_u32(cache->keys_version, metaclass->tp_version_tag); | ||
write_obj(cache->descr, descr); | ||
_Py_SET_OPCODE(*instr, LOAD_ATTR_CLASS); | ||
return 0; | ||
#ifdef Py_STATS | ||
case ABSENT: | ||
if (_PyType_Lookup(Py_TYPE(owner), name) != NULL) { | ||
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_METACLASS_ATTRIBUTE); | ||
} | ||
else { | ||
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_EXPECTED_ERROR); | ||
} | ||
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_EXPECTED_ERROR); | ||
return -1; | ||
#endif | ||
default: | ||
|
Uh oh!
There was an error while loading. Please reload this page.