#!/usr/bin/env python3 """Check type_new_get_bases base local is C89-compatible and assigned in place.""" 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: base must be in the opening declaration block (C89)" assert len(re.findall(r"\bPyTypeObject\s*\*\s*base\b", body)) == 1, ( "declare base exactly once" ) assert re.search(r"\n\s*base = best_base\(ctx->bases\);", body), ( "preserve best_base evaluation at its original point" ) assert re.search( r"base = best_base\(ctx->bases\);\s*if \(base == NULL\) \{\s*return -1;\s*\}\s*" r"ctx->base = base;\s*ctx->bases = Py_NewRef\(ctx->bases\);\s*return 0;", body, ), "preserve best-base validation and subsequent reference updates" print("type_new_get_bases base C89 declaration and evaluation ordering: PASS")