#!/usr/bin/env python3 """Source-level C89 and ownership-order check for bytearray_join.""" import re import sys from pathlib import Path if len(sys.argv) != 2: raise SystemExit("usage: vc6-bytearray-join-c89.test.py bytearrayobject.c") source = Path(sys.argv[1]).read_text() signature = "bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes)" start = source.index(signature) body_start = source.index("{", start) body_end = source.index("\n}", body_start) body = source[body_start + 1:body_end] if not body.lstrip().startswith("PyObject *ret;"): raise SystemExit("ret must be declared in bytearray_join's opening declaration group") if len(re.findall(r"\bPyObject\s*\*\s*ret\s*;", body)) != 1: raise SystemExit("bytearray_join must declare ret exactly once") if re.search(r"\bPyObject\s*\*\s*ret\s*=", body): raise SystemExit("bytearray_join must assign ret separately from its declaration") pattern = re.compile( r"self->ob_exports\+\+;.*?" r"ret\s*=\s*stringlib_bytes_join\(\s*\(PyObject\*\)self,\s*" r"iterable_of_bytes\s*\);\s*" r"self->ob_exports--;\s*(?://[^\n]*\s*)?return ret;", re.DOTALL, ) if not pattern.search(body): raise SystemExit("bytearray_join export/join/unexport/return order changed") print("bytearray_join C89 declaration and export order: PASS")