[Python-checkins] CVS: python/dist/src/Python exceptions.c,1.12,1.13

Fred L. Drake python-dev@python.org
Tue, 15 Aug 2000 08:46:20 -0700


Update of /cvsroot/python/python/dist/src/Python
In directory slayer.i.sourceforge.net:/tmp/cvs-serv25426/Python

Modified Files:
	exceptions.c 
Log Message:

SyntaxError__str__():  Do more formatting of the exception here, rather
	than depending on the site that raises the exception.  If the
	filename and lineno attributes are set on the exception object,
	use them to augment the message displayed.

This is part of what is needed to close SoruceForge bug #110628
(Jitterbug PR#278).


Index: exceptions.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/exceptions.c,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -r1.12 -r1.13
*** exceptions.c	2000/08/15 00:37:32	1.12
--- exceptions.c	2000/08/15 15:46:16	1.13
***************
*** 245,249 ****
      PyObject *out;
  
!     if (!PyArg_ParseTuple(args, "O", &self))
          return NULL;
  
--- 245,249 ----
      PyObject *out;
  
!     if (!PyArg_ParseTuple(args, "O:__str__", &self))
          return NULL;
  
***************
*** 283,287 ****
      PyObject *index;
  
!     if (!PyArg_ParseTuple(args, "OO", &self, &index))
          return NULL;
  
--- 283,287 ----
      PyObject *index;
  
!     if (!PyArg_ParseTuple(args, "OO:__getitem__", &self, &index))
          return NULL;
  
***************
*** 525,529 ****
      PyObject *rtnval = NULL;
  
!     if (!PyArg_ParseTuple(args, "O", &self))
  	return NULL;
      
--- 525,529 ----
      PyObject *rtnval = NULL;
  
!     if (!PyArg_ParseTuple(args, "O:__str__", &self))
  	return NULL;
      
***************
*** 735,748 ****
      PyObject *msg;
      PyObject *str;
  
!     if (!PyArg_ParseTuple(args, "O", &self))
  	return NULL;
  
      if (!(msg = PyObject_GetAttrString(self, "msg")))
  	return NULL;
!     
      str = PyObject_Str(msg);
      Py_DECREF(msg);
!     return str;
  }
  
--- 735,797 ----
      PyObject *msg;
      PyObject *str;
+     PyObject *filename, *lineno, *result;
  
!     if (!PyArg_ParseTuple(args, "O:__str__", &self))
  	return NULL;
  
      if (!(msg = PyObject_GetAttrString(self, "msg")))
  	return NULL;
! 
      str = PyObject_Str(msg);
      Py_DECREF(msg);
!     result = str;
! 
!     /* XXX -- do all the additional formatting with filename and
!        lineno here */
! 
!     if (PyString_Check(str)) {
! 	int have_filename = 0;
! 	int have_lineno = 0;
! 	char *buffer = NULL;
! 
! 	if (filename = PyObject_GetAttrString(self, "filename"))
! 	    have_filename = PyString_Check(filename);
! 	else
! 	    PyErr_Clear();
! 	if (lineno = PyObject_GetAttrString(self, "lineno"))
! 	    have_lineno = PyInt_Check(lineno);
! 	else
! 	    PyErr_Clear();
! 
! 	if (have_filename || have_lineno) {
! 	    int bufsize = (PyString_GET_SIZE(str) + 64 +
! 			   PyString_GET_SIZE(filename));
! 
! 	    buffer = PyMem_Malloc(bufsize);
! 	    if (buffer != NULL) {
! 		if (have_filename && have_lineno)
! 		    sprintf(buffer, "%s (%s, line %d)",
! 			    PyString_AS_STRING(str),
! 			    PyString_AS_STRING(filename),
! 			    PyInt_AsLong(lineno));
! 		else if (have_filename)
! 		    sprintf(buffer, "%s (%s)",
! 			    PyString_AS_STRING(str),
! 			    PyString_AS_STRING(filename));
! 		else if (have_lineno)
! 		    sprintf(buffer, "%s (line %d)",
! 			    PyString_AS_STRING(str),
! 			    PyInt_AsLong(lineno));
! 		result = PyString_FromString(buffer);
! 		if (result == NULL)
! 		    result = str;
! 		else
! 		    Py_DECREF(str);
! 	    }
! 	}
! 	Py_XDECREF(filename);
! 	Py_XDECREF(lineno);
!     }
!     return result;
  }