#!/usr/bin/env python3 """Regress the probe's C2143 at the MRO population loop initializer.""" import re from pathlib import Path source = Path("cpython/Objects/typeobject.c").read_text() match = re.search( r"\nstatic PyObject \*\nmro_implementation\(PyTypeObject \*type\)\n\{(.*?)\n\}", source, re.S, ) assert match, "mro_implementation definition missing" body = match.group(1) declarations = body.split("if (!_PyType_IsReady(type))", 1)[0] assert re.search(r"\bPy_ssize_t i;", declarations), "function-scope i missing" population = body.split("to_merge = PyMem_New(PyObject *, n + 1);", 1)[1] loop = re.search(r"\bfor\s*\((.*?)\)\s*\{(.*?)\n \}", population, re.S) assert loop, "to_merge population loop missing" assert loop.group(1).split() == "i = 0; i < n; i++".split(), ( "C2143 regression: to_merge population loop must reuse existing i: " "for (i = 0; i < n; i++)" ) assert loop.group(2).split() == """ PyTypeObject *base = _PyType_CAST(PyTuple_GET_ITEM(bases, i)); to_merge[i] = base->tp_mro; """.split(), "preserve population loop behavior" print("mro_implementation population index C2143 regression: PASS")