Files
Python3-Win9x/scripts/vc6-typeobject-modified-subclasses-c89.test.py

35 lines
1.2 KiB
Python

#!/usr/bin/env python3
"""Check only PyType_Modified's subclasses declaration and read timing."""
import re
from pathlib import Path
source = Path("cpython/Objects/typeobject.c").read_text()
match = re.search(
r"\nvoid\nPyType_Modified\(PyTypeObject \*type\)\n\{(.*?)\n\}",
source,
re.S,
)
assert match, "PyType_Modified definition missing"
body = re.sub(r"/\*.*?\*/", "", match.group(1), flags=re.S)
assert re.match(r"\s*PyObject \*subclasses;", body), (
"PyType_Modified: PyObject *subclasses; must be declared at function entry"
)
assert len(re.findall(r"\bPyObject\s*\*\s*subclasses\b", body)) == 1, (
"PyType_Modified: declare subclasses exactly once"
)
assert re.match(
r"\s*PyObject \*subclasses;\s*"
r"(?:Py_ssize_t i;\s*)?"
r"PyObject \*ref;\s*"
r"PyObject \*obj;\s*"
r"if \(!_PyType_HasFeature\(type, Py_TPFLAGS_VALID_VERSION_TAG\)\) \{\s*"
r"return;\s*\}\s*"
r"subclasses = type->tp_subclasses;\s*"
r"if \(subclasses != NULL\) \{",
body,
), "PyType_Modified: read tp_subclasses immediately after the early-return guard"
assert body.count("type->tp_subclasses") == 1, (
"PyType_Modified: preserve the single tp_subclasses read"
)
print("PyType_Modified subclasses C89 declaration and read timing: PASS")