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

Unexpected effect of sys.pycache_prefix = "" #90135

Closed
akulakov opened this issue Dec 4, 2021 · 7 comments
Closed

Unexpected effect of sys.pycache_prefix = "" #90135

akulakov opened this issue Dec 4, 2021 · 7 comments
Labels
3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@akulakov
Copy link
Contributor

akulakov commented Dec 4, 2021

BPO 45977
Nosy @tiran, @akulakov

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2021-12-12.01:43:28.393>
created_at = <Date 2021-12-04.03:58:31.172>
labels = ['interpreter-core', 'type-bug', 'invalid', '3.11']
title = 'Unexpected effect of sys.pycache_prefix = ""'
updated_at = <Date 2021-12-12.01:43:28.392>
user = 'https://github.com/akulakov'

bugs.python.org fields:

activity = <Date 2021-12-12.01:43:28.392>
actor = 'andrei.avk'
assignee = 'none'
closed = True
closed_date = <Date 2021-12-12.01:43:28.393>
closer = 'andrei.avk'
components = ['Interpreter Core']
creation = <Date 2021-12-04.03:58:31.172>
creator = 'andrei.avk'
dependencies = []
files = []
hgrepos = []
issue_num = 45977
keywords = []
message_count = 7.0
messages = ['407632', '407633', '407729', '407763', '407764', '407775', '408358']
nosy_count = 2.0
nosy_names = ['christian.heimes', 'andrei.avk']
pr_nums = []
priority = 'low'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue45977'
versions = ['Python 3.11']

@akulakov
Copy link
Contributor Author

akulakov commented Dec 4, 2021

Setting sys.pycache_prefix = "", re-creates CWD structure under current directory, e.g. if run from /Users/foo/test, will create /Users/foo/test/Users/foo/test/myfile.pyc . Is that intentional? It seems a little weird. At least it might be good to document it.

@akulakov akulakov added 3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Dec 4, 2021
@akulakov
Copy link
Contributor Author

akulakov commented Dec 4, 2021

(I forgot to mention this happens on MacOS).

@tiran
Copy link
Member

tiran commented Dec 5, 2021

It works as designed and documented. An empty string is considered a relative directory.

https://docs.python.org/3/library/sys.html#sys.pycache_prefix

If this is set (not None), Python will write bytecode-cache .pyc files to (and read them from) a parallel directory tree rooted at this directory...

A relative path is interpreted relative to the current working directory.

@akulakov
Copy link
Contributor Author

akulakov commented Dec 6, 2021

If CWD=/a/b and pycache_prefix=c, the resulting dir is /a/b/c as I would expect.

If CWD=/a/b and pycache_prefix='', I would expect resulting dir to be /a/b
instead of /a/b/a/b .

For example as the shell cd command accepts relative path as arg, I would expect that cd c changes to /a/b/c ; but it would be very strange to expect cd with empty arg to change to /a/b/a/b .

@akulakov
Copy link
Contributor Author

akulakov commented Dec 6, 2021

Can this also create the risk of 'path too long' issues?

@tiran
Copy link
Member

tiran commented Dec 6, 2021

The cache prefix mirrors the directory structure. Python takes the absolute path to the .py file, converts the path to a relative path by stripping off the leading slash, and finally joins the path with the prefix.

Take this example:

$ PYTHONPYCACHEPREFIX=/tmp/pycache python3.10 -c pass

This will create the cache file "/tmp/pycache/usr/lib64/python3.10/os.cpython-310.pyc" for the Python file "/usr/lib64/python3.10/os.py.

@akulakov
Copy link
Contributor Author

Thanks, I've misunderstood how it works. It makes sense now. Closing as not a bug.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants