Skip to content

Commit b1afcb8

Browse files
committed
Use self.exceptions in BaseExceptionGroup repr by supporting custom reprs in ComplexExtendsException
1 parent 7b0b708 commit b1afcb8

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

Objects/exceptions.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -694,12 +694,12 @@ PyTypeObject _PyExc_ ## EXCNAME = { \
694694

695695
#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCNEW, \
696696
EXCMETHODS, EXCMEMBERS, EXCGETSET, \
697-
EXCSTR, EXCDOC) \
697+
EXCSTR, EXCREPR, EXCDOC) \
698698
static PyTypeObject _PyExc_ ## EXCNAME = { \
699699
PyVarObject_HEAD_INIT(NULL, 0) \
700700
# EXCNAME, \
701701
sizeof(Py ## EXCSTORE ## Object), 0, \
702-
EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
702+
EXCSTORE ## _dealloc, 0, 0, 0, 0, EXCREPR, 0, 0, 0, 0, 0, \
703703
EXCSTR, 0, 0, 0, \
704704
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
705705
PyDoc_STR(EXCDOC), EXCSTORE ## _traverse, \
@@ -792,7 +792,7 @@ StopIteration_traverse(PyObject *op, visitproc visit, void *arg)
792792
}
793793

794794
ComplexExtendsException(PyExc_Exception, StopIteration, StopIteration,
795-
0, 0, StopIteration_members, 0, 0,
795+
0, 0, StopIteration_members, 0, 0, 0,
796796
"Signal the end from iterator.__next__().");
797797

798798

@@ -865,7 +865,7 @@ static PyMemberDef SystemExit_members[] = {
865865
};
866866

867867
ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
868-
0, 0, SystemExit_members, 0, 0,
868+
0, 0, SystemExit_members, 0, 0, 0,
869869
"Request to exit from the interpreter.");
870870

871871
/*
@@ -1063,6 +1063,19 @@ BaseExceptionGroup_str(PyObject *op)
10631063
self->msg, num_excs, num_excs > 1 ? "s" : "");
10641064
}
10651065

1066+
static PyObject *
1067+
BaseExceptionGroup_repr(PyObject *op)
1068+
{
1069+
PyBaseExceptionGroupObject *self = PyBaseExceptionGroupObject_CAST(op);
1070+
assert(self->msg);
1071+
assert(self->excs);
1072+
1073+
const char *name = _PyType_Name(Py_TYPE(self));
1074+
return PyUnicode_FromFormat(
1075+
"%s(%R, %R)", name,
1076+
self->msg, self->excs);
1077+
}
1078+
10661079
/*[clinic input]
10671080
@critical_section
10681081
BaseExceptionGroup.derive
@@ -1697,7 +1710,7 @@ static PyMethodDef BaseExceptionGroup_methods[] = {
16971710
ComplexExtendsException(PyExc_BaseException, BaseExceptionGroup,
16981711
BaseExceptionGroup, BaseExceptionGroup_new /* new */,
16991712
BaseExceptionGroup_methods, BaseExceptionGroup_members,
1700-
0 /* getset */, BaseExceptionGroup_str,
1713+
0 /* getset */, BaseExceptionGroup_str, BaseExceptionGroup_repr,
17011714
"A combination of multiple unrelated exceptions.");
17021715

17031716
/*
@@ -2425,7 +2438,7 @@ static PyGetSetDef OSError_getset[] = {
24252438
ComplexExtendsException(PyExc_Exception, OSError,
24262439
OSError, OSError_new,
24272440
OSError_methods, OSError_members, OSError_getset,
2428-
OSError_str,
2441+
OSError_str, 0,
24292442
"Base class for I/O related errors.");
24302443

24312444

@@ -2566,7 +2579,7 @@ static PyMethodDef NameError_methods[] = {
25662579
ComplexExtendsException(PyExc_Exception, NameError,
25672580
NameError, 0,
25682581
NameError_methods, NameError_members,
2569-
0, BaseException_str, "Name not found globally.");
2582+
0, BaseException_str, 0, "Name not found globally.");
25702583

25712584
/*
25722585
* UnboundLocalError extends NameError
@@ -2700,7 +2713,7 @@ static PyMethodDef AttributeError_methods[] = {
27002713
ComplexExtendsException(PyExc_Exception, AttributeError,
27012714
AttributeError, 0,
27022715
AttributeError_methods, AttributeError_members,
2703-
0, BaseException_str, "Attribute not found.");
2716+
0, BaseException_str, 0, "Attribute not found.");
27042717

27052718
/*
27062719
* SyntaxError extends Exception
@@ -2899,7 +2912,7 @@ static PyMemberDef SyntaxError_members[] = {
28992912

29002913
ComplexExtendsException(PyExc_Exception, SyntaxError, SyntaxError,
29012914
0, 0, SyntaxError_members, 0,
2902-
SyntaxError_str, "Invalid syntax.");
2915+
SyntaxError_str, 0, "Invalid syntax.");
29032916

29042917

29052918
/*
@@ -2959,7 +2972,7 @@ KeyError_str(PyObject *op)
29592972
}
29602973

29612974
ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
2962-
0, 0, 0, 0, KeyError_str, "Mapping key not found.");
2975+
0, 0, 0, 0, KeyError_str, 0, "Mapping key not found.");
29632976

29642977

29652978
/*

0 commit comments

Comments
 (0)