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

blib2to3 tries to store grammar pickle in global site-packages #192

Closed
tiran opened this issue May 5, 2018 · 3 comments
Closed

blib2to3 tries to store grammar pickle in global site-packages #192

tiran opened this issue May 5, 2018 · 3 comments

Comments

@tiran
Copy link
Contributor

tiran commented May 5, 2018

blib2to3 tries to store a version specific pickle of the grammar in the global site-package directory. This works fine for user-owned installations in a virtual env but not for OS vendored installation. Users typically don't have permission to write to system-wide site-packages directory.

$ strace black
...
openat(AT_FDCWD, "/usr/lib/python3.6/site-packages/blib2to3/Grammar3.6.5.final.0.pickle", O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0666) = -1 EACCES (Permission denied)
stat("/usr/lib/python3.6/site-packages/blib2to3/PatternGrammar.txt", {st_mode=S_IFREG|0644, st_size=793, ...}) = 0
stat("/usr/lib/python3.6/site-packages/blib2to3/PatternGrammar3.6.5.final.0.pickle", 0x7ffc225e5c40) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/python3.6/site-packages/blib2to3/PatternGrammar.txt", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=793, ...}) = 0
...

I tracked down the write through Grammar.dump, load_grammar and load_packaged_grammar to https://github.com/ambv/black/blob/master/blib2to3/pygram.py. The load_grammar takes a pickle file name as gp argument, but load_packaged_grammar and pygram don't use it.

Some pickle files are checked into git but not installed by setup.py, too.

@tiran
Copy link
Contributor Author

tiran commented May 5, 2018

The cache shoves off about 18ms startup time on my machine:

$ python3 -m timeit -s "from blib2to3.pgen2 import pgen" "pgen.generate_grammar('blib2to3/Grammar.txt')"
100 loops, best of 3: 18.1 msec per loop
$ python3 -m timeit -s "from blib2to3.pgen2 import driver" "driver.load_grammar('blib2to3/Grammar.txt')"
1000 loops, best of 3: 245 usec per loop

@ambv
Copy link
Collaborator

ambv commented May 5, 2018

We should move this cache to the cache dir in appdirs. @ojii, interested?

@ojii
Copy link
Contributor

ojii commented May 6, 2018

I'll try to have a look next week.

amyreese added a commit to amyreese/black that referenced this issue May 17, 2018
Uses `appdirs.user_cache_dir("black") / "blib2to3"`
tiran added a commit to tiran/black that referenced this issue May 18, 2018
There is no need to keep the pickled grammar files in git. PR psf#203 will
move them into a user-specific cache directory any way.

See: psf#192
Signed-off-by: Christian Heimes <christian@python.org>
ambv pushed a commit that referenced this issue May 18, 2018
There is no need to keep the pickled grammar files in git. PR #203 will
move them into a user-specific cache directory any way.

See: #192
Signed-off-by: Christian Heimes <christian@python.org>
tiran added a commit to tiran/black that referenced this issue May 19, 2018
blib2to3.pygram assumes that blib2to3's directory is writeable. This is
not the case for global installation of black on Linux distributions.

Additionally Python 2 grammar no longer contains async and await keywords.

Fixes: psf#192
Signed-off-by: Christian Heimes <christian@python.org>
tiran added a commit to tiran/black that referenced this issue May 19, 2018
blib2to3.pygram assumes that blib2to3's directory is writeable. This is
not the case for global installation of black on Linux distributions.

Fixes: psf#192
Signed-off-by: Christian Heimes <christian@python.org>
tiran added a commit to tiran/black that referenced this issue May 19, 2018
blib2to3.pygram assumes that blib2to3's directory is writeable. This is
not the case for global installation of black on Linux distributions.

Fixes: psf#192
Signed-off-by: Christian Heimes <christian@python.org>
@ambv ambv closed this as completed in c891c65 May 19, 2018
zsol pushed a commit to zsol/blib2to3 that referenced this issue Jun 19, 2019
There is no need to keep the pickled grammar files in git. PR #203 will
move them into a user-specific cache directory any way.

See: psf/black#192
Signed-off-by: Christian Heimes <christian@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants