56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Keep _PyType_GetSubclasses's ref in opening C89 locals."""
|
|
import re
|
|
from pathlib import Path
|
|
|
|
source = Path("cpython/Objects/typeobject.c").read_text()
|
|
match = re.search(
|
|
r"\nPyObject\*\n_PyType_GetSubclasses\(PyTypeObject \*self\)\n\{(.*?)\n\}",
|
|
source,
|
|
re.S,
|
|
)
|
|
assert match, "_PyType_GetSubclasses definition missing"
|
|
body = re.sub(r"/\*.*?\*/|//[^\n]*", "", match.group(1), flags=re.S)
|
|
assert re.match(
|
|
r"\s*PyObject \*subclasses;\s*Py_ssize_t i;\s*"
|
|
r"PyObject \*list = PyList_New\(0\);\s*PyObject \*ref;",
|
|
body,
|
|
), "_PyType_GetSubclasses: ref must be in the opening declaration block (C89)"
|
|
assert len(re.findall(r"\bPyObject\s*\*\s*ref\b", body)) == 1, (
|
|
"_PyType_GetSubclasses: declare ref exactly once without initialization"
|
|
)
|
|
expected = """
|
|
PyObject *subclasses;
|
|
Py_ssize_t i;
|
|
PyObject *list = PyList_New(0);
|
|
PyObject *ref;
|
|
PyObject *obj;
|
|
if (list == NULL) {
|
|
return NULL;
|
|
}
|
|
subclasses = self->tp_subclasses;
|
|
if (subclasses == NULL) {
|
|
return list;
|
|
}
|
|
assert(PyDict_CheckExact(subclasses));
|
|
i = 0;
|
|
while (PyDict_Next(subclasses, &i, NULL, &ref)) {
|
|
assert(PyWeakref_CheckRef(ref));
|
|
obj = PyWeakref_GET_OBJECT(ref);
|
|
if (obj == Py_None) {
|
|
continue;
|
|
}
|
|
assert(PyType_Check(obj));
|
|
if (PyList_Append(list, obj) < 0) {
|
|
Py_DECREF(list);
|
|
return NULL;
|
|
}
|
|
}
|
|
return list;
|
|
"""
|
|
assert body.split() == expected.split(), (
|
|
"_PyType_GetSubclasses: preserve guards, iteration, ref uses, "
|
|
"and obj assignment timing"
|
|
)
|
|
print("_PyType_GetSubclasses ref C89 declaration and unchanged use order: PASS")
|