diff options
author | Fred Drake <fdrake@acm.org> | 2000-08-15 16:20:36 (GMT) |
---|---|---|
committer | Fred Drake <fdrake@acm.org> | 2000-08-15 16:20:36 (GMT) |
commit | 185a29b08ffcb1370e469f25173f185ed0a60d39 (patch) | |
tree | a8f8bed01bff3ad473445b1145ae48b3b6aebf37 /Python | |
parent | a811a5b13a095d61d0560b95259a957a2c0a4c47 (diff) | |
download | cpython-185a29b08ffcb1370e469f25173f185ed0a60d39.zip cpython-185a29b08ffcb1370e469f25173f185ed0a60d39.tar.gz cpython-185a29b08ffcb1370e469f25173f185ed0a60d39.tar.bz2 |
my_basename(): Removes the leading path components from a path name,
returning a pointer to the start of the file's "base" name;
similar to os.path.basename().
SyntaxError__str__(): Use my_basename() to keep the length of the
file name included in the exception message short.
Diffstat (limited to 'Python')
-rw-r--r-- | Python/exceptions.c | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/Python/exceptions.c b/Python/exceptions.c index 4d27979..abacda4 100644 --- a/Python/exceptions.c +++ b/Python/exceptions.c @@ -19,6 +19,7 @@ */ #include "Python.h" +#include "osdefs.h" /* Caution: MS Visual C++ 6 errors if a single string literal exceeds * 2Kb. So the module docstring has been broken roughly in half, using @@ -729,6 +730,26 @@ SyntaxError__init__(PyObject *self, PyObject *args) } +/* 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; + } + return result; +} + + static PyObject * SyntaxError__str__(PyObject *self, PyObject *args) { @@ -772,12 +793,12 @@ SyntaxError__str__(PyObject *self, PyObject *args) if (have_filename && have_lineno) sprintf(buffer, "%s (%s, line %d)", PyString_AS_STRING(str), - PyString_AS_STRING(filename), + my_basename(PyString_AS_STRING(filename)), PyInt_AsLong(lineno)); else if (have_filename) sprintf(buffer, "%s (%s)", PyString_AS_STRING(str), - PyString_AS_STRING(filename)); + my_basename(PyString_AS_STRING(filename))); else if (have_lineno) sprintf(buffer, "%s (line %d)", PyString_AS_STRING(str), |