Skip to content

[2.7] bpo-24960: use pkgutil.get_data in lib2to3 to read pickled grammar files (GH-4977) #4980

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

Merged
merged 1 commit into from
Dec 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Lib/lib2to3/pgen2/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import codecs
import os
import logging
import pkgutil
import StringIO
import sys

Expand Down Expand Up @@ -143,6 +144,26 @@ def _newer(a, b):
return os.path.getmtime(a) >= os.path.getmtime(b)


def load_packaged_grammar(package, grammar_source):
"""Normally, loads a pickled grammar by doing
pkgutil.get_data(package, pickled_grammar)
where *pickled_grammar* is computed from *grammar_source* by adding the
Python version and using a ``.pickle`` extension.

However, if *grammar_source* is an extant file, load_grammar(grammar_source)
is called instead. This facilities using a packaged grammar file when needed
but preserves load_grammar's automatic regeneration behavior when possible.

"""
if os.path.isfile(grammar_source):
return load_grammar(grammar_source)
pickled_name = _generate_pickle_name(os.path.basename(grammar_source))
data = pkgutil.get_data(package, pickled_name)
g = grammar.Grammar()
g.loads(data)
return g


def main(*args):
"""Main program, when run as a script: produce grammar pickle files.

Expand Down
4 changes: 4 additions & 0 deletions Lib/lib2to3/pgen2/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ def load(self, filename):
f.close()
self.__dict__.update(d)

def loads(self, pkl):
"""Load the grammar tables from a pickle bytes object."""
self.__dict__.update(pickle.loads(pkl))

def copy(self):
"""
Copy the grammar.
Expand Down
4 changes: 2 additions & 2 deletions Lib/lib2to3/pygram.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ def __init__(self, grammar):
setattr(self, name, symbol)


python_grammar = driver.load_grammar(_GRAMMAR_FILE)
python_grammar = driver.load_packaged_grammar("lib2to3", _GRAMMAR_FILE)

python_symbols = Symbols(python_grammar)

python_grammar_no_print_statement = python_grammar.copy()
del python_grammar_no_print_statement.keywords["print"]

pattern_grammar = driver.load_grammar(_PATTERN_GRAMMAR_FILE)
pattern_grammar = driver.load_packaged_grammar("lib2to3", _PATTERN_GRAMMAR_FILE)
pattern_symbols = Symbols(pattern_grammar)
15 changes: 15 additions & 0 deletions Lib/lib2to3/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
from .support import driver, test_dir

# Python imports
import operator
import os
import pickle
import shutil
import subprocess
import sys
import tempfile
import types
import unittest

# Local imports
Expand Down Expand Up @@ -97,6 +100,18 @@ def test_load_grammar_from_subprocess(self):
finally:
shutil.rmtree(tmpdir)

def test_load_packaged_grammar(self):
modname = __name__ + '.load_test'
class MyLoader:
def get_data(self, where):
return pickle.dumps({'elephant': 19})
class MyModule(types.ModuleType):
__file__ = 'parsertestmodule'
__loader__ = MyLoader()
sys.modules[modname] = MyModule(modname)
self.addCleanup(operator.delitem, sys.modules, modname)
g = pgen2_driver.load_packaged_grammar(modname, 'Grammar.txt')
self.assertEqual(g.elephant, 19)


class GrammarTest(support.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2to3 and lib2to3 can now read pickled grammar files using pkgutil.get_data()
rather than probing the filesystem. This lets 2to3 and lib2to3 work when run
from a zipfile.