Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions 35 Lib/test/test_import/test_lazy_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import threading
import types
import unittest
import tempfile
import os

try:
import _testcapi
Expand Down Expand Up @@ -598,6 +600,39 @@ def test_error_during_module_execution_propagates(self):
self.assertEqual(result.returncode, 0, f"stdout: {result.stdout}, stderr: {result.stderr}")
self.assertIn("OK", result.stdout)

def test_circular_lazy_import_does_not_crash_for_gh_144727(self):
with tempfile.TemporaryDirectory() as tmpdir:
a_path = os.path.join(tmpdir, "a.py")
b_path = os.path.join(tmpdir, "b.py")

with open(a_path, "w") as f:
f.write(textwrap.dedent("""\
lazy import b

def something():
b.hello()

something()
"""))

with open(b_path, "w") as f:
f.write(textwrap.dedent("""\
lazy import a

def hello():
print(a)
"""))

result = subprocess.run(
[sys.executable, a_path],
capture_output=True,
text=True,
cwd=tmpdir,
)
# Should get a proper Python error, not a crash
self.assertEqual(result.returncode, 1)
self.assertIn("Error", result.stderr)


class GlobalsAndDictTests(unittest.TestCase):
"""Tests for globals() and __dict__ behavior with lazy imports.
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.