48 lines
1.7 KiB
Python
48 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Check best_base's base_i declaration and post-validation cast timing."""
|
|
import re
|
|
from pathlib import Path
|
|
|
|
source = Path("cpython/Objects/typeobject.c").read_text()
|
|
match = re.search(
|
|
r"\nstatic PyTypeObject \*\nbest_base\(PyObject \*bases\)\n\{(.*?)\n\}",
|
|
source,
|
|
re.S,
|
|
)
|
|
assert match, "best_base definition missing"
|
|
body = re.sub(r"/\*.*?\*/|//[^\n]*", "", match.group(1), flags=re.S)
|
|
loop = body.split("for (i = 0; i < n; i++) {", 1)[1]
|
|
assert re.match(
|
|
r"\s*PyObject \*base_proto = PyTuple_GET_ITEM\(bases, i\);"
|
|
r"\s*PyTypeObject \*base_i;\s*if\b",
|
|
loop,
|
|
), "best_base: base_i must be in the loop's opening declaration block (C89)"
|
|
assert len(re.findall(r"\bPyTypeObject\s*\*\s*base_i\b", body)) == 1, (
|
|
"declare base_i exactly once"
|
|
)
|
|
assert len(re.findall(r"\bbase_i\s*=(?!=)", body)) == 1, (
|
|
"assign base_i exactly once"
|
|
)
|
|
expected = """
|
|
PyObject *base_proto = PyTuple_GET_ITEM(bases, i);
|
|
PyTypeObject *base_i;
|
|
if (!PyType_Check(base_proto)) {
|
|
PyErr_SetString(
|
|
PyExc_TypeError,
|
|
"bases must be types");
|
|
return NULL;
|
|
}
|
|
base_i = (PyTypeObject *)base_proto;
|
|
|
|
if (!_PyType_IsReady(base_i)) {
|
|
if (PyType_Ready(base_i) < 0)
|
|
return NULL;
|
|
}
|
|
"""
|
|
prefix = loop.split("if (!_PyType_HasFeature(base_i, Py_TPFLAGS_BASETYPE))", 1)[0]
|
|
assert prefix.split() == expected.split(), (
|
|
"best_base: preserve item retrieval, successful type validation, cast "
|
|
"assignment, and first base_i use in their original order"
|
|
)
|
|
print("best_base base_i C89 declaration and post-validation cast timing: PASS")
|