mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Don't decode/recode the unicode filename in SyntaxError_str()
* Rewrite my_basename() to use unicode * Use '%U' format
This commit is contained in:
parent
003a5e702b
commit
6237daf8c5
1 changed files with 32 additions and 18 deletions
|
@ -959,20 +959,27 @@ SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
|
||||||
/* This is called "my_basename" instead of just "basename" to avoid name
|
/* This is called "my_basename" instead of just "basename" to avoid name
|
||||||
conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
|
conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
|
||||||
defined, and Python does define that. */
|
defined, and Python does define that. */
|
||||||
static char *
|
static PyObject*
|
||||||
my_basename(char *name)
|
my_basename(PyObject *name)
|
||||||
{
|
{
|
||||||
char *cp = name;
|
Py_UNICODE *unicode;
|
||||||
char *result = name;
|
Py_ssize_t i, size, offset;
|
||||||
|
|
||||||
if (name == NULL)
|
unicode = PyUnicode_AS_UNICODE(name);
|
||||||
return "???";
|
size = PyUnicode_GET_SIZE(name);
|
||||||
while (*cp != '\0') {
|
offset = 0;
|
||||||
if (*cp == SEP)
|
for(i=0; i < size; i++) {
|
||||||
result = cp + 1;
|
if (unicode[i] == SEP)
|
||||||
++cp;
|
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)
|
SyntaxError_str(PySyntaxErrorObject *self)
|
||||||
{
|
{
|
||||||
int have_lineno = 0;
|
int have_lineno = 0;
|
||||||
char *filename = 0;
|
PyObject *filename;
|
||||||
|
PyObject *result;
|
||||||
/* Below, we always ignore overflow errors, just printing -1.
|
/* Below, we always ignore overflow errors, just printing -1.
|
||||||
Still, we cannot allow an OverflowError to be raised, so
|
Still, we cannot allow an OverflowError to be raised, so
|
||||||
we need to call PyLong_AsLongAndOverflow. */
|
we need to call PyLong_AsLongAndOverflow. */
|
||||||
|
@ -990,7 +998,11 @@ SyntaxError_str(PySyntaxErrorObject *self)
|
||||||
lineno here */
|
lineno here */
|
||||||
|
|
||||||
if (self->filename && PyUnicode_Check(self->filename)) {
|
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);
|
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);
|
return PyObject_Str(self->msg ? self->msg : Py_None);
|
||||||
|
|
||||||
if (filename && have_lineno)
|
if (filename && have_lineno)
|
||||||
return PyUnicode_FromFormat("%S (%s, line %ld)",
|
result = PyUnicode_FromFormat("%S (%U, line %ld)",
|
||||||
self->msg ? self->msg : Py_None,
|
self->msg ? self->msg : Py_None,
|
||||||
my_basename(filename),
|
filename,
|
||||||
PyLong_AsLongAndOverflow(self->lineno, &overflow));
|
PyLong_AsLongAndOverflow(self->lineno, &overflow));
|
||||||
else if (filename)
|
else if (filename)
|
||||||
return PyUnicode_FromFormat("%S (%s)",
|
result = PyUnicode_FromFormat("%S (%U)",
|
||||||
self->msg ? self->msg : Py_None,
|
self->msg ? self->msg : Py_None,
|
||||||
my_basename(filename));
|
filename);
|
||||||
else /* only have_lineno */
|
else /* only have_lineno */
|
||||||
return PyUnicode_FromFormat("%S (line %ld)",
|
result = PyUnicode_FromFormat("%S (line %ld)",
|
||||||
self->msg ? self->msg : Py_None,
|
self->msg ? self->msg : Py_None,
|
||||||
PyLong_AsLongAndOverflow(self->lineno, &overflow));
|
PyLong_AsLongAndOverflow(self->lineno, &overflow));
|
||||||
|
Py_XDECREF(filename);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyMemberDef SyntaxError_members[] = {
|
static PyMemberDef SyntaxError_members[] = {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue