summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2012-05-15 05:09:31 (GMT)
committerBenjamin Peterson <benjamin@python.org>2012-05-15 05:09:31 (GMT)
commitd5a1c44455d969968f453f029727bfc45e4ce0a9 (patch)
tree98e91aa8130d600df0b1fdf329bbdc9d60d4b14c /Objects
parentd91dc623791fd9973b914a57540d89cb986da7c9 (diff)
downloadcpython-d5a1c44455d969968f453f029727bfc45e4ce0a9.zip
cpython-d5a1c44455d969968f453f029727bfc45e4ce0a9.tar.gz
cpython-d5a1c44455d969968f453f029727bfc45e4ce0a9.tar.bz2
PEP 415: Implement suppression of __context__ display with an exception attribute
This replaces the original PEP 409 implementation. See #14133.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/exceptions.c39
1 files changed, 20 insertions, 19 deletions
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index a11283e..b994862 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -42,6 +42,7 @@ BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
/* the dict is created on the fly in PyObject_GenericSetAttr */
self->dict = NULL;
self->traceback = self->cause = self->context = NULL;
+ self->suppress_context = 0;
self->args = PyTuple_New(0);
if (!self->args) {
@@ -266,35 +267,28 @@ BaseException_get_cause(PyObject *self) {
PyObject *res = PyException_GetCause(self);
if (res)
return res; /* new reference already returned above */
- Py_INCREF(Py_Ellipsis);
- return Py_Ellipsis;
+ Py_RETURN_NONE;
}
-int
-_PyException_SetCauseChecked(PyObject *self, PyObject *arg) {
- if (arg == Py_Ellipsis) {
+static int
+BaseException_set_cause(PyObject *self, PyObject *arg) {
+ if (arg == NULL) {
+ PyErr_SetString(PyExc_TypeError, "__cause__ may not be deleted");
+ return -1;
+ } else if (arg == Py_None) {
arg = NULL;
- } else if (arg != Py_None && !PyExceptionInstance_Check(arg)) {
- PyErr_SetString(PyExc_TypeError, "exception cause must be None, "
- "Ellipsis or derive from BaseException");
+ } else if (!PyExceptionInstance_Check(arg)) {
+ PyErr_SetString(PyExc_TypeError, "exception cause must be None "
+ "or derive from BaseException");
return -1;
} else {
- /* PyException_SetCause steals a reference */
+ /* PyException_SetCause steals this reference */
Py_INCREF(arg);
}
PyException_SetCause(self, arg);
return 0;
}
-static int
-BaseException_set_cause(PyObject *self, PyObject *arg) {
- if (arg == NULL) {
- PyErr_SetString(PyExc_TypeError, "__cause__ may not be deleted");
- return -1;
- }
- return _PyException_SetCauseChecked(self, arg);
-}
-
static PyGetSetDef BaseException_getset[] = {
{"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
@@ -333,6 +327,7 @@ void
PyException_SetCause(PyObject *self, PyObject *cause) {
PyObject *old_cause = ((PyBaseExceptionObject *)self)->cause;
((PyBaseExceptionObject *)self)->cause = cause;
+ ((PyBaseExceptionObject *)self)->suppress_context = 1;
Py_XDECREF(old_cause);
}
@@ -352,6 +347,12 @@ PyException_SetContext(PyObject *self, PyObject *context) {
}
+static struct PyMemberDef BaseException_members[] = {
+ {"__suppress_context__", T_BOOL,
+ offsetof(PyBaseExceptionObject, suppress_context)}
+};
+
+
static PyTypeObject _PyExc_BaseException = {
PyVarObject_HEAD_INIT(NULL, 0)
"BaseException", /*tp_name*/
@@ -382,7 +383,7 @@ static PyTypeObject _PyExc_BaseException = {
0, /* tp_iter */
0, /* tp_iternext */
BaseException_methods, /* tp_methods */
- 0, /* tp_members */
+ BaseException_members, /* tp_members */
BaseException_getset, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */