#!/usr/bin/env python3 """Check structseq_repr's loop declaration and unchanged iteration order.""" import re import sys from pathlib import Path source = Path(sys.argv[1] if len(sys.argv) > 1 else "cpython/Objects/structseq.c") match = re.search( r"static PyObject \*\nstructseq_repr\(PyStructSequence \*obj\)\n\{(.*?)\n\}", source.read_text(), re.S, ) assert match, "structseq_repr definition not found" body = match.group(1) assert not re.search(r"for\s*\(\s*Py_ssize_t\b", body), ( "structseq_repr: VC6 rejects the C99 loop initializer declaration; " "declare i in the function declaration block" ) assert re.match( r"\s*PyTypeObject \*typ = Py_TYPE\(obj\);" r"\s*_PyUnicodeWriter writer;\s*Py_ssize_t i;", body, ), "i must be declared without initialization in the function declaration block" assert len(re.findall(r"\bPy_ssize_t\s+i\s*;", body)) == 1, ( "declare i exactly once" ) assert re.search( r"if \(_PyUnicodeWriter_WriteChar\(&writer, '\('\) < 0\) " r"\{\s*goto error;\s*\}\s*" r"for \(i\s*=\s*0; i < VISIBLE_SIZE\(obj\); i\+\+\) \{", body, ), "preserve zero initialization after '(' output, per-iteration size lookup, and increment" assert len(re.findall(r"\bfor\s*\(", body)) == 1, "preserve the single item loop" assert "typ->tp_members[i].name" in body, "preserve member-name indexing" assert "PyStructSequence_GET_ITEM(obj, i)" in body, "preserve item indexing" print("structseq_repr C89 loop declaration and iteration order: PASS")