@@ -121,7 +121,11 @@ BaseException_repr(PyBaseExceptionObject *self)
121
121
dot = (const char * ) strrchr (name , '.' );
122
122
if (dot != NULL ) name = dot + 1 ;
123
123
124
- return PyUnicode_FromFormat ("%s%R" , name , self -> args );
124
+ if (PyTuple_GET_SIZE (self -> args ) == 1 )
125
+ return PyUnicode_FromFormat ("%s(%R)" , name ,
126
+ PyTuple_GET_ITEM (self -> args , 0 ));
127
+ else
128
+ return PyUnicode_FromFormat ("%s%R" , name , self -> args );
125
129
}
126
130
127
131
/* Pickling support */
@@ -682,6 +686,30 @@ ImportError_str(PyImportErrorObject *self)
682
686
}
683
687
}
684
688
689
+ static PyObject *
690
+ ImportError_repr (PyImportErrorObject * self )
691
+ {
692
+ int hasargs = PyTuple_GET_SIZE (((PyBaseExceptionObject * )self )-> args ) != 0 ;
693
+ PyObject * r = BaseException_repr ((PyBaseExceptionObject * )self );
694
+ if (r && (self -> name || self -> path )) {
695
+ /* remove ')' */
696
+ Py_SETREF (r , PyUnicode_Substring (r , 0 , PyUnicode_GET_LENGTH (r ) - 1 ));
697
+ if (r && self -> name ) {
698
+ Py_SETREF (r , PyUnicode_FromFormat ("%U%sname=%R" ,
699
+ r , hasargs ? ", " : "" , self -> name ));
700
+ hasargs = 1 ;
701
+ }
702
+ if (r && self -> path ) {
703
+ Py_SETREF (r , PyUnicode_FromFormat ("%U%spath=%R" ,
704
+ r , hasargs ? ", " : "" , self -> path ));
705
+ }
706
+ if (r ) {
707
+ Py_SETREF (r , PyUnicode_FromFormat ("%U)" , r ));
708
+ }
709
+ }
710
+ return r ;
711
+ }
712
+
685
713
static PyMemberDef ImportError_members [] = {
686
714
{"msg" , T_OBJECT , offsetof(PyImportErrorObject , msg ), 0 ,
687
715
PyDoc_STR ("exception message" )},
@@ -696,12 +724,23 @@ static PyMethodDef ImportError_methods[] = {
696
724
{NULL }
697
725
};
698
726
699
- ComplexExtendsException (PyExc_Exception , ImportError ,
700
- ImportError , 0 /* new */ ,
701
- ImportError_methods , ImportError_members ,
702
- 0 /* getset */ , ImportError_str ,
703
- "Import can't find module, or can't find name in "
704
- "module." );
727
+ static PyTypeObject _PyExc_ImportError = {
728
+ PyVarObject_HEAD_INIT (NULL , 0 )
729
+ "ImportError" ,
730
+ sizeof (PyImportErrorObject ), 0 ,
731
+ (destructor )ImportError_dealloc , 0 , 0 , 0 , 0 ,
732
+ (reprfunc )ImportError_repr , 0 , 0 , 0 , 0 , 0 ,
733
+ (reprfunc )ImportError_str , 0 , 0 , 0 ,
734
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC ,
735
+ PyDoc_STR ("Import can't find module, or can't find name in "
736
+ "module." ),
737
+ (traverseproc )ImportError_traverse ,
738
+ (inquiry )ImportError_clear , 0 , 0 , 0 , 0 , ImportError_methods ,
739
+ ImportError_members , 0 , & _PyExc_Exception ,
740
+ 0 , 0 , 0 , offsetof(PyImportErrorObject , dict ),
741
+ (initproc )ImportError_init ,
742
+ };
743
+ PyObject * PyExc_ImportError = (PyObject * )& _PyExc_ImportError ;
705
744
706
745
/*
707
746
* ModuleNotFoundError extends ImportError
0 commit comments