Files
Python3-Win9x/scripts/vc6-typeobject-type-new-classmethod-c89.test.py
T

48 lines
1.4 KiB
Python

#!/usr/bin/env python3
"""Check type_new_classmethod'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_classmethod\(PyTypeObject \*type, PyObject \*attr\)\n\{(.*?)\n\}",
source,
re.S,
)
assert match, "type_new_classmethod definition missing"
body = re.sub(r"/\*.*?\*/|//[^\n]*", "", match.group(1), flags=re.S)
assert re.match(
r"\s*PyObject \*method;"
r"\s*PyObject \*func = PyDict_GetItemWithError\(type->tp_dict, attr\);",
body,
), "type_new_classmethod: method must be in the opening declaration block (C89)"
expected = """
PyObject *method;
PyObject *func = PyDict_GetItemWithError(type->tp_dict, attr);
if (func == NULL) {
if (PyErr_Occurred()) {
return -1;
}
return 0;
}
if (!PyFunction_Check(func)) {
return 0;
}
method = PyClassMethod_New(func);
if (method == NULL) {
return -1;
}
if (PyDict_SetItem(type->tp_dict, attr, method) < 0) {
Py_DECREF(method);
return -1;
}
Py_DECREF(method);
return 0;
"""
assert body.split() == expected.split(), (
"type_new_classmethod: preserve lookup, guards, allocation timing, "
"dictionary insertion, decrefs, and returns"
)
print("type_new_classmethod method C89 declaration and guarded allocation: PASS")