#!/usr/bin/env python3 """Guard _PyTypes_InitState's C89 declaration and initialization order.""" import re import sys from pathlib import Path source = Path(sys.argv[1]).read_text() found = re.search( r"\n_PyTypes_InitState\(PyInterpreterState \*interp\)\s*\{(?P.*?)\n\}", source, re.S, ) assert found, "_PyTypes_InitState definition not found" body = found.group("body") declaration = re.match(r"\s*PyStatus status;", body) assert declaration and len(re.findall(r"\bPyStatus\s+status\b", body)) == 1, ( "_PyTypes_InitState must declare status exactly once without initializer at entry" ) expected_statements = ''' if (!_Py_IsMainInterpreter(interp)) { return _PyStatus_OK(); } status = _PyTypes_InitSlotDefs(); if (_PyStatus_EXCEPTION(status)) { return status; } return _PyStatus_OK(); ''' assert body[declaration.end():].split() == expected_statements.split(), ( "_PyTypes_InitState must preserve the interpreter guard, slot initialization, " "exception propagation, and success return order" ) print("object _PyTypes_InitState C89 status declaration and initialization/return order: PASS")