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.
This commit is contained in:
Fred Drake 2000-08-15 16:20:36 +00:00
parent a811a5b13a
commit 185a29b08f

View 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),