@@ -820,25 +820,42 @@ static int init_celltype(PyObject * mod)
820820
821821 ******************************************************/
822822
823- #define functuplefmt "OOOOOO"
823+ #define functuplefmt_pre38 "OOOOOO"
824+ #define functuplefmt functuplefmt_pre38 "OOOOO"
824825
825826static PyTypeObject wrap_PyFunction_Type ;
826827
827828static PyObject *
828829func_reduce (PyFunctionObject * func , PyObject * unused )
829830{
831+ /* See funcobject.c: some attribute can't be NULL. */
832+ assert (func -> func_code );
833+ assert (func -> func_globals );
834+ assert (func -> func_name );
835+ assert (func -> func_doc );
836+ assert (func -> func_qualname );
837+
838+ PyObject * dict = PyObject_GenericGetDict ((PyObject * )func , NULL );
839+ if (NULL == dict )
840+ return NULL ;
841+
830842 PyObject * tup = Py_BuildValue (
831843 "(O()(" functuplefmt "))" ,
832844 & wrap_PyFunction_Type ,
833845 /* Standard function constructor arguments. */
834- func -> func_code != NULL ? func -> func_code : Py_None ,
835- func -> func_globals != NULL ? func -> func_globals : Py_None ,
836- func -> func_name != NULL ? func -> func_name : Py_None ,
846+ func -> func_code ,
847+ func -> func_globals ,
848+ func -> func_name ,
837849 func -> func_defaults != NULL ? func -> func_defaults : Py_None ,
838850 func -> func_closure != NULL ? func -> func_closure : Py_None ,
839- /* Additional data we need to preserve. */
840- func -> func_module != NULL ? func -> func_module : Py_None
851+ func -> func_module != NULL ? func -> func_module : Py_None ,
852+ func -> func_kwdefaults != NULL ? func -> func_kwdefaults : Py_None ,
853+ func -> func_doc ,
854+ dict ,
855+ func -> func_annotations != NULL ? func -> func_annotations : Py_None ,
856+ func -> func_qualname
841857 );
858+ Py_DECREF (dict );
842859 return tup ;
843860}
844861
@@ -893,25 +910,58 @@ func_setstate(PyObject *self, PyObject *args)
893910 if (args2 == NULL )
894911 return NULL ;
895912
896- fu = (PyFunctionObject * )
897- Py_TYPE (self )-> tp_new (Py_TYPE (self ), args2 , NULL );
913+ fu = (PyFunctionObject * ) Py_TYPE (self )-> tp_new (Py_TYPE (self ), args2 , NULL );
898914 Py_DECREF (args2 );
899- if (fu != NULL ) {
900- PyFunctionObject * target = (PyFunctionObject * ) self ;
901- COPY (fu , target , func_code );
902- COPY (fu , target , func_globals );
903- COPY (fu , target , func_name );
904- COPY (fu , target , func_defaults );
905- COPY (fu , target , func_closure );
915+ if (fu == NULL )
916+ return NULL ;
917+
918+ PyFunctionObject * target = (PyFunctionObject * ) self ;
919+ COPY (fu , target , func_code );
920+ COPY (fu , target , func_globals );
921+ COPY (fu , target , func_name );
922+ COPY (fu , target , func_defaults );
923+ COPY (fu , target , func_closure );
924+ Py_DECREF (fu );
925+
926+ args2 = PyTuple_GetItem (args , 5 );
927+ if (NULL == args2 )
928+ return NULL ;
929+ Py_INCREF (args2 );
930+ Py_XSETREF (target -> func_module , args2 );
931+
932+ if (PyTuple_GET_SIZE (args ) != sizeof (functuplefmt_pre38 )- 1 ) {
933+ /* Stackless 3.8 and up */
934+ if (PyTuple_GET_SIZE (args ) != sizeof (functuplefmt )- 1 ) {
935+ PyErr_Format (PyExc_IndexError , "function.__setstate__ expects a tuple of length %d" , (int )sizeof (functuplefmt )- 1 );
936+ return NULL ;
937+ }
938+ args2 = PyTuple_GET_ITEM (args , 6 );
939+ if (PyFunction_SetKwDefaults (self , args2 ))
940+ return NULL ;
906941
907- Py_XINCREF (PyTuple_GetItem (args , 5 ));
908- target -> func_module = PyTuple_GetItem (args , 5 );
942+ args2 = PyTuple_GET_ITEM (args , 7 );
943+ Py_INCREF (args2 );
944+ Py_XSETREF (target -> func_doc , args2 );
909945
910- Py_DECREF (fu );
911- Py_INCREF (self );
912- return self ;
946+ args2 = PyTuple_GET_ITEM (args , 8 );
947+ if (args2 != Py_None && PyObject_GenericSetDict (self , args2 , NULL ))
948+ return NULL ;
949+
950+ args2 = PyTuple_GET_ITEM (args , 9 );
951+ if (PyFunction_SetAnnotations (self , args2 ))
952+ return NULL ;
953+
954+ args2 = PyTuple_GET_ITEM (args , 10 );
955+ if (!PyUnicode_Check (args2 )) {
956+ PyErr_SetString (PyExc_TypeError , "__qualname__ must be set to a string object" );
957+ return NULL ;
958+ }
959+ Py_INCREF (args2 );
960+ Py_XSETREF (target -> func_qualname , args2 );
913961 }
914- return NULL ;
962+
963+ Py_INCREF (self );
964+ return self ;
915965}
916966
917967#undef COPY
0 commit comments