From 185a29b08ffcb1370e469f25173f185ed0a60d39 Mon Sep 17 00:00:00 2001 From: Fred Drake Date: Tue, 15 Aug 2000 16:20:36 +0000 Subject: [PATCH] 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. --- Python/exceptions.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/Python/exceptions.c b/Python/exceptions.c index 4d279791e8ce..abacda4d995f 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),