Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

coverage run breaks PYTHONSAFEPATH #1696

Closed
flying-sheep opened this issue Nov 10, 2023 · 3 comments
Closed

coverage run breaks PYTHONSAFEPATH #1696

flying-sheep opened this issue Nov 10, 2023 · 3 comments
Labels
bug Something isn't working fixed

Comments

@flying-sheep
Copy link
Contributor

flying-sheep commented Nov 10, 2023

Describe the bug
When executing coverage run -m ... with the environment variable PYTHONSAFEPATH=1 set, the working directory appears in sys.path

To Reproduce

Create the following two files

printpath.py

import sys
import json

print(json.dumps(sys.path))

pyproject.toml

[build-system]
build-backend = "hatchling.build"
requires = ["hatchling"]

[project]
name = "printpath"
version = "0.0.1"
dependencies = ["coverage"]

[tool.hatch.envs.default]
dev-mode = false
[tool.hatch.envs.default.scripts]
printpath = ["PYTHONSAFEPATH=1 python -m printpath"]
printpath-cov = ["PYTHONSAFEPATH=1 coverage run -m printpath"]

then run

$ hatch run printpath
["/usr/lib/python311.zip", "/usr/lib/python3.11", "/usr/lib/python3.11/lib-dynload", "~/.local/share/hatch/env/virtual/printpath/1Fj_Wp0T/printpath/lib/python3.11/site-packages"]
$ hatch run printpath-cov
["~/Dev/Python/_reproducers/coverage-safepath", "/usr/lib/python3.11", "/usr/lib/python3.11/lib-dynload", "~/.local/share/hatch/env/virtual/printpath/1Fj_Wp0T/printpath/lib/python3.11/site-packages"]

Expected behavior
Both commands have the same output

@nedbat
Copy link
Owner

nedbat commented Nov 14, 2023

#1700 is a pull request in progress to fix this.

nedbat pushed a commit that referenced this issue Mar 22, 2025
Fix

make test mostly work

changes from code review

changes from code review

Fix lint error
nedbat added a commit that referenced this issue Mar 22, 2025
temp: fix lint errors from rebases

docs: a note to ourselves about interpreting test failures

test: one more safepath test

fix: use sys.flags instead of reading the PYTHONSAFEPATH

test: skip the test that show Windows gets PYTHONSAFEPATH wrong
@nedbat
Copy link
Owner

nedbat commented Mar 22, 2025

Fixed in commit 32e8d79.

@nedbat nedbat closed this as completed Mar 22, 2025
@nedbat nedbat added the fixed label Mar 22, 2025
@flying-sheep
Copy link
Contributor Author

thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants