From f03ee113c9f3dfeb477f2d4247bfb7de2e5f465c Mon Sep 17 00:00:00 2001 From: Zac Hatfield-Dodds Date: Sat, 2 Mar 2024 19:31:02 -0800 Subject: [PATCH] Ensure `blib2to3.pygram` is initialized before use (#4224) --- src/blib2to3/pytree.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/blib2to3/pytree.py b/src/blib2to3/pytree.py index 2a0cd6d196a..4c55d7ac77d 100644 --- a/src/blib2to3/pytree.py +++ b/src/blib2to3/pytree.py @@ -40,12 +40,15 @@ def type_repr(type_num: int) -> Union[str, int]: global _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 # from .pgen2 import token // token.__dict__.items(): - for name in dir(python_symbols): - val = getattr(python_symbols, name) + for name in dir(pygram.python_symbols): + val = getattr(pygram.python_symbols, name) if type(val) == int: _type_reprs[val] = name return _type_reprs.setdefault(type_num, type_num)