@@ -692,6 +692,53 @@ ImportError_str(PyImportErrorObject *self)
692692 }
693693}
694694
695+ static PyObject *
696+ ImportError_getstate (PyImportErrorObject * self )
697+ {
698+ PyObject * dict = ((PyBaseExceptionObject * )self )-> dict ;
699+ if (self -> name || self -> path ) {
700+ _Py_IDENTIFIER (name );
701+ _Py_IDENTIFIER (path );
702+ dict = dict ? PyDict_Copy (dict ) : PyDict_New ();
703+ if (dict == NULL )
704+ return NULL ;
705+ if (self -> name && _PyDict_SetItemId (dict , & PyId_name , self -> name ) < 0 ) {
706+ Py_DECREF (dict );
707+ return NULL ;
708+ }
709+ if (self -> path && _PyDict_SetItemId (dict , & PyId_path , self -> path ) < 0 ) {
710+ Py_DECREF (dict );
711+ return NULL ;
712+ }
713+ return dict ;
714+ }
715+ else if (dict ) {
716+ Py_INCREF (dict );
717+ return dict ;
718+ }
719+ else {
720+ Py_RETURN_NONE ;
721+ }
722+ }
723+
724+ /* Pickling support */
725+ static PyObject *
726+ ImportError_reduce (PyImportErrorObject * self )
727+ {
728+ PyObject * res ;
729+ PyObject * args ;
730+ PyObject * state = ImportError_getstate (self );
731+ if (state == NULL )
732+ return NULL ;
733+ args = ((PyBaseExceptionObject * )self )-> args ;
734+ if (state == Py_None )
735+ res = PyTuple_Pack (2 , Py_TYPE (self ), args );
736+ else
737+ res = PyTuple_Pack (3 , Py_TYPE (self ), args , state );
738+ Py_DECREF (state );
739+ return res ;
740+ }
741+
695742static PyMemberDef ImportError_members [] = {
696743 {"msg" , T_OBJECT , offsetof(PyImportErrorObject , msg ), 0 ,
697744 PyDoc_STR ("exception message" )},
@@ -703,6 +750,7 @@ static PyMemberDef ImportError_members[] = {
703750};
704751
705752static PyMethodDef ImportError_methods [] = {
753+ {"__reduce__" , (PyCFunction )ImportError_reduce , METH_NOARGS },
706754 {NULL }
707755};
708756
0 commit comments