@@ -605,9 +605,104 @@ SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
605605/*
606606 * ImportError extends Exception
607607 */
608- SimpleExtendsException (PyExc_Exception , ImportError ,
609- "Import can't find module, or can't find name in module." );
610608
609+ static int
610+ ImportError_init (PyImportErrorObject * self , PyObject * args , PyObject * kwds )
611+ {
612+ PyObject * msg = NULL ;
613+ PyObject * name = NULL ;
614+ PyObject * path = NULL ;
615+
616+ /* Macro replacement doesn't allow ## to start the first line of a macro,
617+ so we move the assignment and NULL check into the if-statement. */
618+ #define GET_KWD (kwd ) { \
619+ kwd = PyDict_GetItemString(kwds, #kwd); \
620+ if (kwd) { \
621+ Py_CLEAR(self->kwd); \
622+ self->kwd = kwd; \
623+ Py_INCREF(self->kwd);\
624+ if (PyDict_DelItemString(kwds, #kwd)) \
625+ return -1; \
626+ } \
627+ }
628+
629+ if (kwds ) {
630+ GET_KWD (name );
631+ GET_KWD (path );
632+ }
633+
634+ if (BaseException_init ((PyBaseExceptionObject * )self , args , kwds ) == -1 )
635+ return -1 ;
636+ if (PyTuple_GET_SIZE (args ) != 1 )
637+ return 0 ;
638+ if (!PyArg_UnpackTuple (args , "ImportError" , 1 , 1 , & msg ))
639+ return -1 ;
640+
641+ Py_CLEAR (self -> msg ); /* replacing */
642+ self -> msg = msg ;
643+ Py_INCREF (self -> msg );
644+
645+ return 0 ;
646+ }
647+
648+ static int
649+ ImportError_clear (PyImportErrorObject * self )
650+ {
651+ Py_CLEAR (self -> msg );
652+ Py_CLEAR (self -> name );
653+ Py_CLEAR (self -> path );
654+ return BaseException_clear ((PyBaseExceptionObject * )self );
655+ }
656+
657+ static void
658+ ImportError_dealloc (PyImportErrorObject * self )
659+ {
660+ _PyObject_GC_UNTRACK (self );
661+ ImportError_clear (self );
662+ Py_TYPE (self )-> tp_free ((PyObject * )self );
663+ }
664+
665+ static int
666+ ImportError_traverse (PyImportErrorObject * self , visitproc visit , void * arg )
667+ {
668+ Py_VISIT (self -> msg );
669+ Py_VISIT (self -> name );
670+ Py_VISIT (self -> path );
671+ return BaseException_traverse ((PyBaseExceptionObject * )self , visit , arg );
672+ }
673+
674+ static PyObject *
675+ ImportError_str (PyImportErrorObject * self )
676+ {
677+ if (self -> msg ) {
678+ Py_INCREF (self -> msg );
679+ return self -> msg ;
680+ }
681+ else {
682+ return BaseException_str ((PyBaseExceptionObject * )self );
683+ }
684+ }
685+
686+ static PyMemberDef ImportError_members [] = {
687+ {"msg" , T_OBJECT , offsetof(PyImportErrorObject , msg ), 0 ,
688+ PyDoc_STR ("exception message" )},
689+ {"name" , T_OBJECT , offsetof(PyImportErrorObject , name ), 0 ,
690+ PyDoc_STR ("module name" )},
691+ {"path" , T_OBJECT , offsetof(PyImportErrorObject , path ), 0 ,
692+ PyDoc_STR ("module path" )},
693+ {NULL } /* Sentinel */
694+ };
695+
696+ static PyMethodDef ImportError_methods [] = {
697+ {NULL }
698+ };
699+
700+ ComplexExtendsException (PyExc_Exception , ImportError ,
701+ ImportError , 0 /* new */ ,
702+ ImportError_methods , ImportError_members ,
703+ 0 /* getset */ , ImportError_str ,
704+ "Import can't find module, or can't find name in "
705+ "module." );
611706
612707/*
613708 * OSError extends Exception
0 commit comments