48 lines
1.9 KiB
Python
48 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Check type_set_bases's base 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 int\ntype_set_bases\(PyTypeObject \*type, PyObject \*new_bases, void \*context\)\n\{(.*?)\n\}",
|
|
source,
|
|
re.S,
|
|
)
|
|
assert match, "type_set_bases definition missing"
|
|
body = re.sub(r"/\*.*?\*/|//[^\n]*", "", match.group(1), flags=re.S)
|
|
loop = body.split("for (i = 0; i < n; i++) {", 1)[1].split(
|
|
"new_base = best_base(new_bases);", 1
|
|
)[0]
|
|
assert re.match(
|
|
r"\s*PyObject \*ob = PyTuple_GET_ITEM\(new_bases, i\);\s*PyTypeObject \*base;\s*if\b",
|
|
loop,
|
|
), "type_set_bases: base must be in the loop's opening declaration block"
|
|
assert len(re.findall(r"\bPyTypeObject\s*\*\s*base\b", body)) == 1, "declare base exactly once"
|
|
assert len(re.findall(r"\bbase\s*=(?!=)", body)) == 1, "assign base exactly once"
|
|
expected = """
|
|
PyObject *ob = PyTuple_GET_ITEM(new_bases, i);
|
|
PyTypeObject *base;
|
|
if (!PyType_Check(ob)) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"%s.__bases__ must be tuple of classes, not '%s'",
|
|
type->tp_name, Py_TYPE(ob)->tp_name);
|
|
return -1;
|
|
}
|
|
base = (PyTypeObject*)ob;
|
|
|
|
if (PyType_IsSubtype(base, type) ||
|
|
(base->tp_mro != NULL && type_is_subtype_base_chain(base, type)))
|
|
{
|
|
PyErr_SetString(PyExc_TypeError,
|
|
"a __bases__ item causes an inheritance cycle");
|
|
return -1;
|
|
}
|
|
}
|
|
"""
|
|
assert loop.split() == expected.split(), (
|
|
"type_set_bases: preserve item retrieval, validation, post-validation base "
|
|
"assignment, and inheritance-cycle checks in their original order"
|
|
)
|
|
print("type_set_bases base C89 declaration and unchanged evaluation order: PASS")
|