Files
Python3-Win9x/scripts/vc6-picklebuf-type-c89.test.py

91 lines
3.9 KiB
Python

#!/usr/bin/env python3
"""Compile the exact PickleBuffer 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 PyPickleBuffer_Type = \{.*?\n\};", source, re.S)
assert match, "PyPickleBuffer_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": '"pickle.PickleBuffer"',
"tp_basicsize": "sizeof(PyPickleBufferObject)",
"tp_dealloc": "(destructor)picklebuf_dealloc",
"tp_as_buffer": "&picklebuf_as_buffer",
"tp_flags": "Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC",
"tp_doc": '"Wrapper for potentially out-of-band buffers"',
"tp_traverse": "(traverseproc)picklebuf_traverse",
"tp_clear": "(inquiry)picklebuf_clear",
"tp_weaklistoffset": "offsetof(PyPickleBufferObject, weakreflist)",
"tp_methods": "picklebuf_methods",
"tp_new": "picklebuf_new",
}
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 { int view; PyObject *weakreflist; } PyPickleBufferObject;
#define PyObject_VAR_HEAD long ob_refcnt; PyTypeObject *ob_type; long ob_size;
#define PyVarObject_HEAD_INIT(type, size) 1, type, size,
#define PyDoc_STR(value) value
#define Py_TPFLAGS_DEFAULT 1UL
#define Py_TPFLAGS_HAVE_GC 16384UL
'''
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 ("new", "dealloc", "traverse", "clear"):
harness += f"static void picklebuf_{name}(void) {{ }}\n"
harness += "static PyBufferProcs picklebuf_as_buffer;\n"
harness += "static PyMethodDef picklebuf_methods[1];\n"
harness += match.group(0) + "\nint main(void)\n{\n"
harness += " assert(PyPickleBuffer_Type.ob_refcnt == 1);\n"
harness += " assert(PyPickleBuffer_Type.ob_type == NULL);\n"
harness += " assert(PyPickleBuffer_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(PyPickleBuffer_Type.{field}, {value}) == 0);\n"
else:
harness += f" assert(PyPickleBuffer_Type.{field} == ({value}));\n"
harness += " return 0;\n}\n"
with tempfile.TemporaryDirectory(prefix="vc6-picklebuf-type-") 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("PyPickleBuffer_Type C89 compile and all 48 semantic field mappings: PASS")