]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-145452: Initialize `PyLazyImport_Type` during interpreter startup (#145453)
authorBartosz Sławecki <bartosz@ilikepython.com>
Tue, 3 Mar 2026 16:14:12 +0000 (17:14 +0100)
committerGitHub <noreply@github.com>
Tue, 3 Mar 2026 16:14:12 +0000 (17:14 +0100)
Co-authored-by: Victor Stinner <vstinner@python.org>
Lib/test/test_lazy_import/__init__.py
Objects/object.c

index 5d30ec2299789b321b11854bd3d6ec02583686ce..a4180f05dbbafc4ac72a8dfe49f35cd1e91a1441 100644 (file)
@@ -12,6 +12,7 @@ import tempfile
 import os
 
 from test import support
+from test.support.script_helper import assert_python_ok
 
 try:
     import _testcapi
@@ -219,6 +220,16 @@ class LazyImportTypeTests(unittest.TestCase):
         """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."""
index ab73d2eb1c9c1f3fd472e197f3a5cfaebb4fc3eb..b537c0d104e58c1e31fe6215b832f0b9b8044217 100644 (file)
@@ -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,