40 lines
1.4 KiB
Python
40 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Check type_new_slots_bases's C89 declaration and post-skip cast timing."""
|
|
import re
|
|
from pathlib import Path
|
|
|
|
source = Path("cpython/Objects/typeobject.c").read_text()
|
|
match = re.search(
|
|
r"\nstatic void\ntype_new_slots_bases\(type_new_ctx \*ctx\)\n\{(.*?)\n\}",
|
|
source,
|
|
re.S,
|
|
)
|
|
assert match, "type_new_slots_bases definition missing"
|
|
body = re.sub(r"/\*.*?\*/|//[^\n]*", "", match.group(1), flags=re.S)
|
|
loop = body.split("for (i = 0; i < nbases; i++) {", 1)[1]
|
|
assert re.match(
|
|
r"\s*PyObject \*obj = PyTuple_GET_ITEM\(ctx->bases, i\);"
|
|
r"\s*PyTypeObject \*base;\s*if\b",
|
|
loop,
|
|
), "type_new_slots_bases: base must be in the loop's opening declaration block (C89)"
|
|
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 *obj = PyTuple_GET_ITEM(ctx->bases, i);
|
|
PyTypeObject *base;
|
|
if (obj == (PyObject *)ctx->base) {
|
|
continue;
|
|
}
|
|
base = _PyType_CAST(obj);
|
|
"""
|
|
prefix = loop.split("if (ctx->may_add_dict", 1)[0]
|
|
assert prefix.split() == expected.split(), (
|
|
"type_new_slots_bases: preserve item retrieval, primary-base skip, and "
|
|
"cast assignment before the first base use in their original order"
|
|
)
|
|
print("type_new_slots_bases base C89 declaration and post-skip cast timing: PASS")
|