Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2209,6 +2209,35 @@ def load_module_attr_missing():
finally:
sys.modules.pop("test_module_with_getattr", None)

@cpython_only
@requires_specialization
def test_load_attr_module_subclass_with_data_descriptor(self):
# gh-156399: a types.ModuleType subclass inherits
# PyModule_Type.tp_getattro, so guarding specialization on tp_getattro
# alone let subclasses take LOAD_ATTR_MODULE. That reads the module
# dict directly, silently bypassing a data descriptor defined on the
# subclass once the instruction had specialized.
class Descriptor:
def __get__(self, instance, owner=None):
return "from descriptor"

def __set__(self, instance, value):
instance.__dict__["attr"] = value

class Module(types.ModuleType):
attr = Descriptor()

module = Module("test_module_subclass")
module.__dict__["attr"] = "from dict"

def load_module_attr():
return module.attr

for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
self.assertEqual(load_module_attr(), "from descriptor")

self.assert_no_opcode(load_module_attr, "LOAD_ATTR_MODULE")

@cpython_only
@requires_specialization
def test_specialized_iter_doesnt_skip_send_check(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix a regression where attribute access on a :class:`types.ModuleType`
subclass could bypass a data descriptor defined on the subclass. The
``LOAD_ATTR_MODULE`` specialization guarded on ``tp_getattro``, which
subclasses inherit, and then read the module dictionary directly.
2 changes: 1 addition & 1 deletion Modules/_testinternalcapi/test_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2928,7 +2928,7 @@ dummy_func(

op(_LOAD_ATTR_MODULE, (dict_version/2, index/1, owner -- attr, o)) {
PyObject *owner_o = PyStackRef_AsPyObjectBorrow(owner);
EXIT_IF(Py_TYPE(owner_o)->tp_getattro != PyModule_Type.tp_getattro);
EXIT_IF(!PyModule_CheckExact(owner_o));
PyDictObject *dict = (PyDictObject *)((PyModuleObject *)owner_o)->md_dict;
assert(dict != NULL);
PyDictKeysObject *keys = FT_ATOMIC_LOAD_PTR_ACQUIRE(dict->ma_keys);
Expand Down
2 changes: 1 addition & 1 deletion Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,7 @@ _Py_Specialize_LoadAttr(_PyStackRef owner_st, _Py_CODEUNIT *instr, PyObject *nam
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OTHER);
fail = true;
}
else if (Py_TYPE(owner)->tp_getattro == PyModule_Type.tp_getattro) {
else if (PyModule_CheckExact(owner)) {
fail = specialize_module_load_attr(owner, instr, name);
}
else if (PyType_Check(owner)) {
Expand Down
Loading