41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Check only mro_implementation's bases declaration and guarded read timing."""
|
|
import re
|
|
from pathlib import Path
|
|
|
|
source = Path("cpython/Objects/typeobject.c").read_text()
|
|
match = re.search(
|
|
r"\nstatic PyObject \*\nmro_implementation\(PyTypeObject \*type\)\n\{(.*?)\n\}",
|
|
source,
|
|
re.S,
|
|
)
|
|
assert match, "mro_implementation definition missing"
|
|
body = re.sub(r"/\*.*?\*/", "", match.group(1), flags=re.S)
|
|
assert re.match(r"\s*PyObject \*bases;", body), (
|
|
"mro_implementation: PyObject *bases; must be in the opening declarations"
|
|
)
|
|
assert len(re.findall(r"\bPyObject\s*\*\s*bases\b", body)) == 1, (
|
|
"mro_implementation: declare bases exactly once without initialization"
|
|
)
|
|
expected_prefix = """
|
|
PyObject *bases;
|
|
Py_ssize_t n;
|
|
Py_ssize_t i;
|
|
PyObject **to_merge;
|
|
PyObject *result;
|
|
if (!_PyType_IsReady(type)) {
|
|
if (PyType_Ready(type) < 0)
|
|
return NULL;
|
|
}
|
|
|
|
bases = type->tp_bases;
|
|
n = PyTuple_GET_SIZE(bases);
|
|
"""
|
|
assert body.split()[:len(expected_prefix.split())] == expected_prefix.split(), (
|
|
"mro_implementation: preserve the ready check and bases read timing"
|
|
)
|
|
assert len(re.findall(r"\bbases\s*=", body)) == 1, (
|
|
"mro_implementation: assign bases exactly once"
|
|
)
|
|
print("mro_implementation bases C89 declaration and guarded read timing: PASS")
|