#!/usr/bin/env python3 """Check only mro_implementation's n declaration and evaluation timing.""" 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: Py_ssize_t n; must be in the opening declaration block" ) assert len(re.findall(r"\bPy_ssize_t\s+n\b", body)) == 1, ( "mro_implementation: declare n exactly once" ) expected_prefix = """ PyObject *bases; Py_ssize_t n; Py_ssize_t i; PyObject **to_merge; PyObject *result; if (!_PyType_IsReady(type)) { if (PyType_Ready(type) < 0) return NULL; } bases = type->tp_bases; n = PyTuple_GET_SIZE(bases); for (i = 0; i < n; i++) { """ assert body.split()[:len(expected_prefix.split())] == expected_prefix.split(), ( "mro_implementation: preserve n evaluation timing and loop index initialization" ) assert len(re.findall(r"\bn\s*=(?!=)", body)) == 1, ( "mro_implementation: assign n exactly once" ) print("mro_implementation n C89 declaration and unchanged evaluation order: PASS")