Skip to content
Open
Show file tree
Hide file tree
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
43 changes: 43 additions & 0 deletions Lib/test/test_lazy_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,49 @@ def test_env_var_lazy_imports_none_disables_all_lazy(self):
self.assertEqual(result.returncode, 0, f"stderr: {result.stderr}")
self.assertIn("EAGER", result.stdout)

def test_cli_lazy_imports_none_disables_dunder_lazy_modules(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can merge the two tests since they have code in common:

    def test_cli_disable_lazy_imports(self):
        code = textwrap.dedent("""
            import sys
            __lazy_modules__ = ["json"]
            import json
            if 'json' in sys.modules:
                print("EAGER")
            else:
                print("LAZY")
        """)

        # -X lazy_imports=none should override __lazy_modules__
        result = subprocess.run(
            [sys.executable, "-X", "lazy_imports=none", "-c", code],
            capture_output=True,
            text=True,
        )
        self.assertEqual(result.returncode, 0, f"stderr: {result.stderr}")
        self.assertIn("EAGER", result.stdout)

        # PYTHON_LAZY_IMPORTS=none should override __lazy_modules__
        env = dict(os.environ, PYTHON_LAZY_IMPORTS="none")
        result = subprocess.run(
            [sys.executable, "-c", code],
            capture_output=True,
            text=True,
            env=env,
        )
        self.assertEqual(result.returncode, 0, f"stderr: {result.stderr}")
        self.assertIn("EAGER", result.stdout)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only common code is the example in code, so I think that's okay?

They are testing two different scenarios, so separate cases are fine. It's similar to the preceding two tests, which also share code and test different things.

Or we could use subtests?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only common code is the example in code, so I think that's okay?

It's just a suggestion, feel free to ignore it. Your PR was already approved ;-)

"""-X lazy_imports=none should override __lazy_modules__."""
code = textwrap.dedent("""
import sys
__lazy_modules__ = ["json"]
import json
if 'json' in sys.modules:
print("EAGER")
else:
print("LAZY")
""")
result = subprocess.run(
[sys.executable, "-X", "lazy_imports=none", "-c", code],
capture_output=True,
text=True,
)
self.assertEqual(result.returncode, 0, f"stderr: {result.stderr}")
self.assertIn("EAGER", result.stdout)

def test_env_var_lazy_imports_none_disables_dunder_lazy_modules(self):
"""PYTHON_LAZY_IMPORTS=none should override __lazy_modules__."""
code = textwrap.dedent("""
import sys
__lazy_modules__ = ["json"]
import json
if 'json' in sys.modules:
print("EAGER")
else:
print("LAZY")
""")
import os

env = os.environ.copy()
env["PYTHON_LAZY_IMPORTS"] = "none"
result = subprocess.run(
[sys.executable, "-c", code],
capture_output=True,
text=True,
env=env,
)
self.assertEqual(result.returncode, 0, f"stderr: {result.stderr}")
self.assertIn("EAGER", result.stdout)

def test_cli_overrides_env_var(self):
"""Command-line option should take precedence over environment variable."""
# PEP 810: -X lazy_imports takes precedence over PYTHON_LAZY_IMPORTS
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Ensure ``-X lazy_imports=none``` and ``PYTHON_LAZY_IMPORTS=none``` override
``__lazy_modules__``. Patch by Hugo van Kemenade.
2 changes: 1 addition & 1 deletion Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -3086,7 +3086,7 @@ _PyEval_LazyImportName(PyThreadState *tstate, PyObject *builtins,
break;
}

if (!lazy) {
if (!lazy && PyImport_GetLazyImportsMode() != PyImport_LAZY_NONE) {
// See if __lazy_modules__ forces this to be lazy.
lazy = check_lazy_import_compatibility(tstate, globals, name, level);
if (lazy < 0) {
Expand Down
Loading