28 lines
1.3 KiB
Python
28 lines
1.3 KiB
Python
"""Check type_set_bases declares old_base before statements without moving its read."""
|
|
from pathlib import Path
|
|
import re
|
|
import sys
|
|
|
|
source = Path(sys.argv[1] if len(sys.argv) > 1 else "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 = match.group(1)
|
|
assert re.search(
|
|
r"\A\n\s*Py_ssize_t n;\n\s*Py_ssize_t i;\n\s*PyTypeObject \*new_base;\n\s*PyObject \*old_bases;\n\s*PyTypeObject \*old_base;",
|
|
body,
|
|
), "type_set_bases: old_base must be declared with the opening locals"
|
|
assert body.count("PyTypeObject *old_base;") == 1, "old_base declaration must occur once"
|
|
assert body.count("old_base = type->tp_base;") == 1, "old_base read must occur once"
|
|
assert re.search(
|
|
r'if \(!compatible_for_assignment\(type->tp_base, new_base, "__bases__"\)\) \{\s*'
|
|
r'return -1;\s*\}\s*old_bases = type->tp_bases;\s*'
|
|
r'assert\(old_bases != NULL\);\s*old_base = type->tp_base;\s*'
|
|
r'type->tp_bases = Py_NewRef\(new_bases\);\s*'
|
|
r'type->tp_base = \(PyTypeObject \*\)Py_NewRef\(new_base\);',
|
|
body,
|
|
), "type_set_bases: preserve old_base read after validation and before base replacement"
|