comparison Python/exceptions.c @ 13996:d8069efcd76e legacy-trunk

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.
author Fred Drake <fdrake@acm.org>
date Tue, 15 Aug 2000 16:20:36 +0000
parents 7834a113ce24
children 6c7fa523726f
comparison
equal deleted inserted replaced
13995:65c1abf451dc 13996:d8069efcd76e
17 * 17 *
18 * Copyright (c) 1998-2000 by Secret Labs AB. All rights reserved. 18 * Copyright (c) 1998-2000 by Secret Labs AB. All rights reserved.
19 */ 19 */
20 20
21 #include "Python.h" 21 #include "Python.h"
22 #include "osdefs.h"
22 23
23 /* Caution: MS Visual C++ 6 errors if a single string literal exceeds 24 /* Caution: MS Visual C++ 6 errors if a single string literal exceeds
24 * 2Kb. So the module docstring has been broken roughly in half, using 25 * 2Kb. So the module docstring has been broken roughly in half, using
25 * compile-time literal concatenation. 26 * compile-time literal concatenation.
26 */ 27 */
727 Py_DECREF(args); 728 Py_DECREF(args);
728 return rtnval; 729 return rtnval;
729 } 730 }
730 731
731 732
733 /* This is called "my_basename" instead of just "basename" to avoid name
734 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
735 defined, and Python does define that. */
736 static char *
737 my_basename(char *name)
738 {
739 char *cp = name;
740 char *result = name;
741
742 if (name == NULL)
743 return "???";
744 while (*cp != '\0') {
745 if (*cp == SEP)
746 result = cp + 1;
747 ++cp;
748 }
749 return result;
750 }
751
752
732 static PyObject * 753 static PyObject *
733 SyntaxError__str__(PyObject *self, PyObject *args) 754 SyntaxError__str__(PyObject *self, PyObject *args)
734 { 755 {
735 PyObject *msg; 756 PyObject *msg;
736 PyObject *str; 757 PyObject *str;
770 buffer = PyMem_Malloc(bufsize); 791 buffer = PyMem_Malloc(bufsize);
771 if (buffer != NULL) { 792 if (buffer != NULL) {
772 if (have_filename && have_lineno) 793 if (have_filename && have_lineno)
773 sprintf(buffer, "%s (%s, line %d)", 794 sprintf(buffer, "%s (%s, line %d)",
774 PyString_AS_STRING(str), 795 PyString_AS_STRING(str),
775 PyString_AS_STRING(filename), 796 my_basename(PyString_AS_STRING(filename)),
776 PyInt_AsLong(lineno)); 797 PyInt_AsLong(lineno));
777 else if (have_filename) 798 else if (have_filename)
778 sprintf(buffer, "%s (%s)", 799 sprintf(buffer, "%s (%s)",
779 PyString_AS_STRING(str), 800 PyString_AS_STRING(str),
780 PyString_AS_STRING(filename)); 801 my_basename(PyString_AS_STRING(filename)));
781 else if (have_lineno) 802 else if (have_lineno)
782 sprintf(buffer, "%s (line %d)", 803 sprintf(buffer, "%s (line %d)",
783 PyString_AS_STRING(str), 804 PyString_AS_STRING(str),
784 PyInt_AsLong(lineno)); 805 PyInt_AsLong(lineno));
785 result = PyString_FromString(buffer); 806 result = PyString_FromString(buffer);