48 lines
1.5 KiB
Python
48 lines
1.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Check type_new_staticmethod's C89 declaration and guarded allocation."""
|
|
import re
|
|
from pathlib import Path
|
|
|
|
source = Path("cpython/Objects/typeobject.c").read_text()
|
|
match = re.search(
|
|
r"\nstatic int\ntype_new_staticmethod\(PyTypeObject \*type, PyObject \*attr\)\n\{(.*?)\n\}",
|
|
source,
|
|
re.S,
|
|
)
|
|
assert match, "type_new_staticmethod definition missing"
|
|
body = re.sub(r"/\*.*?\*/|//[^\n]*", "", match.group(1), flags=re.S)
|
|
assert re.match(
|
|
r"\s*PyObject \*static_func;"
|
|
r"\s*PyObject \*func = PyDict_GetItemWithError\(type->tp_dict, attr\);",
|
|
body,
|
|
), "type_new_staticmethod: static_func must be in the opening declaration block (C89)"
|
|
expected = """
|
|
PyObject *static_func;
|
|
PyObject *func = PyDict_GetItemWithError(type->tp_dict, attr);
|
|
if (func == NULL) {
|
|
if (PyErr_Occurred()) {
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
if (!PyFunction_Check(func)) {
|
|
return 0;
|
|
}
|
|
|
|
static_func = PyStaticMethod_New(func);
|
|
if (static_func == NULL) {
|
|
return -1;
|
|
}
|
|
if (PyDict_SetItem(type->tp_dict, attr, static_func) < 0) {
|
|
Py_DECREF(static_func);
|
|
return -1;
|
|
}
|
|
Py_DECREF(static_func);
|
|
return 0;
|
|
"""
|
|
assert body.split() == expected.split(), (
|
|
"type_new_staticmethod: preserve lookup, guards, allocation timing, "
|
|
"dictionary insertion, decrefs, and returns"
|
|
)
|
|
print("type_new_staticmethod static_func C89 declaration and guarded allocation: PASS")
|