#!/usr/bin/env python3 """Check only mro_implementation's to_merge population loop index.""" 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 = re.sub(r"/\*.*?\*/", "", match.group(1), flags=re.S) assert re.match( r"\s*PyObject \*bases;\s*Py_ssize_t n;\s*Py_ssize_t i;" r"\s*PyObject \*\*to_merge;\s*PyObject \*result;\s*if\b", body ), "mro_implementation: reuse i from the opening declaration block" population = body.split("to_merge = PyMem_New(PyObject *, n + 1);", 1)[1].split( "result = PyList_New(1);", 1 )[0] assert not re.search(r"\bfor\s*\(\s*Py_ssize_t\s+i\b", population), ( "mro_implementation: to_merge population loop must reuse function-scope i for C89" ) expected = """ if (to_merge == NULL) { PyErr_NoMemory(); return NULL; } for (i = 0; i < n; i++) { PyTypeObject *base = _PyType_CAST(PyTuple_GET_ITEM(bases, i)); to_merge[i] = base->tp_mro; } to_merge[n] = bases; """ assert population.split() == expected.split(), ( "mro_implementation: preserve allocation error handling, loop initialization, " "bounds, increment, base MRO writes, and final bases entry" ) print("mro_implementation to_merge loop index C89 declaration and unchanged semantics: PASS")