diff -r 15bad3abfac9 Doc/c-api/type.rst --- a/Doc/c-api/type.rst +++ b/Doc/c-api/type.rst @@ -97,3 +97,13 @@ Type Objects types. This allows the caller to reference other heap types as base types. .. versionadded:: 3.3 + +.. c:function:: void* PyType_GetSlot(PyTypeObject *type, int slot) + + Return the function pointer stored int the given slot. If the + result is *NULL*, this indicates that either the slot is *NULL*, + or that the function was called with invalid parameters. + Callers will typically cast the result pointer into the appropriate + function type. + + .. versionadded:: 3.4 diff -r 15bad3abfac9 Include/object.h --- a/Include/object.h +++ b/Include/object.h @@ -439,6 +439,9 @@ PyAPI_FUNC(PyObject*) PyType_FromSpec(Py #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000 PyAPI_FUNC(PyObject*) PyType_FromSpecWithBases(PyType_Spec*, PyObject*); #endif +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000 +PyAPI_FUNC(void*) PyType_GetSlot(PyTypeObject*, int); +#endif #ifndef Py_LIMITED_API /* The *real* layout of a type object when allocated on the heap */ diff -r 15bad3abfac9 Modules/xxlimited.c --- a/Modules/xxlimited.c +++ b/Modules/xxlimited.c @@ -44,7 +44,7 @@ static void Xxo_dealloc(XxoObject *self) { Py_XDECREF(self->x_attr); - PyObject_Del(self); + ((freefunc)PyType_GetSlot(Py_TYPE(self), Py_tp_free))(self); } static PyObject * diff -r 15bad3abfac9 Objects/typeobject.c --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -2562,6 +2562,19 @@ PyType_FromSpec(PyType_Spec *spec) return PyType_FromSpecWithBases(spec, NULL); } +void * +PyType_GetSlot(PyTypeObject *type, int slot) +{ + if (!PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE)) { + PyErr_BadInternalCall(); + return NULL; + } + if (slot >= Py_ARRAY_LENGTH(slotoffsets)) { + /* Extension module requesting slot from a future version */ + return NULL; + } + return *(void**)(((char*)type) + slotoffsets[slot]); +} /* Internal API to look for a name through the MRO. This returns a borrowed reference, and doesn't set an exception! */ diff -r 15bad3abfac9 setup.py --- a/setup.py +++ b/setup.py @@ -1535,7 +1535,7 @@ class PyBuildExt(build_ext): if 'd' not in sys.abiflags: ext = Extension('xxlimited', ['xxlimited.c'], - define_macros=[('Py_LIMITED_API', 1)]) + define_macros=[('Py_LIMITED_API', '0x03040000')]) self.extensions.append(ext) return missing