Commit f5bec7c6 authored by Walter Dörwald's avatar Walter Dörwald

Change tp_str implementations of exception classes

to return unicode strings.
parent 63236cf3
...@@ -78,21 +78,14 @@ BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg) ...@@ -78,21 +78,14 @@ BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg)
static PyObject * static PyObject *
BaseException_str(PyBaseExceptionObject *self) BaseException_str(PyBaseExceptionObject *self)
{ {
PyObject *out;
switch (PyTuple_GET_SIZE(self->args)) { switch (PyTuple_GET_SIZE(self->args)) {
case 0: case 0:
out = PyString_FromString(""); return PyUnicode_FromString("");
break;
case 1: case 1:
out = PyObject_Str(PyTuple_GET_ITEM(self->args, 0)); return PyObject_Unicode(PyTuple_GET_ITEM(self->args, 0));
break;
default: default:
out = PyObject_Str(self->args); return PyObject_Unicode(self->args);
break;
} }
return out;
} }
static PyObject * static PyObject *
...@@ -505,93 +498,17 @@ EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit, ...@@ -505,93 +498,17 @@ EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit,
static PyObject * static PyObject *
EnvironmentError_str(PyEnvironmentErrorObject *self) EnvironmentError_str(PyEnvironmentErrorObject *self)
{ {
PyObject *rtnval = NULL; if (self->filename)
return PyUnicode_FromFormat("[Errno %S] %S: %R",
if (self->filename) { self->myerrno ? self->myerrno: Py_None,
PyObject *fmt; self->strerror ? self->strerror: Py_None,
PyObject *repr; self->filename);
PyObject *tuple; else if (self->myerrno && self->strerror)
return PyUnicode_FromFormat("[Errno %S] %S",
fmt = PyString_FromString("[Errno %s] %s: %s"); self->myerrno ? self->myerrno: Py_None,
if (!fmt) self->strerror ? self->strerror: Py_None);
return NULL;
repr = PyObject_ReprStr8(self->filename);
if (!repr) {
Py_DECREF(fmt);
return NULL;
}
tuple = PyTuple_New(3);
if (!tuple) {
Py_DECREF(repr);
Py_DECREF(fmt);
return NULL;
}
if (self->myerrno) {
Py_INCREF(self->myerrno);
PyTuple_SET_ITEM(tuple, 0, self->myerrno);
}
else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(tuple, 0, Py_None);
}
if (self->strerror) {
Py_INCREF(self->strerror);
PyTuple_SET_ITEM(tuple, 1, self->strerror);
}
else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(tuple, 1, Py_None);
}
PyTuple_SET_ITEM(tuple, 2, repr);
rtnval = PyString_Format(fmt, tuple);
Py_DECREF(fmt);
Py_DECREF(tuple);
}
else if (self->myerrno && self->strerror) {
PyObject *fmt;
PyObject *tuple;
fmt = PyString_FromString("[Errno %s] %s");
if (!fmt)
return NULL;
tuple = PyTuple_New(2);
if (!tuple) {
Py_DECREF(fmt);
return NULL;
}
if (self->myerrno) {
Py_INCREF(self->myerrno);
PyTuple_SET_ITEM(tuple, 0, self->myerrno);
}
else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(tuple, 0, Py_None);
}
if (self->strerror) {
Py_INCREF(self->strerror);
PyTuple_SET_ITEM(tuple, 1, self->strerror);
}
else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(tuple, 1, Py_None);
}
rtnval = PyString_Format(fmt, tuple);
Py_DECREF(fmt);
Py_DECREF(tuple);
}
else else
rtnval = BaseException_str((PyBaseExceptionObject *)self); return BaseException_str((PyBaseExceptionObject *)self);
return rtnval;
} }
static PyMemberDef EnvironmentError_members[] = { static PyMemberDef EnvironmentError_members[] = {
...@@ -736,93 +653,17 @@ WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds) ...@@ -736,93 +653,17 @@ WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
static PyObject * static PyObject *
WindowsError_str(PyWindowsErrorObject *self) WindowsError_str(PyWindowsErrorObject *self)
{ {
PyObject *rtnval = NULL; if (self->filename)
return PyUnicode_FromFormat("[Error %S] %S: %R",
if (self->filename) { self->winerror ? self->winerror: Py_None,
PyObject *fmt; self->strerror ? self->strerror: Py_None,
PyObject *repr; self->filename);
PyObject *tuple; else if (self->winerror && self->strerror)
return PyUnicode_FromFormat("[Error %S] %S",
fmt = PyString_FromString("[Error %s] %s: %s"); self->winerror ? self->winerror: Py_None,
if (!fmt) self->strerror ? self->strerror: Py_None);
return NULL;
repr = PyObject_ReprStr8(self->filename);
if (!repr) {
Py_DECREF(fmt);
return NULL;
}
tuple = PyTuple_New(3);
if (!tuple) {
Py_DECREF(repr);
Py_DECREF(fmt);
return NULL;
}
if (self->winerror) {
Py_INCREF(self->winerror);
PyTuple_SET_ITEM(tuple, 0, self->winerror);
}
else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(tuple, 0, Py_None);
}
if (self->strerror) {
Py_INCREF(self->strerror);
PyTuple_SET_ITEM(tuple, 1, self->strerror);
}
else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(tuple, 1, Py_None);
}
PyTuple_SET_ITEM(tuple, 2, repr);
rtnval = PyString_Format(fmt, tuple);
Py_DECREF(fmt);
Py_DECREF(tuple);
}
else if (self->winerror && self->strerror) {
PyObject *fmt;
PyObject *tuple;
fmt = PyString_FromString("[Error %s] %s");
if (!fmt)
return NULL;
tuple = PyTuple_New(2);
if (!tuple) {
Py_DECREF(fmt);
return NULL;
}
if (self->winerror) {
Py_INCREF(self->winerror);
PyTuple_SET_ITEM(tuple, 0, self->winerror);
}
else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(tuple, 0, Py_None);
}
if (self->strerror) {
Py_INCREF(self->strerror);
PyTuple_SET_ITEM(tuple, 1, self->strerror);
}
else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(tuple, 1, Py_None);
}
rtnval = PyString_Format(fmt, tuple);
Py_DECREF(fmt);
Py_DECREF(tuple);
}
else else
rtnval = EnvironmentError_str((PyEnvironmentErrorObject *)self); return EnvironmentError_str((PyEnvironmentErrorObject *)self);
return rtnval;
} }
static PyMemberDef WindowsError_members[] = { static PyMemberDef WindowsError_members[] = {
...@@ -998,20 +839,8 @@ my_basename(char *name) ...@@ -998,20 +839,8 @@ my_basename(char *name)
static PyObject * static PyObject *
SyntaxError_str(PySyntaxErrorObject *self) SyntaxError_str(PySyntaxErrorObject *self)
{ {
PyObject *str;
PyObject *result;
int have_filename = 0; int have_filename = 0;
int have_lineno = 0; int have_lineno = 0;
char *buffer = NULL;
Py_ssize_t bufsize;
if (self->msg)
str = PyObject_Str(self->msg);
else
str = PyObject_Str(Py_None);
if (!str) return NULL;
/* Don't fiddle with non-string return (shouldn't happen anyway) */
if (!PyString_Check(str)) return str;
/* XXX -- do all the additional formatting with filename and /* XXX -- do all the additional formatting with filename and
lineno here */ lineno here */
...@@ -1021,38 +850,21 @@ SyntaxError_str(PySyntaxErrorObject *self) ...@@ -1021,38 +850,21 @@ SyntaxError_str(PySyntaxErrorObject *self)
have_lineno = (self->lineno != NULL) && PyInt_CheckExact(self->lineno); have_lineno = (self->lineno != NULL) && PyInt_CheckExact(self->lineno);
if (!have_filename && !have_lineno) if (!have_filename && !have_lineno)
return str; return PyObject_Unicode(self->msg ? self->msg : Py_None);
bufsize = PyString_GET_SIZE(str) + 64;
if (have_filename)
bufsize += PyString_GET_SIZE(self->filename);
buffer = PyMem_MALLOC(bufsize);
if (buffer == NULL)
return str;
if (have_filename && have_lineno) if (have_filename && have_lineno)
PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)", return PyUnicode_FromFormat("%S (%s, line %ld)",
PyString_AS_STRING(str), self->msg ? self->msg : Py_None,
my_basename(PyString_AS_STRING(self->filename)), my_basename(PyString_AS_STRING(self->filename)),
PyInt_AsLong(self->lineno)); PyInt_AsLong(self->lineno));
else if (have_filename) else if (have_filename)
PyOS_snprintf(buffer, bufsize, "%s (%s)", return PyUnicode_FromFormat("%S (%s)",
PyString_AS_STRING(str), self->msg ? self->msg : Py_None,
my_basename(PyString_AS_STRING(self->filename))); my_basename(PyString_AS_STRING(self->filename)));
else /* only have_lineno */ else /* only have_lineno */
PyOS_snprintf(buffer, bufsize, "%s (line %ld)", return PyUnicode_FromFormat("%S (line %ld)",
PyString_AS_STRING(str), self->msg ? self->msg : Py_None,
PyInt_AsLong(self->lineno)); PyInt_AsLong(self->lineno));
result = PyString_FromString(buffer);
PyMem_FREE(buffer);
if (result == NULL)
result = str;
else
Py_DECREF(str);
return result;
} }
static PyMemberDef SyntaxError_members[] = { static PyMemberDef SyntaxError_members[] = {
...@@ -1121,7 +933,7 @@ KeyError_str(PyBaseExceptionObject *self) ...@@ -1121,7 +933,7 @@ KeyError_str(PyBaseExceptionObject *self)
If args is anything else, use the default BaseException__str__(). If args is anything else, use the default BaseException__str__().
*/ */
if (PyTuple_GET_SIZE(self->args) == 1) { if (PyTuple_GET_SIZE(self->args) == 1) {
return PyObject_ReprStr8(PyTuple_GET_ITEM(self->args, 0)); return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
} }
return BaseException_str(self); return BaseException_str(self);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment