mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
bpo-24960: use pkgutil.get_data in lib2to3 to read pickled grammar files (#4977)
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.
This commit is contained in:
parent
62ed6be8da
commit
8a5877165e
5 changed files with 45 additions and 2 deletions
|
@ -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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue