summaryrefslogtreecommitdiffstats
path: root/Lib/lib2to3/tests/test_parser.py
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2017-12-22 20:51:46 (GMT)
committerBenjamin Peterson <benjamin@python.org>2017-12-22 20:51:46 (GMT)
commitc1b8eb8006ed79bbf91dc7d89918f39cc10b4fe6 (patch)
tree0736b8d7f637e6f4cf4c4d0c26fa0cfa04398d02 /Lib/lib2to3/tests/test_parser.py
parent2e1ef00171179a8b906631b175cde2e68a804522 (diff)
downloadcpython-c1b8eb8006ed79bbf91dc7d89918f39cc10b4fe6.zip
cpython-c1b8eb8006ed79bbf91dc7d89918f39cc10b4fe6.tar.gz
cpython-c1b8eb8006ed79bbf91dc7d89918f39cc10b4fe6.tar.bz2
bpo-24960: use pkgutil.get_data in lib2to3 to read pickled grammar files (GH-4977) (#4979)
This is more complicated than it should be because we need to preserve the useful mtime-based regeneration feature that lib2to3.pgen2.driver.load_grammar has. We only look for the pickled grammar file with pkgutil.get_data and only if the source file does not exist. (cherry picked from commit 8a5877165e993afb2633cd48da5222326d3f6e0e)
Diffstat (limited to 'Lib/lib2to3/tests/test_parser.py')
-rw-r--r--Lib/lib2to3/tests/test_parser.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/Lib/lib2to3/tests/test_parser.py b/Lib/lib2to3/tests/test_parser.py
index 3f7ab97..0a13950 100644
--- a/Lib/lib2to3/tests/test_parser.py
+++ b/Lib/lib2to3/tests/test_parser.py
@@ -12,7 +12,10 @@ from .support import driver
from test.support import verbose
# Python imports
+import importlib
+import operator
import os
+import pickle
import shutil
import subprocess
import sys
@@ -99,6 +102,18 @@ pgen2_driver.load_grammar(%r, save=True, force=True)
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:
+ __file__ = 'parsertestmodule'
+ __spec__ = importlib.util.spec_from_loader(modname, MyLoader())
+ sys.modules[modname] = MyModule()
+ 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):