fix: runpy.run_path(path) crashes with pathlib.Path by askhl · Pull Request #1819 · nedbat/coveragepy (original) (raw)
I ran into the following problem where coverage replaces runpy.run_path()
in a way that does not accept Path objects:
import runpy
script = "print('hello, world!')\n"
def test_path(tmp_path):
pyfile = tmp_path / 'script.py'
pyfile.write_text(script)
runpy.run_path(pyfile)
This passes with ordinary pytest but crashes with pytest --cov
with the following traceback:
Traceback (most recent call last):
File "/home/askhl/install/pyenv/lib/python3.10/site-packages/_pytest/runner.py", line 341, in from_call
result: Optional[TResult] = func()
File "/home/askhl/install/pyenv/lib/python3.10/site-packages/flaky/flaky_pytest_plugin.py", line 146, in <lambda>
lambda: ihook(item=item, **kwds), when=when, reraise=reraise
File "/home/askhl/install/pyenv/lib/python3.10/site-packages/pluggy/_hooks.py", line 513, in __call__
return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
File "/home/askhl/install/pyenv/lib/python3.10/site-packages/pluggy/_manager.py", line 120, in _hookexec
return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
File "/home/askhl/install/pyenv/lib/python3.10/site-packages/pluggy/_callers.py", line 182, in _multicall
return outcome.get_result()
File "/home/askhl/install/pyenv/lib/python3.10/site-packages/pluggy/_result.py", line 100, in get_result
raise exc.with_traceback(exc.__traceback__)
File "/home/askhl/install/pyenv/lib/python3.10/site-packages/pluggy/_callers.py", line 103, in _multicall
res = hook_impl.function(*args)
File "/home/askhl/install/pyenv/lib/python3.10/site-packages/_pytest/runner.py", line 177, in pytest_runtest_call
raise e
File "/home/askhl/install/pyenv/lib/python3.10/site-packages/_pytest/runner.py", line 169, in pytest_runtest_call
item.runtest()
File "/home/askhl/install/pyenv/lib/python3.10/site-packages/_pytest/python.py", line 1792, in runtest
self.ihook.pytest_pyfunc_call(pyfuncitem=self)
File "/home/askhl/install/pyenv/lib/python3.10/site-packages/pluggy/_hooks.py", line 513, in __call__
return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
File "/home/askhl/install/pyenv/lib/python3.10/site-packages/pluggy/_manager.py", line 120, in _hookexec
return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
File "/home/askhl/install/pyenv/lib/python3.10/site-packages/pluggy/_callers.py", line 139, in _multicall
raise exception.with_traceback(exception.__traceback__)
File "/home/askhl/install/pyenv/lib/python3.10/site-packages/pluggy/_callers.py", line 103, in _multicall
res = hook_impl.function(*args)
File "/home/askhl/install/pyenv/lib/python3.10/site-packages/_pytest/python.py", line 194, in pytest_pyfunc_call
result = testfunction(**testargs)
File "/home/askhl/coverage-runpy/test.py", line 11, in test_path
runpy.run_path(pyfile)
File "/usr/lib/python3.10/runpy.py", line 289, in run_path
return _run_module_code(code, init_globals, run_name,
File "/usr/lib/python3.10/runpy.py", line 96, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "/home/askhl/src/coveragepy/coverage/control.py", line 395, in _should_trace
disp = self._inorout.should_trace(filename, frame)
File "/home/askhl/src/coveragepy/coverage/inorout.py", line 324, in should_trace
filename = source_for_file(dunder_file)
File "/home/askhl/src/coveragepy/coverage/python.py", line 107, in source_for_file
if filename.endswith(".py"):
AttributeError: 'PosixPath' object has no attribute 'endswith'
This PR contains a minimal fix which sends the input through str(Path(...))
. However a proper fix might require some awareness of the surrounding code as well as a test, so I'd expect the PR in its current form to be insufficient. Feedback would be appreciated.