Skip to content

Commit 87c498d

Browse files
committed
Add lazy import dis tests
1 parent 074cb7b commit 87c498d

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

Lib/test/test_import/test_lazy_imports.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""Tests for PEP 810 lazy imports."""
22

3-
import sys
3+
import io
4+
import dis
45
import subprocess
6+
import sys
57
import textwrap
68
import threading
79
import types
@@ -1606,5 +1608,27 @@ def stress_lazy_imports(idx):
16061608
self.assertIn("OK", result.stdout)
16071609

16081610

1611+
class LazyImportDisTests(unittest.TestCase):
1612+
def test_lazy_import_dis(self):
1613+
"""dis should properly show lazy import"""
1614+
code = compile("lazy import foo", "exec", "exec")
1615+
f = io.StringIO()
1616+
dis.dis(code, file=f)
1617+
self.assertIn("foo + lazy", f.getvalue())
1618+
1619+
def test_normal_import_dis(self):
1620+
"""non lazy imports should just show the name"""
1621+
code = compile("import foo", "exec", "exec")
1622+
f = io.StringIO()
1623+
dis.dis(code, file=f)
1624+
for line in f.getvalue().split('\n'):
1625+
if "IMPORT_NAME" in line:
1626+
self.assertIn("(foo)", line)
1627+
break
1628+
else:
1629+
self.assertFail("IMPORT_NAME not found")
1630+
1631+
1632+
16091633
if __name__ == '__main__':
16101634
unittest.main()

0 commit comments

Comments
 (0)