@@ -1021,6 +1021,36 @@ type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
10211021 return obj ;
10221022}
10231023
1024+ PyObject *
1025+ PyType_FromSpec_Alloc (PyTypeObject * type , Py_ssize_t nitems )
1026+ {
1027+ PyObject * obj ;
1028+ const size_t size = _PyObject_VAR_SIZE (type , nitems + 1 ) + sizeof (traverseproc );
1029+ /* note that we need to add one, for the sentinel and space for the
1030+ provided tp-traverse: See bpo-40217 for more details */
1031+
1032+ if (PyType_IS_GC (type ))
1033+ obj = _PyObject_GC_Malloc (size );
1034+ else
1035+ obj = (PyObject * )PyObject_MALLOC (size );
1036+
1037+ if (obj == NULL )
1038+ return PyErr_NoMemory ();
1039+
1040+ obj = obj ;
1041+
1042+ memset (obj , '\0' , size );
1043+
1044+ if (type -> tp_itemsize == 0 )
1045+ (void )PyObject_INIT (obj , type );
1046+ else
1047+ (void ) PyObject_INIT_VAR ((PyVarObject * )obj , type , nitems );
1048+
1049+ if (PyType_IS_GC (type ))
1050+ _PyObject_GC_TRACK (obj );
1051+ return obj ;
1052+ }
1053+
10241054PyObject *
10251055PyType_GenericAlloc (PyTypeObject * type , Py_ssize_t nitems )
10261056{
@@ -2846,6 +2876,36 @@ static const short slotoffsets[] = {
28462876#include "typeslots.inc"
28472877};
28482878
2879+ static int
2880+ PyType_FromSpec_tp_traverse (PyObject * self , visitproc visit , void * arg )
2881+ {
2882+ PyTypeObject * parent = Py_TYPE (self );
2883+
2884+ // Only a instance of a type that is directly created by
2885+ // PyType_FromSpec (not subclasses) must visit its parent.
2886+ if (parent -> tp_traverse == PyType_FromSpec_tp_traverse ) {
2887+ Py_VISIT (parent );
2888+ }
2889+
2890+ // Search for the original type that was created using PyType_FromSpec
2891+ PyTypeObject * base ;
2892+ base = parent ;
2893+ while (base -> tp_traverse != PyType_FromSpec_tp_traverse ) {
2894+ base = base -> tp_base ;
2895+ assert (base );
2896+ }
2897+
2898+ // Extract the user defined traverse function that we placed at the end
2899+ // of the type and call it.
2900+ size_t size = Py_SIZE (base );
2901+ size_t _offset = _PyObject_VAR_SIZE (& PyType_Type , size + 1 );
2902+ traverseproc fun = * (traverseproc * )((char * )base + _offset );
2903+ if (fun == NULL ) {
2904+ return 0 ;
2905+ }
2906+ return fun (self , visit , arg );
2907+ }
2908+
28492909PyObject *
28502910PyType_FromSpecWithBases (PyType_Spec * spec , PyObject * bases )
28512911{
@@ -2880,7 +2940,7 @@ PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
28802940 }
28812941 }
28822942
2883- res = (PyHeapTypeObject * )PyType_GenericAlloc (& PyType_Type , nmembers );
2943+ res = (PyHeapTypeObject * )PyType_FromSpec_Alloc (& PyType_Type , nmembers );
28842944 if (res == NULL )
28852945 return NULL ;
28862946 res_start = (char * )res ;
@@ -2985,6 +3045,26 @@ PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
29853045 memcpy (PyHeapType_GET_MEMBERS (res ), slot -> pfunc , len );
29863046 type -> tp_members = PyHeapType_GET_MEMBERS (res );
29873047 }
3048+ else if (slot -> slot == Py_tp_traverse ) {
3049+
3050+ /* Types created by PyType_FromSpec own a strong reference to their
3051+ * type, but this was added in Python 3.8. The tp_traverse function
3052+ * needs to call Py_VISIT on the type but all existing traverse
3053+ * functions cannot be updated (especially the ones from existing user
3054+ * functions) so we need to provide a tp_traverse that manually calls
3055+ * Py_VISIT(Py_TYPE(self)) and then call the provided tp_traverse. In
3056+ * this way, user functions do not need to be updated, preserve
3057+ * backwards compatibility.
3058+ *
3059+ * We store the user-provided traverse function at the end of the type
3060+ * (we have allocated space for it) so we can call it from our
3061+ * PyType_FromSpec_tp_traverse wrapper. */
3062+
3063+ type -> tp_traverse = PyType_FromSpec_tp_traverse ;
3064+ size_t _offset = _PyObject_VAR_SIZE (& PyType_Type , nmembers + 1 );
3065+ traverseproc * user_traverse = (traverseproc * )((char * )type + _offset );
3066+ * user_traverse = slot -> pfunc ;
3067+ }
29883068 else {
29893069 /* Copy other slots directly */
29903070 * (void * * )(res_start + slotoffsets [slot -> slot ]) = slot -> pfunc ;
0 commit comments