63 lines
2.4 KiB
Python
63 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Check type_set_bases's loop index declaration and initialization 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)
|
|
assert re.match(
|
|
r"\s*Py_ssize_t n;\s*Py_ssize_t i;\s*PyTypeObject \*new_base;\s*PyObject \*old_bases;\s*PyTypeObject \*old_base;\s*PyObject \*temp;\s*int res;\s*if\b",
|
|
body,
|
|
), (
|
|
"type_set_bases: Py_ssize_t i; must follow n in the opening declarations"
|
|
)
|
|
assert len(re.findall(r"\bPy_ssize_t\s+n\b", body)) == 1, (
|
|
"type_set_bases: declare n exactly once"
|
|
)
|
|
assert len(re.findall(r"\bn\s*=\s*PyTuple_GET_SIZE\(new_bases\)", body)) == 1, (
|
|
"type_set_bases: read the new_bases size into n exactly once"
|
|
)
|
|
validation = body.split("new_base = best_base(new_bases);", 1)[0]
|
|
assert len(re.findall(r"\bPy_ssize_t\s+i\b", validation)) == 1, "declare i exactly once"
|
|
assert len(re.findall(r"\bi\s*=(?!=)", validation)) == 1, "initialize i exactly once"
|
|
assert not re.search(r"for\s*\(\s*Py_ssize_t\s+i\b", validation), "C99 loop index declaration remains"
|
|
expected = """
|
|
Py_ssize_t n;
|
|
Py_ssize_t i;
|
|
PyTypeObject *new_base;
|
|
PyObject *old_bases;
|
|
PyTypeObject *old_base;
|
|
PyObject *temp;
|
|
int res;
|
|
if (!check_set_special_type_attr(type, new_bases, "__bases__")) {
|
|
return -1;
|
|
}
|
|
assert(new_bases != NULL);
|
|
|
|
if (!PyTuple_Check(new_bases)) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"can only assign tuple to %s.__bases__, not %s",
|
|
type->tp_name, Py_TYPE(new_bases)->tp_name);
|
|
return -1;
|
|
}
|
|
if (PyTuple_GET_SIZE(new_bases) == 0) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"can only assign non-empty tuple to %s.__bases__, not ()",
|
|
type->tp_name);
|
|
return -1;
|
|
}
|
|
n = PyTuple_GET_SIZE(new_bases);
|
|
for (i = 0; i < n; i++) {
|
|
"""
|
|
assert body.split()[:len(expected.split())] == expected.split(), (
|
|
"type_set_bases: preserve validation, empty-tuple guard, n assignment timing, "
|
|
"and index initialization at the original loop point"
|
|
)
|
|
print("type_set_bases loop index C89 declaration and unchanged evaluation order: PASS")
|