31 lines
1.1 KiB
Python
31 lines
1.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Check only PyType_Modified'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"\nvoid\nPyType_Modified\(PyTypeObject \*type\)\n\{(.*?)\n\}",
|
|
source,
|
|
re.S,
|
|
)
|
|
assert match, "PyType_Modified definition missing"
|
|
body = re.sub(r"/\*.*?\*/", "", match.group(1), flags=re.S)
|
|
assert re.match(r"\s*PyObject \*subclasses;\s*Py_ssize_t i;", body), (
|
|
"PyType_Modified: Py_ssize_t i; must be declared at function entry"
|
|
)
|
|
assert len(re.findall(r"\bPy_ssize_t\s+i\b", body)) == 1, (
|
|
"PyType_Modified: declare i exactly once"
|
|
)
|
|
assert re.search(
|
|
r"if \(subclasses != NULL\) \{\s*"
|
|
r"assert\(PyDict_CheckExact\(subclasses\)\);\s*"
|
|
r"i = 0;\s*"
|
|
r"while \(PyDict_Next\(subclasses, &i, NULL, &ref\)\) \{",
|
|
body,
|
|
), "PyType_Modified: initialize i at its original point before the while loop"
|
|
assert len(re.findall(r"\bi\s*=", body)) == 1, (
|
|
"PyType_Modified: initialize i exactly once"
|
|
)
|
|
print("PyType_Modified index C89 declaration and initialization timing: PASS")
|