Ensure blib2to3.pygram is initialized before use (#4224)

This commit is contained in:
Zac Hatfield-Dodds 2024-03-02 19:31:02 -08:00 committed by GitHub
parent e4bfedbec2
commit f03ee113c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -40,12 +40,15 @@
def type_repr(type_num: int) -> Union[str, int]: def type_repr(type_num: int) -> Union[str, int]:
global _type_reprs global _type_reprs
if not _type_reprs: if not _type_reprs:
from .pygram import python_symbols from . import pygram
if not hasattr(pygram, "python_symbols"):
pygram.initialize(cache_dir=None)
# printing tokens is possible but not as useful # printing tokens is possible but not as useful
# from .pgen2 import token // token.__dict__.items(): # from .pgen2 import token // token.__dict__.items():
for name in dir(python_symbols): for name in dir(pygram.python_symbols):
val = getattr(python_symbols, name) val = getattr(pygram.python_symbols, name)
if type(val) == int: if type(val) == int:
_type_reprs[val] = name _type_reprs[val] = name
return _type_reprs.setdefault(type_num, type_num) return _type_reprs.setdefault(type_num, type_num)