"""Check type_set_bases declares temp before statements without moving its initialization.""" 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;\n\s*PyTypeObject \*old_base;\n\s*PyObject \*temp;", body, ), "type_set_bases: temp must be declared with the opening locals" assert body.count("PyObject *temp;") == 1, "temp declaration must occur once" assert body.count("temp = PyList_New(0);") == 1, "temp initialization must occur once" assert re.search( r"old_base = type->tp_base;\s*type->tp_bases = Py_NewRef\(new_bases\);\s*" r"type->tp_base = \(PyTypeObject \*\)Py_NewRef\(new_base\);\s*" r"temp = PyList_New\(0\);", body, ), "type_set_bases: preserve temp initialization after base replacement"