Skip to content

Commit 0b36549

Browse files
committed
Fix tests
1 parent 76846fe commit 0b36549

File tree

9 files changed

+36
-9
lines changed

9 files changed

+36
-9
lines changed

Lib/dis.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,8 @@ def _find_imports(co):
10201020
(level_op[0] in hasconst or level_op[0] == LOAD_SMALL_INT)):
10211021
level = _get_const_value(level_op[0], level_op[1], consts)
10221022
fromlist = _get_const_value(from_op[0], from_op[1], consts)
1023-
yield (names[oparg], level, fromlist)
1023+
# IMPORT_NAME encodes lazy/eager flags in bits 0-1, name index in bits 2+
1024+
yield (names[oparg >> 2], level, fromlist)
10241025

10251026
def _find_store_names(co):
10261027
"""Find names of variables which are written in the code

Lib/test/test_ast/data/ast_repr.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ Module(body=[Import(names=[alias(name='sys', asname=None)], is_lazy=0)], type_ig
7373
Module(body=[Import(names=[alias(name='foo', asname='bar')], is_lazy=0)], type_ignores=[])
7474
Module(body=[ImportFrom(module='sys', names=[alias(name='x', asname='y')], level=0, is_lazy=0)], type_ignores=[])
7575
Module(body=[ImportFrom(module='sys', names=[alias(name='v', asname=None)], level=0, is_lazy=0)], type_ignores=[])
76+
Module(body=[Import(names=[alias(name='sys', asname=None)], is_lazy=1)], type_ignores=[])
77+
Module(body=[Import(names=[alias(name='foo', asname='bar')], is_lazy=1)], type_ignores=[])
78+
Module(body=[ImportFrom(module='sys', names=[alias(name='x', asname='y')], level=0, is_lazy=1)], type_ignores=[])
79+
Module(body=[ImportFrom(module='sys', names=[alias(name='v', asname=None)], level=0, is_lazy=1)], type_ignores=[])
7680
Module(body=[Global(names=['v'])], type_ignores=[])
7781
Module(body=[Expr(value=Constant(value=1, kind=None))], type_ignores=[])
7882
Module(body=[Pass()], type_ignores=[])

Lib/test/test_ast/snippets.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@
118118
# ImportFrom
119119
"from sys import x as y",
120120
"from sys import v",
121+
# Lazy Import
122+
"lazy import sys",
123+
"lazy import foo as bar",
124+
# Lazy ImportFrom
125+
"lazy from sys import x as y",
126+
"lazy from sys import v",
121127
# Global
122128
"global v",
123129
# Expr
@@ -460,10 +466,14 @@ def main():
460466
('Module', [('TryStar', (1, 0, 7, 6), [('Pass', (2, 2, 2, 6))], [('ExceptHandler', (3, 0, 4, 6), ('Name', (3, 8, 3, 17), 'Exception', ('Load',)), 'exc', [('Pass', (4, 2, 4, 6))])], [('Pass', (5, 7, 5, 11))], [('Pass', (7, 2, 7, 6))])], []),
461467
('Module', [('Assert', (1, 0, 1, 8), ('Name', (1, 7, 1, 8), 'v', ('Load',)), None)], []),
462468
('Module', [('Assert', (1, 0, 1, 19), ('Name', (1, 7, 1, 8), 'v', ('Load',)), ('Constant', (1, 10, 1, 19), 'message', None))], []),
463-
('Module', [('Import', (1, 0, 1, 10), [('alias', (1, 7, 1, 10), 'sys', None)])], []),
464-
('Module', [('Import', (1, 0, 1, 17), [('alias', (1, 7, 1, 17), 'foo', 'bar')])], []),
465-
('Module', [('ImportFrom', (1, 0, 1, 22), 'sys', [('alias', (1, 16, 1, 22), 'x', 'y')], 0)], []),
466-
('Module', [('ImportFrom', (1, 0, 1, 17), 'sys', [('alias', (1, 16, 1, 17), 'v', None)], 0)], []),
469+
('Module', [('Import', (1, 0, 1, 10), [('alias', (1, 7, 1, 10), 'sys', None)], 0)], []),
470+
('Module', [('Import', (1, 0, 1, 17), [('alias', (1, 7, 1, 17), 'foo', 'bar')], 0)], []),
471+
('Module', [('ImportFrom', (1, 0, 1, 22), 'sys', [('alias', (1, 16, 1, 22), 'x', 'y')], 0, 0)], []),
472+
('Module', [('ImportFrom', (1, 0, 1, 17), 'sys', [('alias', (1, 16, 1, 17), 'v', None)], 0, 0)], []),
473+
('Module', [('Import', (1, 0, 1, 15), [('alias', (1, 12, 1, 15), 'sys', None)], 1)], []),
474+
('Module', [('Import', (1, 0, 1, 22), [('alias', (1, 12, 1, 22), 'foo', 'bar')], 1)], []),
475+
('Module', [('ImportFrom', (1, 0, 1, 27), 'sys', [('alias', (1, 21, 1, 27), 'x', 'y')], 0, 1)], []),
476+
('Module', [('ImportFrom', (1, 0, 1, 22), 'sys', [('alias', (1, 21, 1, 22), 'v', None)], 0, 1)], []),
467477
('Module', [('Global', (1, 0, 1, 8), ['v'])], []),
468478
('Module', [('Expr', (1, 0, 1, 1), ('Constant', (1, 0, 1, 1), 1, None))], []),
469479
('Module', [('Pass', (1, 0, 1, 4))], []),

Lib/test/test_capi/test_config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def test_config_get(self):
6262
("int_max_str_digits", int, None),
6363
("interactive", bool, None),
6464
("isolated", bool, None),
65+
("lazy_imports", int, None),
6566
("malloc_stats", bool, None),
6667
("module_search_paths", list[str], "path"),
6768
("optimization_level", int, None),

Lib/test/test_dis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def wrap_func_w_kwargs():
292292
293293
1 LOAD_SMALL_INT 0
294294
LOAD_CONST 1 (('*',))
295-
IMPORT_NAME 0 (math)
295+
IMPORT_NAME 2 (math + eager)
296296
CALL_INTRINSIC_1 2 (INTRINSIC_IMPORT_STAR)
297297
POP_TOP
298298
LOAD_CONST 2 (None)

Lib/test/test_embed.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
635635
'tracemalloc': 0,
636636
'perf_profiling': 0,
637637
'import_time': 0,
638+
'lazy_imports': -1,
638639
'thread_inherit_context': DEFAULT_THREAD_INHERIT_CONTEXT,
639640
'context_aware_warnings': DEFAULT_CONTEXT_AWARE_WARNINGS,
640641
'code_debug_ranges': True,

Lib/test/test_sys.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,8 @@ def test_sys_flags(self):
866866
"dont_write_bytecode", "no_user_site", "no_site",
867867
"ignore_environment", "verbose", "bytes_warning", "quiet",
868868
"hash_randomization", "isolated", "dev_mode", "utf8_mode",
869-
"warn_default_encoding", "safe_path", "int_max_str_digits")
869+
"warn_default_encoding", "safe_path", "int_max_str_digits",
870+
"lazy_imports")
870871
for attr in attrs:
871872
self.assertHasAttr(sys.flags, attr)
872873
attr_type = bool if attr in ("dev_mode", "safe_path") else int

Lib/test/test_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def test_names(self):
5555
'CoroutineType', 'EllipsisType', 'FrameType', 'FunctionType',
5656
'FrameLocalsProxyType',
5757
'GeneratorType', 'GenericAlias', 'GetSetDescriptorType',
58-
'LambdaType', 'MappingProxyType', 'MemberDescriptorType',
58+
'LambdaType', 'LazyImportType', 'MappingProxyType', 'MemberDescriptorType',
5959
'MethodDescriptorType', 'MethodType', 'MethodWrapperType',
6060
'ModuleType', 'NoneType', 'NotImplementedType', 'SimpleNamespace',
6161
'TracebackType', 'UnionType', 'WrapperDescriptorType',

Objects/lazyimportobject.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ static PyMethodDef lazy_methods[] = {
136136
};
137137

138138

139+
PyDoc_STRVAR(lazy_import_doc,
140+
"lazy_import(builtins, name, fromlist=None, /)\n"
141+
"--\n"
142+
"\n"
143+
"Represents a deferred import that will be resolved on first use.\n"
144+
"\n"
145+
"This type is used internally by the 'lazy import' statement.\n"
146+
"Users should not typically create instances directly.");
147+
139148
PyTypeObject PyLazyImport_Type = {
140149
PyVarObject_HEAD_INIT(&PyType_Type, 0)
141150
"lazy_import", /* tp_name */
@@ -158,7 +167,7 @@ PyTypeObject PyLazyImport_Type = {
158167
0, /* tp_as_buffer */
159168
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
160169
Py_TPFLAGS_BASETYPE, /* tp_flags */
161-
0, /* tp_doc */
170+
lazy_import_doc, /* tp_doc */
162171
(traverseproc)lazy_import_traverse, /* tp_traverse */
163172
(inquiry)lazy_import_clear, /* tp_clear */
164173
0, /* tp_richcompare */

0 commit comments

Comments
 (0)