Skip to content

Commit 61f2a26

Browse files
committed
Code/getfslineno: keep empty co_filename
Previously this would be turned via `py.path.local("")` into the current working directory. This appears to be what `fspath = fn and py.path.local(fn) or None` tries to avoid in `getfslineno`'s `TypeError` handling already, if `Code` would raise it.
1 parent b0d4526 commit 61f2a26

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/_pytest/_code/code.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ def path(self) -> Union[py.path.local, str]:
7272
""" return a path object pointing to source code (or a str in case
7373
of OSError / non-existing file).
7474
"""
75+
if not self.raw.co_filename:
76+
return ""
7577
try:
7678
p = py.path.local(self.raw.co_filename)
7779
# maybe don't try this checking

testing/code/test_source.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,14 @@ class B:
524524
B.__name__ = "B2"
525525
assert getfslineno(B)[1] == -1
526526

527+
co = compile("...", "", "eval")
528+
assert co.co_filename == ""
529+
530+
if hasattr(sys, "pypy_version_info"):
531+
assert getfslineno(co) == ("", -1)
532+
else:
533+
assert getfslineno(co) == ("", 0)
534+
527535

528536
def test_code_of_object_instance_with_call() -> None:
529537
class A:

0 commit comments

Comments
 (0)