Skip to content

Commit

Permalink
Merge pull request #2215 from adriangb/fix-anyio
Browse files Browse the repository at this point in the history
postpone referencing sys.modules["__main__"]
  • Loading branch information
davidism committed Mar 17, 2022
2 parents 5ba2320 + 38e6174 commit bf9da48
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
5 changes: 4 additions & 1 deletion src/click/utils.py
Expand Up @@ -475,7 +475,7 @@ def __getattr__(self, attr: str) -> t.Any:


def _detect_program_name(
path: t.Optional[str] = None, _main: ModuleType = sys.modules["__main__"]
path: t.Optional[str] = None, _main: t.Optional[ModuleType] = None
) -> str:
"""Determine the command used to run the program, for use in help
text. If a file or entry point was executed, the file name is
Expand All @@ -497,6 +497,9 @@ def _detect_program_name(
:meta private:
"""
if _main is None:
_main = sys.modules["__main__"]

if not path:
path = sys.argv[0]

Expand Down
14 changes: 3 additions & 11 deletions tests/test_utils.py
Expand Up @@ -444,20 +444,12 @@ def __init__(self, package_name):
("example.py", None, "example.py"),
(str(pathlib.Path("/foo/bar/example.py")), None, "example.py"),
("example", None, "example"),
(
str(pathlib.Path("example/__main__.py")),
MockMain(".example"),
"python -m example",
),
(
str(pathlib.Path("example/cli.py")),
MockMain(".example"),
"python -m example.cli",
),
(str(pathlib.Path("example/__main__.py")), "example", "python -m example"),
(str(pathlib.Path("example/cli.py")), "example", "python -m example.cli"),
],
)
def test_detect_program_name(path, main, expected):
assert click.utils._detect_program_name(path, _main=main) == expected
assert click.utils._detect_program_name(path, _main=MockMain(main)) == expected


def test_expand_args(monkeypatch):
Expand Down

0 comments on commit bf9da48

Please sign in to comment.