@@ -682,6 +682,53 @@ ImportError_str(PyImportErrorObject *self)
682682 }
683683}
684684
685+ static PyObject *
686+ ImportError_getstate (PyImportErrorObject * self )
687+ {
688+ PyObject * dict = ((PyBaseExceptionObject * )self )-> dict ;
689+ if (self -> name || self -> path ) {
690+ _Py_IDENTIFIER (name );
691+ _Py_IDENTIFIER (path );
692+ dict = dict ? PyDict_Copy (dict ) : PyDict_New ();
693+ if (dict == NULL )
694+ return NULL ;
695+ if (self -> name && _PyDict_SetItemId (dict , & PyId_name , self -> name ) < 0 ) {
696+ Py_DECREF (dict );
697+ return NULL ;
698+ }
699+ if (self -> path && _PyDict_SetItemId (dict , & PyId_path , self -> path ) < 0 ) {
700+ Py_DECREF (dict );
701+ return NULL ;
702+ }
703+ return dict ;
704+ }
705+ else if (dict ) {
706+ Py_INCREF (dict );
707+ return dict ;
708+ }
709+ else {
710+ Py_RETURN_NONE ;
711+ }
712+ }
713+
714+ /* Pickling support */
715+ static PyObject *
716+ ImportError_reduce (PyImportErrorObject * self )
717+ {
718+ PyObject * res ;
719+ PyObject * args ;
720+ PyObject * state = ImportError_getstate (self );
721+ if (state == NULL )
722+ return NULL ;
723+ args = ((PyBaseExceptionObject * )self )-> args ;
724+ if (state == Py_None )
725+ res = PyTuple_Pack (2 , Py_TYPE (self ), args );
726+ else
727+ res = PyTuple_Pack (3 , Py_TYPE (self ), args , state );
728+ Py_DECREF (state );
729+ return res ;
730+ }
731+
685732static PyMemberDef ImportError_members [] = {
686733 {"msg" , T_OBJECT , offsetof(PyImportErrorObject , msg ), 0 ,
687734 PyDoc_STR ("exception message" )},
@@ -693,6 +740,7 @@ static PyMemberDef ImportError_members[] = {
693740};
694741
695742static PyMethodDef ImportError_methods [] = {
743+ {"__reduce__" , (PyCFunction )ImportError_reduce , METH_NOARGS },
696744 {NULL }
697745};
698746
0 commit comments