bpo-41073: PyType_GetSlot() can now accept static types. (GH-21931)

PyType_GetSlot() can now accept static types.

Co-Authored-By: Petr Viktorin <encukou@gmail.com>

Automerge-Triggered-By: GH:encukou
This commit is contained in:
Hai Shi 2020-11-11 04:53:46 +08:00 committed by GitHub
parent ace3f9a0ce
commit a13b26cac1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 205 additions and 101 deletions

View File

@ -105,10 +105,12 @@ Type Objects
See :c:member:`PyType_Slot.slot` for possible values of the *slot* argument. See :c:member:`PyType_Slot.slot` for possible values of the *slot* argument.
An exception is raised if *type* is not a heap type.
.. versionadded:: 3.4 .. versionadded:: 3.4
.. versionchanged:: 3.10
:c:func:`PyType_GetSlot` can now accept all types.
Previously, it was limited to heap types.
.. c:function:: PyObject* PyType_GetModule(PyTypeObject *type) .. c:function:: PyObject* PyType_GetModule(PyTypeObject *type)
Return the module object associated with the given type when the type was Return the module object associated with the given type when the type was

View File

@ -436,6 +436,9 @@ New Features
slot. slot.
(Contributed by Hai Shi in :issue:`41832`.) (Contributed by Hai Shi in :issue:`41832`.)
* The :c:func:`PyType_GetSlot` function can accept static types.
(Contributed by Hai Shi and Petr Viktorin in :issue:`41073`.)
Porting to Python 3.10 Porting to Python 3.10
---------------------- ----------------------

View File

@ -0,0 +1 @@
:c:func:`PyType_GetSlot()` can now accept static types.

View File

