"""Check type_set_bases declares old_bases in its opening declaration block.""" from pathlib import Path import re import sys source = Path(sys.argv[1] if len(sys.argv) > 1 else "cpython/Objects/typeobject.c").read_text() match = re.search( r"\nstatic int\ntype_set_bases\(PyTypeObject \*type, PyObject \*new_bases, void \*context\)\n\{(.*?)\n\}", source, re.S, ) assert match, "type_set_bases definition missing" body = match.group(1) assert re.search( r"\A\n\s*Py_ssize_t n;\n\s*Py_ssize_t i;\n\s*PyTypeObject \*new_base;\n\s*PyObject \*old_bases;", body, ), "type_set_bases: old_bases must be declared with the opening locals" assert body.count("PyObject *old_bases;") == 1, "old_bases declaration must occur once" assert re.search(r"\n\s*old_bases = type->tp_bases;\n\s*assert\(old_bases != NULL\);", body), ( "type_set_bases: preserve old_bases assignment and assertion order" )