#!/usr/bin/env python3 """Check only maybe_freelist_push's index declaration and evaluation order.""" import re import sys from pathlib import Path source = Path(sys.argv[1] if len(sys.argv) > 1 else "cpython/Objects/tupleobject.c") match = re.search( r"static inline int\nmaybe_freelist_push\(PyTupleObject \*op\)\n\{(.*?)\n\}", source.read_text(), re.S, ) assert match, "maybe_freelist_push definition missing" body = match.group(1) assert re.match( r"\s*#if PyTuple_NFREELISTS > 0\s*" r"PyInterpreterState \*interp = _PyInterpreterState_GET\(\);\s*" r"Py_ssize_t index;\s*#ifdef Py_DEBUG", body, ), "maybe_freelist_push: index must be declared before the debug assertion and empty-tuple check" assert len(re.findall(r"\bPy_ssize_t\s+index\b", body)) == 1, ( "maybe_freelist_push: exactly one index declaration required" ) assert re.search( r"if \(Py_SIZE\(op\) == 0\)\s*\{\s*return 0;\s*\}\s*" r"index = Py_SIZE\(op\) - 1;\s*if \(index < PyTuple_NFREELISTS", body, ), "maybe_freelist_push: assign index at its original evaluation point after the empty-tuple return" assert len(re.findall(r"\bindex\s*=", body)) == 1, ( "maybe_freelist_push: exactly one index assignment required" ) print("maybe_freelist_push index C89 declaration and evaluation order: PASS")