Files
Python3-Win9x/scripts/vc6-typeobject-mro-hierarchy-tuple-c89.test.py

52 lines
1.6 KiB
Python

#!/usr/bin/env python3
"""Check only mro_hierarchy's tuple declaration and branch assignment timing."""
import re
from pathlib import Path
source = Path("cpython/Objects/typeobject.c").read_text()
match = re.search(
r"\nstatic int\nmro_hierarchy\(PyTypeObject \*type, PyObject \*temp\)\n\{(.*?)\n\}",
source,
re.S,
)
assert match, "mro_hierarchy definition missing"
body = re.sub(r"/\*.*?\*/", "", match.group(1), flags=re.S)
assert re.match(
r"\s*PyObject \*old_mro;\s*PyObject \*new_mro;\s*PyObject \*tuple;", body
), "mro_hierarchy: PyObject *tuple; must be in the opening declarations"
assert len(re.findall(r"\bPyObject\s*\*\s*tuple\b", body)) == 1, (
"mro_hierarchy: declare tuple exactly once without initialization"
)
expected_prefix = """
PyObject *old_mro;
PyObject *new_mro;
PyObject *tuple;
Py_ssize_t n;
Py_ssize_t i;
int res = mro_internal(type, &old_mro);
if (res <= 0) {
return res;
}
new_mro = type->tp_mro;
if (old_mro != NULL) {
tuple = PyTuple_Pack(3, type, new_mro, old_mro);
}
else {
tuple = PyTuple_Pack(2, type, new_mro);
}
if (tuple != NULL) {
res = PyList_Append(temp, tuple);
}
else {
res = -1;
}
Py_XDECREF(tuple);
"""
assert body.split()[:len(expected_prefix.split())] == expected_prefix.split(), (
"mro_hierarchy: preserve the guard, branch assignments, and tuple consumption"
)
assert len(re.findall(r"\btuple\s*=", body)) == 2, (
"mro_hierarchy: assign tuple only in the two original branches"
)
print("mro_hierarchy tuple C89 declaration and branch assignment timing: PASS")