mirror of
https://github.com/python/cpython.git
synced 2025-10-22 06:32:43 +00:00
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.
This commit is contained in:
parent
83cb797380
commit
dcf08e0dfe
1 changed files with 29 additions and 9 deletions
|
@ -31,6 +31,7 @@ redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
||||||
#include "compile.h"
|
#include "compile.h"
|
||||||
#include "opcode.h"
|
#include "opcode.h"
|
||||||
#include "structmember.h"
|
#include "structmember.h"
|
||||||
|
#include "osdefs.h" /* SEP */
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#ifdef HAVE_LIMITS_H
|
#ifdef HAVE_LIMITS_H
|
||||||
|
@ -304,8 +305,7 @@ static void
|
||||||
com_error(struct compiling *c, PyObject *exc, char *msg)
|
com_error(struct compiling *c, PyObject *exc, char *msg)
|
||||||
{
|
{
|
||||||
size_t n = strlen(msg);
|
size_t n = strlen(msg);
|
||||||
PyObject *v;
|
PyObject *v, *tb, *tmp;
|
||||||
char buffer[30];
|
|
||||||
char *s;
|
char *s;
|
||||||
c->c_errors++;
|
c->c_errors++;
|
||||||
if (c->c_lineno <= 1) {
|
if (c->c_lineno <= 1) {
|
||||||
|
@ -313,15 +313,34 @@ com_error(struct compiling *c, PyObject *exc, char *msg)
|
||||||
PyErr_SetString(exc, msg);
|
PyErr_SetString(exc, msg);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
sprintf(buffer, " (line %d)", c->c_lineno);
|
v = PyString_FromString(msg);
|
||||||
v = PyString_FromStringAndSize((char *)NULL, n + strlen(buffer));
|
|
||||||
if (v == NULL)
|
if (v == NULL)
|
||||||
return; /* MemoryError, too bad */
|
return; /* MemoryError, too bad */
|
||||||
s = PyString_AS_STRING((PyStringObject *)v);
|
|
||||||
strcpy(s, msg);
|
|
||||||
strcat(s, buffer);
|
|
||||||
PyErr_SetObject(exc, v);
|
PyErr_SetObject(exc, v);
|
||||||
Py_DECREF(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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1178,7 +1197,7 @@ com_argument(struct compiling *c, node *n, PyObject **pkeywords)
|
||||||
if (NCH(n) == 1) {
|
if (NCH(n) == 1) {
|
||||||
if (*pkeywords != NULL) {
|
if (*pkeywords != NULL) {
|
||||||
com_error(c, PyExc_SyntaxError,
|
com_error(c, PyExc_SyntaxError,
|
||||||
"non-keyword arg after keyword arg");
|
"non-keyword arg after keyword arg");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
com_node(c, CHILD(n, 0));
|
com_node(c, CHILD(n, 0));
|
||||||
|
@ -2997,7 +3016,8 @@ com_arglist(struct compiling *c, node *n)
|
||||||
c->c_errors++;
|
c->c_errors++;
|
||||||
}
|
}
|
||||||
if (PyDict_GetItem(c->c_locals, nameval)) {
|
if (PyDict_GetItem(c->c_locals, nameval)) {
|
||||||
com_error(c, PyExc_SyntaxError,"duplicate argument in function definition");
|
com_error(c, PyExc_SyntaxError,
|
||||||
|
"duplicate argument in function definition");
|
||||||
}
|
}
|
||||||
com_newlocal_o(c, nameval);
|
com_newlocal_o(c, nameval);
|
||||||
Py_DECREF(nameval);
|
Py_DECREF(nameval);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue