#!/usr/bin/env python3 """Check only the late bases declaration in assign_version_tag near MRO code.""" import re from pathlib import Path source = Path("cpython/Objects/typeobject.c").read_text() match = re.search( r"\nstatic int\nassign_version_tag\(struct type_cache \*cache, PyTypeObject \*type\)\n\{(.*?)\n\}", source, re.S, ) assert match, "assign_version_tag definition missing" body = re.sub(r"/\*.*?\*/", "", match.group(1), flags=re.S) assert re.match(r"\s*PyObject \*bases;", body), ( "assign_version_tag: PyObject *bases; must be in the opening declarations" ) assert len(re.findall(r"\bPyObject\s*\*\s*bases\b", body)) == 1, ( "assign_version_tag: declare bases exactly once" ) expected = """ PyObject *bases; Py_ssize_t n; Py_ssize_t i; if (_PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) { return 1; } if (!_PyType_HasFeature(type, Py_TPFLAGS_READY)) { return 0; } if (next_version_tag == 0) { return 0; } type->tp_version_tag = next_version_tag++; assert (type->tp_version_tag != 0); bases = type->tp_bases; n = PyTuple_GET_SIZE(bases); for (i = 0; i < n; i++) { PyObject *b = PyTuple_GET_ITEM(bases, i); if (!assign_version_tag(cache, _PyType_CAST(b))) return 0; } type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG; return 1; """ assert body.split() == expected.split(), ( "assign_version_tag: preserve bases read timing, guards, n, and loop index" ) print("assign_version_tag bases C89 declaration and unchanged evaluation order: PASS")