#!/usr/bin/env python3 """Check type_cache_clear's C89 index declaration and unchanged loop body.""" import re import sys from pathlib import Path source = Path(sys.argv[1] if len(sys.argv) > 1 else "cpython/Objects/typeobject.c") match = re.search( r"static void\ntype_cache_clear\(struct type_cache \*cache, PyObject \*value\)\n\{(.*?)\n\}", source.read_text(), re.S, ) assert match, "type_cache_clear definition missing" body = match.group(1) assert re.match(r"\s*Py_ssize_t i;", body), ( "type_cache_clear: Py_ssize_t i; must be declared at function entry" ) assert re.fullmatch( r"\s*Py_ssize_t i;\s*" r"for \(i = 0; i < \(1 << MCACHE_SIZE_EXP\); i\+\+\) \{\s*" r"struct type_cache_entry \*entry = &cache->hashtable\[i\];\s*" r"entry->version = 0;\s*" r"Py_XSETREF\(entry->name, _Py_XNewRef\(value\)\);\s*" r"entry->value = NULL;\s*\}\s*", body, ), "type_cache_clear: preserve loop assignment, bound, increment, body and order" print("type_cache_clear index C89 declaration and unchanged loop body: PASS")