Files
Python3-Win9x/scripts/vc6-tupleobject-new-c89.test.py
T

40 lines
1.2 KiB
Python

#!/usr/bin/env python3
"""Check PyTuple_New's C89 loop declaration and initialization order."""
import re
import sys
from pathlib import Path
source = Path(sys.argv[1] if len(sys.argv) > 1 else "cpython/Objects/tupleobject.c")
match = re.search(
r"PyObject \*\nPyTuple_New\(Py_ssize_t size\)\n\{(.*?)\n\}",
source.read_text(),
re.S,
)
assert match, "PyTuple_New definition not found"
body = match.group(1)
assert re.match(r"\s*PyTupleObject \*op;\s*Py_ssize_t i;", body), (
"PyTuple_New: i must be declared before the size check; "
"for-loop declarations are not C89 compatible"
)
expected = """
PyTupleObject *op;
Py_ssize_t i;
if (size == 0) {
return tuple_get_empty();
}
op = tuple_alloc(size);
if (op == NULL) {
return NULL;
}
for (i = 0; i < size; i++) {
op->ob_item[i] = NULL;
}
_PyObject_GC_TRACK(op);
return (PyObject *) op;
"""
assert re.sub(r"\s+", "", body) == re.sub(r"\s+", "", expected), (
"preserve empty-tuple return, single allocation and failure return, loop "
"initialization/condition/increment, item clearing, GC tracking, and return order"
)
print("PyTuple_New C89 loop declaration and initialization order: PASS")