* Lib/test/exception_hierarchy.txt
*/
+static inline PyBaseExceptionObject *
+PyBaseExceptionObject_CAST(PyObject *exc)
+{
+ assert(PyExceptionInstance_Check(exc));
+ return (PyBaseExceptionObject *)exc;
+}
+
/*
* BaseException
*/
}
static int
-BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
+BaseException_init(PyObject *op, PyObject *args, PyObject *kwds)
{
+ PyBaseExceptionObject *self = PyBaseExceptionObject_CAST(op);
if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
return -1;
static int
-BaseException_clear(PyBaseExceptionObject *self)
+BaseException_clear(PyObject *op)
{
+ PyBaseExceptionObject *self = PyBaseExceptionObject_CAST(op);
Py_CLEAR(self->dict);
Py_CLEAR(self->args);
Py_CLEAR(self->notes);
}
static void
-BaseException_dealloc(PyBaseExceptionObject *self)
+BaseException_dealloc(PyObject *op)
{
+ PyBaseExceptionObject *self = PyBaseExceptionObject_CAST(op);
PyObject_GC_UnTrack(self);
// bpo-44348: The trashcan mechanism prevents stack overflow when deleting
// long chains of exceptions. For example, exceptions can be chained
// through the __context__ attributes or the __traceback__ attribute.
Py_TRASHCAN_BEGIN(self, BaseException_dealloc)
- BaseException_clear(self);
- Py_TYPE(self)->tp_free((PyObject *)self);
+ (void)BaseException_clear(op);
+ Py_TYPE(self)->tp_free(self);
Py_TRASHCAN_END
}
static int
-BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg)
+BaseException_traverse(PyObject *op, visitproc visit, void *arg)
{
+ PyBaseExceptionObject *self = PyBaseExceptionObject_CAST(op);
Py_VISIT(self->dict);
Py_VISIT(self->args);
Py_VISIT(self->notes);
}
static PyObject *
-BaseException_str(PyBaseExceptionObject *self)
+BaseException_str(PyObject *op)
{
+ PyBaseExceptionObject *self = PyBaseExceptionObject_CAST(op);
+
PyObject *res;
Py_BEGIN_CRITICAL_SECTION(self);
switch (PyTuple_GET_SIZE(self->args)) {
}
static PyObject *
-BaseException_repr(PyBaseExceptionObject *self)
+BaseException_repr(PyObject *op)
{
+
+ PyBaseExceptionObject *self = PyBaseExceptionObject_CAST(op);
+
PyObject *res;
Py_BEGIN_CRITICAL_SECTION(self);
const char *name = _PyType_Name(Py_TYPE(self));
return Py_NewRef(self);
}
-static inline PyBaseExceptionObject*
-_PyBaseExceptionObject_cast(PyObject *exc)
-{
- assert(PyExceptionInstance_Check(exc));
- return (PyBaseExceptionObject *)exc;
-}
-
/*[clinic input]
@critical_section
BaseException.add_note
{
PyObject *traceback;
Py_BEGIN_CRITICAL_SECTION(self);
- traceback = Py_XNewRef(_PyBaseExceptionObject_cast(self)->traceback);
+ traceback = Py_XNewRef(PyBaseExceptionObject_CAST(self)->traceback);
Py_END_CRITICAL_SECTION();
return traceback;
}
{
int res;
Py_BEGIN_CRITICAL_SECTION(self);
- res = BaseException___traceback___set_impl(_PyBaseExceptionObject_cast(self), tb);
+ res = BaseException___traceback___set_impl(PyBaseExceptionObject_CAST(self), tb);
Py_END_CRITICAL_SECTION();
return res;
}
{
PyObject *cause;
Py_BEGIN_CRITICAL_SECTION(self);
- cause = Py_XNewRef(_PyBaseExceptionObject_cast(self)->cause);
+ cause = Py_XNewRef(PyBaseExceptionObject_CAST(self)->cause);
Py_END_CRITICAL_SECTION();
return cause;
}
PyException_SetCause(PyObject *self, PyObject *cause)
{
Py_BEGIN_CRITICAL_SECTION(self);
- PyBaseExceptionObject *base_self = _PyBaseExceptionObject_cast(self);
+ PyBaseExceptionObject *base_self = PyBaseExceptionObject_CAST(self);
base_self->suppress_context = 1;
Py_XSETREF(base_self->cause, cause);
Py_END_CRITICAL_SECTION();
{
PyObject *context;
Py_BEGIN_CRITICAL_SECTION(self);
- context = Py_XNewRef(_PyBaseExceptionObject_cast(self)->context);
+ context = Py_XNewRef(PyBaseExceptionObject_CAST(self)->context);
Py_END_CRITICAL_SECTION();
return context;
}
PyException_SetContext(PyObject *self, PyObject *context)
{
Py_BEGIN_CRITICAL_SECTION(self);
- Py_XSETREF(_PyBaseExceptionObject_cast(self)->context, context);
+ Py_XSETREF(PyBaseExceptionObject_CAST(self)->context, context);
Py_END_CRITICAL_SECTION();
}
{
PyObject *args;
Py_BEGIN_CRITICAL_SECTION(self);
- args = Py_NewRef(_PyBaseExceptionObject_cast(self)->args);
+ args = Py_NewRef(PyBaseExceptionObject_CAST(self)->args);
Py_END_CRITICAL_SECTION();
return args;
}
{
Py_BEGIN_CRITICAL_SECTION(self);
Py_INCREF(args);
- Py_XSETREF(_PyBaseExceptionObject_cast(self)->args, args);
+ Py_XSETREF(PyBaseExceptionObject_CAST(self)->args, args);
Py_END_CRITICAL_SECTION();
}
"BaseException", /*tp_name*/
sizeof(PyBaseExceptionObject), /*tp_basicsize*/
0, /*tp_itemsize*/
- (destructor)BaseException_dealloc, /*tp_dealloc*/
+ BaseException_dealloc, /*tp_dealloc*/
0, /*tp_vectorcall_offset*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_as_async*/
- (reprfunc)BaseException_repr, /*tp_repr*/
+ BaseException_repr, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
- (reprfunc)BaseException_str, /*tp_str*/
+ BaseException_str, /*tp_str*/
PyObject_GenericGetAttr, /*tp_getattro*/
PyObject_GenericSetAttr, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/
PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
- (traverseproc)BaseException_traverse, /* tp_traverse */
- (inquiry)BaseException_clear, /* tp_clear */
+ BaseException_traverse, /* tp_traverse */
+ BaseException_clear, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_descr_get */
0, /* tp_descr_set */
offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
- (initproc)BaseException_init, /* tp_init */
+ BaseException_init, /* tp_init */
0, /* tp_alloc */
BaseException_new, /* tp_new */
.tp_vectorcall = BaseException_vectorcall,
PyVarObject_HEAD_INIT(NULL, 0) \
# EXCNAME, \
sizeof(PyBaseExceptionObject), \
- 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
+ 0, BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, \
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
- PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
- (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
+ PyDoc_STR(EXCDOC), BaseException_traverse, \
+ BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
- (initproc)BaseException_init, 0, BaseException_new,\
+ BaseException_init, 0, BaseException_new,\
}; \
PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
PyVarObject_HEAD_INIT(NULL, 0) \
# PYEXCNAME, \
sizeof(Py ## EXCSTORE ## Object), \
- 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
+ 0, EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, \
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
- PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
- (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
+ PyDoc_STR(EXCDOC), EXCSTORE ## _traverse, \
+ EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
- (initproc)EXCSTORE ## _init, 0, 0, \
+ EXCSTORE ## _init, 0, 0, \
};
#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
PyVarObject_HEAD_INIT(NULL, 0) \
# EXCNAME, \
sizeof(Py ## EXCSTORE ## Object), 0, \
- (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
- (reprfunc)EXCSTR, 0, 0, 0, \
+ EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
+ EXCSTR, 0, 0, 0, \
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
- PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
- (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
+ PyDoc_STR(EXCDOC), EXCSTORE ## _traverse, \
+ EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
EXCMEMBERS, EXCGETSET, &_ ## EXCBASE, \
0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
- (initproc)EXCSTORE ## _init, 0, EXCNEW,\
+ EXCSTORE ## _init, 0, EXCNEW,\
}; \
PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
{NULL} /* Sentinel */
};
+static inline PyStopIterationObject *
+PyStopIterationObject_CAST(PyObject *self)
+{
+ assert(PyObject_TypeCheck(self, (PyTypeObject *)PyExc_StopIteration));
+ return (PyStopIterationObject *)self;
+}
+
static int
-StopIteration_init(PyStopIterationObject *self, PyObject *args, PyObject *kwds)
+StopIteration_init(PyObject *op, PyObject *args, PyObject *kwds)
{
Py_ssize_t size = PyTuple_GET_SIZE(args);
PyObject *value;
- if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
+ if (BaseException_init(op, args, kwds) == -1)
return -1;
+ PyStopIterationObject *self = PyStopIterationObject_CAST(op);
Py_CLEAR(self->value);
if (size > 0)
value = PyTuple_GET_ITEM(args, 0);
}
static int
-StopIteration_clear(PyStopIterationObject *self)
+StopIteration_clear(PyObject *op)
{
+ PyStopIterationObject *self = PyStopIterationObject_CAST(op);
Py_CLEAR(self->value);
- return BaseException_clear((PyBaseExceptionObject *)self);
+ return BaseException_clear(op);
}
static void
-StopIteration_dealloc(PyStopIterationObject *self)
+StopIteration_dealloc(PyObject *self)
{
PyObject_GC_UnTrack(self);
- StopIteration_clear(self);
- Py_TYPE(self)->tp_free((PyObject *)self);
+ (void)StopIteration_clear(self);
+ Py_TYPE(self)->tp_free(self);
}
static int
-StopIteration_traverse(PyStopIterationObject *self, visitproc visit, void *arg)
+StopIteration_traverse(PyObject *op, visitproc visit, void *arg)
{
+ PyStopIterationObject *self = PyStopIterationObject_CAST(op);
Py_VISIT(self->value);
- return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
+ return BaseException_traverse(op, visit, arg);
}
ComplexExtendsException(PyExc_Exception, StopIteration, StopIteration,
* SystemExit extends BaseException
*/
+static inline PySystemExitObject *
+PySystemExitObject_CAST(PyObject *self)
+{
+ assert(PyObject_TypeCheck(self, (PyTypeObject *)PyExc_SystemExit));
+ return (PySystemExitObject *)self;
+}
+
static int
-SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
+SystemExit_init(PyObject *op, PyObject *args, PyObject *kwds)
{
Py_ssize_t size = PyTuple_GET_SIZE(args);
- if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
+ if (BaseException_init(op, args, kwds) == -1)
return -1;
+ PySystemExitObject *self = PySystemExitObject_CAST(op);
if (size == 0)
return 0;
if (size == 1) {
}
static int
-SystemExit_clear(PySystemExitObject *self)
+SystemExit_clear(PyObject *op)
{
+ PySystemExitObject *self = PySystemExitObject_CAST(op);
Py_CLEAR(self->code);
- return BaseException_clear((PyBaseExceptionObject *)self);
+ return BaseException_clear(op);
}
static void
-SystemExit_dealloc(PySystemExitObject *self)
+SystemExit_dealloc(PyObject *self)
{
_PyObject_GC_UNTRACK(self);
- SystemExit_clear(self);
- Py_TYPE(self)->tp_free((PyObject *)self);
+ (void)SystemExit_clear(self);
+ Py_TYPE(self)->tp_free(self);
}
static int
-SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
+SystemExit_traverse(PyObject *op, visitproc visit, void *arg)
{
+ PySystemExitObject *self = PySystemExitObject_CAST(op);
Py_VISIT(self->code);
- return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
+ return BaseException_traverse(op, visit, arg);
}
static PyMemberDef SystemExit_members[] = {
static inline PyBaseExceptionGroupObject*
-_PyBaseExceptionGroupObject_cast(PyObject *exc)
+PyBaseExceptionGroupObject_CAST(PyObject *exc)
{
assert(_PyBaseExceptionGroup_Check(exc));
return (PyBaseExceptionGroupObject *)exc;
cls = (PyTypeObject*)PyExc_BaseExceptionGroup;
}
PyBaseExceptionGroupObject *self =
- _PyBaseExceptionGroupObject_cast(BaseException_new(cls, args, kwds));
+ PyBaseExceptionGroupObject_CAST(BaseException_new(cls, args, kwds));
if (!self) {
goto error;
}
}
static int
-BaseExceptionGroup_init(PyBaseExceptionGroupObject *self,
- PyObject *args, PyObject *kwds)
+BaseExceptionGroup_init(PyObject *self, PyObject *args, PyObject *kwds)
{
if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds)) {
return -1;
}
- if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) {
+ if (BaseException_init(self, args, kwds) == -1) {
return -1;
}
return 0;
}
static int
-BaseExceptionGroup_clear(PyBaseExceptionGroupObject *self)
+BaseExceptionGroup_clear(PyObject *op)
{
+ PyBaseExceptionGroupObject *self = PyBaseExceptionGroupObject_CAST(op);
Py_CLEAR(self->msg);
Py_CLEAR(self->excs);
- return BaseException_clear((PyBaseExceptionObject *)self);
+ return BaseException_clear(op);
}
static void
-BaseExceptionGroup_dealloc(PyBaseExceptionGroupObject *self)
+BaseExceptionGroup_dealloc(PyObject *self)
{
_PyObject_GC_UNTRACK(self);
- BaseExceptionGroup_clear(self);
- Py_TYPE(self)->tp_free((PyObject *)self);
+ (void)BaseExceptionGroup_clear(self);
+ Py_TYPE(self)->tp_free(self);
}
static int
-BaseExceptionGroup_traverse(PyBaseExceptionGroupObject *self,
- visitproc visit, void *arg)
+BaseExceptionGroup_traverse(PyObject *op, visitproc visit, void *arg)
{
+ PyBaseExceptionGroupObject *self = PyBaseExceptionGroupObject_CAST(op);
Py_VISIT(self->msg);
Py_VISIT(self->excs);
- return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
+ return BaseException_traverse(op, visit, arg);
}
static PyObject *
-BaseExceptionGroup_str(PyBaseExceptionGroupObject *self)
+BaseExceptionGroup_str(PyObject *op)
{
+ PyBaseExceptionGroupObject *self = PyBaseExceptionGroupObject_CAST(op);
assert(self->msg);
assert(PyUnicode_Check(self->msg));
/* Partial match */
- PyBaseExceptionGroupObject *eg = _PyBaseExceptionGroupObject_cast(exc);
+ PyBaseExceptionGroupObject *eg = PyBaseExceptionGroupObject_CAST(exc);
assert(PyTuple_CheckExact(eg->excs));
Py_ssize_t num_excs = PyTuple_Size(eg->excs);
if (num_excs < 0) {
Py_DECREF(exc_id);
return res;
}
- PyBaseExceptionGroupObject *eg = _PyBaseExceptionGroupObject_cast(exc);
+ PyBaseExceptionGroupObject *eg = PyBaseExceptionGroupObject_CAST(exc);
Py_ssize_t num_excs = PyTuple_GET_SIZE(eg->excs);
/* recursive calls */
for (Py_ssize_t i = 0; i < num_excs; i++) {
{
/* orig must be a raised & caught exception, so it has a traceback */
assert(PyExceptionInstance_Check(orig));
- assert(_PyBaseExceptionObject_cast(orig)->traceback != NULL);
+ assert(PyBaseExceptionObject_CAST(orig)->traceback != NULL);
assert(PyList_Check(excs));
};
static PyMethodDef BaseExceptionGroup_methods[] = {
- {"__class_getitem__", (PyCFunction)Py_GenericAlias,
+ {"__class_getitem__", Py_GenericAlias,
METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
BASEEXCEPTIONGROUP_DERIVE_METHODDEF
BASEEXCEPTIONGROUP_SPLIT_METHODDEF
* ImportError extends Exception
*/
+static inline PyImportErrorObject *
+PyImportErrorObject_CAST(PyObject *self)
+{
+ assert(PyObject_TypeCheck(self, (PyTypeObject *)PyExc_ImportError));
+ return (PyImportErrorObject *)self;
+}
+
static int
-ImportError_init(PyImportErrorObject *self, PyObject *args, PyObject *kwds)
+ImportError_init(PyObject *op, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"name", "path", "name_from", 0};
PyObject *empty_tuple;
PyObject *path = NULL;
PyObject *name_from = NULL;
- if (BaseException_init((PyBaseExceptionObject *)self, args, NULL) == -1)
+ if (BaseException_init(op, args, NULL) == -1)
return -1;
+ PyImportErrorObject *self = PyImportErrorObject_CAST(op);
empty_tuple = PyTuple_New(0);
if (!empty_tuple)
return -1;
}
static int
-ImportError_clear(PyImportErrorObject *self)
+ImportError_clear(PyObject *op)
{
+ PyImportErrorObject *self = PyImportErrorObject_CAST(op);
Py_CLEAR(self->msg);
Py_CLEAR(self->name);
Py_CLEAR(self->path);
Py_CLEAR(self->name_from);
- return BaseException_clear((PyBaseExceptionObject *)self);
+ return BaseException_clear(op);
}
static void
-ImportError_dealloc(PyImportErrorObject *self)
+ImportError_dealloc(PyObject *self)
{
_PyObject_GC_UNTRACK(self);
- ImportError_clear(self);
- Py_TYPE(self)->tp_free((PyObject *)self);
+ (void)ImportError_clear(self);
+ Py_TYPE(self)->tp_free(self);
}
static int
-ImportError_traverse(PyImportErrorObject *self, visitproc visit, void *arg)
+ImportError_traverse(PyObject *op, visitproc visit, void *arg)
{
+ PyImportErrorObject *self = PyImportErrorObject_CAST(op);
Py_VISIT(self->msg);
Py_VISIT(self->name);
Py_VISIT(self->path);
Py_VISIT(self->name_from);
- return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
+ return BaseException_traverse(op, visit, arg);
}
static PyObject *
-ImportError_str(PyImportErrorObject *self)
+ImportError_str(PyObject *op)
{
+ PyImportErrorObject *self = PyImportErrorObject_CAST(op);
if (self->msg && PyUnicode_CheckExact(self->msg)) {
return Py_NewRef(self->msg);
}
- else {
- return BaseException_str((PyBaseExceptionObject *)self);
- }
+ return BaseException_str(op);
}
static PyObject *
-ImportError_getstate(PyImportErrorObject *self)
+ImportError_getstate(PyObject *op)
{
- PyObject *dict = ((PyBaseExceptionObject *)self)->dict;
+ PyImportErrorObject *self = PyImportErrorObject_CAST(op);
+ PyObject *dict = self->dict;
if (self->name || self->path || self->name_from) {
dict = dict ? PyDict_Copy(dict) : PyDict_New();
if (dict == NULL)
/* Pickling support */
static PyObject *
-ImportError_reduce(PyImportErrorObject *self, PyObject *Py_UNUSED(ignored))
+ImportError_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
{
PyObject *res;
- PyObject *args;
PyObject *state = ImportError_getstate(self);
if (state == NULL)
return NULL;
- args = ((PyBaseExceptionObject *)self)->args;
+ PyBaseExceptionObject *exc = PyBaseExceptionObject_CAST(self);
if (state == Py_None)
- res = PyTuple_Pack(2, Py_TYPE(self), args);
+ res = PyTuple_Pack(2, Py_TYPE(self), exc->args);
else
- res = PyTuple_Pack(3, Py_TYPE(self), args, state);
+ res = PyTuple_Pack(3, Py_TYPE(self), exc->args, state);
Py_DECREF(state);
return res;
}
};
static PyMethodDef ImportError_methods[] = {
- {"__reduce__", (PyCFunction)ImportError_reduce, METH_NOARGS},
+ {"__reduce__", ImportError_reduce, METH_NOARGS},
{NULL}
};
* OSError extends Exception
*/
+static inline PyOSErrorObject *
+PyOSErrorObject_CAST(PyObject *self)
+{
+ assert(PyObject_TypeCheck(self, (PyTypeObject *)PyExc_OSError));
+ return (PyOSErrorObject *)self;
+}
+
#ifdef MS_WINDOWS
#include "errmap.h"
#endif
static PyObject *
OSError_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
static int
-OSError_init(PyOSErrorObject *self, PyObject *args, PyObject *kwds);
+OSError_init(PyObject *self, PyObject *args, PyObject *kwds);
static int
oserror_use_init(PyTypeObject *type)
(see http://bugs.python.org/issue12555#msg148829 )
*/
- if (type->tp_init != (initproc) OSError_init &&
- type->tp_new == (newfunc) OSError_new) {
+ if (type->tp_init != OSError_init && type->tp_new == OSError_new) {
assert((PyObject *) type != PyExc_OSError);
return 1;
}
}
static int
-OSError_init(PyOSErrorObject *self, PyObject *args, PyObject *kwds)
+OSError_init(PyObject *op, PyObject *args, PyObject *kwds)
{
+ PyOSErrorObject *self = PyOSErrorObject_CAST(op);
PyObject *myerrno = NULL, *strerror = NULL;
PyObject *filename = NULL, *filename2 = NULL;
#ifdef MS_WINDOWS
}
static int
-OSError_clear(PyOSErrorObject *self)
+OSError_clear(PyObject *op)
{
+ PyOSErrorObject *self = PyOSErrorObject_CAST(op);
Py_CLEAR(self->myerrno);
Py_CLEAR(self->strerror);
Py_CLEAR(self->filename);
#ifdef MS_WINDOWS
Py_CLEAR(self->winerror);
#endif
- return BaseException_clear((PyBaseExceptionObject *)self);
+ return BaseException_clear(op);
}
static void
-OSError_dealloc(PyOSErrorObject *self)
+OSError_dealloc(PyObject *self)
{
_PyObject_GC_UNTRACK(self);
- OSError_clear(self);
- Py_TYPE(self)->tp_free((PyObject *)self);
+ (void)OSError_clear(self);
+ Py_TYPE(self)->tp_free(self);
}
static int
-OSError_traverse(PyOSErrorObject *self, visitproc visit,
- void *arg)
+OSError_traverse(PyObject *op, visitproc visit, void *arg)
{
+ PyOSErrorObject *self = PyOSErrorObject_CAST(op);
Py_VISIT(self->myerrno);
Py_VISIT(self->strerror);
Py_VISIT(self->filename);
#ifdef MS_WINDOWS
Py_VISIT(self->winerror);
#endif
- return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
+ return BaseException_traverse(op, visit, arg);
}
static PyObject *
-OSError_str(PyOSErrorObject *self)
+OSError_str(PyObject *op)
{
+ PyOSErrorObject *self = PyOSErrorObject_CAST(op);
#define OR_NONE(x) ((x)?(x):Py_None)
#ifdef MS_WINDOWS
/* If available, winerror has the priority over myerrno */
if (self->myerrno && self->strerror)
return PyUnicode_FromFormat("[Errno %S] %S",
self->myerrno, self->strerror);
- return BaseException_str((PyBaseExceptionObject *)self);
+ return BaseException_str(op);
}
static PyObject *
-OSError_reduce(PyOSErrorObject *self, PyObject *Py_UNUSED(ignored))
+OSError_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
{
+ PyOSErrorObject *self = PyOSErrorObject_CAST(op);
PyObject *args = self->args;
PyObject *res = NULL;
}
static PyObject *
-OSError_written_get(PyOSErrorObject *self, void *context)
+OSError_written_get(PyObject *op, void *context)
{
+ PyOSErrorObject *self = PyOSErrorObject_CAST(op);
if (self->written == -1) {
PyErr_SetString(PyExc_AttributeError, "characters_written");
return NULL;
}
static int
-OSError_written_set(PyOSErrorObject *self, PyObject *arg, void *context)
+OSError_written_set(PyObject *op, PyObject *arg, void *context)
{
+ PyOSErrorObject *self = PyOSErrorObject_CAST(op);
if (arg == NULL) {
if (self->written == -1) {
PyErr_SetString(PyExc_AttributeError, "characters_written");
};
static PyMethodDef OSError_methods[] = {
- {"__reduce__", (PyCFunction)OSError_reduce, METH_NOARGS},
+ {"__reduce__", OSError_reduce, METH_NOARGS},
{NULL}
};
static PyGetSetDef OSError_getset[] = {
- {"characters_written", (getter) OSError_written_get,
- (setter) OSError_written_set, NULL},
+ {"characters_written", OSError_written_get,
+ OSError_written_set, NULL},
{NULL}
};
* NameError extends Exception
*/
+static inline PyNameErrorObject *
+PyNameErrorObject_CAST(PyObject *self)
+{
+ assert(PyObject_TypeCheck(self, (PyTypeObject *)PyExc_NameError));
+ return (PyNameErrorObject *)self;
+}
+
static int
-NameError_init(PyNameErrorObject *self, PyObject *args, PyObject *kwds)
+NameError_init(PyObject *op, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"name", NULL};
PyObject *name = NULL;
- if (BaseException_init((PyBaseExceptionObject *)self, args, NULL) == -1) {
+ if (BaseException_init(op, args, NULL) == -1) {
return -1;
}
}
Py_DECREF(empty_tuple);
+ PyNameErrorObject *self = PyNameErrorObject_CAST(op);
Py_XSETREF(self->name, Py_XNewRef(name));
return 0;
}
static int
-NameError_clear(PyNameErrorObject *self)
+NameError_clear(PyObject *op)
{
+ PyNameErrorObject *self = PyNameErrorObject_CAST(op);
Py_CLEAR(self->name);
- return BaseException_clear((PyBaseExceptionObject *)self);
+ return BaseException_clear(op);
}
static void
-NameError_dealloc(PyNameErrorObject *self)
+NameError_dealloc(PyObject *self)
{
_PyObject_GC_UNTRACK(self);
- NameError_clear(self);
- Py_TYPE(self)->tp_free((PyObject *)self);
+ (void)NameError_clear(self);
+ Py_TYPE(self)->tp_free(self);
}
static int
-NameError_traverse(PyNameErrorObject *self, visitproc visit, void *arg)
+NameError_traverse(PyObject *op, visitproc visit, void *arg)
{
+ PyNameErrorObject *self = PyNameErrorObject_CAST(op);
Py_VISIT(self->name);
- return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
+ return BaseException_traverse(op, visit, arg);
}
static PyMemberDef NameError_members[] = {
* AttributeError extends Exception
*/
+static inline PyAttributeErrorObject *
+PyAttributeErrorObject_CAST(PyObject *self)
+{
+ assert(PyObject_TypeCheck(self, (PyTypeObject *)PyExc_AttributeError));
+ return (PyAttributeErrorObject *)self;
+}
+
static int
-AttributeError_init(PyAttributeErrorObject *self, PyObject *args, PyObject *kwds)
+AttributeError_init(PyObject *op, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"name", "obj", NULL};
PyObject *name = NULL;
PyObject *obj = NULL;
- if (BaseException_init((PyBaseExceptionObject *)self, args, NULL) == -1) {
+ if (BaseException_init(op, args, NULL) == -1) {
return -1;
}
}
Py_DECREF(empty_tuple);
+ PyAttributeErrorObject *self = PyAttributeErrorObject_CAST(op);
Py_XSETREF(self->name, Py_XNewRef(name));
Py_XSETREF(self->obj, Py_XNewRef(obj));
}
static int
-AttributeError_clear(PyAttributeErrorObject *self)
+AttributeError_clear(PyObject *op)
{
+ PyAttributeErrorObject *self = PyAttributeErrorObject_CAST(op);
Py_CLEAR(self->obj);
Py_CLEAR(self->name);
- return BaseException_clear((PyBaseExceptionObject *)self);
+ return BaseException_clear(op);
}
static void
-AttributeError_dealloc(PyAttributeErrorObject *self)
+AttributeError_dealloc(PyObject *self)
{
_PyObject_GC_UNTRACK(self);
- AttributeError_clear(self);
- Py_TYPE(self)->tp_free((PyObject *)self);
+ (void)AttributeError_clear(self);
+ Py_TYPE(self)->tp_free(self);
}
static int
-AttributeError_traverse(PyAttributeErrorObject *self, visitproc visit, void *arg)
+AttributeError_traverse(PyObject *op, visitproc visit, void *arg)
{
+ PyAttributeErrorObject *self = PyAttributeErrorObject_CAST(op);
Py_VISIT(self->obj);
Py_VISIT(self->name);
- return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
+ return BaseException_traverse(op, visit, arg);
}
/* Pickling support */
static PyObject *
-AttributeError_getstate(PyAttributeErrorObject *self, PyObject *Py_UNUSED(ignored))
+AttributeError_getstate(PyObject *op, PyObject *Py_UNUSED(ignored))
{
- PyObject *dict = ((PyAttributeErrorObject *)self)->dict;
+ PyAttributeErrorObject *self = PyAttributeErrorObject_CAST(op);
+ PyObject *dict = self->dict;
if (self->name || self->args) {
dict = dict ? PyDict_Copy(dict) : PyDict_New();
if (dict == NULL) {
}
static PyObject *
-AttributeError_reduce(PyAttributeErrorObject *self, PyObject *Py_UNUSED(ignored))
+AttributeError_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
{
- PyObject *state = AttributeError_getstate(self, NULL);
+ PyObject *state = AttributeError_getstate(op, NULL);
if (state == NULL) {
return NULL;
}
+ PyAttributeErrorObject *self = PyAttributeErrorObject_CAST(op);
PyObject *return_value = PyTuple_Pack(3, Py_TYPE(self), self->args, state);
Py_DECREF(state);
return return_value;
};
static PyMethodDef AttributeError_methods[] = {
- {"__getstate__", (PyCFunction)AttributeError_getstate, METH_NOARGS},
- {"__reduce__", (PyCFunction)AttributeError_reduce, METH_NOARGS },
+ {"__getstate__", AttributeError_getstate, METH_NOARGS},
+ {"__reduce__", AttributeError_reduce, METH_NOARGS },
{NULL}
};
* SyntaxError extends Exception
*/
+static inline PySyntaxErrorObject *
+PySyntaxErrorObject_CAST(PyObject *self)
+{
+ assert(PyObject_TypeCheck(self, (PyTypeObject *)PyExc_SyntaxError));
+ return (PySyntaxErrorObject *)self;
+}
+
static int
-SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
+SyntaxError_init(PyObject *op, PyObject *args, PyObject *kwds)
{
PyObject *info = NULL;
Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
- if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
+ if (BaseException_init(op, args, kwds) == -1)
return -1;
+ PySyntaxErrorObject *self = PySyntaxErrorObject_CAST(op);
if (lenargs >= 1) {
Py_XSETREF(self->msg, Py_NewRef(PyTuple_GET_ITEM(args, 0)));
}
}
static int
-SyntaxError_clear(PySyntaxErrorObject *self)
+SyntaxError_clear(PyObject *op)
{
+ PySyntaxErrorObject *self = PySyntaxErrorObject_CAST(op);
Py_CLEAR(self->msg);
Py_CLEAR(self->filename);
Py_CLEAR(self->lineno);
Py_CLEAR(self->end_offset);
Py_CLEAR(self->text);
Py_CLEAR(self->print_file_and_line);
- return BaseException_clear((PyBaseExceptionObject *)self);
+ return BaseException_clear(op);
}
static void
-SyntaxError_dealloc(PySyntaxErrorObject *self)
+SyntaxError_dealloc(PyObject *self)
{
_PyObject_GC_UNTRACK(self);
- SyntaxError_clear(self);
- Py_TYPE(self)->tp_free((PyObject *)self);
+ (void)SyntaxError_clear(self);
+ Py_TYPE(self)->tp_free(self);
}
static int
-SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
+SyntaxError_traverse(PyObject *op, visitproc visit, void *arg)
{
+ PySyntaxErrorObject *self = PySyntaxErrorObject_CAST(op);
Py_VISIT(self->msg);
Py_VISIT(self->filename);
Py_VISIT(self->lineno);
Py_VISIT(self->end_offset);
Py_VISIT(self->text);
Py_VISIT(self->print_file_and_line);
- return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
+ return BaseException_traverse(op, visit, arg);
}
/* This is called "my_basename" instead of just "basename" to avoid name
static PyObject *
-SyntaxError_str(PySyntaxErrorObject *self)
+SyntaxError_str(PyObject *op)
{
+ PySyntaxErrorObject *self = PySyntaxErrorObject_CAST(op);
int have_lineno = 0;
PyObject *filename;
PyObject *result;
/*
* KeyError extends LookupError
*/
+
static PyObject *
-KeyError_str(PyBaseExceptionObject *self)
+KeyError_str(PyObject *op)
{
/* If args is a tuple of exactly one item, apply repr to args[0].
This is done so that e.g. the exception raised by {}[''] prints
string, that string will be displayed in quotes. Too bad.
If args is anything else, use the default BaseException__str__().
*/
+ PyBaseExceptionObject *self = PyBaseExceptionObject_CAST(op);
if (PyTuple_GET_SIZE(self->args) == 1) {
return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
}
- return BaseException_str(self);
+ return BaseException_str(op);
}
ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
return end;
}
+#define PyUnicodeError_Check(PTR) \
+ PyObject_TypeCheck((PTR), (PyTypeObject *)PyExc_UnicodeError)
+#define PyUnicodeErrorObject_CAST(op) \
+ (assert(PyUnicodeError_Check(op)), ((PyUnicodeErrorObject *)(op)))
/* Assert some properties of the adjusted 'end' value. */
#ifndef NDEBUG
static int
UnicodeError_clear(PyObject *self)
{
- PyUnicodeErrorObject *exc = PyUnicodeError_CAST(self);
+ PyUnicodeErrorObject *exc = PyUnicodeErrorObject_CAST(self);
Py_CLEAR(exc->encoding);
Py_CLEAR(exc->object);
Py_CLEAR(exc->reason);
- return BaseException_clear((PyBaseExceptionObject *)self);
+ return BaseException_clear(self);
}
static void
static int
UnicodeError_traverse(PyObject *self, visitproc visit, void *arg)
{
- PyUnicodeErrorObject *exc = PyUnicodeError_CAST(self);
+ PyUnicodeErrorObject *exc = PyUnicodeErrorObject_CAST(self);
Py_VISIT(exc->encoding);
Py_VISIT(exc->object);
Py_VISIT(exc->reason);
- return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
+ return BaseException_traverse(self, visit, arg);
}
static PyMemberDef UnicodeError_members[] = {
static int
UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
{
- if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) {
+ if (BaseException_init(self, args, kwds) == -1) {
return -1;
}
return -1;
}
- PyUnicodeErrorObject *exc = PyUnicodeError_CAST(self);
+ PyUnicodeErrorObject *exc = PyUnicodeErrorObject_CAST(self);
Py_XSETREF(exc->encoding, Py_NewRef(encoding));
Py_XSETREF(exc->object, Py_NewRef(object));
exc->start = start;
static PyObject *
UnicodeEncodeError_str(PyObject *self)
{
- PyUnicodeErrorObject *exc = PyUnicodeError_CAST(self);
+ PyUnicodeErrorObject *exc = PyUnicodeErrorObject_CAST(self);
PyObject *result = NULL;
PyObject *reason_str = NULL;
PyObject *encoding_str = NULL;
PyVarObject_HEAD_INIT(NULL, 0)
"UnicodeEncodeError",
sizeof(PyUnicodeErrorObject), 0,
- (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
+ UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ UnicodeEncodeError_str, 0, 0, 0,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
- PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
- (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
+ PyDoc_STR("Unicode encoding error."), UnicodeError_traverse,
+ UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
- (initproc)UnicodeEncodeError_init, 0, BaseException_new,
+ UnicodeEncodeError_init, 0, BaseException_new,
};
PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
static int
UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
{
- if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) {
+ if (BaseException_init(self, args, kwds) == -1) {
return -1;
}
}
}
- PyUnicodeErrorObject *exc = PyUnicodeError_CAST(self);
+ PyUnicodeErrorObject *exc = PyUnicodeErrorObject_CAST(self);
Py_XSETREF(exc->encoding, Py_NewRef(encoding));
Py_XSETREF(exc->object, object /* already a strong reference */);
exc->start = start;
static PyObject *
UnicodeDecodeError_str(PyObject *self)
{
- PyUnicodeErrorObject *exc = PyUnicodeError_CAST(self);
+ PyUnicodeErrorObject *exc = PyUnicodeErrorObject_CAST(self);
PyObject *result = NULL;
PyObject *reason_str = NULL;
PyObject *encoding_str = NULL;
PyVarObject_HEAD_INIT(NULL, 0)
"UnicodeDecodeError",
sizeof(PyUnicodeErrorObject), 0,
- (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
+ UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ UnicodeDecodeError_str, 0, 0, 0,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
- PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
- (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
+ PyDoc_STR("Unicode decoding error."), UnicodeError_traverse,
+ UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
- (initproc)UnicodeDecodeError_init, 0, BaseException_new,
+ UnicodeDecodeError_init, 0, BaseException_new,
};
PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
static int
UnicodeTranslateError_init(PyObject *self, PyObject *args, PyObject *kwds)
{
- if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) {
+ if (BaseException_init(self, args, kwds) == -1) {
return -1;
}
return -1;
}
- PyUnicodeErrorObject *exc = PyUnicodeError_CAST(self);
+ PyUnicodeErrorObject *exc = PyUnicodeErrorObject_CAST(self);
Py_XSETREF(exc->object, Py_NewRef(object));
exc->start = start;
exc->end = end;
static PyObject *
UnicodeTranslateError_str(PyObject *self)
{
- PyUnicodeErrorObject *exc = PyUnicodeError_CAST(self);
+ PyUnicodeErrorObject *exc = PyUnicodeErrorObject_CAST(self);
PyObject *result = NULL;
PyObject *reason_str = NULL;
PyVarObject_HEAD_INIT(NULL, 0)
"UnicodeTranslateError",
sizeof(PyUnicodeErrorObject), 0,
- (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
+ UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ UnicodeTranslateError_str, 0, 0, 0,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
- PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
- (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
+ PyDoc_STR("Unicode translation error."), UnicodeError_traverse,
+ UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
- (initproc)UnicodeTranslateError_init, 0, BaseException_new,
+ UnicodeTranslateError_init, 0, BaseException_new,
};
PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
}
static void
-MemoryError_dealloc(PyObject *obj)
+MemoryError_dealloc(PyObject *op)
{
- PyBaseExceptionObject *self = (PyBaseExceptionObject *)obj;
+ PyBaseExceptionObject *self = PyBaseExceptionObject_CAST(op);
_PyObject_GC_UNTRACK(self);
- BaseException_clear(self);
+ (void)BaseException_clear(op);
/* If this is a subclass of MemoryError, we don't need to
* do anything in the free-list*/
if (!Py_IS_TYPE(self, (PyTypeObject *) PyExc_MemoryError)) {
- Py_TYPE(self)->tp_free((PyObject *)self);
+ Py_TYPE(self)->tp_free(op);
return;
}
while (state->memerrors_freelist != NULL) {
PyObject *self = (PyObject *) state->memerrors_freelist;
state->memerrors_freelist = (PyBaseExceptionObject *)state->memerrors_freelist->dict;
- Py_TYPE(self)->tp_free((PyObject *)self);
+ Py_TYPE(self)->tp_free(self);
}
}
0, MemoryError_dealloc, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
- PyDoc_STR("Out of memory."), (traverseproc)BaseException_traverse,
- (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_PyExc_Exception,
+ PyDoc_STR("Out of memory."), BaseException_traverse,
+ BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_PyExc_Exception,
0, 0, 0, offsetof(PyBaseExceptionObject, dict),
- (initproc)BaseException_init, 0, MemoryError_new
+ BaseException_init, 0, MemoryError_new
};
PyObject *PyExc_MemoryError = (PyObject *) &_PyExc_MemoryError;
return -1;
}
if (exc->tp_new == BaseException_new
- && exc->tp_init == (initproc)BaseException_init)
+ && exc->tp_init == BaseException_init)
{
exc->tp_vectorcall = BaseException_vectorcall;
}