]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
my_basename(): Removes the leading path components from a path name,
authorFred Drake <fdrake@acm.org>
Tue, 15 Aug 2000 16:20:36 +0000 (16:20 +0000)
committerFred Drake <fdrake@acm.org>
Tue, 15 Aug 2000 16:20:36 +0000 (16:20 +0000)
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

index 4d279791e8ce972f651d4c1b4ec0c2829a2419ce..abacda4d995fa61cf47f0c26650fed0f502b3f70 100644 (file)
@@ -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),