Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

gh-146369: EnsurePYTHON_LAZY_IMPORTS=none overrides__lazy_modules__#146371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
hugovk wants to merge3 commits intopython:main
base:main
Choose a base branch
Loading
fromhugovk:3.15-fix-lazy-dunder-none
Open
Show file tree
Hide file tree
Changes fromall 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 deletionsLib/test/test_lazy_import/__init__.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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:

deftest_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
MemberAuthor

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 incode, 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 sharecode 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 ;-)

hugovk reacted with thumbs up emoji
"""-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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionPython/ceval.c
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2026 Movatter.jp