https://github.com/python/cpython/commit/e6c3c04fab43057baf70b4f24b57a4679d646d84 commit: e6c3c04fab43057baf70b4f24b57a4679d646d84 branch: main author: Bartosz Sławecki <[email protected]> committer: vstinner <[email protected]> date: 2026-03-03T17:14:12+01:00 summary:
gh-145452: Initialize `PyLazyImport_Type` during interpreter startup (#145453) Co-authored-by: Victor Stinner <[email protected]> files: M Lib/test/test_lazy_import/__init__.py M Objects/object.c diff --git a/Lib/test/test_lazy_import/__init__.py b/Lib/test/test_lazy_import/__init__.py index 5d30ec2299789b..a4180f05dbbafc 100644 --- a/Lib/test/test_lazy_import/__init__.py +++ b/Lib/test/test_lazy_import/__init__.py @@ -12,6 +12,7 @@ import os from test import support +from test.support.script_helper import assert_python_ok try: import _testcapi @@ -219,6 +220,16 @@ def test_lazy_import_type_cant_construct(self): """LazyImportType should not be directly constructible.""" self.assertRaises(TypeError, types.LazyImportType, {}, "module") + @support.requires_subprocess() + def test_lazy_import_type_attributes_accessible(self): + """Check that static PyLazyImport_Type is initialized at startup.""" + code = textwrap.dedent(""" + lazy import json + print(globals()["json"].resolve) + """) + proc = assert_python_ok("-c", code) + self.assertIn(b"<built-in method resolve of lazy_import object at", proc.out) + class SyntaxRestrictionTests(unittest.TestCase): """Tests for syntax restrictions on lazy imports.""" diff --git a/Objects/object.c b/Objects/object.c index ab73d2eb1c9c1f..b537c0d104e58c 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -17,6 +17,7 @@ #include "pycore_instruction_sequence.h" // _PyInstructionSequence_Type #include "pycore_interpframe.h" // _PyFrame_Stackbase() #include "pycore_interpolation.h" // _PyInterpolation_Type +#include "pycore_lazyimportobject.h" // PyLazyImport_Type #include "pycore_list.h" // _PyList_DebugMallocStats() #include "pycore_long.h" // _PyLong_GetZero() #include "pycore_memoryobject.h" // _PyManagedBuffer_Type @@ -2540,6 +2541,7 @@ static PyTypeObject* static_types[] = { &PyGen_Type, &PyGetSetDescr_Type, &PyInstanceMethod_Type, + &PyLazyImport_Type, &PyListIter_Type, &PyListRevIter_Type, &PyList_Type, _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3//lists/python-checkins.python.org Member address: [email protected]
