#!/usr/bin/env python3 """Check only mro_hierarchy's new_mro declaration and guarded read timing.""" import re from pathlib import Path source = Path("cpython/Objects/typeobject.c").read_text() match = re.search( r"\nstatic int\nmro_hierarchy\(PyTypeObject \*type, PyObject \*temp\)\n\{(.*?)\n\}", source, re.S, ) assert match, "mro_hierarchy definition missing" body = re.sub(r"/\*.*?\*/", "", match.group(1), flags=re.S) assert re.match(r"\s*PyObject \*old_mro;\s*PyObject \*new_mro;", body), ( "mro_hierarchy: PyObject *new_mro; must be in the opening declarations" ) assert len(re.findall(r"\bPyObject\s*\*\s*new_mro\b", body)) == 1, ( "mro_hierarchy: declare new_mro exactly once without initialization" ) expected_prefix = """ PyObject *old_mro; PyObject *new_mro; PyObject *tuple; Py_ssize_t n; Py_ssize_t i; int res = mro_internal(type, &old_mro); if (res <= 0) { return res; } new_mro = type->tp_mro; if (old_mro != NULL) { tuple = PyTuple_Pack(3, type, new_mro, old_mro); } else { tuple = PyTuple_Pack(2, type, new_mro); } """ assert body.split()[:len(expected_prefix.split())] == expected_prefix.split(), ( "mro_hierarchy: preserve the guard, new_mro read timing, and tuple branches" ) assert len(re.findall(r"\bnew_mro\s*=", body)) == 1, ( "mro_hierarchy: assign new_mro exactly once" ) print("mro_hierarchy new_mro C89 declaration and guarded read timing: PASS")