FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.15] gh-158824: Fix `__getattr__` blocking reification of lazy imports (GH-158825) by miss-islington · Pull Request #159022 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (2) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
13 changes: 13 additions & 0 deletions Lib/test/test_lazy_import/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,19 @@ def test_from_import_with_imported_module_getattr(self):
""")
assert_python_ok("-c", code)

@support.requires_subprocess()
def test_module_getattr_does_not_shadow_own_lazy_import(self):
"""Module __getattr__ should not shadow the module's own lazy imports."""
code = textwrap.dedent("""
import types
import test.test_lazy_import.data.module_with_lazy_import_and_getattr as mod
assert mod.basic2.__name__ == "test.test_lazy_import.data.basic2"
assert mod.f is mod.basic2.f
assert not isinstance(vars(mod)["basic2"], types.LazyImportType)
assert mod.missing == "from_getattr:missing"
""")
assert_python_ok("-c", code)


class GlobalLazyImportModeTests(LazyImportTestCase):
"""Tests for sys.set_lazy_imports() global mode control."""
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
lazy import test.test_lazy_import.data.basic2 as basic2
lazy from test.test_lazy_import.data.basic2 import f

def __getattr__(name):
return f"from_getattr:{name}"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix module-level :meth:`~module.__getattr__` being called before a module's
own lazy imports were resolved, which allowed it to shadow them.
23 changes: 0 additions & 23 deletions Objects/moduleobject.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1362,29 +1362,6 @@ _Py_module_getattro_impl(PyModuleObject *m, PyObject *name, int suppress)
}
if (attr) {
if (PyLazyImport_CheckExact(attr)) {
// gh-144957: Module __getattr__ should get a chance to provide
// the attribute before resolving a lazy import placeholder.
// Resolving __getattr__ itself must not invoke the hook.
getattr = NULL;
if (!_PyUnicode_EqualToASCIIString(name, "__getattr__") &&
module_get_resolved_dict_item(
m->md_dict, &_Py_ID(__getattr__), &getattr) < 0) {
Py_DECREF(attr);
return NULL;
}
if (getattr) {
PyObject *result = PyObject_CallOneArg(getattr, name);
Py_DECREF(getattr);
if (result != NULL) {
Py_DECREF(attr);
return result;
}
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Py_DECREF(attr);
return NULL;
}
PyErr_Clear();
}
PyThreadState *tstate = PyThreadState_GET();
if (suppress && _PyLazyImport_IsResolving(tstate, attr)) {
// Only direct reentry means this attribute is unavailable.
Expand Down
Loading

Back | FazBrowse Home | New Git URL