@@ -1021,6 +1021,36 @@ type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
1021
1021
return obj ;
1022
1022
}
1023
1023
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
+
1024
1054
PyObject *
1025
1055
PyType_GenericAlloc (PyTypeObject * type , Py_ssize_t nitems )
1026
1056
{
@@ -2846,6 +2876,36 @@ static const short slotoffsets[] = {
2846
2876
#include "typeslots.inc"
2847
2877
};
2848
2878
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
+
2849
2909
PyObject *
2850
2910
PyType_FromSpecWithBases (PyType_Spec * spec , PyObject * bases )
2851
2911
{
@@ -2880,7 +2940,7 @@ PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
2880
2940
}
2881
2941
}
2882
2942
2883
- res = (PyHeapTypeObject * )PyType_GenericAlloc (& PyType_Type , nmembers );
2943
+ res = (PyHeapTypeObject * )PyType_FromSpec_Alloc (& PyType_Type , nmembers );
2884
2944
if (res == NULL )
2885
2945
return NULL ;
2886
2946
res_start = (char * )res ;
@@ -2985,6 +3045,26 @@ PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
2985
3045
memcpy (PyHeapType_GET_MEMBERS (res ), slot -> pfunc , len );
2986
3046
type -> tp_members = PyHeapType_GET_MEMBERS (res );
2987
3047
}
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
+ }
2988
3068
else {
2989
3069
/* Copy other slots directly */
2990
3070
* (void * * )(res_start + slotoffsets [slot -> slot ]) = slot -> pfunc ;
0 commit comments