Files
Python3-Win9x/scripts/vc6-typeobject-get-module-by-def-ht-module-c89.test.py

58 lines
2.0 KiB
Python

#!/usr/bin/env python3
"""Keep heap-type/module locals at entry and their reads after the static-type skip."""
import re
from pathlib import Path
source = Path("cpython/Objects/typeobject.c").read_text()
match = re.search(
r"\nPyObject \*\nPyType_GetModuleByDef\(PyTypeObject \*type, PyModuleDef \*def\)\n"
r"\{(.*?)\n\}", source, re.S,
)
assert match, "PyType_GetModuleByDef definition missing"
body = re.sub(r"/\*.*?\*/|//[^\n]*", "", match.group(1), flags=re.S)
assert re.match(r"\s*PyObject \*mro;\s*Py_ssize_t n;\s*Py_ssize_t i;\s*PyHeapTypeObject \*ht;\s*PyObject \*module;\s*assert\(", body), (
"PyType_GetModuleByDef: ht and module must be declared in the opening declaration block (C89)"
)
for declaration in (r"\bPyHeapTypeObject\s*\*ht\b", r"\bPyObject\s*\*module\b"):
assert len(re.findall(declaration, body)) == 1, (
"PyType_GetModuleByDef: declare ht and module exactly once"
)
expected = """
PyObject *mro;
Py_ssize_t n;
Py_ssize_t i;
PyHeapTypeObject *ht;
PyObject *module;
assert(PyType_Check(type));
mro = type->tp_mro;
assert(mro != NULL);
assert(PyTuple_Check(mro));
assert(PyTuple_GET_SIZE(mro) >= 1);
n = PyTuple_GET_SIZE(mro);
for (i = 0; i < n; i++) {
PyObject *super = PyTuple_GET_ITEM(mro, i);
if(!_PyType_HasFeature((PyTypeObject *)super, Py_TPFLAGS_HEAPTYPE)) {
continue;
}
ht = (PyHeapTypeObject*)super;
module = ht->ht_module;
if (module && _PyModule_GetDef(module) == def) {
return module;
}
}
PyErr_Format(
PyExc_TypeError,
"PyType_GetModuleByDef: No superclass of '%s' has the given module",
type->tp_name);
return NULL;
"""
assert body.split() == expected.split(), (
"PyType_GetModuleByDef: preserve ht/module assignments after the static-type skip and before module lookup, "
"loop, module lookup, and error handling"
)
print("PyType_GetModuleByDef ht/module C89 declarations and same-point assignments: PASS")