Files
Python3-Win9x/scripts/vc6-tupleobject-from-array-steal-tuple-c89.test.py

47 lines
1.5 KiB
Python

#!/usr/bin/env python3
"""Check only _PyTuple_FromArraySteal's tuple declaration placement and 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 \*\n_PyTuple_FromArraySteal\(PyObject \*const \*src, Py_ssize_t n\)\n\{(.*?)\n\}",
source.read_text(),
re.S,
)
assert match, "_PyTuple_FromArraySteal definition not found"
body = match.group(1)
assert re.match(r"\s*PyTupleObject \*tuple;\s*Py_ssize_t i;\s*PyObject \*\*dst;\s*if \(n == 0\)", body), (
"_PyTuple_FromArraySteal: tuple must be declared without an initializer "
"before the empty-tuple check"
)
expected = """
PyTupleObject *tuple;
Py_ssize_t i;
PyObject **dst;
if (n == 0) {
return tuple_get_empty();
}
tuple = tuple_alloc(n);
if (tuple == NULL) {
for (i = 0; i < n; i++) {
Py_DECREF(src[i]);
}
return NULL;
}
dst = tuple->ob_item;
for (i = 0; i < n; i++) {
PyObject *item = src[i];
dst[i] = item;
}
_PyObject_GC_TRACK(tuple);
return (PyObject *)tuple;
"""
assert re.sub(r"\s+", "", body) == re.sub(r"\s+", "", expected), (
"preserve empty return before single allocation, allocation failure DECREFs "
"and NULL return, unchanged dst/loop/item declarations, item stores, "
"GC tracking and return order"
)
print("_PyTuple_FromArraySteal tuple declaration placement and evaluation/refcount/error order: PASS")