@@ -37,6 +37,13 @@ get_exc_state(void)
37
37
* Lib/test/exception_hierarchy.txt
38
38
*/
39
39
40
+ static inline PyBaseExceptionObject *
41
+ _PyBaseExceptionObject_CAST (PyObject * exc )
42
+ {
43
+ assert (PyExceptionInstance_Check (exc ));
44
+ return (PyBaseExceptionObject * )exc ;
45
+ }
46
+
40
47
/*
41
48
* BaseException
42
49
*/
@@ -69,8 +76,9 @@ BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
69
76
}
70
77
71
78
static int
72
- BaseException_init (PyBaseExceptionObject * self , PyObject * args , PyObject * kwds )
79
+ BaseException_init (PyObject * op , PyObject * args , PyObject * kwds )
73
80
{
81
+ PyBaseExceptionObject * self = _PyBaseExceptionObject_CAST (op );
74
82
if (!_PyArg_NoKeywords (Py_TYPE (self )-> tp_name , kwds ))
75
83
return -1 ;
76
84
@@ -113,8 +121,9 @@ BaseException_vectorcall(PyObject *type_obj, PyObject * const*args,
113
121
114
122
115
123
static int
116
- BaseException_clear (PyBaseExceptionObject * self )
124
+ BaseException_clear (PyObject * op )
117
125
{
126
+ PyBaseExceptionObject * self = _PyBaseExceptionObject_CAST (op );
118
127
Py_CLEAR (self -> dict );
119
128
Py_CLEAR (self -> args );
120
129
Py_CLEAR (self -> notes );
@@ -125,21 +134,23 @@ BaseException_clear(PyBaseExceptionObject *self)
125
134
}
126
135
127
136
static void
128
- BaseException_dealloc (PyBaseExceptionObject * self )
137
+ BaseException_dealloc (PyObject * op )
129
138
{
139
+ PyBaseExceptionObject * self = _PyBaseExceptionObject_CAST (op );
130
140
PyObject_GC_UnTrack (self );
131
141
// bpo-44348: The trashcan mechanism prevents stack overflow when deleting
132
142
// long chains of exceptions. For example, exceptions can be chained
133
143
// through the __context__ attributes or the __traceback__ attribute.
134
144
Py_TRASHCAN_BEGIN (self , BaseException_dealloc )
135
- BaseException_clear (self );
136
- Py_TYPE (self )-> tp_free (( PyObject * ) self );
145
+ ( void ) BaseException_clear (op );
146
+ Py_TYPE (self )-> tp_free (self );
137
147
Py_TRASHCAN_END
138
148
}
139
149
140
150
static int
141
- BaseException_traverse (PyBaseExceptionObject * self , visitproc visit , void * arg )
151
+ BaseException_traverse (PyObject * op , visitproc visit , void * arg )
142
152
{
153
+ PyBaseExceptionObject * self = _PyBaseExceptionObject_CAST (op );
143
154
Py_VISIT (self -> dict );
144
155
Py_VISIT (self -> args );
145
156
Py_VISIT (self -> notes );
@@ -150,8 +161,9 @@ BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg)
150
161
}
151
162
152
163
static PyObject *
153
- BaseException_str (PyBaseExceptionObject * self )
164
+ BaseException_str (PyObject * op )
154
165
{
166
+ PyBaseExceptionObject * self = _PyBaseExceptionObject_CAST (op );
155
167
switch (PyTuple_GET_SIZE (self -> args )) {
156
168
case 0 :
157
169
return Py_GetConstant (Py_CONSTANT_EMPTY_STR );
@@ -163,8 +175,9 @@ BaseException_str(PyBaseExceptionObject *self)
163
175
}
164
176
165
177
static PyObject *
166
- BaseException_repr (PyBaseExceptionObject * self )
178
+ BaseException_repr (PyObject * op )
167
179
{
180
+ PyBaseExceptionObject * self = _PyBaseExceptionObject_CAST (op );
168
181
const char * name = _PyType_Name (Py_TYPE (self ));
169
182
if (PyTuple_GET_SIZE (self -> args ) == 1 )
170
183
return PyUnicode_FromFormat ("%s(%R)" , name ,
@@ -175,8 +188,9 @@ BaseException_repr(PyBaseExceptionObject *self)
175
188
176
189
/* Pickling support */
177
190
static PyObject *
178
- BaseException_reduce (PyBaseExceptionObject * self , PyObject * Py_UNUSED (ignored ))
191
+ BaseException_reduce (PyObject * op , PyObject * Py_UNUSED (ignored ))
179
192
{
193
+ PyBaseExceptionObject * self = _PyBaseExceptionObject_CAST (op );
180
194
if (self -> args && self -> dict )
181
195
return PyTuple_Pack (3 , Py_TYPE (self ), self -> args , self -> dict );
182
196
else
@@ -225,13 +239,6 @@ PyDoc_STRVAR(with_traceback_doc,
225
239
"Exception.with_traceback(tb) --\n\
226
240
set self.__traceback__ to tb and return self." );
227
241
228
- static inline PyBaseExceptionObject *
229
- _PyBaseExceptionObject_cast (PyObject * exc )
230
- {
231
- assert (PyExceptionInstance_Check (exc ));
232
- return (PyBaseExceptionObject * )exc ;
233
- }
234
-
235
242
static PyObject *
236
243
BaseException_add_note (PyObject * self , PyObject * note )
237
244
{
@@ -274,26 +281,26 @@ PyDoc_STRVAR(add_note_doc,
274
281
add a note to the exception" );
275
282
276
283
static PyMethodDef BaseException_methods [] = {
277
- {"__reduce__" , ( PyCFunction ) BaseException_reduce , METH_NOARGS },
278
- {"__setstate__" , ( PyCFunction ) BaseException_setstate , METH_O },
279
- {"with_traceback" , ( PyCFunction ) BaseException_with_traceback , METH_O ,
284
+ {"__reduce__" , BaseException_reduce , METH_NOARGS },
285
+ {"__setstate__" , BaseException_setstate , METH_O },
286
+ {"with_traceback" , BaseException_with_traceback , METH_O ,
280
287
with_traceback_doc },
281
- {"add_note" , (PyCFunction )BaseException_add_note , METH_O ,
282
- add_note_doc },
288
+ {"add_note" , BaseException_add_note , METH_O , add_note_doc },
283
289
{NULL , NULL , 0 , NULL },
284
290
};
285
291
286
292
static PyObject *
287
- BaseException_get_args (PyBaseExceptionObject * self , void * Py_UNUSED (ignored ))
293
+ BaseException_get_args (PyObject * op , void * Py_UNUSED (ignored ))
288
294
{
295
+ PyBaseExceptionObject * self = _PyBaseExceptionObject_CAST (op );
289
296
if (self -> args == NULL ) {
290
297
Py_RETURN_NONE ;
291
298
}
292
299
return Py_NewRef (self -> args );
293
300
}
294
301
295
302
static int
296
- BaseException_set_args (PyBaseExceptionObject * self , PyObject * val , void * Py_UNUSED (ignored ))
303
+ BaseException_set_args (PyObject * op , PyObject * val , void * Py_UNUSED (ignored ))
297
304
{
298
305
PyObject * seq ;
299
306
if (val == NULL ) {
@@ -303,22 +310,25 @@ BaseException_set_args(PyBaseExceptionObject *self, PyObject *val, void *Py_UNUS
303
310
seq = PySequence_Tuple (val );
304
311
if (!seq )
305
312
return -1 ;
313
+ PyBaseExceptionObject * self = _PyBaseExceptionObject_CAST (op );
306
314
Py_XSETREF (self -> args , seq );
307
315
return 0 ;
308
316
}
309
317
310
318
static PyObject *
311
- BaseException_get_tb (PyBaseExceptionObject * self , void * Py_UNUSED (ignored ))
319
+ BaseException_get_tb (PyObject * op , void * Py_UNUSED (ignored ))
312
320
{
321
+ PyBaseExceptionObject * self = _PyBaseExceptionObject_CAST (op );
313
322
if (self -> traceback == NULL ) {
314
323
Py_RETURN_NONE ;
315
324
}
316
325
return Py_NewRef (self -> traceback );
317
326
}
318
327
319
328
static int
320
- BaseException_set_tb (PyBaseExceptionObject * self , PyObject * tb , void * Py_UNUSED (ignored ))
329
+ BaseException_set_tb (PyObject * op , PyObject * tb , void * Py_UNUSED (ignored ))
321
330
{
331
+ PyBaseExceptionObject * self = _PyBaseExceptionObject_CAST (op );
322
332
if (tb == NULL ) {
323
333
PyErr_SetString (PyExc_TypeError , "__traceback__ may not be deleted" );
324
334
return -1 ;
@@ -398,8 +408,8 @@ BaseException_set_cause(PyObject *self, PyObject *arg, void *Py_UNUSED(ignored))
398
408
399
409
static PyGetSetDef BaseException_getset [] = {
400
410
{"__dict__" , PyObject_GenericGetDict , PyObject_GenericSetDict },
401
- {"args" , ( getter ) BaseException_get_args , ( setter ) BaseException_set_args },
402
- {"__traceback__" , ( getter ) BaseException_get_tb , ( setter ) BaseException_set_tb },
411
+ {"args" , BaseException_get_args , BaseException_set_args },
412
+ {"__traceback__" , BaseException_get_tb , BaseException_set_tb },
403
413
{"__context__" , BaseException_get_context ,
404
414
BaseException_set_context , PyDoc_STR ("exception context" )},
405
415
{"__cause__" , BaseException_get_cause ,
@@ -411,59 +421,61 @@ static PyGetSetDef BaseException_getset[] = {
411
421
PyObject *
412
422
PyException_GetTraceback (PyObject * self )
413
423
{
414
- PyBaseExceptionObject * base_self = _PyBaseExceptionObject_cast (self );
415
- return Py_XNewRef (base_self -> traceback );
424
+ PyBaseExceptionObject * exc = _PyBaseExceptionObject_CAST (self );
425
+ return Py_XNewRef (exc -> traceback );
416
426
}
417
427
418
428
419
429
int
420
430
PyException_SetTraceback (PyObject * self , PyObject * tb )
421
431
{
422
- return BaseException_set_tb (_PyBaseExceptionObject_cast ( self ) , tb , NULL );
432
+ return BaseException_set_tb (self , tb , NULL );
423
433
}
424
434
425
435
PyObject *
426
436
PyException_GetCause (PyObject * self )
427
437
{
428
- PyObject * cause = _PyBaseExceptionObject_cast (self )-> cause ;
429
- return Py_XNewRef (cause );
438
+ PyBaseExceptionObject * exc = _PyBaseExceptionObject_CAST (self );
439
+ return Py_XNewRef (exc -> cause );
430
440
}
431
441
432
442
/* Steals a reference to cause */
433
443
void
434
444
PyException_SetCause (PyObject * self , PyObject * cause )
435
445
{
436
- PyBaseExceptionObject * base_self = _PyBaseExceptionObject_cast (self );
437
- base_self -> suppress_context = 1 ;
438
- Py_XSETREF (base_self -> cause , cause );
446
+ PyBaseExceptionObject * exc = _PyBaseExceptionObject_CAST (self );
447
+ exc -> suppress_context = 1 ;
448
+ Py_XSETREF (exc -> cause , cause );
439
449
}
440
450
441
451
PyObject *
442
452
PyException_GetContext (PyObject * self )
443
453
{
444
- PyObject * context = _PyBaseExceptionObject_cast (self )-> context ;
445
- return Py_XNewRef (context );
454
+ PyBaseExceptionObject * exc = _PyBaseExceptionObject_CAST (self );
455
+ return Py_XNewRef (exc -> context );
446
456
}
447
457
448
458
/* Steals a reference to context */
449
459
void
450
460
PyException_SetContext (PyObject * self , PyObject * context )
451
461
{
452
- Py_XSETREF (_PyBaseExceptionObject_cast (self )-> context , context );
462
+ PyBaseExceptionObject * exc = _PyBaseExceptionObject_CAST (self );
463
+ Py_XSETREF (exc -> context , context );
453
464
}
454
465
455
466
PyObject *
456
467
PyException_GetArgs (PyObject * self )
457
468
{
458
- PyObject * args = _PyBaseExceptionObject_cast (self )-> args ;
459
- return Py_NewRef (args );
469
+ PyBaseExceptionObject * exc = _PyBaseExceptionObject_CAST (self );
470
+ return Py_NewRef (exc -> args );
460
471
}
461
472
462
473
void
463
474
PyException_SetArgs (PyObject * self , PyObject * args )
464
475
{
465
476
Py_INCREF (args );
466
- Py_XSETREF (_PyBaseExceptionObject_cast (self )-> args , args );
477
+ PyBaseExceptionObject * exc = _PyBaseExceptionObject_CAST (self );
478
+ Py_XSETREF (exc -> args , args );
467
479
}
468
480
469
481
const char *
@@ -485,26 +497,26 @@ static PyTypeObject _PyExc_BaseException = {
485
497
"BaseException" , /*tp_name*/
486
498
sizeof (PyBaseExceptionObject ), /*tp_basicsize*/
487
499
0 , /*tp_itemsize*/
488
- ( destructor ) BaseException_dealloc , /*tp_dealloc*/
500
+ BaseException_dealloc , /*tp_dealloc*/
489
501
0 , /*tp_vectorcall_offset*/
490
502
0 , /*tp_getattr*/
491
503
0 , /*tp_setattr*/
492
504
0 , /*tp_as_async*/
493
- ( reprfunc ) BaseException_repr , /*tp_repr*/
505
+ BaseException_repr , /*tp_repr*/
494
506
0 , /*tp_as_number*/
495
507
0 , /*tp_as_sequence*/
496
508
0 , /*tp_as_mapping*/
497
509
0 , /*tp_hash */
498
510
0 , /*tp_call*/
499
- ( reprfunc ) BaseException_str , /*tp_str*/
511
+ BaseException_str , /*tp_str*/
500
512
PyObject_GenericGetAttr , /*tp_getattro*/
501
513
PyObject_GenericSetAttr , /*tp_setattro*/
502
514
0 , /*tp_as_buffer*/
503
515
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
504
516
Py_TPFLAGS_BASE_EXC_SUBCLASS , /*tp_flags*/
505
517
PyDoc_STR ("Common base class for all exceptions" ), /* tp_doc */
506
- ( traverseproc ) BaseException_traverse , /* tp_traverse */
507
- ( inquiry ) BaseException_clear , /* tp_clear */
518
+ BaseException_traverse , /* tp_traverse */
519
+ BaseException_clear , /* tp_clear */
508
520
0 , /* tp_richcompare */
509
521
0 , /* tp_weaklistoffset */
510
522
0 , /* tp_iter */
@@ -517,7 +529,7 @@ static PyTypeObject _PyExc_BaseException = {
517
529
0 , /* tp_descr_get */
518
530
0 , /* tp_descr_set */
519
531
offsetof(PyBaseExceptionObject , dict ), /* tp_dictoffset */
520
- ( initproc ) BaseException_init , /* tp_init */
532
+ BaseException_init , /* tp_init */
521
533
0 , /* tp_alloc */
522
534
BaseException_new , /* tp_new */
523
535
.tp_vectorcall = BaseException_vectorcall ,
@@ -535,13 +547,13 @@ static PyTypeObject _PyExc_ ## EXCNAME = { \
535
547
PyVarObject_HEAD_INIT(NULL, 0) \
536
548
# EXCNAME, \
537
549
sizeof(PyBaseExceptionObject), \
538
- 0, (destructor) BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
550
+ 0, BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
539
551
0, 0, 0, 0, 0, 0, 0, \
540
552
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
541
- PyDoc_STR(EXCDOC), (traverseproc) BaseException_traverse, \
542
- (inquiry) BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
553
+ PyDoc_STR(EXCDOC), BaseException_traverse, \
554
+ BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
543
555
0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
544
- (initproc) BaseException_init, 0, BaseException_new,\
556
+ BaseException_init, 0, BaseException_new,\
545
557
}; \
546
558
PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
547
559
@@ -1378,7 +1390,7 @@ _PyExc_PrepReraiseStar(PyObject *orig, PyObject *excs)
1378
1390
{
1379
1391
/* orig must be a raised & caught exception, so it has a traceback */
1380
1392
assert (PyExceptionInstance_Check (orig ));
1381
- assert (_PyBaseExceptionObject_cast (orig )-> traceback != NULL );
1393
+ assert (_PyBaseExceptionObject_CAST (orig )-> traceback != NULL );
1382
1394
1383
1395
assert (PyList_Check (excs ));
1384
1396
0 commit comments