Skip to content

Commit bf9da48

Browse files
authored
Merge pull request #2215 from adriangb/fix-anyio
postpone referencing sys.modules["__main__"]
2 parents 5ba2320 + 38e6174 commit bf9da48

2 files changed

Lines changed: 7 additions & 12 deletions

File tree

src/click/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ def __getattr__(self, attr: str) -> t.Any:
475475

476476

477477
def _detect_program_name(
478-
path: t.Optional[str] = None, _main: ModuleType = sys.modules["__main__"]
478+
path: t.Optional[str] = None, _main: t.Optional[ModuleType] = None
479479
) -> str:
480480
"""Determine the command used to run the program, for use in help
481481
text. If a file or entry point was executed, the file name is
@@ -497,6 +497,9 @@ def _detect_program_name(
497497
498498
:meta private:
499499
"""
500+
if _main is None:
501+
_main = sys.modules["__main__"]
502+
500503
if not path:
501504
path = sys.argv[0]
502505

tests/test_utils.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -444,20 +444,12 @@ def __init__(self, package_name):
444444
("example.py", None, "example.py"),
445445
(str(pathlib.Path("/foo/bar/example.py")), None, "example.py"),
446446
("example", None, "example"),
447-
(
448-
str(pathlib.Path("example/__main__.py")),
449-
MockMain(".example"),
450-
"python -m example",
451-
),
452-
(
453-
str(pathlib.Path("example/cli.py")),
454-
MockMain(".example"),
455-
"python -m example.cli",
456-
),
447+
(str(pathlib.Path("example/__main__.py")), "example", "python -m example"),
448+
(str(pathlib.Path("example/cli.py")), "example", "python -m example.cli"),
457449
],
458450
)
459451
def test_detect_program_name(path, main, expected):
460-
assert click.utils._detect_program_name(path, _main=main) == expected
452+
assert click.utils._detect_program_name(path, _main=MockMain(main)) == expected
461453

462454

463455
def test_expand_args(monkeypatch):

0 commit comments

Comments
 (0)