#!/usr/bin/env python3 """Check _PyType_Name's C89 declaration and preserve strrchr evaluation order.""" import re from pathlib import Path source = Path("cpython/Objects/typeobject.c").read_text() match = re.search( r"\nconst char \*\n_PyType_Name\(PyTypeObject \*type\)\n\{(.*?)\n\}", source, re.S, ) assert match, "_PyType_Name definition missing" body = match.group(1) assert re.match(r"\s*const char \*s;\s*assert\b", body), ( "_PyType_Name: const char *s; must be in the opening declaration block" ) assert len(re.findall(r"\bconst char\s*\*\s*s\b", body)) == 1, ( "_PyType_Name: declare s exactly once" ) expected = """ const char *s; assert(type->tp_name != NULL); s = strrchr(type->tp_name, '.'); if (s == NULL) { s = type->tp_name; } else { s++; } return s; """ assert body.split() == expected.split(), ( "_PyType_Name: preserve assertion before strrchr assignment, " "fallback, suffix increment, and return" ) print("_PyType_Name s C89 declaration and unchanged evaluation order: PASS")