42 lines
1.3 KiB
Python
42 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Keep PyType_GetModule's heap-type declaration C89-safe and cast in place."""
|
|
import re
|
|
from pathlib import Path
|
|
|
|
source = Path("cpython/Objects/typeobject.c").read_text()
|
|
match = re.search(
|
|
r"\nPyObject \*\nPyType_GetModule\(PyTypeObject \*type\)\n\{(.*?)\n\}",
|
|
source,
|
|
re.S,
|
|
)
|
|
assert match, "PyType_GetModule definition missing"
|
|
body = re.sub(r"/\*.*?\*/|//[^\n]*", "", match.group(1), flags=re.S)
|
|
assert re.match(r"\s*PyHeapTypeObject \*et;\s*assert\(", body), (
|
|
"PyType_GetModule: et must be declared at function entry (C89)"
|
|
)
|
|
expected = """
|
|
PyHeapTypeObject *et;
|
|
assert(PyType_Check(type));
|
|
if (!_PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE)) {
|
|
PyErr_Format(
|
|
PyExc_TypeError,
|
|
"PyType_GetModule: Type '%s' is not a heap type",
|
|
type->tp_name);
|
|
return NULL;
|
|
}
|
|
|
|
et = (PyHeapTypeObject*)type;
|
|
if (!et->ht_module) {
|
|
PyErr_Format(
|
|
PyExc_TypeError,
|
|
"PyType_GetModule: Type '%s' has no associated module",
|
|
type->tp_name);
|
|
return NULL;
|
|
}
|
|
return et->ht_module;
|
|
"""
|
|
assert body.split() == expected.split(), (
|
|
"PyType_GetModule: preserve validation, cast timing, module check, and return"
|
|
)
|
|
print("PyType_GetModule et C89 declaration and same-point cast: PASS")
|