#!/usr/bin/env python3 """Keep overrides_hash's query after its assertion with C89 locals.""" import re from pathlib import Path source = Path("cpython/Objects/typeobject.c").read_text() match = re.search(r"\nstatic int\noverrides_hash\(PyTypeObject \*type\)\n\{(.*?)\n\}", source, re.S) assert match, "overrides_hash definition missing" body = match.group(1) assert re.match(r"\s*PyObject \*dict = type->tp_dict;\s*int r;\s*assert", body), ( "overrides_hash: r must be in the opening C89 declaration block" ) assert len(re.findall(r"\bint\s+r\b", body)) == 1 expected = '''assert(dict != NULL); r = PyDict_Contains(dict, &_Py_ID(__eq__)); if (r == 0) { r = PyDict_Contains(dict, &_Py_ID(__hash__)); } return r;''' assert body[body.index("assert(dict") :].split() == expected.split(), ( "overrides_hash: preserve assertion, query ordering, conditional and error propagation" ) print("overrides_hash C89 r, query ordering and result propagation: PASS")