@ -1018,6 +1018,62 @@ test_buildvalue_N(PyObject *self, PyObject *Py_UNUSED(ignored))
} }
static PyObject *
test_get_statictype_slots(PyObject *self, PyObject *Py_UNUSED(ignored))
{
newfunc tp_new = PyType_GetSlot(&PyLong_Type, Py_tp_new);
if (PyLong_Type.tp_new != tp_new) {
PyErr_SetString(PyExc_AssertionError, "mismatch: tp_new of long");
return NULL;
}
reprfunc tp_repr = PyType_GetSlot(&PyLong_Type, Py_tp_repr);
if (PyLong_Type.tp_repr != tp_repr) {
PyErr_SetString(PyExc_AssertionError, "mismatch: tp_repr of long");
return NULL;
}
ternaryfunc tp_call = PyType_GetSlot(&PyLong_Type, Py_tp_call);
if (tp_call != NULL) {
PyErr_SetString(PyExc_AssertionError, "mismatch: tp_call of long");
return NULL;
}
binaryfunc nb_add = PyType_GetSlot(&PyLong_Type, Py_nb_add);
if (PyLong_Type.tp_as_number->nb_add != nb_add) {
PyErr_SetString(PyExc_AssertionError, "mismatch: nb_add of long");
return NULL;
}
lenfunc mp_length = PyType_GetSlot(&PyLong_Type, Py_mp_length);
if (mp_length != NULL) {
PyErr_SetString(PyExc_AssertionError, "mismatch: mp_length of long");
return NULL;
}
void *over_value = PyType_GetSlot(&PyLong_Type, Py_bf_releasebuffer + 1);
if (over_value != NULL) {
PyErr_SetString(PyExc_AssertionError, "mismatch: max+1 of long");
return NULL;
}
tp_new = PyType_GetSlot(&PyLong_Type, 0);
if (tp_new != NULL) {
PyErr_SetString(PyExc_AssertionError, "mismatch: slot 0 of long");
return NULL;
}
if (PyErr_ExceptionMatches(PyExc_SystemError)) {
// This is the right exception
PyErr_Clear();
}
else {
return NULL;
}
Py_RETURN_NONE;
}
static PyObject * static PyObject *
get_args(PyObject *self, PyObject *args) get_args(PyObject *self, PyObject *args)
{ {
@ -5627,8 +5683,10 @@ static PyMethodDef TestMethods[] = {
{"PyBuffer_SizeFromFormat", test_PyBuffer_SizeFromFormat, METH_VARARGS}, {"PyBuffer_SizeFromFormat", test_PyBuffer_SizeFromFormat, METH_VARARGS},
{"test_buildvalue_N", test_buildvalue_N, METH_NOARGS}, {"test_buildvalue_N", test_buildvalue_N, METH_NOARGS},
{"test_buildvalue_issue38913", test_buildvalue_issue38913, METH_NOARGS}, {"test_buildvalue_issue38913", test_buildvalue_issue38913, METH_NOARGS},
{"get_args", get_args, METH_VARARGS}, {"get_args", get_args, METH_VARARGS},
{"get_kwargs", (PyCFunction)(void(*)(void))get_kwargs, METH_VARARGS|METH_KEYWORDS}, {"test_get_statictype_slots", test_get_statictype_slots, METH_NOARGS},
{"get_kwargs", (PyCFunction)(void(*)(void))get_kwargs,
METH_VARARGS|METH_KEYWORDS},
{"getargs_tuple", getargs_tuple, METH_VARARGS}, {"getargs_tuple", getargs_tuple, METH_VARARGS},
{"getargs_keywords", (PyCFunction)(void(*)(void))getargs_keywords, {"getargs_keywords", (PyCFunction)(void(*)(void))getargs_keywords,
METH_VARARGS|METH_KEYWORDS}, METH_VARARGS|METH_KEYWORDS},

View File

@ -56,6 +56,11 @@ static struct method_cache_entry method_cache[1 << MCACHE_SIZE_EXP];
static unsigned int next_version_tag = 0; static unsigned int next_version_tag = 0;
#endif #endif
typedef struct PySlot_Offset {
short subslot_offset;
short slot_offset;
} PySlot_Offset;
#define MCACHE_STATS 0 #define MCACHE_STATS 0
#if MCACHE_STATS #if MCACHE_STATS
@ -2870,8 +2875,18 @@ error:
return NULL; return NULL;
} }
static const short slotoffsets[] = { /* An array of type slot offsets corresponding to Py_tp_* constants,
-1, /* invalid slot */ * for use in e.g. PyType_Spec and PyType_GetSlot.
* Each entry has two offsets: "slot_offset" and "subslot_offset".
* If is subslot_offset is -1, slot_offset is an offset within the
* PyTypeObject struct.
* Otherwise slot_offset is an offset to a pointer to a sub-slots struct
* (such as "tp_as_number"), and subslot_offset is the offset within
* that struct.
* The actual table is generated by a script.
*/
static const PySlot_Offset pyslot_offsets[] = {
{0, 0},
#include "typeslots.inc" #include "typeslots.inc"
}; };
@ -2892,6 +2907,7 @@ PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
const PyType_Slot *slot; const PyType_Slot *slot;
Py_ssize_t nmembers, weaklistoffset, dictoffset, vectorcalloffset; Py_ssize_t nmembers, weaklistoffset, dictoffset, vectorcalloffset;
char *res_start; char *res_start;
short slot_offset, subslot_offset;
nmembers = weaklistoffset = dictoffset = vectorcalloffset = 0; nmembers = weaklistoffset = dictoffset = vectorcalloffset = 0;
for (slot = spec->slots; slot->slot; slot++) { for (slot = spec->slots; slot->slot; slot++) {
@ -3001,7 +3017,7 @@ PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
for (slot = spec->slots; slot->slot; slot++) { for (slot = spec->slots; slot->slot; slot++) {
if (slot->slot < 0 if (slot->slot < 0
|| (size_t)slot->slot >= Py_ARRAY_LENGTH(slotoffsets)) { || (size_t)slot->slot >= Py_ARRAY_LENGTH(pyslot_offsets)) {
PyErr_SetString(PyExc_RuntimeError, "invalid slot offset"); PyErr_SetString(PyExc_RuntimeError, "invalid slot offset");
goto fail; goto fail;
} }
@ -3034,7 +3050,15 @@ PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
} }
else { else {
/* Copy other slots directly */ /* Copy other slots directly */
*(void**)(res_start + slotoffsets[slot->slot]) = slot->pfunc; PySlot_Offset slotoffsets = pyslot_offsets[slot->slot];
slot_offset = slotoffsets.slot_offset;
if (slotoffsets.subslot_offset == -1) {
*(void**)((char*)res_start + slot_offset) = slot->pfunc;
} else {
void *parent_slot = *(void**)((char*)res_start + slot_offset);
subslot_offset = slotoffsets.subslot_offset;
*(void**)((char*)parent_slot + subslot_offset) = slot->pfunc;
}
} }
} }
if (type->tp_dealloc == NULL) { if (type->tp_dealloc == NULL) {
@ -3117,15 +3141,23 @@ PyType_FromSpec(PyType_Spec *spec)
void * void *
PyType_GetSlot(PyTypeObject *type, int slot) PyType_GetSlot(PyTypeObject *type, int slot)
{ {
if (!_PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE) || slot < 0) { void *parent_slot;
int slots_len = Py_ARRAY_LENGTH(pyslot_offsets);
if (slot <= 0 || slot >= slots_len) {
PyErr_BadInternalCall(); PyErr_BadInternalCall();
return NULL; return NULL;
} }
if ((size_t)slot >= Py_ARRAY_LENGTH(slotoffsets)) {
/* Extension module requesting slot from a future version */ parent_slot = *(void**)((char*)type + pyslot_offsets[slot].slot_offset);
if (parent_slot == NULL) {
return NULL; return NULL;
} }
return *(void**)(((char*)type) + slotoffsets[slot]); /* Return slot directly if we have no sub slot. */
if (pyslot_offsets[slot].subslot_offset == -1) {
return parent_slot;
}
return *(void**)((char*)parent_slot + pyslot_offsets[slot].subslot_offset);
} }
PyObject * PyObject *

162
Objects/typeslots.inc generated
View File

@ -1,82 +1,82 @@
/* Generated by typeslots.py */ /* Generated by typeslots.py */
offsetof(PyHeapTypeObject, as_buffer.bf_getbuffer), {offsetof(PyBufferProcs, bf_getbuffer), offsetof(PyTypeObject, tp_as_buffer)},
offsetof(PyHeapTypeObject, as_buffer.bf_releasebuffer), {offsetof(PyBufferProcs, bf_releasebuffer), offsetof(PyTypeObject, tp_as_buffer)},
offsetof(PyHeapTypeObject, as_mapping.mp_ass_subscript), {offsetof(PyMappingMethods, mp_ass_subscript), offsetof(PyTypeObject, tp_as_mapping)},
offsetof(PyHeapTypeObject, as_mapping.mp_length), {offsetof(PyMappingMethods, mp_length), offsetof(PyTypeObject, tp_as_mapping)},
offsetof(PyHeapTypeObject, as_mapping.mp_subscript), {offsetof(PyMappingMethods, mp_subscript), offsetof(PyTypeObject, tp_as_mapping)},
offsetof(PyHeapTypeObject, as_number.nb_absolute), {offsetof(PyNumberMethods, nb_absolute), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_add), {offsetof(PyNumberMethods, nb_add), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_and), {offsetof(PyNumberMethods, nb_and), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_bool), {offsetof(PyNumberMethods, nb_bool), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_divmod), {offsetof(PyNumberMethods, nb_divmod), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_float), {offsetof(PyNumberMethods, nb_float), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_floor_divide), {offsetof(PyNumberMethods, nb_floor_divide), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_index), {offsetof(PyNumberMethods, nb_index), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_inplace_add), {offsetof(PyNumberMethods, nb_inplace_add), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_inplace_and), {offsetof(PyNumberMethods, nb_inplace_and), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_inplace_floor_divide), {offsetof(PyNumberMethods, nb_inplace_floor_divide), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_inplace_lshift), {offsetof(PyNumberMethods, nb_inplace_lshift), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_inplace_multiply), {offsetof(PyNumberMethods, nb_inplace_multiply), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_inplace_or), {offsetof(PyNumberMethods, nb_inplace_or), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_inplace_power), {offsetof(PyNumberMethods, nb_inplace_power), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_inplace_remainder), {offsetof(PyNumberMethods, nb_inplace_remainder), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_inplace_rshift), {offsetof(PyNumberMethods, nb_inplace_rshift), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_inplace_subtract), {offsetof(PyNumberMethods, nb_inplace_subtract), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_inplace_true_divide), {offsetof(PyNumberMethods, nb_inplace_true_divide), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_inplace_xor), {offsetof(PyNumberMethods, nb_inplace_xor), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_int), {offsetof(PyNumberMethods, nb_int), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_invert), {offsetof(PyNumberMethods, nb_invert), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_lshift), {offsetof(PyNumberMethods, nb_lshift), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_multiply), {offsetof(PyNumberMethods, nb_multiply), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_negative), {offsetof(PyNumberMethods, nb_negative), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_or), {offsetof(PyNumberMethods, nb_or), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_positive), {offsetof(PyNumberMethods, nb_positive), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_power), {offsetof(PyNumberMethods, nb_power), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_remainder), {offsetof(PyNumberMethods, nb_remainder), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_rshift), {offsetof(PyNumberMethods, nb_rshift), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_subtract), {offsetof(PyNumberMethods, nb_subtract), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_true_divide), {offsetof(PyNumberMethods, nb_true_divide), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_xor), {offsetof(PyNumberMethods, nb_xor), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_sequence.sq_ass_item), {offsetof(PySequenceMethods, sq_ass_item), offsetof(PyTypeObject, tp_as_sequence)},
offsetof(PyHeapTypeObject, as_sequence.sq_concat), {offsetof(PySequenceMethods, sq_concat), offsetof(PyTypeObject, tp_as_sequence)},
offsetof(PyHeapTypeObject, as_sequence.sq_contains), {offsetof(PySequenceMethods, sq_contains), offsetof(PyTypeObject, tp_as_sequence)},
offsetof(PyHeapTypeObject, as_sequence.sq_inplace_concat), {offsetof(PySequenceMethods, sq_inplace_concat), offsetof(PyTypeObject, tp_as_sequence)},
offsetof(PyHeapTypeObject, as_sequence.sq_inplace_repeat), {offsetof(PySequenceMethods, sq_inplace_repeat), offsetof(PyTypeObject, tp_as_sequence)},
offsetof(PyHeapTypeObject, as_sequence.sq_item), {offsetof(PySequenceMethods, sq_item), offsetof(PyTypeObject, tp_as_sequence)},
offsetof(PyHeapTypeObject, as_sequence.sq_length), {offsetof(PySequenceMethods, sq_length), offsetof(PyTypeObject, tp_as_sequence)},
offsetof(PyHeapTypeObject, as_sequence.sq_repeat), {offsetof(PySequenceMethods, sq_repeat), offsetof(PyTypeObject, tp_as_sequence)},
offsetof(PyHeapTypeObject, ht_type.tp_alloc), {-1, offsetof(PyTypeObject, tp_alloc)},
offsetof(PyHeapTypeObject, ht_type.tp_base), {-1, offsetof(PyTypeObject, tp_base)},
offsetof(PyHeapTypeObject, ht_type.tp_bases), {-1, offsetof(PyTypeObject, tp_bases)},
offsetof(PyHeapTypeObject, ht_type.tp_call), {-1, offsetof(PyTypeObject, tp_call)},
offsetof(PyHeapTypeObject, ht_type.tp_clear), {-1, offsetof(PyTypeObject, tp_clear)},
offsetof(PyHeapTypeObject, ht_type.tp_dealloc), {-1, offsetof(PyTypeObject, tp_dealloc)},
offsetof(PyHeapTypeObject, ht_type.tp_del), {-1, offsetof(PyTypeObject, tp_del)},
offsetof(PyHeapTypeObject, ht_type.tp_descr_get), {-1, offsetof(PyTypeObject, tp_descr_get)},
offsetof(PyHeapTypeObject, ht_type.tp_descr_set), {-1, offsetof(PyTypeObject, tp_descr_set)},
offsetof(PyHeapTypeObject, ht_type.tp_doc), {-1, offsetof(PyTypeObject, tp_doc)},
offsetof(PyHeapTypeObject, ht_type.tp_getattr), {-1, offsetof(PyTypeObject, tp_getattr)},
offsetof(PyHeapTypeObject, ht_type.tp_getattro), {-1, offsetof(PyTypeObject, tp_getattro)},
offsetof(PyHeapTypeObject, ht_type.tp_hash), {-1, offsetof(PyTypeObject, tp_hash)},
offsetof(PyHeapTypeObject, ht_type.tp_init), {-1, offsetof(PyTypeObject, tp_init)},
offsetof(PyHeapTypeObject, ht_type.tp_is_gc), {-1, offsetof(PyTypeObject, tp_is_gc)},
offsetof(PyHeapTypeObject, ht_type.tp_iter), {-1, offsetof(PyTypeObject, tp_iter)},
offsetof(PyHeapTypeObject, ht_type.tp_iternext), {-1, offsetof(PyTypeObject, tp_iternext)},
offsetof(PyHeapTypeObject, ht_type.tp_methods), {-1, offsetof(PyTypeObject, tp_methods)},
offsetof(PyHeapTypeObject, ht_type.tp_new), {-1, offsetof(PyTypeObject, tp_new)},
offsetof(PyHeapTypeObject, ht_type.tp_repr), {-1, offsetof(PyTypeObject, tp_repr)},
offsetof(PyHeapTypeObject, ht_type.tp_richcompare), {-1, offsetof(PyTypeObject, tp_richcompare)},
offsetof(PyHeapTypeObject, ht_type.tp_setattr), {-1, offsetof(PyTypeObject, tp_setattr)},
offsetof(PyHeapTypeObject, ht_type.tp_setattro), {-1, offsetof(PyTypeObject, tp_setattro)},
offsetof(PyHeapTypeObject, ht_type.tp_str), {-1, offsetof(PyTypeObject, tp_str)},
offsetof(PyHeapTypeObject, ht_type.tp_traverse), {-1, offsetof(PyTypeObject, tp_traverse)},
offsetof(PyHeapTypeObject, ht_type.tp_members), {-1, offsetof(PyTypeObject, tp_members)},
offsetof(PyHeapTypeObject, ht_type.tp_getset), {-1, offsetof(PyTypeObject, tp_getset)},
offsetof(PyHeapTypeObject, ht_type.tp_free), {-1, offsetof(PyTypeObject, tp_free)},
offsetof(PyHeapTypeObject, as_number.nb_matrix_multiply), {offsetof(PyNumberMethods, nb_matrix_multiply), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_number.nb_inplace_matrix_multiply), {offsetof(PyNumberMethods, nb_inplace_matrix_multiply), offsetof(PyTypeObject, tp_as_number)},
offsetof(PyHeapTypeObject, as_async.am_await), {offsetof(PyAsyncMethods, am_await), offsetof(PyTypeObject, tp_as_async)},
offsetof(PyHeapTypeObject, as_async.am_aiter), {offsetof(PyAsyncMethods, am_aiter), offsetof(PyTypeObject, tp_as_async)},
offsetof(PyHeapTypeObject, as_async.am_anext), {offsetof(PyAsyncMethods, am_anext), offsetof(PyTypeObject, tp_as_async)},
offsetof(PyHeapTypeObject, ht_type.tp_finalize), {-1, offsetof(PyTypeObject, tp_finalize)},
offsetof(PyHeapTypeObject, as_async.am_send), {offsetof(PyAsyncMethods, am_send), offsetof(PyTypeObject, tp_as_async)},

View File

@ -3,6 +3,7 @@
import sys, re import sys, re
def generate_typeslots(out=sys.stdout): def generate_typeslots(out=sys.stdout):
out.write("/* Generated by typeslots.py */\n") out.write("/* Generated by typeslots.py */\n")
res = {} res = {}
@ -10,27 +11,34 @@ def generate_typeslots(out=sys.stdout):
m = re.match("#define Py_([a-z_]+) ([0-9]+)", line) m = re.match("#define Py_([a-z_]+) ([0-9]+)", line)
if not m: if not m:
continue continue
member = m.group(1) member = m.group(1)
if member.startswith("tp_"): if member.startswith("tp_"):
member = "ht_type."+member member = f'{{-1, offsetof(PyTypeObject, {member})}}'
elif member.startswith("am_"): elif member.startswith("am_"):
member = "as_async."+member member = (f'{{offsetof(PyAsyncMethods, {member}),'+
' offsetof(PyTypeObject, tp_as_async)}')
elif member.startswith("nb_"): elif member.startswith("nb_"):
member = "as_number."+member member = (f'{{offsetof(PyNumberMethods, {member}),'+
' offsetof(PyTypeObject, tp_as_number)}')
elif member.startswith("mp_"): elif member.startswith("mp_"):
member = "as_mapping."+member member = (f'{{offsetof(PyMappingMethods, {member}),'+
' offsetof(PyTypeObject, tp_as_mapping)}')
elif member.startswith("sq_"): elif member.startswith("sq_"):
member = "as_sequence."+member member = (f'{{offsetof(PySequenceMethods, {member}),'+
' offsetof(PyTypeObject, tp_as_sequence)}')
elif member.startswith("bf_"): elif member.startswith("bf_"):
member = "as_buffer."+member member = (f'{{offsetof(PyBufferProcs, {member}),'+
' offsetof(PyTypeObject, tp_as_buffer)}')
res[int(m.group(2))] = member res[int(m.group(2))] = member
M = max(res.keys())+1 M = max(res.keys())+1
for i in range(1,M): for i in range(1,M):
if i in res: if i in res:
out.write("offsetof(PyHeapTypeObject, %s),\n" % res[i]) out.write("%s,\n" % res[i])
else: else:
out.write("0,\n") out.write("{0, 0},\n")
def main(): def main():
if len(sys.argv) == 2: if len(sys.argv) == 2: