if sys.version_info >= (3, 10): from typing import TypeGuard else: from typing_extensions import TypeGuard from mypy_extensions import mypyc_attr # lib2to3 fork from blib2to3.pytree import Node, Leaf, type_repr, NL from blib2to3 import pygram from blib2to3.pgen2 import token from black.cache import CACHE_DIR from black.strings import has_triple_quotes pygram.initialize(CACHE_DIR) syms: Final = pygram.python_symbols # types T = TypeVar("T") LN = Union[Leaf, Node] LeafID = int NodeType = int WHITESPACE: Final = {token.DEDENT, token.INDENT, token.NEWLINE} STATEMENT: Final = { syms.if_stmt, syms.while_stmt, syms.for_stmt, syms.try_stmt, syms.except_clause,
import tempfile from pathlib import Path import pyperf from blib2to3 import pygram # First try the relevant function post-refactor in 21.5b1, and fallback # to old function location. try: from black.parsing import lib2to3_parse except ImportError: from black import lib2to3_parse runner = pyperf.Runner() code = Path(r"{target}").read_text(encoding="utf8") with tempfile.TemporaryDirectory(prefix="blackbench-parsing-") as path: # Block the parser code from using any pre-existing cache. pygram.initialize(path) runner.bench_func("{name}", lib2to3_parse, code)