104 lines
4.3 KiB
Python
104 lines
4.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Compile the exact range type as C89 and verify every static slot."""
|
|
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
source_path = Path(sys.argv[1])
|
|
source = source_path.read_text()
|
|
match = re.search(r"PyTypeObject PyRange_Type = \{.*?\n\};", source, re.S)
|
|
assert match, "PyRange_Type initializer not found"
|
|
header = (source_path.parent.parent / "Include/cpython/object.h").read_text()
|
|
layout = re.search(r"struct _typeobject \{(.*?)\n\};", header, re.S)
|
|
assert layout, "PyTypeObject layout not found"
|
|
body = re.sub(r"/\*.*?\*/|//[^\n]*", "", layout.group(1), flags=re.S)
|
|
fields = re.findall(r"\b(tp_\w+)\b", body)
|
|
assert len(fields) == len(set(fields)) == 48, "review changed PyTypeObject layout"
|
|
expected = {
|
|
"tp_name": '"range"',
|
|
"tp_basicsize": "sizeof(rangeobject)",
|
|
"tp_dealloc": "(destructor)range_dealloc",
|
|
"tp_repr": "(reprfunc)range_repr",
|
|
"tp_as_number": "&range_as_number",
|
|
"tp_as_sequence": "&range_as_sequence",
|
|
"tp_as_mapping": "&range_as_mapping",
|
|
"tp_hash": "(hashfunc)range_hash",
|
|
"tp_getattro": "PyObject_GenericGetAttr",
|
|
"tp_flags": "Py_TPFLAGS_DEFAULT | Py_TPFLAGS_SEQUENCE",
|
|
"tp_doc": "range_doc",
|
|
"tp_richcompare": "range_richcompare",
|
|
"tp_iter": "range_iter",
|
|
"tp_methods": "range_methods",
|
|
"tp_members": "range_members",
|
|
"tp_new": "range_new",
|
|
"tp_vectorcall": "(vectorcallfunc)range_vectorcall",
|
|
}
|
|
assert expected.keys() <= set(fields)
|
|
# Use the real field declarations/order, with minimal stand-ins for dependencies.
|
|
harness = r'''
|
|
#include <assert.h>
|
|
#include <stddef.h>
|
|
#include <string.h>
|
|
typedef long Py_ssize_t;
|
|
typedef struct { int unused; } PyObject;
|
|
typedef struct { int unused; } PyAsyncMethods;
|
|
typedef struct { int unused; } PyNumberMethods;
|
|
typedef struct { int unused; } PySequenceMethods;
|
|
typedef struct { int unused; } PyMappingMethods;
|
|
typedef struct { int unused; } PyBufferProcs;
|
|
typedef struct { int unused; } PyMethodDef;
|
|
typedef struct { int unused; } PyMemberDef;
|
|
typedef struct { int unused; } PyGetSetDef;
|
|
typedef struct _typeobject PyTypeObject;
|
|
typedef struct { PyObject *start, *stop, *step, *length; } rangeobject;
|
|
#define PyObject_VAR_HEAD long ob_refcnt; PyTypeObject *ob_type; long ob_size;
|
|
#define PyVarObject_HEAD_INIT(type, size) 1, type, size,
|
|
#define Py_TPFLAGS_DEFAULT 1UL
|
|
#define Py_TPFLAGS_SEQUENCE 32UL
|
|
'''
|
|
for name in (
|
|
"destructor getattrfunc setattrfunc reprfunc hashfunc ternaryfunc "
|
|
"getattrofunc setattrofunc traverseproc inquiry richcmpfunc getiterfunc "
|
|
"iternextfunc descrgetfunc descrsetfunc initproc allocfunc newfunc "
|
|
"freefunc vectorcallfunc"
|
|
).split():
|
|
harness += f"typedef void (*{name})(void);\n"
|
|
harness += "struct _typeobject {" + body + "};\n"
|
|
for name in (
|
|
"range_new range_dealloc range_repr range_hash PyObject_GenericGetAttr "
|
|
"range_richcompare range_iter range_vectorcall"
|
|
).split():
|
|
harness += f"static void {name}(void) {{ }}\n"
|
|
harness += "static PyTypeObject PyType_Type;\n"
|
|
harness += "static PyNumberMethods range_as_number;\n"
|
|
harness += "static PySequenceMethods range_as_sequence;\n"
|
|
harness += "static PyMappingMethods range_as_mapping;\n"
|
|
harness += "static PyMethodDef range_methods[1];\n"
|
|
harness += "static PyMemberDef range_members[1];\n"
|
|
harness += 'static const char range_doc[] = "range documentation";\n'
|
|
harness += match.group(0) + "\nint main(void)\n{\n"
|
|
harness += " assert(PyRange_Type.ob_refcnt == 1);\n"
|
|
harness += " assert(PyRange_Type.ob_type == &PyType_Type);\n"
|
|
harness += " assert(PyRange_Type.ob_size == 0);\n"
|
|
for field in fields:
|
|
value = expected.get(field, "0")
|
|
if field in ("tp_name", "tp_doc"):
|
|
harness += f" assert(strcmp(PyRange_Type.{field}, {value}) == 0);\n"
|
|
else:
|
|
harness += f" assert(PyRange_Type.{field} == ({value}));\n"
|
|
harness += " return 0;\n}\n"
|
|
with tempfile.TemporaryDirectory(prefix="vc6-rangeobject-vectorcall-") as directory:
|
|
path = Path(directory)
|
|
(path / "test.c").write_text(harness)
|
|
result = subprocess.run([
|
|
"cc", "-std=c89", "-pedantic-errors", "-Wall", "-Wextra", "-Werror",
|
|
str(path / "test.c"), "-o", str(path / "test"),
|
|
])
|
|
if result.returncode:
|
|
sys.exit(result.returncode)
|
|
subprocess.run([str(path / "test")], check=True)
|
|
print("PyRange_Type C89 compile and all 48 semantic field mappings: PASS")
|