#!/usr/bin/env python3 """Check set_issuperset's C89 declarations and unchanged iterator ownership.""" import re import sys from pathlib import Path source = Path(sys.argv[1]).read_text() match = re.search( r"static PyObject \*\nset_issuperset\(PySetObject \*so, PyObject \*other\)" r"\n\{(.*?)\n\}", source, re.S, ) assert match, "set_issuperset not found" body = match.group(1) assert re.match(r"\s*PyObject \*key;\s*PyObject \*it;", body), ( "set_issuperset: key and it must be declared in the initial declaration " "block; late PyObject declaration after PyAnySet_Check is not C89 compatible" ) expected = """ PyObject *key; PyObject *it; if (PyAnySet_Check(other)) { return set_issubset((PySetObject *)other, (PyObject *)so); } it = PyObject_GetIter(other); if (it == NULL) { return NULL; } while ((key = PyIter_Next(it)) != NULL) { int rv = set_contains_key(so, key); Py_DECREF(key); if (rv < 0) { Py_DECREF(it); return NULL; } if (!rv) { Py_DECREF(it); Py_RETURN_FALSE; } } Py_DECREF(it); if (PyErr_Occurred()) { return NULL; } Py_RETURN_TRUE; """ assert re.sub(r"\s+", "", body) == re.sub(r"\s+", "", expected), ( "preserve the set fast path, iterator acquisition, loop, reference ownership, " "error handling, and return behavior" ) print("set_issuperset C89 declarations and iterator ownership: PASS")