@@ -686,6 +686,53 @@ ImportError_str(PyImportErrorObject *self)
686686 }
687687}
688688
689+ static PyObject *
690+ ImportError_getstate (PyImportErrorObject * self )
691+ {
692+ PyObject * dict = ((PyBaseExceptionObject * )self )-> dict ;
693+ if (self -> name || self -> path ) {
694+ _Py_IDENTIFIER (name );
695+ _Py_IDENTIFIER (path );
696+ dict = dict ? PyDict_Copy (dict ) : PyDict_New ();
697+ if (dict == NULL )
698+ return NULL ;
699+ if (self -> name && _PyDict_SetItemId (dict , & PyId_name , self -> name ) < 0 ) {
700+ Py_DECREF (dict );
701+ return NULL ;
702+ }
703+ if (self -> path && _PyDict_SetItemId (dict , & PyId_path , self -> path ) < 0 ) {
704+ Py_DECREF (dict );
705+ return NULL ;
706+ }
707+ return dict ;
708+ }
709+ else if (dict ) {
710+ Py_INCREF (dict );
711+ return dict ;
712+ }
713+ else {
714+ Py_RETURN_NONE ;
715+ }
716+ }
717+
718+ /* Pickling support */
719+ static PyObject *
720+ ImportError_reduce (PyImportErrorObject * self )
721+ {
722+ PyObject * res ;
723+ PyObject * args ;
724+ PyObject * state = ImportError_getstate (self );
725+ if (state == NULL )
726+ return NULL ;
727+ args = ((PyBaseExceptionObject * )self )-> args ;
728+ if (state == Py_None )
729+ res = PyTuple_Pack (2 , Py_TYPE (self ), args );
730+ else
731+ res = PyTuple_Pack (3 , Py_TYPE (self ), args , state );
732+ Py_DECREF (state );
733+ return res ;
734+ }
735+
689736static PyMemberDef ImportError_members [] = {
690737 {"msg" , T_OBJECT , offsetof(PyImportErrorObject , msg ), 0 ,
691738 PyDoc_STR ("exception message" )},
@@ -697,6 +744,7 @@ static PyMemberDef ImportError_members[] = {
697744};
698745
699746static PyMethodDef ImportError_methods [] = {
747+ {"__reduce__" , (PyCFunction )ImportError_reduce , METH_NOARGS },
700748 {NULL }
701749};
702750
0 commit comments