#!/usr/bin/env python3 """Regress VC6 C89 compatibility of type_new_get_bases() loop index.""" import re from pathlib import Path source = Path("cpython/Objects/typeobject.c").read_text() match = re.search( r"\nstatic int\ntype_new_get_bases\(type_new_ctx \*ctx, PyObject \*\*type\)\n\{(.*?)\n\}", source, re.S, ) assert match, "type_new_get_bases definition missing" body = re.sub(r"/\*.*?\*/|//[^\n]*", "", match.group(1), flags=re.S) assert re.match( r"\s*Py_ssize_t nbases = PyTuple_GET_SIZE\(ctx->bases\);" r"\s*Py_ssize_t i;\s*PyTypeObject \*winner;\s*PyTypeObject \*base;" r"\s*if \(nbases == 0\)", body, ), "type_new_get_bases: C89 loop index i must be declared before statements" assert len(re.findall(r"\bPy_ssize_t\s+i\b", body)) == 1, "declare i exactly once" assert re.search( r"for \(i = 0; i < nbases; i\+\+\) \{\s*" r"PyObject \*base = PyTuple_GET_ITEM\(ctx->bases, i\);", body, ), "type_new_get_bases: preserve loop start, bound, increment, and base lookup" assert not re.search(r"for\s*\(\s*Py_ssize_t\b", body), ( "type_new_get_bases: loop initializer must reuse the declared index" ) print("type_new_get_bases C89 loop index: PASS")