Skip to content

Commit fcd6529

Browse files
committed
Add lazy import dis tests
1 parent a80c794 commit fcd6529

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
@@ -1607,5 +1609,27 @@ def stress_lazy_imports(idx):
16071609
self.assertIn("OK", result.stdout)
16081610

16091611

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

0 commit comments

Comments
 (0)