Skip to content

Commit 65fbdf2

Browse files
authored
Don't crash with --pyargs and a filename that looks like a modu… (#5503)
Don't crash with --pyargs and a filename that looks like a module
2 parents 5ef5079 + 3e0e31a commit 65fbdf2

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/_pytest/main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,10 @@ def _tryconvertpyarg(self, x):
631631
"""Convert a dotted module name to path."""
632632
try:
633633
spec = importlib.util.find_spec(x)
634-
except (ValueError, ImportError):
634+
# AttributeError: looks like package module, but actually filename
635+
# ImportError: module does not exist
636+
# ValueError: not a module name
637+
except (AttributeError, ImportError, ValueError):
635638
return x
636639
if spec is None or spec.origin in {None, "namespace"}:
637640
return x

testing/acceptance_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,12 @@ def test_pyargs_only_imported_once(self, testdir):
646646
# should only configure once
647647
assert result.outlines.count("configuring") == 1
648648

649+
def test_pyargs_filename_looks_like_module(self, testdir):
650+
testdir.tmpdir.join("conftest.py").ensure()
651+
testdir.tmpdir.join("t.py").write("def test(): pass")
652+
result = testdir.runpytest("--pyargs", "t.py")
653+
assert result.ret == ExitCode.OK
654+
649655
def test_cmdline_python_package(self, testdir, monkeypatch):
650656
import warnings
651657

0 commit comments

Comments
 (0)