summaryrefslogtreecommitdiffstats
path: root/Objects/exceptions.c
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2009-09-16 20:30:09 (GMT)
committerGeorg Brandl <georg@python.org>2009-09-16 20:30:09 (GMT)
commit0674d3fb5fb9adde412c15ea12b6b473e8895126 (patch)
tree6e441bb57c8f1a7c90f29e2a060ca03ed57d0b80 /Objects/exceptions.c
parent4676048b437a696b42ecd4cf2c3b050e7d5154cc (diff)
downloadcpython-0674d3fb5fb9adde412c15ea12b6b473e8895126.zip
cpython-0674d3fb5fb9adde412c15ea12b6b473e8895126.tar.gz
cpython-0674d3fb5fb9adde412c15ea12b6b473e8895126.tar.bz2
#6844: do not emit DeprecationWarnings on access if Exception.message has been set by the user.
This works by always setting it in __dict__, except when it's implicitly set in __init__.
Diffstat (limited to 'Objects/exceptions.c')
-rw-r--r--Objects/exceptions.c57
1 files changed, 39 insertions, 18 deletions
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index fbdc781..0a39a6b 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -300,30 +300,51 @@ BaseException_set_args(PyBaseExceptionObject *self, PyObject *val)
static PyObject *
BaseException_get_message(PyBaseExceptionObject *self)
{
- int ret;
- ret = PyErr_WarnEx(PyExc_DeprecationWarning,
- "BaseException.message has been deprecated as "
- "of Python 2.6", 1);
- if (ret < 0)
- return NULL;
+ PyObject *msg;
+
+ /* if "message" is in self->dict, accessing a user-set message attribute */
+ if (self->dict &&
+ (msg = PyDict_GetItemString(self->dict, "message"))) {
+ Py_INCREF(msg);
+ return msg;
+ }
+
+ if (self->message == NULL) {
+ PyErr_SetString(PyExc_AttributeError, "message attribute was deleted");
+ return NULL;
+ }
- Py_INCREF(self->message);
- return self->message;
+ /* accessing the deprecated "builtin" message attribute of Exception */
+ if (PyErr_WarnEx(PyExc_DeprecationWarning,
+ "BaseException.message has been deprecated as "
+ "of Python 2.6", 1) < 0)
+ return NULL;
+
+ Py_INCREF(self->message);
+ return self->message;
}
static int
BaseException_set_message(PyBaseExceptionObject *self, PyObject *val)
{
- int ret;
- ret = PyErr_WarnEx(PyExc_DeprecationWarning,
- "BaseException.message has been deprecated as "
- "of Python 2.6", 1);
- if (ret < 0)
- return -1;
- Py_INCREF(val);
- Py_DECREF(self->message);
- self->message = val;
- return 0;
+ /* if val is NULL, delete the message attribute */
+ if (val == NULL) {
+ if (self->dict && PyDict_GetItemString(self->dict, "message")) {
+ if (PyDict_DelItemString(self->dict, "message") < 0)
+ return -1;
+ }
+ Py_XDECREF(self->message);
+ self->message = NULL;
+ return 0;
+ }
+
+ /* else set it in __dict__, but may need to create the dict first */
+ if (self->dict == NULL) {
+ self->dict = PyDict_New();
+ if (!self->dict)
+ return -1;
+ }
+ return PyDict_SetItemString(self->dict, "message", val);
}
static PyGetSetDef BaseException_getset[] = {