[Python-checkins] CVS: python/dist/src/Python compile.c,2.123,2.124

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


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

Modified Files:
	compile.c 
Log Message:

When raising a SyntaxError, make a best-effort attempt to set the
filename and lineno attributes, but do not mask the SyntaxError if we
fail.

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

Wrap a long line to fit in under 80 columns.


Index: compile.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/compile.c,v
retrieving revision 2.123
retrieving revision 2.124
diff -C2 -r2.123 -r2.124
*** compile.c	2000/08/13 17:05:17	2.123
--- compile.c	2000/08/15 15:49:44	2.124
***************
*** 32,35 ****
--- 32,36 ----
  #include "opcode.h"
  #include "structmember.h"
+ #include "osdefs.h"			/* SEP */
  
  #include <ctype.h>
***************
*** 305,310 ****
  {
  	size_t n = strlen(msg);
! 	PyObject *v;
! 	char buffer[30];
  	char *s;
  	c->c_errors++;
--- 306,310 ----
  {
  	size_t n = strlen(msg);
! 	PyObject *v, *tb, *tmp;
  	char *s;
  	c->c_errors++;
***************
*** 314,326 ****
  		return;
  	}
! 	sprintf(buffer, " (line %d)", c->c_lineno);
! 	v = PyString_FromStringAndSize((char *)NULL, n + strlen(buffer));
  	if (v == NULL)
  		return; /* MemoryError, too bad */
- 	s = PyString_AS_STRING((PyStringObject *)v);
- 	strcpy(s, msg);
- 	strcat(s, buffer);
  	PyErr_SetObject(exc, v);
  	Py_DECREF(v);
  }
  
--- 314,345 ----
  		return;
  	}
! 	v = PyString_FromString(msg);
  	if (v == NULL)
  		return; /* MemoryError, too bad */
  	PyErr_SetObject(exc, v);
  	Py_DECREF(v);
+ 
+ 	/* add attributes for the line number and filename for the error */
+ 	PyErr_Fetch(&exc, &v, &tb);
+ 	PyErr_NormalizeException(&exc, &v, &tb);
+ 	tmp = PyInt_FromLong(c->c_lineno);
+ 	if (tmp == NULL)
+ 		PyErr_Clear();
+ 	else {
+ 		if (PyObject_SetAttrString(v, "lineno", tmp))
+ 			PyErr_Clear();
+ 		Py_DECREF(tmp);
+ 	}
+ 	if (c->c_filename != NULL) {
+ 		tmp = PyString_FromString(c->c_filename);
+ 		if (tmp == NULL)
+ 			PyErr_Clear();
+ 		else {
+ 			if (PyObject_SetAttrString(v, "filename", tmp))
+ 				PyErr_Clear();
+ 			Py_DECREF(tmp);
+ 		}
+ 	}
+ 	PyErr_Restore(exc, v, tb);
  }
  
***************
*** 1179,1183 ****
  		if (*pkeywords != NULL) {
  			com_error(c, PyExc_SyntaxError,
! 				   "non-keyword arg after keyword arg");
  		}
  		else {
--- 1198,1202 ----
  		if (*pkeywords != NULL) {
  			com_error(c, PyExc_SyntaxError,
! 				  "non-keyword arg after keyword arg");
  		}
  		else {
***************
*** 2998,3002 ****
  		}
  		if (PyDict_GetItem(c->c_locals, nameval)) {
! 			com_error(c, PyExc_SyntaxError,"duplicate argument in function definition");
  		}
  		com_newlocal_o(c, nameval);
--- 3017,3022 ----
  		}
  		if (PyDict_GetItem(c->c_locals, nameval)) {
! 			com_error(c, PyExc_SyntaxError,
! 				  "duplicate argument in function definition");
  		}
  		com_newlocal_o(c, nameval);