From 21d1f0ceb890f118e6a20614c0304283ab859a0a Mon Sep 17 00:00:00 2001 From: syfyra <258908070+syfyra@users.noreply.github.com> Date: Thu, 26 Nov 2026 10:44:43 +0100 Subject: [PATCH 1/4] tests: support.py compiles the translator from pieces, 7 s -> 1 s per uncached import (TEST-TRANSLATOR-PIECEWISE-PARSE) On CPython 3.12.0 to 3.14.7 the tokenizer copies the rest of its input at every f-string field (update_fstring_expr: strlen, PyMem_Malloc, strncpy), and import hands a module over as one string, so compiling the 1.2 MB tools/rdna2spirv.py takes 7 s, 0.1 s of it code generation. CPython fixed it in 3.14.8 and 3.15.0 (gh-155525); 3.12, which CI pins, keeps it. tests/semantic/piecewise_parse.py finds statement starts with tokenize, parses top-level statements in pieces of 300 lines (a longer class is cut between the statements of its body) with one newline in front for every line before the piece, and joins them into the tree a whole-file parse gives. Whatever does not fit returns None and the source is compiled the usual way. tests/semantic/support.py loads the modules of TOOLS_DIR through a SourceFileLoader subclass that compiles that tree, by a sys.meta_path finder placed just before PathFinder, only on the interpreters above. A valid .pyc is still used first. tests/test_piecewise_parse.py compares the trees for every Python file of the repository, hand-written boundary cases, the exceptions and warnings against compile(), and code objects against compile(bytes). Co-Authored-By: Claude Fable 5.2 --- tests/semantic/README.md | 10 + tests/semantic/piecewise_parse.py | 197 +++++++ tests/semantic/support.py | 43 ++ tests/test_piecewise_parse.py | 935 ++++++++++++++++++++++++++++++ 4 files changed, 1185 insertions(+) create mode 100644 tests/semantic/piecewise_parse.py create mode 100644 tests/test_piecewise_parse.py diff --git a/tests/semantic/README.md b/tests/semantic/README.md index 5360ab2443..22d5d92f6d 100644 --- a/tests/semantic/README.md +++ b/tests/semantic/README.md @@ -18,6 +18,16 @@ fail and one summary line; `-j N` sets how many run at once (`-j 1` is the disco every file's output. Files run side by side, so a test writes only into its own temporary folder (`tempfile`), never to a fixed path, and does not rely on another file having run first. +A process that finds no valid `.pyc` of `tools/rdna2spirv.py` compiles it from source: after every edit of the +translator (most files here set `sys.dont_write_bytecode`, so they do not leave a new one), for each mutant of +`test_mutations.py`, and always where `PYTHONDONTWRITEBYTECODE` is set. On CPython 3.12 to 3.14.7 parsing that file +as one string takes 7 s, because the tokenizer copies the rest of the source at every f-string field (fixed in +3.14.8 and 3.15.0, gh-155525; CI runs 3.12, which keeps it). On those interpreters `support.py` loads the modules +of `tools/` through `piecewise_parse.py`, which parses a source in pieces of 300 lines and joins them into the tree +a whole-file parse gives: under 1 s for the translator, and the same code object. `tests/test_piecewise_parse.py` +compares the trees for every Python file of the repository and the code objects for some. A file that imports +`rdna2spirv` before `support`, or without it, still pays the 7 s. + Every file here must call `tools/rdna2spirv.py` (directly, through `support.py`, or by running the tool) and assert on what it emitted. A test that reimplements the instruction in Python passes whatever the translator does, so it is not a regression test. `tests/test_semantic_translator_use.py` enforces this; the interpreter self-tests and the diff --git a/tests/semantic/piecewise_parse.py b/tests/semantic/piecewise_parse.py new file mode 100644 index 0000000000..b2d453cbb1 --- /dev/null +++ b/tests/semantic/piecewise_parse.py @@ -0,0 +1,197 @@ +"""Parse a Python source in pieces and join them into the tree a whole-file parse gives (standard library only). + + tree = piecewise_parse.parse(source, filename) # equal to ast.parse(source, filename), positions included + +Why: from CPython 3.12.0 (PEP 701) the tokenizer copies the rest of its input at every replacement field of an +f-string: `update_fstring_expr` in Parser/tokenizer.c (in Parser/lexer/lexer.c from 3.13, as +`_PyLexer_update_fstring_expr` and then `_PyLexer_update_ftstring_expr`) does `strlen(tok->cur)`, `PyMem_Malloc` +and `strncpy`. `import`, `compile` and `ast.parse` hand the module over as one string, so "the rest" is the rest of +the file and the time grows with the square of its size: tools/rdna2spirv.py (1.2 MB, 4,759 f-strings with about +12,700 fields) takes 7 s to parse and 0.1 s to turn into code. A file run as a script is read line by line and is +not affected. CPython 3.14.8 and 3.15.0 fixed it (gh-155525); 3.12, which CI runs, only gets security fixes now and +keeps it, and so does every 3.13 up to 3.13.15. `needed()` says whether the running interpreter is one of those. + +What a field costs depends on how much source follows it, so the same statements are cheap in a short string: + +* `tokenize` reads line by line (linear). It gives the depth of every logical line (INDENT/DEDENT) and its first + token. A statement starts at a logical line that does not begin with `elif`, `else`, `except` or `finally` and + does not follow a decorator line. +* Consecutive top-level statements are gathered into pieces of about PIECE_LINES lines. Each piece is parsed with + one "\\n" in front for every line before it, so every node keeps its line and column. +* A longer top-level `class` is cut between the statements of its body. The first piece, with the header, gives the + real ClassDef. Each later piece is parsed under `class _:` written on the line before it, its statements are + appended to the real body, and the class ends where the last piece's `class _:` ends (both end at the last token + of the body). + +A piece starts where the whole-file tokenizer starts a logical line with the same indentation stack and ends where +it has closed every bracket and string, so the tokens are the same, and no rule of the grammar looks past a +statement except for the clauses and decorators kept together above. Whatever does not fit is not guessed at: +`parse_pieces` returns None for a source it cannot split, a piece that does not parse, or a piece in which a row +`tokenize` gave is not where a statement starts, and the caller compiles the whole source the usual way, with the +usual exception. A SyntaxWarning the parser gives for a line of a piece (an invalid escape, say) is given once, +with the file's name and line, as in a whole-file parse; only a source that then fails in a later piece gets it a +second time, from the whole-file parse that reports the error. + +tests/test_piecewise_parse.py compares the trees, every field and position, for every Python file of the +repository (`ast.dump(..., include_attributes=True)` for the hand-written cases and the translator), and the code +objects for some. +""" +import ast +import importlib.util +import io +import sys +import tokenize + +# tools/rdna2spirv.py on Python 3.14.7: 0.89 s in pieces of 50 or 100 lines, 0.90 s at 300, 1.10 s at 1,000, +# 1.85 s at 5,000 and 7.0 s whole. Below 300 the copying is already gone and smaller pieces only add calls. +PIECE_LINES = 300 +DUMMY = "class _:\n" + +_CONTINUES = frozenset(("elif", "else", "except", "finally")) +_BLANK = frozenset((tokenize.NL, tokenize.COMMENT, tokenize.ENDMARKER)) + + +class _Whole(Exception): + """This source is to be parsed whole.""" + + +# What tokenizing or parsing a piece can raise for a source that a whole-file parse may still accept, or reports +# in its own words: all of them mean "parse it whole". +_GIVE_UP = (_Whole, SyntaxError, ValueError, RecursionError, MemoryError, OverflowError, tokenize.TokenError) + + +def needed(version=None, implementation=None): + """True on the interpreters whose tokenizer copies the rest of the source at every f-string field: CPython + 3.12.0 up to 3.14.7 (see the module docstring). Elsewhere a whole-file parse is as fast and simpler.""" + version = tuple((sys.version_info if version is None else version)[:3]) + implementation = sys.implementation.name if implementation is None else implementation + return implementation == "cpython" and (3, 12) <= version < (3, 14, 8) + + +def parse(source, filename="", piece_lines=PIECE_LINES): + """The module tree of `source` (str, or the bytes of a file): what `ast.parse(source, filename)` returns, and + the same exception for a source with an error.""" + tree = parse_pieces(source, filename, piece_lines) + if tree is None: + tree = compile(source, filename, "exec", ast.PyCF_ONLY_AST, dont_inherit=True) + return tree + + +def parse_pieces(source, filename="", piece_lines=PIECE_LINES): + """The same tree built from pieces of about `piece_lines` lines, or None when the source has to be parsed whole + (see the module docstring). Nothing is raised for a source with an error: the whole-file parse reports it.""" + if sys.version_info < (3, 12): + # Before 3.12 `tokenize` is not the parser's tokenizer (it does not apply the tab rule, for one), so the + # statements it finds prove nothing about what the parser accepts; and there is nothing to gain there. + return None + try: + if not isinstance(source, str): + source = importlib.util.decode_source(source) # as import does: coding line, BOM, newlines + if "\r" in source: # the tokenizer reads "\r\n" and "\r" as "\n" + source = source.replace("\r\n", "\n").replace("\r", "\n") + # A NUL is an error. `from __future__ import barry_as_FLUFL` changes what the parser accepts after it. + if "\0" in source or "barry_as_FLUFL" in source: + return None + rows = io.StringIO(source).readlines() # split at "\n" only, as tokenize numbers them + if len(rows) <= piece_lines: # one piece: nothing to look for + return compile(source, filename, "exec", ast.PyCF_ONLY_AST, dont_inherit=True) + + def statements(first, end, header=""): + """What rows first..end-1 parse to, `header` standing on the row before them.""" + text = "\n" * (first - 1 - bool(header)) + header + "".join(rows[first - 1:end - 1]) + return compile(text, filename, "exec", ast.PyCF_ONLY_AST, dont_inherit=True).body + + body = [] + for first, end, cuts, starts in _pieces(_statements(source), len(rows), piece_lines): + found = statements(first, cuts[0] if cuts else end) + body += found + if cuts: + if len(found) != 1 or type(found[0]) is not ast.ClassDef: + raise _Whole + for at, stop in zip(cuts, cuts[1:] + [end]): + rest = statements(at, stop, DUMMY) + if len(rest) != 1 or type(rest[0]) is not ast.ClassDef: + raise _Whole + found[0].body += rest[0].body + found[0].end_lineno, found[0].end_col_offset = rest[0].end_lineno, rest[0].end_col_offset + found = found[0].body + if not _rows(found).issuperset(starts): # a row `tokenize` gave is not where a statement starts + raise _Whole + return ast.Module(body=body, type_ignores=[]) + except _GIVE_UP: + return None + + +def _rows(nodes): + """The rows where these statements start, decorators included. A statement after a `;` can start on a row of + its own (`b` when `a = (` is closed by `); b = 2` on the next row), so there can be more rows than logical + lines.""" + return {min([node.lineno] + [d.lineno for d in getattr(node, "decorator_list", ())]) for node in nodes} + + +def _statements(source): + """[row, word, rows] for every top-level statement: the row it starts on, the word its own line starts with + (the one after its decorators; None for a statement that must not be cut) and the rows where the statements + one level down start.""" + tops = [] + depth = 0 + fresh = True # the next token that is not blank starts a logical line + before = None # (depth, first token) of the logical line before + indent = None # what the statement one level down before this one is indented by + for kind, word, (row, column), _, line in tokenize.generate_tokens(io.StringIO(source).readline): + if kind == tokenize.INDENT: + depth += 1 + elif kind == tokenize.DEDENT: + depth -= 1 + elif kind == tokenize.NEWLINE: + fresh = True + elif fresh and kind not in _BLANK: + fresh = False + if depth < 2: + decorated = before == (depth, "@") + if not tops and (depth or decorated or word in _CONTINUES): + raise _Whole # indented, or a clause, before any statement + if depth: + if not decorated and word not in _CONTINUES: + tops[-1][2].append(row) + if indent not in (None, line[:column]): + # Each piece of a class starts a new indentation stack, so the tokenizer's tab rule + # between two statements of its body would go unchecked: such a body is not cut. + tops[-1][1] = None + indent = line[:column] + elif decorated: + tops[-1][1] = word + elif word in _CONTINUES: + tops[-1][1] = None # several clauses: not a class + else: + tops.append([row, word, []]) + indent = None + before = (depth, word) + return tops + + +def _pieces(tops, last_row, piece_lines): + """(first row, row after the last, cuts, starts) of every piece, in order. For a run of whole statements `cuts` + is empty and `starts` are the rows they start on; for a class too long for one piece `cuts` are the rows where + its later pieces start and `starts` the rows where the statements of its body start.""" + start, starts = 1, [] # the first piece takes the lines above the first statement + ends = [top[0] for top in tops[1:]] + [last_row + 1] + for (row, word, inner), end in zip(tops, ends): + cuts, at = [], row + if word == "class" and end - row > piece_lines: + for below in inner[1:]: # the first statement of the body stays with the header + if below - at >= piece_lines: + cuts.append(below) + at = below + if cuts: + if row > start: + yield start, row, [], starts + yield row, end, cuts, inner + start, starts = end, [] + else: + starts.append(row) + if end - start >= piece_lines: + yield start, end, [], starts + start, starts = end, [] + if start <= last_row: + yield start, last_row + 1, [], starts diff --git a/tests/semantic/support.py b/tests/semantic/support.py index 950890271e..0d5cae6672 100644 --- a/tests/semantic/support.py +++ b/tests/semantic/support.py @@ -1,19 +1,62 @@ """Shared helpers for the translator tests (standard library only).""" import gc +import importlib.machinery import os import struct import sys import tempfile import warnings +import piecewise_parse + HERE = os.path.dirname(os.path.abspath(__file__)) ROOT = os.path.dirname(os.path.dirname(HERE)) # The mutation tests point this at a sabotaged copy of tools/; normally it is the repository's own. TOOLS_DIR = os.environ.get("LEONIDA_TOOLS_DIR") or os.path.join(ROOT, "tools") FIXTURE_DIR = os.path.join(HERE, "fixtures", "disasm") + +class PiecewiseLoader(importlib.machinery.SourceFileLoader): + """Compiles a module from the tree piecewise_parse.py builds: the same code object as the usual loader's, + without the 6 s CPython 3.12 to 3.14.7 spends on rdna2spirv.py's f-strings when it parses the file as one + string. A valid .pyc is still used first, and a source the pieces cannot give goes to the usual compile.""" + + def source_to_code(self, data, path, *args, **kwargs): + try: + tree = piecewise_parse.parse_pieces(data, path) + if tree is not None: + return super().source_to_code(tree, path, *args, **kwargs) + except Exception: # nothing to report from here: the usual compile says what is wrong, in its words + pass + return super().source_to_code(data, path, *args, **kwargs) + + +class ToolsFinder: + """Hands the modules of TOOLS_DIR to PiecewiseLoader. It asks PathFinder, so sys.path decides which file a + name means exactly as it does without this finder; a name found anywhere else, or one that is not a plain + source file, is left to the finders after it.""" + + @staticmethod + def find_spec(name, path=None, target=None): + if path is not None or not os.path.isfile(os.path.join(TOOLS_DIR, name + ".py")): + return None + spec = importlib.machinery.PathFinder.find_spec(name, path, target) + loader = getattr(spec, "loader", None) + if type(loader) is not importlib.machinery.SourceFileLoader or not _same_folder(loader.path, TOOLS_DIR): + return None + spec.loader = PiecewiseLoader(loader.name, loader.path) + return spec + + +def _same_folder(path, folder): + return os.path.normcase(os.path.dirname(os.path.abspath(path))) == os.path.normcase(os.path.abspath(folder)) + + sys.dont_write_bytecode = True # never drop __pycache__ into the tree under test sys.path.insert(0, TOOLS_DIR) +# Only where the tokenizer has that cost; just before PathFinder, which would otherwise load these modules. +if piecewise_parse.needed() and importlib.machinery.PathFinder in sys.meta_path: + sys.meta_path.insert(sys.meta_path.index(importlib.machinery.PathFinder), ToolsFinder) import rdna2spirv # noqa: E402 (path set above) from spirv_mini_interp import run_fragment # noqa: E402 diff --git a/tests/test_piecewise_parse.py b/tests/test_piecewise_parse.py new file mode 100644 index 0000000000..768f19eb7c --- /dev/null +++ b/tests/test_piecewise_parse.py @@ -0,0 +1,935 @@ +"""tests/semantic/piecewise_parse.py builds the tree a whole-file parse builds, and support.py's loader the same code. + + python -I tests/test_piecewise_parse.py + +The translator tests load tools/rdna2spirv.py through that loader, so "the same" is the whole point: a test that ran a +slightly different translator would prove nothing. Checked here, with nothing but the standard library: + +* hand-written sources for each boundary rule (decorators, the clauses of `if`/`try`/`for`/`while` at top level and + in a class, a class header over several lines, nested classes, a docstring and `from __future__`, strings and + brackets whose inner lines start at column 0, non-ASCII text, CRLF), cut into pieces of 1, 2, 3, 5 and 8 lines: + `ast.dump(tree, include_attributes=True)` equals the whole-file parse's; +* sources with an error: the pieces are given up and `parse` raises what `compile` raises (type, message, line, + column, text); +* every Python file under tools/, scripts/ and tests/ and make_game.py in pieces of one statement, and the long + files of tools/ also in pieces of the size the loader uses: the two trees agree in every field and position + (`same_tree`, which is held against the dumps on the hand-written sources); +* the code object compiled from the pieces against the one `compile` makes from the file's bytes, for some files; +* tools/rdna2spirv.py, tree (by its dump) and code object, in one test of its own: on the interpreters the pieces + are for (CPython 3.12 to 3.14.7) each of its two whole-file parses takes 7 s, which is why it is `_slow`; +* support.py's finder and loader on throwaway folders, and that support.py installs them only where needed. + +About 45 s in all on those interpreters, most of it the repository scan and the translator. +""" +import ast +import importlib.machinery +import importlib.util +import os +import py_compile +import sys +import tempfile +import types +import unittest +import warnings +from unittest import mock + +ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +SEMANTIC = os.path.join(ROOT, "tests", "semantic") +TOOLS = os.path.join(ROOT, "tools") +TRANSLATOR = os.path.join(TOOLS, "rdna2spirv.py") +sys.path.insert(0, SEMANTIC) + +import piecewise_parse as pp # noqa: E402 +import support # noqa: E402 (imports the translator the way the translator tests do) + +SPLITS = sys.version_info >= (3, 12) # older interpreters are always parsed whole (piecewise_parse.parse_pieces) +SIZES = (1, 2, 3, 5, 8) +DUMP = dict(include_attributes=True, **({"show_empty": True} if sys.version_info >= (3, 13) else {})) + + +def whole(source, filename=""): + return compile(source, filename, "exec", ast.PyCF_ONLY_AST, dont_inherit=True) + + +def dump(tree): + return ast.dump(tree, **DUMP) + + +def difference(got, expected): + """Where two dumps part, with a little of each: the dumps themselves run to megabytes.""" + at = len(os.path.commonprefix([got, expected])) + near = slice(max(0, at - 90), at + 90) + return f"dumps differ at character {at}: ...{got[near]!r} against ...{expected[near]!r}" + + +def same_tree(a, b): + """Whether two trees agree in every field and every position: what equal dumps say, without building them. + The dumps of the repository's 24 MB of Python take 12 s, this takes 3; PiecesTest holds it against `dump`.""" + todo = [(a, b)] + while todo: + x, y = todo.pop() + if type(x) is not type(y): + return False + if isinstance(x, ast.AST): + todo.extend((getattr(x, name, None), getattr(y, name, None)) for name in x._fields + x._attributes) + elif type(x) is list: + if len(x) != len(y): + return False + todo.extend(zip(x, y)) + elif (repr(x) != repr(y)) if isinstance(x, (float, complex)) else (x != y): + return False + return True + + +def error_facts(call, *args): + """What the SyntaxError `call` raises says (a ValueError before 3.12 for a NUL), or None when it raises none.""" + try: + call(*args) + except SyntaxError as e: + return type(e).__name__, e.msg, e.filename, e.lineno, e.offset, e.end_lineno, e.end_offset, e.text + except ValueError as e: + return type(e).__name__, str(e) + return None + + +def code_facts(code): + """Everything a code object holds as plain values, the code objects among its constants included.""" + return (code.co_name, code.co_qualname, code.co_filename, code.co_firstlineno, code.co_flags, code.co_argcount, + code.co_posonlyargcount, code.co_kwonlyargcount, code.co_nlocals, code.co_stacksize, code.co_code, + code.co_names, code.co_varnames, code.co_freevars, code.co_cellvars, code.co_linetable, + code.co_exceptiontable, list(code.co_positions()), + [code_facts(c) if isinstance(c, types.CodeType) else (type(c).__name__, repr(c)) for c in code.co_consts]) + + +def python_files(): + """Every .py file under tools/, scripts/ and tests/, and make_game.py.""" + yield os.path.join(ROOT, "make_game.py") + for top in ("tools", "scripts", "tests"): + for base, dirs, names in os.walk(os.path.join(ROOT, top)): + dirs[:] = sorted(d for d in dirs if d != "__pycache__" and not d.startswith(".")) + for name in sorted(names): + if name.endswith(".py"): + yield os.path.join(base, name) + + +def read(path): + with open(path, "rb") as f: + return f.read() + + +# --- hand-written sources, one per boundary rule --------------------------------------------------------------- + +DECORATORS = r''' +import functools + + +def deco(f): + return f + + +@deco +def a(): + return 1 + + +@deco +@functools.wraps(a) +def b(): + return 2 + + +@deco +class C: + @staticmethod + def m(): + return 3 + + @property + @deco + def p(self): + return 4 + + @functools.lru_cache( + maxsize=None, + ) + def q(self): + return 5 + + @deco + class Inner: + pass + + tail = 6 +x = 1 +''' + +CLAUSES_AT_TOP_LEVEL = r''' +import os +if os.sep == "/": + a = 1 +elif os.sep == ":": + a = 2 +else: + a = 3 +try: + b = int("x") +except ValueError: + b = 0 +except (TypeError, KeyError) as e: + b = 1 +else: + b = 2 +finally: + c = 3 +try: + d = 4 +finally: + d = 5 +for i in range(3): + pass +else: + e = 6 +while False: + pass +else: + f = 7 +with open(os.devnull) as g: + pass +if a: b = 1 +elif b: b = 2 +else: b = 3 +try: h = 8 +except Exception: h = 9 +i = 0; j = 1; k = 2 +''' + +CLAUSES_IN_A_CLASS = r''' +class K: + if True: + a = 1 + elif False: + a = 2 + else: + a = 3 + try: + b = 1 + except Exception: + b = 2 + else: + b = 3 + finally: + c = 4 + for i in (): + pass + else: + d = 5 + while False: + pass + else: + e = 6 + + def m(self): + if self: + return 1 + else: + return 2 + try: + pass + finally: + pass + + async def n(self): + async with self as s: + async for t in s: + await t + f = 7; g = 8 + h = 9; +last = K +''' + +MATCH_AND_EXCEPT_STAR = r''' +def f(x): + match x: + case 1: + return "one" + case [a, b]: + return a + case _: + return None + + +match = 1 +case = match +match case: + case 1: + pass + + +class M: + match case: + case 1: + y = 1 + case _: + y = 2 + case = 3 + try: + z = 1 + except* ValueError: + z = 2 + except* TypeError: + z = 3 +after = M +''' + +CLASS_HEADER_OVER_SEVERAL_LINES = r''' +class Base: + pass + + +class A( + Base, + metaclass=type, +): + """Doc.""" + x = 1 + + def m(self): + return self.x + + y = 2 + + +class B(Base, + object): z = 3 + + +class C( + Base): + def n(self): return 4 + w = 5 +''' + +NESTED_CLASSES = r''' +class Outer: + a = 1 + + class Middle: + b = 2 + + class Inner: + c = 3 + + def m(self): + class Local: + d = 4 + return Local + + e = 5 + + f = 6 + + class Second: pass + + g = 7 + + +class After(Outer.Middle): + pass +''' + +DOCSTRING_AND_FUTURE = r'''"""Module docstring. + +class NotCode: + else: +""" +from __future__ import annotations +from __future__ import division, generators + +import sys + + +def f(a: Undefined) -> AlsoUndefined: + """Function docstring.""" + return a + + +class D: + """Class docstring.""" + + x: NotDefinedEither = 1 +''' + +COLUMN_ZERO = r''' +text = """ +class NotAClass: + def not_a_method(self): +else: +@not_a_decorator +""" +value = ( +1, +2, +[ +3, +], +) +call = dict( +a=1, +) +other = text + \ +"tail" +if value: first = { +"k": 1, +} +else: first = None + + +class Holder: + doc = """ +class StillNotAClass: + pass +finally: +""" + tup = ( +1, +2, +) + + def method(self): + return f""" +{self.doc} +{ +self.tup +!r:>{ +10 +}} +""" + \ +"x" +# a comment at column 0 inside the class body + # and one that fits no indentation level + + after = 3 + lst = [ +# a comment inside brackets +4, +] + + +last = Holder +''' + +F_STRINGS = r''' +name = "x" +plain = f"{name}" +spec = f"{name!r:>{10}} {name=} {{literal}} {3.14:.{2}f}" +joined = (f"{name}" + f"{name:{name}}" + "tail") + + +class F: + def m(self, a, b): + self.emit(f"{a} = OpIAdd %uint {b} {self.cu(1)}") + return f"""{a} + {b}""", rf"\d{a}", f'{a["k"]}' + + table = {f"k{i}": f"{i:02x}" for i in range(4)} +''' + +LEFTOVERS = r'''#!/usr/bin/env python3 +# a comment before anything + +x = 1 # trailing comment +lam = lambda: (yield) +star, *rest = 1, 2, 3 +print(x); print(x) +global_name: int +assert x, "message" +del star +if x: + pass +# comment after the last statement +''' + +NO_FINAL_NEWLINE = "x = 1\nclass A:\n y = 2\n z = 3" +ONLY_COMMENTS = "# nothing\n\n# here\n" +BACKSLASH_LINES = "a = 1\n\\\nb = 2\nclass A:\n c = 3\n\\\n d = 4\n e = 5\nf = 6\n" +FORM_FEED = "a = 1\n\x0c\nb = 2\nclass A:\n c = 3\n\x0c d = 4\ne = 5\n" +NON_ASCII = ('caf\xe9 = "\u017c\xf3\u0142w"; after = caf\xe9\n' + 'class \xdc:\n' + ' \u03c0 = 3; q = "\U0001f600" + f"{\u03c0}\u2713"; r = q\n' + ' def m(self): return self.\u03c0, "\xe9"\n' + ' s = "\xe9" + \\\n' + ' "\u03c0"; t = s\n' + 'last = "\xe9"; end = last\n') +LATIN_1 = ('# -*- coding: latin-1 -*-\n' + 'a = "\xe9\xe8"; b = a\n' + 'class L:\n' + ' c = "\xfc"; d = c\n' + ' def m(self): return "\xdf", self.c\n') + +SOURCES = { + "decorators": DECORATORS, + "clauses at top level": CLAUSES_AT_TOP_LEVEL, + "clauses in a class": CLAUSES_IN_A_CLASS, + "tabs": CLAUSES_IN_A_CLASS.replace(" ", "\t"), + "match and except*": MATCH_AND_EXCEPT_STAR, + "class header over several lines": CLASS_HEADER_OVER_SEVERAL_LINES, + "nested classes": NESTED_CLASSES, + "docstring and __future__": DOCSTRING_AND_FUTURE, + "strings and brackets at column 0": COLUMN_ZERO, + "f-strings": F_STRINGS, + "leftovers": LEFTOVERS, + "no final newline": NO_FINAL_NEWLINE, + "form feed": FORM_FEED, + "non-ASCII": NON_ASCII, +} +if sys.version_info >= (3, 12): + SOURCES["type parameters"] = r''' +type Pair[T] = tuple[T, T] + + +def first[T](pair: Pair[T]) -> T: + return pair[0] + + +class Box[T]: + type Inner = list[T] + + def get[U](self, other: U) -> T | U: + return other + + nested = f"{'a' + f'{1}'}" +last = Box +''' + +# A source with an error, and why it is one. None of them may come back as a tree. +ERRORS = { + "bracket never closed": "a = 1\nb = (\nc = 3\nd = 4\n", + "else without if": "a = 1\nelse:\n b = 2\n", + "else first": "else:\n pass\n", + "elif after a class": "class A:\n a = 1\n b = 2\nelif a:\n pass\n", + "indented first line": " a = 1\nb = 2\n", + "unexpected indent": "a = 1\n b = 2\nc = 3\n", + "unindent matches no level": "if a:\n b = 1\n c = 2\nd = 3\n", + "unindent matches no level in a class": "class A:\n a = 1\n b = 2\n", + "tabs against spaces": "if a:\n b = 1\n\tc = 2\n", + "tabs against spaces between class statements": "class A:\n a = 1\n\tb = 2\n", + "string never closed": 'a = 1\nb = """open\nc = 3\n', + "error in a class body": "class A:\n a = 1\n b = = 2\n c = 3\nd = 4\n", + "error in the last statement of a class": "class A:\n a = 1\n b = 2\n def m(self) return 3\n", + "except without try in a class": "class A:\n a = 1\n except E:\n pass\n", + "decorator with nothing after it": "a = 1\n@dec\n", + "decorator before an assignment": "@dec\na = 1\nb = 2\n", + "decorator before an assignment in a class": "class A:\n a = 1\n @dec\n b = 2\n c = 3\n", + "character that is no token": "a = 1\nb = $\nc = 3\n", + "backslash at the end": "a = 1\nb = 2 \\", + "backslash line at the end": "a = 1\n\\\n", + "NUL": "a = 1\0\nb = 2\n", + "try without except": "try:\n a = 1\nb = 2\n", + "class without a name": "a = 1\nclass:\n pass\n", + "missing colon": "a = 1\ndef f()\n return 1\n", + "f-string conversion": 'a = 1\nb = f"{a!z}"\nc = 3\n', + "f-string never closed": 'a = 1\nb = f"{a\nc = 3\n', + "assignment to a literal": "a = 1\n1 = a\nb = 2\n", + "statement after a one-line class": "class A: a = 1\n b = 2\n", +} + +# What compile refuses after parsing (an unknown feature already while parsing, from 3.14). Parsing in pieces and +# compiling the tree says the same. +COMPILE_ERRORS = { + "return outside a function": "def f():\n pass\nreturn 1\n", + "return in a class": "class A:\n a = 1\n return 2\n b = 3\n", + "__future__ after code": "a = 1\nfrom __future__ import annotations\nb = 2\n", + "break outside a loop": "a = 1\nbreak\n", + "nonlocal at module level": "a = 1\nnonlocal a\n", + "unknown __future__ feature": "from __future__ import no_such_feature\na = 1\n", + "from __future__ import braces": "a = 1\nb = 2\nfrom __future__ import braces\n", + "the same argument twice": "a = 1\ndef f(x, x):\n pass\n", + "await outside a function": "class A:\n a = 1\n b = [await c for c in a]\n d = 2\n", +} + + +class PiecesTest(unittest.TestCase): + def assert_same_tree(self, source, sizes=SIZES, must_split=SPLITS): + expected = dump(whole(source)) + for size in sizes: + tree = pp.parse_pieces(source, "", size) + if must_split: + self.assertIsNotNone(tree, f"pieces of {size} lines were given up") + if tree is not None: + got = dump(tree) + self.assertTrue(got == expected, f"pieces of {size} lines: " + difference(got, expected)) + self.assertTrue(same_tree(tree, whole(source)), f"same_tree, pieces of {size} lines") + got = dump(pp.parse(source, "", size)) + self.assertTrue(got == expected, f"parse, pieces of {size} lines: " + difference(got, expected)) + + def test_each_boundary_rule_gives_the_whole_file_tree(self): + for name, source in SOURCES.items(): + with self.subTest(name): + self.assert_same_tree(source) + + def test_same_tree_sees_what_the_dumps_see(self): + source = "class A:\n x = 1.0\n def m(self):\n return [self.x, 'a']\ny = A\n" + changes = { + "a column": lambda t: setattr(t.body[0].body[1].body[0].value.elts[0], "col_offset", 99), + "an end line": lambda t: setattr(t.body[0], "end_lineno", 99), + "a name": lambda t: setattr(t.body[1].value, "id", "B"), + "a constant": lambda t: setattr(t.body[0].body[0].value, "value", 2.0), + "the sign of a zero": lambda t: setattr(t.body[0].body[0].value, "value", -0.0), + "the type of a constant": lambda t: setattr(t.body[0].body[0].value, "value", 1), + "a statement fewer": lambda t: t.body[0].body.pop(), + "a statement more": lambda t: t.body.append(t.body[1]), + "another kind of node": lambda t: t.body.__setitem__(1, ast.Pass(lineno=5, col_offset=0, end_lineno=5, + end_col_offset=5)), + } + self.assertTrue(same_tree(whole(source), whole(source))) + for name, change in changes.items(): + with self.subTest(name): + tree = whole(source.replace("1.0", "0.0") if "zero" in name else source) + reference = whole(source.replace("1.0", "0.0") if "zero" in name else source) + change(tree) + self.assertNotEqual(dump(tree), dump(reference)) + self.assertFalse(same_tree(tree, reference)) + self.assertFalse(same_tree(reference, tree)) + + def test_the_same_from_bytes_with_lf_crlf_and_cr_line_ends(self): + for name, source in SOURCES.items(): + for ending in ("\n", "\r\n", "\r"): + with self.subTest(name, ending=ending): + text = source.replace("\n", ending) + self.assert_same_tree(text, sizes=(1, 3)) + self.assert_same_tree(text.encode("utf-8"), sizes=(1, 3)) + + def test_a_bom_and_a_coding_line_are_read_as_compile_reads_them(self): + self.assert_same_tree(b"\xef\xbb\xbf" + NON_ASCII.encode("utf-8")) + self.assert_same_tree(LATIN_1.encode("latin-1")) + self.assertEqual(whole(LATIN_1.encode("latin-1")).body[0].value.value,"\xe9\xe8") + # undecodable bytes and an unknown coding are compile's to report + for data in (b"a = 1\nb = '\xff'\n", b"# coding: no-such-codec\na = 1\n"): + with self.subTest(data): + self.assertIsNone(pp.parse_pieces(data, "", 1)) + self.assertIsNotNone(error_facts(whole, data)) + self.assertEqual(error_facts(pp.parse, data, "", 1), error_facts(whole, data)) + + def test_sources_with_nothing_to_cut(self): + for source in ("", "\n", ONLY_COMMENTS, "x = 1", "x = 1\n", "class A: pass\n", '"""Only a docstring."""\n'): + with self.subTest(source): + self.assert_same_tree(source) + + def test_a_backslash_line_before_a_statement_is_given_up_not_guessed(self): + # "\\\n" on a line of its own joins the next line, so the statement after it starts a row early: valid + # Python that no cut may separate. The piece before it ends in the backslash and does not parse. + whole(BACKSLASH_LINES) + self.assert_same_tree(BACKSLASH_LINES, must_split=False) + if SPLITS: + self.assertIsNone(pp.parse_pieces(BACKSLASH_LINES, "", 1)) + + def test_the_class_ends_where_its_last_token_ends(self): + # `x = 1;` ends at the 1 and its class at the semicolon: the end comes from the parser, not from the body + source = "class A:\n a = 1\n b = 2\n x = 1;\ny = A\n" + self.assertNotEqual(whole(source).body[0].end_col_offset, whole(source).body[0].body[-1].end_col_offset) + self.assert_same_tree(source) + + @unittest.skipUnless(SPLITS, "sources are parsed whole before Python 3.12") + def test_a_long_class_is_cut_inside_and_other_long_statements_are_not(self): + method = " def m{0}(self):\n return f'{{self}} {0}'\n\n" + source = "class Big:\n" + "".join(method.format(i) for i in range(40)) + "x = 1\n" + parsed = [] + real_compile = compile + with mock.patch.object(pp, "compile", lambda text, *a, **k: parsed.append(text) or real_compile(text, *a, **k), + create=True): + tree = pp.parse_pieces(source, "", 30) + self.assertEqual(dump(tree), dump(whole(source))) + self.assertEqual(len(parsed), 5, [p.lstrip("\n")[:40] for p in parsed]) + self.assertEqual([p.lstrip("\n").startswith(pp.DUMMY) for p in parsed], [False, True, True, True, False]) + # a function of the same length is one statement and stays one piece + function = "def big():\n" + "".join(f" a{i} = f'{{{i}}}'\n" for i in range(120)) + "x = 1\n" + del parsed[:] + with mock.patch.object(pp, "compile", lambda text, *a, **k: parsed.append(text) or real_compile(text, *a, **k), + create=True): + tree = pp.parse_pieces(function, "", 30) + self.assertEqual(dump(tree), dump(whole(function))) + self.assertEqual(len(parsed), 2) + # Each piece of a class starts a new indentation stack, so the tokenizer's tab rule between two statements + # of the body would go unchecked: a body not indented by the same characters throughout is never cut. + # (A form feed is the one valid way to get there; tabs against spaces are an error, see ErrorsTest.) + mixed = "class A:\n a = 1\n\x0c b = 2\n c = 3\nd = 4\n" + del parsed[:] + with mock.patch.object(pp, "compile", lambda text, *a, **k: parsed.append(text) or real_compile(text, *a, **k), + create=True): + tree = pp.parse_pieces(mixed, "", 1) + self.assertEqual(dump(tree), dump(whole(mixed))) + self.assertEqual([p.lstrip("\n").startswith(pp.DUMMY) for p in parsed], [False, False]) + + def test_a_short_source_is_one_piece(self): + with mock.patch.object(pp, "_statements", side_effect=AssertionError("tokenized a source of one piece")): + self.assert_same_tree(DECORATORS, sizes=(pp.PIECE_LINES,)) + + +class ErrorsTest(unittest.TestCase): + def test_a_source_with_an_error_raises_what_compile_raises(self): + for name, source in ERRORS.items(): + for variant in (source, source.encode("utf-8"), "ok = 0\n" * 12 + source, source + "ok = 0\n" * 12): + expected = error_facts(whole, variant, "some/file.py") + if variant is source: + self.assertIsNotNone(expected, f"compile accepts {name!r}") + if expected is None: + continue # the lines added after it completed the statement + with self.subTest(name, variant=variant): + for size in SIZES + (pp.PIECE_LINES,): + self.assertIsNone(pp.parse_pieces(variant, "some/file.py", size), f"pieces of {size} lines") + self.assertEqual(error_facts(pp.parse, variant, "some/file.py", size), expected) + + def test_what_the_compiler_rejects_is_rejected_in_the_same_words(self): + for name, source in COMPILE_ERRORS.items(): + with self.subTest(name): + expected = error_facts(compile, source, "f.py", "exec") + self.assertIsNotNone(expected, "compile accepts this source") + for size in (1, 2): + got = error_facts(lambda: compile(pp.parse(source, "f.py", size), "f.py", "exec")) + self.assertEqual(got, expected, f"pieces of {size} lines") + if error_facts(whole, source) is None: # the parser takes it: the pieces give its tree + self.assertEqual(dump(pp.parse(source, "f.py", 1)), dump(whole(source))) + + def test_barry_as_flufl_is_parsed_whole(self): + # the one __future__ import that changes the grammar for the rest of the file + accepted = "from __future__ import barry_as_FLUFL\na = 1\nb = a <> 2\n" + refused = "from __future__ import barry_as_FLUFL\na = 1\nb = a != 2\n" + self.assertIsNone(pp.parse_pieces(accepted, "", 1)) + self.assertEqual(dump(pp.parse(accepted, "", 1)), dump(whole(accepted))) + self.assertIsNotNone(error_facts(whole, refused)) + self.assertEqual(error_facts(pp.parse, refused, "", 1), error_facts(whole, refused)) + + +class WarningsTest(unittest.TestCase): + SOURCE = ('a = 1\n' + 'b = "\\d"\n' + 'class W:\n' + ' c = 2\n' + ' d = "\\w" + "\\s"\n' + ' e = 3\n' + 'f = 1if a else 2\n') + + @staticmethod + def recorded(call, *args): + with warnings.catch_warnings(record=True) as caught: + warnings.simplefilter("always") + call(*args) + return sorted((w.lineno, w.category.__name__, str(w.message), w.filename) for w in caught) + + def test_the_parser_warns_once_with_the_files_name_and_line(self): + expected = self.recorded(whole, self.SOURCE, "warned.py") + self.assertEqual([line for line, _, _, _ in expected], [2, 5, 5, 7], expected) + self.assertEqual({name for _, _, _, name in expected}, {"warned.py"}) + for size in SIZES: + self.assertEqual(self.recorded(pp.parse, self.SOURCE, "warned.py", size), expected, f"{size} lines") + + def test_the_compiler_warns_once_too(self): + # compiling a tree repeats nothing the parser said, and says what compiling the source says + for source in ("a = 1\nb = a is 1\nclass C:\n c = 2\n d = c is 2\n e = 3\n", + "a = 1\nassert (a, 'always true')\nb = 2\n", + "def f():\n try:\n pass\n finally:\n return 1\ng = 2\n", + self.SOURCE): + expected = self.recorded(compile, source, "warned.py", "exec") + for size in (1, 2): + got = self.recorded(lambda: compile(pp.parse(source, "warned.py", size), "warned.py", "exec")) + self.assertEqual(got, expected, f"pieces of {size} lines") + self.assertTrue(self.recorded(compile, "a = 1\nb = a is 1\n", "warned.py", "exec")) + + def test_a_warning_turned_into_an_error_is_the_error_compile_raises(self): + with warnings.catch_warnings(): + warnings.simplefilter("error") + expected = error_facts(whole, self.SOURCE, "warned.py") + self.assertIsNotNone(expected) + for size in SIZES: + self.assertEqual(error_facts(pp.parse, self.SOURCE, "warned.py", size), expected) + + +class NeededTest(unittest.TestCase): + def test_the_interpreters_that_copy_the_source_at_every_field(self): + for version, expected in (((3, 10, 12), False), ((3, 11, 9), False), ((3, 12, 0), True), ((3, 12, 13), True), + ((3, 13, 0), True), ((3, 13, 15), True), ((3, 14, 0), True), ((3, 14, 7), True), + ((3, 14, 8), False), ((3, 14, 10), False), ((3, 15, 0), False), ((3, 16, 1), False)): + self.assertIs(pp.needed(version + ("final", 0), "cpython"), expected, version) + self.assertIs(pp.needed((3, 12, 4, "final", 0), "pypy"), False) + self.assertIs(pp.needed(), pp.needed(sys.version_info, sys.implementation.name)) + + +class RepositoryTest(unittest.TestCase): + def test_every_python_file_gives_the_whole_file_tree(self): + checked = cut = 0 + for path in python_files(): + if os.path.samefile(path, TRANSLATOR): + continue # test_the_translator_tree_and_code_object_slow + data = read(path) + with self.subTest(os.path.relpath(path, ROOT)): + try: + expected = whole(data, path) + except (SyntaxError, ValueError): # a stray file that is not Python: refused, not guessed + self.assertIsNone(pp.parse_pieces(data, path, 1)) + continue + # one statement a piece for every file; the size the loader uses where the loader can meet it + long_tool = path.startswith(TOOLS + os.sep) and data.count(b"\n") >= pp.PIECE_LINES + for size in (1, pp.PIECE_LINES) if long_tool else (1,): + tree = pp.parse_pieces(data, path, size) + if tree is None: # allowed for the one documented reason + self.assertFalse(SPLITS and b"barry_as_FLUFL" not in data, f"pieces of {size} given up") + continue + if not same_tree(tree, expected): + self.fail(f"pieces of {size} lines: " + difference(dump(tree), dump(expected))) + cut += 1 + checked += 1 + self.assertGreater(checked, 1000, "the scan no longer finds the repository's Python files") + if SPLITS: + self.assertGreater(cut, checked) + + def test_the_code_object_equals_the_one_compile_makes_from_the_bytes(self): + for rel in ("make_game.py", "tools/shader_ir.py", "tools/spirv_asm.py", "tools/wave64_halves.py", + "scripts/run_gpu_tests.py", "scripts/run_semantic_tests.py", "tests/semantic/spirv_mini_interp.py"): + path = os.path.join(ROOT, *rel.split("/")) + data = read(path) + expected = compile(data, path, "exec", dont_inherit=True) + for size in (1, 40, pp.PIECE_LINES): + with self.subTest(rel, size=size): + code = compile(pp.parse(data, path, size), path, "exec", dont_inherit=True) + self.assertEqual(code, expected) + self.assertTrue(code_facts(code) == code_facts(expected), "positions or nested code differ") + + def test_the_translator_tree_and_code_object_slow(self): + data = read(TRANSLATOR) + expected = dump(whole(data, TRANSLATOR)) # seconds where the pieces are needed + tree = pp.parse_pieces(data, TRANSLATOR) + if SPLITS: + self.assertIsNotNone(tree, "the pieces were given up") + if tree is not None: + got = dump(tree) + self.assertTrue(got == expected, difference(got, expected)) + del got + del expected, tree + reference = compile(data, TRANSLATOR, "exec", dont_inherit=True) # and seconds again + code = support.PiecewiseLoader("rdna2spirv", TRANSLATOR).source_to_code(data, TRANSLATOR) + self.assertEqual(code, reference) + self.assertTrue(code_facts(code) == code_facts(reference), "positions or nested code differ") + + +MODULE = '''"""A module of a throwaway tools folder.""" +import os + +ANSWER = 41 + + +class Thing: + def name(self, x): + return f"{x}:{ANSWER + 1}" + + +def where(): + return os.path.abspath(__file__) +''' + "".join(f"\n\ndef f{i}(x):\n return f'{{x}}{i}'\n" for i in range(pp.PIECE_LINES // 4 + 5)) + + +class LoaderTest(unittest.TestCase): + """support.py's ToolsFinder and PiecewiseLoader, on a folder standing in for tools/.""" + + def setUp(self): + td = tempfile.TemporaryDirectory(prefix="leonida pieces ") + self.addCleanup(td.cleanup) + self.tools = os.path.join(td.name, "tools") + self.other = os.path.join(td.name, "other") + os.mkdir(self.tools) + os.mkdir(self.other) + for p in (mock.patch.object(support, "TOOLS_DIR", self.tools), + mock.patch.object(sys, "path", [self.tools] + sys.path)): + p.start() + self.addCleanup(p.stop) + self.addCleanup(importlib.invalidate_caches) + self.addCleanup(lambda: [sys.path_importer_cache.pop(d, None) for d in (self.tools, self.other)]) + + def write(self, folder, name, text): + path = os.path.join(folder, name) + os.makedirs(os.path.dirname(path), exist_ok=True) + with open(path, "wb") as f: + f.write(text.encode("utf-8")) + importlib.invalidate_caches() + return path + + def test_a_module_of_the_tools_folder_gets_the_loader_and_the_same_code(self): + self.assertGreater(MODULE.count("\n"), pp.PIECE_LINES) # long enough to be cut + path = self.write(self.tools, "leonida_pieces_probe.py", MODULE) + spec = support.ToolsFinder.find_spec("leonida_pieces_probe") + self.assertIs(type(spec.loader), support.PiecewiseLoader) + self.assertTrue(os.path.samefile(spec.origin, path)) + with mock.patch.object(pp, "parse_pieces", wraps=pp.parse_pieces) as used: + code = spec.loader.get_code("leonida_pieces_probe") + self.assertEqual(used.call_count, 1) + reference = compile(MODULE.encode("utf-8"), spec.origin, "exec", dont_inherit=True) + self.assertEqual(code, reference) + self.assertTrue(code_facts(code) == code_facts(reference)) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + self.assertEqual(module.Thing().name("a"), "a:42") + self.assertEqual(module.f3(7), "73") + self.assertTrue(os.path.samefile(module.where(), path)) + self.assertEqual(module.__doc__, "A module of a throwaway tools folder.") + + def test_names_it_leaves_to_the_usual_finders(self): + self.write(self.tools, "leonida_pieces_shadowed.py", "WHERE = 'tools'\n") + self.write(self.other, "leonida_pieces_shadowed.py", "WHERE = 'other'\n") + self.write(self.other, "leonida_pieces_elsewhere.py", "X = 1\n") + self.write(self.tools, os.path.join("leonida_pieces_package", "__init__.py"), "X = 1\n") + self.write(self.tools, os.path.join("leonida_pieces_package", "inner.py"), "X = 1\n") + with mock.patch.object(sys, "path", [self.other] + sys.path): + # found first in another folder of sys.path: that file is the module, as without the finder + self.assertIsNone(support.ToolsFinder.find_spec("leonida_pieces_shadowed")) + self.assertIsNone(support.ToolsFinder.find_spec("leonida_pieces_elsewhere")) + self.assertIsNone(support.ToolsFinder.find_spec("leonida_pieces_missing")) + self.assertIsNone(support.ToolsFinder.find_spec("leonida_pieces_package")) + self.assertIsNone(support.ToolsFinder.find_spec("leonida_pieces_package.inner", + [os.path.join(self.tools, "leonida_pieces_package")])) + self.assertIsNone(support.ToolsFinder.find_spec("os")) + self.assertIsNotNone(support.ToolsFinder.find_spec("leonida_pieces_shadowed")) + + def test_an_error_in_a_module_is_the_one_the_usual_loader_raises(self): + for name, source in (("syntax", MODULE + "\nbroken = (\n"), ("compiler", MODULE + "\nreturn 1\n")): + self.write(self.tools, f"leonida_pieces_bad_{name}.py", source) + spec = support.ToolsFinder.find_spec(f"leonida_pieces_bad_{name}") + self.assertIs(type(spec.loader), support.PiecewiseLoader) + usual = importlib.machinery.SourceFileLoader(f"leonida_pieces_bad_{name}", spec.origin) + expected = error_facts(usual.get_code, f"leonida_pieces_bad_{name}") + self.assertIsNotNone(expected) + self.assertEqual(error_facts(spec.loader.get_code, f"leonida_pieces_bad_{name}"), expected) + + def test_a_failure_in_the_pieces_falls_back_to_the_usual_compile(self): + path = self.write(self.tools, "leonida_pieces_fallback.py", MODULE) + reference = compile(MODULE.encode("utf-8"), path, "exec", dont_inherit=True) + loader = support.PiecewiseLoader("leonida_pieces_fallback", path) + for failure in (mock.patch.object(pp, "parse_pieces", return_value=None), + mock.patch.object(pp, "parse_pieces", side_effect=RuntimeError("anything")), + mock.patch.object(pp, "parse_pieces", return_value=ast.Module( # a tree compile refuses + body=[ast.Expr(value=ast.Constant(value=1))], type_ignores=[]))): + with failure: + self.assertEqual(loader.get_code("leonida_pieces_fallback"), reference) + + def test_a_valid_pyc_is_used_without_parsing(self): + path = self.write(self.tools, "leonida_pieces_cached.py", MODULE) + py_compile.compile(path, doraise=True) + spec = support.ToolsFinder.find_spec("leonida_pieces_cached") + with mock.patch.object(support.PiecewiseLoader, "source_to_code", side_effect=AssertionError("parsed")): + code = spec.loader.get_code("leonida_pieces_cached") + self.assertEqual(code, compile(MODULE.encode("utf-8"), spec.origin, "exec", dont_inherit=True)) + + def test_support_installs_the_finder_only_where_the_tokenizer_is_slow(self): + translator = sys.modules["rdna2spirv"] + self.assertTrue(os.path.samefile(translator.__file__, TRANSLATOR)) + if pp.needed(): + at = sys.meta_path.index(support.ToolsFinder) + self.assertIs(sys.meta_path[at + 1], importlib.machinery.PathFinder) + self.assertIs(type(translator.__spec__.loader), support.PiecewiseLoader) + else: + self.assertNotIn(support.ToolsFinder, sys.meta_path) + self.assertIs(type(translator.__spec__.loader), importlib.machinery.SourceFileLoader) + + +if __name__ == "__main__": + unittest.main() From e654d10fdf77072a388017b1872a8c517104b608 Mon Sep 17 00:00:00 2001 From: syfyra <258908070+syfyra@users.noreply.github.com> Date: Thu, 26 Nov 2026 10:52:13 +0100 Subject: [PATCH 2/4] piecewise_parse: CPython 3.13.16 has the tokenizer fix too (TEST-TRANSLATOR-PIECEWISE-PARSE) v3.13.16 and v3.13.17 no longer copy the source at an f-string field (Parser/lexer/lexer.c keeps last_expr_start); v3.12.16 still does. needed() is now true on 3.12, 3.13.0 to 3.13.15 and 3.14.0 to 3.14.7. The README paragraph moves below the one about calling the translator, so it no longer touches the lines #2114 adds. Co-Authored-By: Claude Fable 5.2 --- tests/semantic/README.md | 20 ++++++++++---------- tests/semantic/piecewise_parse.py | 9 +++++---- tests/test_piecewise_parse.py | 7 ++++--- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/tests/semantic/README.md b/tests/semantic/README.md index 22d5d92f6d..96fac69684 100644 --- a/tests/semantic/README.md +++ b/tests/semantic/README.md @@ -18,22 +18,22 @@ fail and one summary line; `-j N` sets how many run at once (`-j 1` is the disco every file's output. Files run side by side, so a test writes only into its own temporary folder (`tempfile`), never to a fixed path, and does not rely on another file having run first. -A process that finds no valid `.pyc` of `tools/rdna2spirv.py` compiles it from source: after every edit of the -translator (most files here set `sys.dont_write_bytecode`, so they do not leave a new one), for each mutant of -`test_mutations.py`, and always where `PYTHONDONTWRITEBYTECODE` is set. On CPython 3.12 to 3.14.7 parsing that file -as one string takes 7 s, because the tokenizer copies the rest of the source at every f-string field (fixed in -3.14.8 and 3.15.0, gh-155525; CI runs 3.12, which keeps it). On those interpreters `support.py` loads the modules -of `tools/` through `piecewise_parse.py`, which parses a source in pieces of 300 lines and joins them into the tree -a whole-file parse gives: under 1 s for the translator, and the same code object. `tests/test_piecewise_parse.py` -compares the trees for every Python file of the repository and the code objects for some. A file that imports -`rdna2spirv` before `support`, or without it, still pays the 7 s. - Every file here must call `tools/rdna2spirv.py` (directly, through `support.py`, or by running the tool) and assert on what it emitted. A test that reimplements the instruction in Python passes whatever the translator does, so it is not a regression test. `tests/test_semantic_translator_use.py` enforces this; the interpreter self-tests and the files whose subject is hand-written SPIR-V are listed there with a reason. The guard checks that a file reaches the translator, not that every assertion depends on it. +On CPython 3.12, 3.13.0 to 3.13.15 and 3.14.0 to 3.14.7 `support.py` loads the modules of `tools/` through +`piecewise_parse.py`. There the tokenizer copies the rest of a source string at every f-string field (fixed in +3.13.16, 3.14.8 and 3.15.0, gh-155525; CI runs 3.12, which keeps it), so a process that finds no valid `.pyc` of +`tools/rdna2spirv.py` spends 7 s parsing it: after every edit of the translator (most files here set +`sys.dont_write_bytecode`, so they do not leave a new one), for each mutant of `test_mutations.py`, and always where +`PYTHONDONTWRITEBYTECODE` is set. `piecewise_parse.py` parses a source in pieces of 300 lines and joins them into the +tree a whole-file parse gives: under 1 s for the translator, and the same code object. +`tests/test_piecewise_parse.py` compares the trees for every Python file of the repository and the code objects for +some. A file that imports `rdna2spirv` before `support`, or without it, still pays the 7 s. + What they check (`tools/rdna2spirv.py` on pixel, vertex, compute and other stages, wave32 and wave64, mostly one instruction family per file; the list below describes some of them, not every file): diff --git a/tests/semantic/piecewise_parse.py b/tests/semantic/piecewise_parse.py index b2d453cbb1..b72fea0e59 100644 --- a/tests/semantic/piecewise_parse.py +++ b/tests/semantic/piecewise_parse.py @@ -8,8 +8,8 @@ and `strncpy`. `import`, `compile` and `ast.parse` hand the module over as one string, so "the rest" is the rest of the file and the time grows with the square of its size: tools/rdna2spirv.py (1.2 MB, 4,759 f-strings with about 12,700 fields) takes 7 s to parse and 0.1 s to turn into code. A file run as a script is read line by line and is -not affected. CPython 3.14.8 and 3.15.0 fixed it (gh-155525); 3.12, which CI runs, only gets security fixes now and -keeps it, and so does every 3.13 up to 3.13.15. `needed()` says whether the running interpreter is one of those. +not affected. CPython 3.13.16, 3.14.8 and 3.15.0 fixed it (gh-155525); 3.12, which CI runs, only gets security fixes +now and keeps it (3.12.16 has it). `needed()` says whether the running interpreter is one that still copies. What a field costs depends on how much source follows it, so the same statements are cheap in a short string: @@ -62,10 +62,11 @@ class _Whole(Exception): def needed(version=None, implementation=None): """True on the interpreters whose tokenizer copies the rest of the source at every f-string field: CPython - 3.12.0 up to 3.14.7 (see the module docstring). Elsewhere a whole-file parse is as fast and simpler.""" + 3.12, 3.13.0 to 3.13.15 and 3.14.0 to 3.14.7 (see the module docstring). Elsewhere a whole-file parse is as + fast and simpler.""" version = tuple((sys.version_info if version is None else version)[:3]) implementation = sys.implementation.name if implementation is None else implementation - return implementation == "cpython" and (3, 12) <= version < (3, 14, 8) + return implementation == "cpython" and ((3, 12) <= version < (3, 13, 16) or (3, 14) <= version < (3, 14, 8)) def parse(source, filename="", piece_lines=PIECE_LINES): diff --git a/tests/test_piecewise_parse.py b/tests/test_piecewise_parse.py index 768f19eb7c..3046d19be6 100644 --- a/tests/test_piecewise_parse.py +++ b/tests/test_piecewise_parse.py @@ -748,9 +748,10 @@ def test_a_warning_turned_into_an_error_is_the_error_compile_raises(self): class NeededTest(unittest.TestCase): def test_the_interpreters_that_copy_the_source_at_every_field(self): - for version, expected in (((3, 10, 12), False), ((3, 11, 9), False), ((3, 12, 0), True), ((3, 12, 13), True), - ((3, 13, 0), True), ((3, 13, 15), True), ((3, 14, 0), True), ((3, 14, 7), True), - ((3, 14, 8), False), ((3, 14, 10), False), ((3, 15, 0), False), ((3, 16, 1), False)): + for version, expected in (((3, 10, 12), False), ((3, 11, 9), False), ((3, 12, 0), True), ((3, 12, 16), True), + ((3, 12, 99), True), ((3, 13, 0), True), ((3, 13, 15), True), ((3, 13, 16), False), + ((3, 13, 17), False), ((3, 14, 0), True), ((3, 14, 7), True), ((3, 14, 8), False), + ((3, 14, 10), False), ((3, 15, 0), False), ((3, 16, 1), False)): self.assertIs(pp.needed(version + ("final", 0), "cpython"), expected, version) self.assertIs(pp.needed((3, 12, 4, "final", 0), "pypy"), False) self.assertIs(pp.needed(), pp.needed(sys.version_info, sys.implementation.name)) From 53604abbfcdab3a9203ddce7135ef88475788173 Mon Sep 17 00:00:00 2001 From: syfyra <258908070+syfyra@users.noreply.github.com> Date: Thu, 26 Nov 2026 12:27:30 +0100 Subject: [PATCH 3/4] tests: the five tests that parse the translator source do it in pieces, 60 s -> 17 s (TEST-TRANSLATOR-PIECEWISE-PARSE) A .pyc does not help ast.parse. test_runtime_paths.py, test_isa_coverage.py, test_shader_stage_resume.py, test_cmake_translator_deps.py and test_resource_hygiene.py read the syntax tree of tools/rdna2spirv.py, seven whole-file parses of 7 s each on CPython 3.12, 3.13.0 to 3.13.15 and 3.14.0 to 3.14.7. They now call tests/semantic/piecewise_parse.parse, which gives the same tree (tests/test_piecewise_parse.py) in about 1 s. tools/isa_coverage.py parses inside scan_translator and accepted_names; its test patches ast.parse around those two calls on the real translator instead of changing the tool. Python 3.14.7, valid .pyc of the translator, two runs each: test_runtime_paths 16.0 -> 3.7 s, test_isa_coverage 15.0 -> 2.5 s, test_shader_stage_resume 14.2 -> 8.2 s, test_cmake_translator_deps 7.6 -> 1.4 s, test_resource_hygiene 7.6 -> 1.3 s. Co-Authored-By: Claude Fable 5.2 --- tests/semantic/README.md | 5 ++++- tests/test_cmake_translator_deps.py | 4 +++- tests/test_isa_coverage.py | 19 +++++++++++++++---- tests/test_resource_hygiene.py | 4 +++- tests/test_runtime_paths.py | 5 ++++- tests/test_shader_stage_resume.py | 6 ++++-- 6 files changed, 33 insertions(+), 10 deletions(-) diff --git a/tests/semantic/README.md b/tests/semantic/README.md index 96fac69684..858ef7024d 100644 --- a/tests/semantic/README.md +++ b/tests/semantic/README.md @@ -32,7 +32,10 @@ On CPython 3.12, 3.13.0 to 3.13.15 and 3.14.0 to 3.14.7 `support.py` loads the m `PYTHONDONTWRITEBYTECODE` is set. `piecewise_parse.py` parses a source in pieces of 300 lines and joins them into the tree a whole-file parse gives: under 1 s for the translator, and the same code object. `tests/test_piecewise_parse.py` compares the trees for every Python file of the repository and the code objects for -some. A file that imports `rdna2spirv` before `support`, or without it, still pays the 7 s. +some. A file that imports `rdna2spirv` before `support`, or without it, still pays the 7 s. The tests under `tests/` +that read the translator's syntax tree (`test_runtime_paths.py`, `test_isa_coverage.py`, +`test_shader_stage_resume.py`, `test_cmake_translator_deps.py`, `test_resource_hygiene.py`) call +`piecewise_parse.parse` where they would call `ast.parse`, for the same reason: no `.pyc` helps a parse. What they check (`tools/rdna2spirv.py` on pixel, vertex, compute and other stages, wave32 and wave64, mostly one instruction family per file; the list below describes some of them, not every file): diff --git a/tests/test_cmake_translator_deps.py b/tests/test_cmake_translator_deps.py index e3af66bcc2..132d4060a6 100644 --- a/tests/test_cmake_translator_deps.py +++ b/tests/test_cmake_translator_deps.py @@ -18,7 +18,9 @@ ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT)) +sys.path.append(str(ROOT / "tests" / "semantic")) import make_game # noqa: E402 +import piecewise_parse # noqa: E402 (ast.parse takes 7 s for tools/rdna2spirv.py on CPython 3.12) TOOLS = ROOT / "tools" SHADERS = ROOT / "runtime" / "test" / "shaders" @@ -46,7 +48,7 @@ def reached(): if path in seen: continue seen.add(path) - for node in ast.walk(ast.parse(path.read_text(encoding="utf-8"))): # function-level imports too + for node in ast.walk(piecewise_parse.parse(path.read_text(encoding="utf-8"))): # function-level imports too if isinstance(node, ast.Import): mods = [a.name for a in node.names] elif isinstance(node, ast.ImportFrom) and node.module and not node.level: diff --git a/tests/test_isa_coverage.py b/tests/test_isa_coverage.py index ad653dca40..ebf2fff77f 100644 --- a/tests/test_isa_coverage.py +++ b/tests/test_isa_coverage.py @@ -23,13 +23,21 @@ TOOLS_DIR = os.path.join(ROOT, "tools") if TOOLS_DIR not in sys.path: sys.path.insert(0, TOOLS_DIR) +sys.path.append(os.path.join(ROOT, "tests", "semantic")) import isa_coverage as ic # noqa: E402 +import piecewise_parse # noqa: E402 TRANSLATOR = os.path.join(TOOLS_DIR, "rdna2spirv.py") REAL_XML = os.environ.get(ic.ENV_XML) +def in_pieces(): + """While this is entered, the tool's `ast.parse` of the real translator is tests/semantic/piecewise_parse.py's: + the same tree in 1 s, where parsing the 1.2 MB file whole takes 7 s on CPython 3.12 (twice per scan pair).""" + return patch.object(ic.ast, "parse", lambda source, filename="": piecewise_parse.parse(source, filename)) + + def _ins(name, *encodings): encs = "".join(f"{e}" for e in encodings) return (f"{name}" @@ -279,7 +287,8 @@ class TranslatorScanTests(unittest.TestCase): @classmethod def setUpClass(cls): - cls.scan = ic.scan_translator(TRANSLATOR) + with in_pieces(): + cls.scan = ic.scan_translator(TRANSLATOR) def test_finds_well_known_rules(self): for name in ("v_mov_b32", "s_mov_b32", "v_cndmask_b32", "v_add_f32", "s_endpgm", "exp"): @@ -292,7 +301,8 @@ def test_dispatch_size_sanity(self): self.assertGreater(len(self.scan["exact"]), 300) def test_accepted_names_matches_non_refused_exact(self): - accepted = ic.accepted_names(TRANSLATOR) + with in_pieces(): + accepted = ic.accepted_names(TRANSLATOR) self.assertEqual(set(self.scan["exact"]) - accepted, set(self.scan["refused"])) @@ -319,8 +329,9 @@ class RealIsaTests(unittest.TestCase): @classmethod def setUpClass(cls): - cls.rep = ic.build_report(ic.read_isa(REAL_XML), ic.scan_translator(TRANSLATOR), - ic.accepted_names(TRANSLATOR)) + with in_pieces(): + cls.rep = ic.build_report(ic.read_isa(REAL_XML), ic.scan_translator(TRANSLATOR), + ic.accepted_names(TRANSLATOR)) def test_translator_names_exist_in_rdna2(self): stray = sorted(e["name"] for e in self.rep["not_in_rdna2"] if e["name"] not in self.KNOWN_NOT_IN_XML) diff --git a/tests/test_resource_hygiene.py b/tests/test_resource_hygiene.py index d1dacf3fb2..8ccd49cdc9 100644 --- a/tests/test_resource_hygiene.py +++ b/tests/test_resource_hygiene.py @@ -7,12 +7,14 @@ import unittest ROOT = pathlib.Path(__file__).resolve().parent.parent +sys.path.append(str(ROOT / "tests" / "semantic")) +import piecewise_parse # noqa: E402 (ast.parse takes 7 s for tools/rdna2spirv.py on CPython 3.12) def _bare_open_calls(path: pathlib.Path) -> list[tuple[int, str]]: """Return (lineno, source_line) for any open(...) call not managed by a with statement.""" src = path.read_text(encoding="utf-8") - tree = ast.parse(src, filename=str(path)) + tree = piecewise_parse.parse(src, str(path)) managed: set[int] = set() for node in ast.walk(tree): if isinstance(node, (ast.With, ast.AsyncWith)): diff --git a/tests/test_runtime_paths.py b/tests/test_runtime_paths.py index 800cdb3001..6afc9228bb 100644 --- a/tests/test_runtime_paths.py +++ b/tests/test_runtime_paths.py @@ -32,10 +32,13 @@ import os import re import subprocess +import sys import tempfile import unittest ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +sys.path.append(os.path.join(ROOT, "tests", "semantic")) +import piecewise_parse # noqa: E402 (ast.parse takes 7 s for tools/rdna2spirv.py on CPython 3.12) WORKFLOW = ".github/workflows/runtime.yml" # Entries that are needed for reasons a source scan cannot see: the build's own trees, the workflow, and the @@ -93,7 +96,7 @@ def python_refs(source): """(modules, strings) of a Python file: what it imports, including importlib.import_module("x") and __import__("x"), and its string constants other than docstrings. Comments never reach the AST.""" try: - tree = ast.parse(source) + tree = piecewise_parse.parse(source) except SyntaxError: return set(), set() docstrings = set() diff --git a/tests/test_shader_stage_resume.py b/tests/test_shader_stage_resume.py index e3645dbc5b..7a0e34d027 100644 --- a/tests/test_shader_stage_resume.py +++ b/tests/test_shader_stage_resume.py @@ -16,7 +16,9 @@ ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, ROOT) sys.path.insert(0, os.path.join(ROOT, "tools")) +sys.path.append(os.path.join(ROOT, "tests", "semantic")) import make_game # noqa: E402 +import piecewise_parse # noqa: E402 (ast.parse takes 7 s for tools/rdna2spirv.py on CPython 3.12) import shader_carve # noqa: E402 UNPACK = os.path.join(ROOT, "tools", "fssm_unpack.py") @@ -540,7 +542,7 @@ def test_the_list_covers_the_translator_and_native_catalog_imports(self): continue seen.add(name) with open(os.path.join(make_game.TOOLS, name + ".py"), encoding="utf-8") as f: - tree = ast.parse(f.read()) + tree = piecewise_parse.parse(f.read()) for node in ast.walk(tree): # function-level imports too if isinstance(node, ast.Import): mods = [a.name for a in node.names] @@ -565,7 +567,7 @@ def test_each_compute_stage_declares_every_module_its_tools_import(self): def local_imports(name: str): with open(os.path.join(make_game.TOOLS, name + ".py"), encoding="utf-8") as f: - tree = ast.parse(f.read()) + tree = piecewise_parse.parse(f.read()) out = [] for node in ast.walk(tree): # function-level imports too if isinstance(node, ast.Import): From 1f92ae6a5d515db5b0a7b0641e3470833785d0ba Mon Sep 17 00:00:00 2001 From: syfyra <258908070+syfyra@users.noreply.github.com> Date: Thu, 26 Nov 2026 12:32:56 +0100 Subject: [PATCH 4/4] piecewise_parse: the barry test holds on CPython 3.12.1; parse() takes pieces only where needed (TEST-TRANSLATOR-PIECEWISE-PARSE) Two findings from the review of #2237: test_barry_as_flufl_is_parsed_whole errored on CPython 3.12.1 (lixonik): there the parser does not read `from __future__ import barry_as_FLUFL` from the source, so the stock compile it took as its reference rejects `<>`. _PyPegen_checked_future_import is missing in v3.12.0, v3.12.1 and v3.13.0 and present in v3.12.16, v3.13.13 and v3.14.7. The test now takes this interpreter's compile outcome, tree or error, for both spellings, and still checks that the pieces give up. On a fixed interpreter the pieces are the slower way (GrahamboJangles: 0.21 s whole, 0.54 s in pieces for the translator on 3.14.9). support.py already installs its loader only where needed(); parse() now does the same for its callers, the five standalone tests, unless piece_lines is named. Co-Authored-By: Claude Fable 5.2 --- tests/semantic/piecewise_parse.py | 9 +++++--- tests/test_piecewise_parse.py | 36 +++++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/tests/semantic/piecewise_parse.py b/tests/semantic/piecewise_parse.py index b72fea0e59..36fc1c8580 100644 --- a/tests/semantic/piecewise_parse.py +++ b/tests/semantic/piecewise_parse.py @@ -69,10 +69,13 @@ def needed(version=None, implementation=None): return implementation == "cpython" and ((3, 12) <= version < (3, 13, 16) or (3, 14) <= version < (3, 14, 8)) -def parse(source, filename="", piece_lines=PIECE_LINES): +def parse(source, filename="", piece_lines=None): """The module tree of `source` (str, or the bytes of a file): what `ast.parse(source, filename)` returns, and - the same exception for a source with an error.""" - tree = parse_pieces(source, filename, piece_lines) + the same exception for a source with an error. It is built from pieces where `needed()` says a whole-file parse + is the slow way, or where the caller names `piece_lines`; elsewhere the whole-file parse is the faster one.""" + tree = None + if piece_lines is not None or needed(): + tree = parse_pieces(source, filename, piece_lines or PIECE_LINES) if tree is None: tree = compile(source, filename, "exec", ast.PyCF_ONLY_AST, dont_inherit=True) return tree diff --git a/tests/test_piecewise_parse.py b/tests/test_piecewise_parse.py index 3046d19be6..6df27b7f53 100644 --- a/tests/test_piecewise_parse.py +++ b/tests/test_piecewise_parse.py @@ -92,6 +92,14 @@ def error_facts(call, *args): return None +def outcome(parse, *args): + """What `parse` makes of a source: the dump of its tree, or what the error it raises says.""" + try: + return dump(parse(*args)) + except (SyntaxError, ValueError): + return error_facts(parse, *args) + + def code_facts(code): """Everything a code object holds as plain values, the code objects among its constants included.""" return (code.co_name, code.co_qualname, code.co_filename, code.co_firstlineno, code.co_flags, code.co_argcount, @@ -693,13 +701,16 @@ def test_what_the_compiler_rejects_is_rejected_in_the_same_words(self): self.assertEqual(dump(pp.parse(source, "f.py", 1)), dump(whole(source))) def test_barry_as_flufl_is_parsed_whole(self): - # the one __future__ import that changes the grammar for the rest of the file - accepted = "from __future__ import barry_as_FLUFL\na = 1\nb = a <> 2\n" - refused = "from __future__ import barry_as_FLUFL\na = 1\nb = a != 2\n" - self.assertIsNone(pp.parse_pieces(accepted, "", 1)) - self.assertEqual(dump(pp.parse(accepted, "", 1)), dump(whole(accepted))) - self.assertIsNotNone(error_facts(whole, refused)) - self.assertEqual(error_facts(pp.parse, refused, "", 1), error_facts(whole, refused)) + # The one __future__ import that changes the grammar for the rest of the file. Which of `<>` and `!=` the + # parser then takes depends on the release: 3.12.16, 3.13.13 and 3.14.7 read the import from the source + # (_PyPegen_checked_future_import), 3.12.1 and 3.13.0 look at the compile flags only. So nothing is said + # here about which one is right: the pieces give up, and `parse` gives what this interpreter's compile gives. + sources = [f"from __future__ import barry_as_FLUFL\na = 1\nb = a {operator} 2\n" for operator in ("<>", "!=")] + for source in sources: + with self.subTest(source=source): + self.assertIsNone(pp.parse_pieces(source, "", 1)) + self.assertEqual(outcome(pp.parse, source, "", 1), outcome(whole, source)) + self.assertEqual(sorted(error_facts(whole, source) is None for source in sources), [False, True]) class WarningsTest(unittest.TestCase): @@ -756,6 +767,17 @@ def test_the_interpreters_that_copy_the_source_at_every_field(self): self.assertIs(pp.needed((3, 12, 4, "final", 0), "pypy"), False) self.assertIs(pp.needed(), pp.needed(sys.version_info, sys.implementation.name)) + def test_parse_takes_pieces_where_they_are_needed_or_asked_for(self): + source = "".join(f"a{i} = f'{{{i}}}'\n" for i in range(pp.PIECE_LINES + 40)) + expected = dump(whole(source)) + for needed, sizes, pieces in ((True, (), 1), (False, (), 0), (False, (7,), 1), (True, (7,), 1)): + with self.subTest(needed=needed, sizes=sizes): + with mock.patch.object(pp, "needed", return_value=needed), \ + mock.patch.object(pp, "parse_pieces", wraps=pp.parse_pieces) as parse_pieces: + self.assertEqual(dump(pp.parse(source, "", *sizes)), expected) + size = sizes[0] if sizes else pp.PIECE_LINES + self.assertEqual(parse_pieces.call_args_list, [mock.call(source, "", size)] * pieces) + class RepositoryTest(unittest.TestCase): def test_every_python_file_gives_the_whole_file_tree(self):