#!/usr/bin/env python3 """Regress type_new_get_slots' C89 local and guarded tuple conversion.""" import re from pathlib import Path source = Path("cpython/Objects/typeobject.c").read_text() match = re.search( r"\nstatic int\ntype_new_get_slots\(type_new_ctx \*ctx, PyObject \*dict\)\n\{(.*?)\n\}", source, re.S, ) assert match, "type_new_get_slots definition missing" body = re.sub(r"/\*.*?\*/|//[^\n]*", "", match.group(1), flags=re.S) assert re.match( r"\s*PyObject \*new_slots;\s*PyObject \*slots = PyDict_GetItemWithError\(", body, ), "type_new_get_slots: new_slots must be in the opening declaration block (C89)" expected = """ PyObject *new_slots; PyObject *slots = PyDict_GetItemWithError(dict, &_Py_ID(__slots__)); if (slots == NULL) { if (PyErr_Occurred()) { return -1; } ctx->slots = NULL; ctx->nslot = 0; return 0; } if (PyUnicode_Check(slots)) { new_slots = PyTuple_Pack(1, slots); } else { new_slots = PySequence_Tuple(slots); } if (new_slots == NULL) { return -1; } assert(PyTuple_CheckExact(new_slots)); ctx->slots = new_slots; ctx->nslot = PyTuple_GET_SIZE(new_slots); return 0; """ assert body.split() == expected.split(), ( "preserve lookup, missing-slot handling, guarded conversions, error checks, " "and context updates" ) print("type_new_get_slots new_slots C89 declaration and guarded conversion: PASS")