Skip to content

Commit

Permalink
fix: nicer tracebacks in run mode on scripts (#1191)
Browse files Browse the repository at this point in the history
Co-authored-by: Philipp A. <flying-sheep@web.de>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 16, 2024
1 parent 6918c4d commit 18a0784
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelog.d/1191.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Report correct filename in tracebacks with `pipx run <scriptname>`
22 changes: 15 additions & 7 deletions src/pipx/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import urllib.request
from pathlib import Path
from shutil import which
from typing import List, NoReturn, Optional
from typing import List, NoReturn, Optional, Union

from packaging.requirements import InvalidRequirement, Requirement

Expand Down Expand Up @@ -42,14 +42,14 @@
{app_lines}"""


def maybe_script_content(app: str, is_path: bool) -> Optional[str]:
def maybe_script_content(app: str, is_path: bool) -> Optional[Union[str, Path]]:
# If the app is a script, return its content.
# Return None if it should be treated as a package name.

# Look for a local file first.
app_path = Path(app)
if app_path.is_file():
return app_path.read_text(encoding="utf-8")
return app_path
elif is_path:
raise PipxError(f"The specified path {app} does not exist")

Expand All @@ -71,7 +71,7 @@ def maybe_script_content(app: str, is_path: bool) -> Optional[str]:


def run_script(
content: str,
content: Union[str, Path],
app_args: List[str],
python: str,
pip_args: List[str],
Expand All @@ -81,7 +81,7 @@ def run_script(
) -> NoReturn:
requirements = _get_requirements_from_script(content)
if requirements is None:
exec_app([python, "-c", content, *app_args])
python_path = Path(python)
else:
# Note that the environment name is based on the identified
# requirements, and *not* on the script name. This is deliberate, as
Expand All @@ -99,7 +99,12 @@ def run_script(
venv = Venv(venv_dir, python=python, verbose=verbose)
venv.create_venv(venv_args, pip_args)
venv.install_unmanaged_packages(requirements, pip_args)
exec_app([venv.python_path, "-c", content, *app_args])
python_path = venv.python_path

if isinstance(content, Path):
exec_app([python_path, content, *app_args])
else:
exec_app([python_path, "-c", content, *app_args])


def run_package(
Expand Down Expand Up @@ -323,11 +328,14 @@ def _http_get_request(url: str) -> str:
INLINE_SCRIPT_METADATA = re.compile(r"(?m)^# /// (?P<type>[a-zA-Z0-9-]+)$\s(?P<content>(^#(| .*)$\s)+)^# ///$")


def _get_requirements_from_script(content: str) -> Optional[List[str]]:
def _get_requirements_from_script(content: Union[str, Path]) -> Optional[List[str]]:
"""
Supports inline script metadata.
"""

if isinstance(content, Path):
content = content.read_text(encoding="utf-8")

name = "script"

# Windows is currently getting un-normalized line endings, so normalize
Expand Down
18 changes: 18 additions & 0 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,24 @@ def test_run_with_requirements_old(caplog, pipx_temp_env, tmp_path):
run_pipx_cli_exit(["run", script.as_uri()])


@mock.patch("os.execvpe", new=execvpe_mock)
def test_run_correct_traceback(capfd, pipx_temp_env, tmp_path):
script = tmp_path / "test.py"
script.write_text(
textwrap.dedent(
"""
raise RuntimeError("Should fail")
"""
).strip()
)

with pytest.raises(SystemExit):
run_pipx_cli(["run", str(script)])

captured = capfd.readouterr()
assert "test.py" in captured.err


@mock.patch("os.execvpe", new=execvpe_mock)
def test_run_with_args(caplog, pipx_temp_env, tmp_path):
script = tmp_path / "test.py"
Expand Down

0 comments on commit 18a0784

Please sign in to comment.