Skip to content

Commit e6c3c04

Browse files
johnslavikvstinner
andauthored
gh-145452: Initialize PyLazyImport_Type during interpreter startup (#145453)
Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 34df70c commit e6c3c04

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

Lib/test/test_lazy_import/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import os
1313

1414
from test import support
15+
from test.support.script_helper import assert_python_ok
1516

1617
try:
1718
import _testcapi
@@ -219,6 +220,16 @@ def test_lazy_import_type_cant_construct(self):
219220
"""LazyImportType should not be directly constructible."""
220221
self.assertRaises(TypeError, types.LazyImportType, {}, "module")
221222

223+
@support.requires_subprocess()
224+
def test_lazy_import_type_attributes_accessible(self):
225+
"""Check that static PyLazyImport_Type is initialized at startup."""
226+
code = textwrap.dedent("""
227+
lazy import json
228+
print(globals()["json"].resolve)
229+
""")
230+
proc = assert_python_ok("-c", code)
231+
self.assertIn(b"<built-in method resolve of lazy_import object at", proc.out)
232+
222233

223234
class SyntaxRestrictionTests(unittest.TestCase):
224235
"""Tests for syntax restrictions on lazy imports."""

Objects/object.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "pycore_instruction_sequence.h" // _PyInstructionSequence_Type
1818
#include "pycore_interpframe.h" // _PyFrame_Stackbase()
1919
#include "pycore_interpolation.h" // _PyInterpolation_Type
20+
#include "pycore_lazyimportobject.h" // PyLazyImport_Type
2021
#include "pycore_list.h" // _PyList_DebugMallocStats()
2122
#include "pycore_long.h" // _PyLong_GetZero()
2223
#include "pycore_memoryobject.h" // _PyManagedBuffer_Type
@@ -2540,6 +2541,7 @@ static PyTypeObject* static_types[] = {
25402541
&PyGen_Type,
25412542
&PyGetSetDescr_Type,
25422543
&PyInstanceMethod_Type,
2544+
&PyLazyImport_Type,
25432545
&PyListIter_Type,
25442546
&PyListRevIter_Type,
25452547
&PyList_Type,

0 commit comments

Comments
 (0)