#!/usr/bin/env python3 """Check only find_name_in_mro's n declaration and initialization timing.""" import re from pathlib import Path source = (Path(__file__).resolve().parents[1] / "cpython/Objects/typeobject.c").read_text() match = re.search( r"\nfind_name_in_mro\(PyTypeObject \*type, PyObject \*name, int \*error\)\n" r"\{(.*?)\n\}", source, re.S, ) assert match, "find_name_in_mro definition missing" body = re.sub(r"/\*.*?\*/|//[^\n]*", "", match.group(1), flags=re.S) assert re.match( r"\s*Py_hash_t hash;\s*PyObject \*mro;\s*PyObject \*res;" r"\s*Py_ssize_t n;\s*Py_ssize_t i;\s*if\b", body, ), "find_name_in_mro: Py_ssize_t n; must be in the opening declaration block" assert len(re.findall(r"\bPy_ssize_t\s+n\b", body)) == 1, ( "find_name_in_mro: declare n exactly once" ) assert len(re.findall(r"\bn\s*=", body)) == 1, ( "find_name_in_mro: initialize n exactly once" ) assert re.search( r"\bres = NULL;\s*Py_INCREF\(mro\);\s*n = PyTuple_GET_SIZE\(mro\);" r"\s*for \(i = 0; i < n; i\+\+\) \{", body, ), "find_name_in_mro: preserve n assignment after INCREF and the separate i loop" print("find_name_in_mro n C89 declaration and same-point assignment: PASS")