@@ -5912,118 +5912,75 @@ load_inst(UnpicklerObject *self)
5912
5912
return 0 ;
5913
5913
}
5914
5914
5915
- static int
5916
- load_newobj ( UnpicklerObject * self )
5915
+ static void
5916
+ newobj_unpickling_error ( const char * msg , int use_kwargs , PyObject * arg )
5917
5917
{
5918
- PyObject * args = NULL ;
5919
- PyObject * clsraw = NULL ;
5920
- PyTypeObject * cls ; /* clsraw cast to its true type */
5921
- PyObject * obj ;
5922
5918
PickleState * st = _Pickle_GetGlobalState ();
5923
-
5924
- /* Stack is ... cls argtuple, and we want to call
5925
- * cls.__new__(cls, *argtuple).
5926
- */
5927
- PDATA_POP (self -> stack , args );
5928
- if (args == NULL )
5929
- goto error ;
5930
- if (!PyTuple_Check (args )) {
5931
- PyErr_SetString (st -> UnpicklingError ,
5932
- "NEWOBJ expected an arg " "tuple." );
5933
- goto error ;
5934
- }
5935
-
5936
- PDATA_POP (self -> stack , clsraw );
5937
- cls = (PyTypeObject * )clsraw ;
5938
- if (cls == NULL )
5939
- goto error ;
5940
- if (!PyType_Check (cls )) {
5941
- PyErr_SetString (st -> UnpicklingError , "NEWOBJ class argument "
5942
- "isn't a type object" );
5943
- goto error ;
5944
- }
5945
- if (cls -> tp_new == NULL ) {
5946
- PyErr_SetString (st -> UnpicklingError , "NEWOBJ class argument "
5947
- "has NULL tp_new" );
5948
- goto error ;
5949
- }
5950
-
5951
- /* Call __new__. */
5952
- obj = cls -> tp_new (cls , args , NULL );
5953
- if (obj == NULL )
5954
- goto error ;
5955
-
5956
- Py_DECREF (args );
5957
- Py_DECREF (clsraw );
5958
- PDATA_PUSH (self -> stack , obj , -1 );
5959
- return 0 ;
5960
-
5961
- error :
5962
- Py_XDECREF (args );
5963
- Py_XDECREF (clsraw );
5964
- return -1 ;
5919
+ PyErr_Format (st -> UnpicklingError , msg ,
5920
+ use_kwargs ? "NEWOBJ_EX" : "NEWOBJ" ,
5921
+ Py_TYPE (arg )-> tp_name );
5965
5922
}
5966
5923
5967
5924
static int
5968
- load_newobj_ex (UnpicklerObject * self )
5925
+ load_newobj (UnpicklerObject * self , int use_kwargs )
5969
5926
{
5970
- PyObject * cls , * args , * kwargs ;
5927
+ PyObject * cls , * args , * kwargs = NULL ;
5971
5928
PyObject * obj ;
5972
- PickleState * st = _Pickle_GetGlobalState ();
5973
5929
5974
- PDATA_POP (self -> stack , kwargs );
5975
- if (kwargs == NULL ) {
5976
- return -1 ;
5930
+ /* Stack is ... cls args [kwargs], and we want to call
5931
+ * cls.__new__(cls, *args, **kwargs).
5932
+ */
5933
+ if (use_kwargs ) {
5934
+ PDATA_POP (self -> stack , kwargs );
5935
+ if (kwargs == NULL ) {
5936
+ return -1 ;
5937
+ }
5977
5938
}
5978
5939
PDATA_POP (self -> stack , args );
5979
5940
if (args == NULL ) {
5980
- Py_DECREF (kwargs );
5941
+ Py_XDECREF (kwargs );
5981
5942
return -1 ;
5982
5943
}
5983
5944
PDATA_POP (self -> stack , cls );
5984
5945
if (cls == NULL ) {
5985
- Py_DECREF (kwargs );
5946
+ Py_XDECREF (kwargs );
5986
5947
Py_DECREF (args );
5987
5948
return -1 ;
5988
5949
}
5989
5950
5990
5951
if (!PyType_Check (cls )) {
5991
- PyErr_Format (st -> UnpicklingError ,
5992
- "NEWOBJ_EX class argument must be a type, not %.200s" ,
5993
- Py_TYPE (cls )-> tp_name );
5952
+ newobj_unpickling_error ("%s class argument must be a type, not %.200s" ,
5953
+ use_kwargs , cls );
5994
5954
goto error ;
5995
5955
}
5996
-
5997
5956
if (((PyTypeObject * )cls )-> tp_new == NULL ) {
5998
- PyErr_SetString ( st -> UnpicklingError ,
5999
- "NEWOBJ_EX class argument doesn't have __new__" );
5957
+ newobj_unpickling_error ( "%s class argument '%.200s' doesn't have __new__" ,
5958
+ use_kwargs , cls );
6000
5959
goto error ;
6001
5960
}
6002
5961
if (!PyTuple_Check (args )) {
6003
- PyErr_Format (st -> UnpicklingError ,
6004
- "NEWOBJ_EX args argument must be a tuple, not %.200s" ,
6005
- Py_TYPE (args )-> tp_name );
5962
+ newobj_unpickling_error ("%s args argument must be a tuple, not %.200s" ,
5963
+ use_kwargs , args );
6006
5964
goto error ;
6007
5965
}
6008
- if (!PyDict_Check (kwargs )) {
6009
- PyErr_Format (st -> UnpicklingError ,
6010
- "NEWOBJ_EX kwargs argument must be a dict, not %.200s" ,
6011
- Py_TYPE (kwargs )-> tp_name );
5966
+ if (use_kwargs && !PyDict_Check (kwargs )) {
5967
+ newobj_unpickling_error ("%s kwargs argument must be a dict, not %.200s" ,
5968
+ use_kwargs , kwargs );
6012
5969
goto error ;
6013
5970
}
6014
5971
6015
5972
obj = ((PyTypeObject * )cls )-> tp_new ((PyTypeObject * )cls , args , kwargs );
6016
- Py_DECREF (kwargs );
6017
- Py_DECREF (args );
6018
- Py_DECREF (cls );
6019
5973
if (obj == NULL ) {
6020
- return -1 ;
5974
+ goto error ;
6021
5975
}
5976
+ Py_XDECREF (kwargs );
5977
+ Py_DECREF (args );
5978
+ Py_DECREF (cls );
6022
5979
PDATA_PUSH (self -> stack , obj , -1 );
6023
5980
return 0 ;
6024
5981
6025
5982
error :
6026
- Py_DECREF (kwargs );
5983
+ Py_XDECREF (kwargs );
6027
5984
Py_DECREF (args );
6028
5985
Py_DECREF (cls );
6029
5986
return -1 ;
@@ -6956,8 +6913,8 @@ load(UnpicklerObject *self)
6956
6913
OP (FROZENSET , load_frozenset )
6957
6914
OP (OBJ , load_obj )
6958
6915
OP (INST , load_inst )
6959
- OP (NEWOBJ , load_newobj )
6960
- OP (NEWOBJ_EX , load_newobj_ex )
6916
+ OP_ARG (NEWOBJ , load_newobj , 0 )
6917
+ OP_ARG (NEWOBJ_EX , load_newobj , 1 )
6961
6918
OP (GLOBAL , load_global )
6962
6919
OP (STACK_GLOBAL , load_stack_global )
6963
6920
OP (APPEND , load_append )
0 commit comments