Issue #17162: Add PyType_GetSlot.
authorMartin v. Löwis <martin@v.loewis.de>
Tue, 4 Feb 2014 08:33:05 +0000 (09:33 +0100)
committerMartin v. Löwis <martin@v.loewis.de>
Tue, 4 Feb 2014 08:33:05 +0000 (09:33 +0100)
Doc/c-api/type.rst
Include/object.h
Misc/NEWS
Modules/xxlimited.c
Objects/typeobject.c
setup.py

index 5d832541f20c1b62336386d07b8deaa9eed8108c..9e2b9399a5606b1b0c8714206035c58a8a51ab98 100644 (file)
@@ -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
index 05bffc9804c6c0240a841a46c927868f3df97c44..68ca7b446aae2f9f4d1f73632d2db254daa0f4cc 100644 (file)
@@ -439,6 +439,9 @@ PyAPI_FUNC(PyObject*) PyType_FromSpec(PyType_Spec*);
 #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 */
index ff73d0ba40c945013a9efb6d3fb8b5990815264b..993836756707748eb54e4315f8e11f7ffcfd145f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@ Release date: 2014-02-09
 Core and Builtins
 -----------------
 
+- Issue #17162: Add PyType_GetSlot.
+
 - Issue #20162: Fix an alignment issue in the siphash24() hash function which
   caused a crash on PowerPC 64-bit (ppc64).
 
index 661b6e294af363d1b2c7ac8e49c9ba141f7c53e0..eecdab96975d28d4e8e2aa815a24fb5844035e14 100644 (file)
@@ -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 *
index cbbb58a949a5933edd78ecf29c59e80ffede1548..349a6fd0a44beb4b0772791b5ff57b094e9b4fc8 100644 (file)
@@ -2641,6 +2641,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! */
index 448d605faffe99a8079e56b9ca8091bd72e316fe..8269e1caba44e443fe37aa1dc0fcf6dc3cbf14fe 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -1539,7 +1539,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