diff options
-rw-r--r-- | Objects/exceptions.c | 54 |
1 files changed, 34 insertions, 20 deletions
diff --git a/Objects/exceptions.c b/Objects/exceptions.c index b432d6b..64e2141 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -959,20 +959,27 @@ SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg) /* This is called "my_basename" instead of just "basename" to avoid name conflicts with glibc; basename is already prototyped if _GNU_SOURCE is defined, and Python does define that. */ -static char * -my_basename(char *name) -{ - char *cp = name; - char *result = name; - - if (name == NULL) - return "???"; - while (*cp != '\0') { - if (*cp == SEP) - result = cp + 1; - ++cp; +static PyObject* +my_basename(PyObject *name) +{ + Py_UNICODE *unicode; + Py_ssize_t i, size, offset; + + unicode = PyUnicode_AS_UNICODE(name); + size = PyUnicode_GET_SIZE(name); + offset = 0; + for(i=0; i < size; i++) { + if (unicode[i] == SEP) + offset = i + 1; + } + if (offset != 0) { + return PyUnicode_FromUnicode( + PyUnicode_AS_UNICODE(name) + offset, + size - offset); + } else { + Py_INCREF(name); + return name; } - return result; } @@ -980,7 +987,8 @@ static PyObject * SyntaxError_str(PySyntaxErrorObject *self) { int have_lineno = 0; - char *filename = 0; + PyObject *filename; + PyObject *result; /* Below, we always ignore overflow errors, just printing -1. Still, we cannot allow an OverflowError to be raised, so we need to call PyLong_AsLongAndOverflow. */ @@ -990,7 +998,11 @@ SyntaxError_str(PySyntaxErrorObject *self) lineno here */ if (self->filename && PyUnicode_Check(self->filename)) { - filename = _PyUnicode_AsString(self->filename); + filename = my_basename(self->filename); + if (filename == NULL) + return NULL; + } else { + filename = NULL; } have_lineno = (self->lineno != NULL) && PyLong_CheckExact(self->lineno); @@ -998,18 +1010,20 @@ SyntaxError_str(PySyntaxErrorObject *self) return PyObject_Str(self->msg ? self->msg : Py_None); if (filename && have_lineno) - return PyUnicode_FromFormat("%S (%s, line %ld)", + result = PyUnicode_FromFormat("%S (%U, line %ld)", self->msg ? self->msg : Py_None, - my_basename(filename), + filename, PyLong_AsLongAndOverflow(self->lineno, &overflow)); else if (filename) - return PyUnicode_FromFormat("%S (%s)", + result = PyUnicode_FromFormat("%S (%U)", self->msg ? self->msg : Py_None, - my_basename(filename)); + filename); else /* only have_lineno */ - return PyUnicode_FromFormat("%S (line %ld)", + result = PyUnicode_FromFormat("%S (line %ld)", self->msg ? self->msg : Py_None, PyLong_AsLongAndOverflow(self->lineno, &overflow)); + Py_XDECREF(filename); + return result; } static PyMemberDef SyntaxError_members[] = { |