summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorCollin Winter <collinw@gmail.com>2007-08-31 00:04:24 (GMT)
committerCollin Winter <collinw@gmail.com>2007-08-31 00:04:24 (GMT)
commit828f04ac3f0dd3b68b4dbf42a79ebb846d1de568 (patch)
tree21e25d3d969ce636c32539e4d4b5255dc4c85702 /Python
parent150b7d7d02eca6970d792f3e6887f957a36b6ca2 (diff)
downloadcpython-828f04ac3f0dd3b68b4dbf42a79ebb846d1de568.zip
cpython-828f04ac3f0dd3b68b4dbf42a79ebb846d1de568.tar.gz
cpython-828f04ac3f0dd3b68b4dbf42a79ebb846d1de568.tar.bz2
Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Diffstat (limited to 'Python')
-rw-r--r--Python/Python-ast.c32
-rw-r--r--Python/ast.c39
-rw-r--r--Python/ceval.c134
-rw-r--r--Python/compile.c22
-rw-r--r--Python/graminit.c51
-rw-r--r--Python/import.c1
-rw-r--r--Python/symtable.c12
7 files changed, 113 insertions, 178 deletions
diff --git a/Python/Python-ast.c b/Python/Python-ast.c
index 605a152..9089b9a 100644
--- a/Python/Python-ast.c
+++ b/Python/Python-ast.c
@@ -2,7 +2,7 @@
/*
- __version__ 56266.
+ __version__ 465.
This module must be committed separately after each AST grammar change;
The __version__ number is set to the revision number of the commit
@@ -101,9 +101,8 @@ static char *With_fields[]={
};
static PyTypeObject *Raise_type;
static char *Raise_fields[]={
- "type",
- "inst",
- "tback",
+ "exc",
+ "cause",
};
static PyTypeObject *TryExcept_type;
static char *TryExcept_fields[]={
@@ -510,7 +509,7 @@ static int init_types(void)
if (!If_type) return 0;
With_type = make_type("With", stmt_type, With_fields, 3);
if (!With_type) return 0;
- Raise_type = make_type("Raise", stmt_type, Raise_fields, 3);
+ Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
if (!Raise_type) return 0;
TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3);
if (!TryExcept_type) return 0;
@@ -1052,17 +1051,15 @@ With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int lineno,
}
stmt_ty
-Raise(expr_ty type, expr_ty inst, expr_ty tback, int lineno, int col_offset,
- PyArena *arena)
+Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, PyArena *arena)
{
stmt_ty p;
p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
if (!p)
return NULL;
p->kind = Raise_kind;
- p->v.Raise.type = type;
- p->v.Raise.inst = inst;
- p->v.Raise.tback = tback;
+ p->v.Raise.exc = exc;
+ p->v.Raise.cause = cause;
p->lineno = lineno;
p->col_offset = col_offset;
return p;
@@ -2221,19 +2218,14 @@ ast2obj_stmt(void* _o)
case Raise_kind:
result = PyType_GenericNew(Raise_type, NULL, NULL);
if (!result) goto failed;
- value = ast2obj_expr(o->v.Raise.type);
- if (!value) goto failed;
- if (PyObject_SetAttrString(result, "type", value) == -1)
- goto failed;
- Py_DECREF(value);
- value = ast2obj_expr(o->v.Raise.inst);
+ value = ast2obj_expr(o->v.Raise.exc);
if (!value) goto failed;
- if (PyObject_SetAttrString(result, "inst", value) == -1)
+ if (PyObject_SetAttrString(result, "exc", value) == -1)
goto failed;
Py_DECREF(value);
- value = ast2obj_expr(o->v.Raise.tback);
+ value = ast2obj_expr(o->v.Raise.cause);
if (!value) goto failed;
- if (PyObject_SetAttrString(result, "tback", value) == -1)
+ if (PyObject_SetAttrString(result, "cause", value) == -1)
goto failed;
Py_DECREF(value);
break;
@@ -3179,7 +3171,7 @@ init_ast(void)
if (PyDict_SetItemString(d, "AST", (PyObject*)AST_type) < 0) return;
if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
return;
- if (PyModule_AddStringConstant(m, "__version__", "56266") < 0)
+ if (PyModule_AddStringConstant(m, "__version__", "465") < 0)
return;
if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return;
if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
diff --git a/Python/ast.c b/Python/ast.c
index c13d093..8dd3c4ab 100644
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -2199,39 +2199,18 @@ ast_for_flow_stmt(struct compiling *c, const node *n)
}
case raise_stmt:
if (NCH(ch) == 1)
- return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
- else if (NCH(ch) == 2) {
+ return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
+ else if (NCH(ch) >= 2) {
+ expr_ty cause = NULL;
expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
if (!expression)
return NULL;
- return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
- }
- else if (NCH(ch) == 4) {
- expr_ty expr1, expr2;
-
- expr1 = ast_for_expr(c, CHILD(ch, 1));
- if (!expr1)
- return NULL;
- expr2 = ast_for_expr(c, CHILD(ch, 3));
- if (!expr2)
- return NULL;
-
- return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena);
- }
- else if (NCH(ch) == 6) {
- expr_ty expr1, expr2, expr3;
-
- expr1 = ast_for_expr(c, CHILD(ch, 1));
- if (!expr1)
- return NULL;
- expr2 = ast_for_expr(c, CHILD(ch, 3));
- if (!expr2)
- return NULL;
- expr3 = ast_for_expr(c, CHILD(ch, 5));
- if (!expr3)
- return NULL;
-
- return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena);
+ if (NCH(ch) == 4) {
+ cause = ast_for_expr(c, CHILD(ch, 3));
+ if (!cause)
+ return NULL;
+ }
+ return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
}
default:
PyErr_Format(PyExc_SystemError,
diff --git a/Python/ceval.c b/Python/ceval.c
index be804ad..24d4dec 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -484,7 +484,7 @@ enum why_code {
WHY_YIELD = 0x0040 /* 'yield' operator */
};
-static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
+static enum why_code do_raise(PyObject *, PyObject *);
static int unpack_iterable(PyObject *, int, int, PyObject **);
/* for manipulating the thread switch and periodic "stuff" - used to be
@@ -1465,18 +1465,14 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
default: switch (opcode) {
#endif
case RAISE_VARARGS:
- u = v = w = NULL;
+ v = w = NULL;
switch (oparg) {
- case 3:
- u = POP(); /* traceback */
- /* Fallthrough */
- case 2:
- v = POP(); /* value */
- /* Fallthrough */
+ case 2:
+ v = POP(); /* cause */
case 1:
w = POP(); /* exc */
case 0: /* Fallthrough */
- why = do_raise(w, v, u);
+ why = do_raise(w, v);
break;
default:
PyErr_SetString(PyExc_SystemError,
@@ -2880,6 +2876,7 @@ set_exc_info(PyThreadState *tstate,
tstate->exc_type = type;
tstate->exc_value = value;
tstate->exc_traceback = tb;
+ PyException_SetTraceback(value, tb);
Py_XDECREF(tmp_type);
Py_XDECREF(tmp_value);
Py_XDECREF(tmp_tb);
@@ -2928,95 +2925,78 @@ reset_exc_info(PyThreadState *tstate)
/* Logic for the raise statement (too complicated for inlining).
This *consumes* a reference count to each of its arguments. */
static enum why_code
-do_raise(PyObject *type, PyObject *value, PyObject *tb)
+do_raise(PyObject *exc, PyObject *cause)
{
- if (type == NULL) {
+ PyObject *type = NULL, *value = NULL, *tb = NULL;
+
+ if (exc == NULL) {
/* Reraise */
PyThreadState *tstate = PyThreadState_GET();
- type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
+ type = tstate->exc_type;
value = tstate->exc_value;
tb = tstate->exc_traceback;
- Py_XINCREF(type);
+ if (type == Py_None) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "No active exception to reraise");
+ return WHY_EXCEPTION;
+ }
+ Py_XINCREF(type);
Py_XINCREF(value);
Py_XINCREF(tb);
+ PyErr_Restore(type, value, tb);
+ return WHY_RERAISE;
}
/* We support the following forms of raise:
- raise <class>, <classinstance>
- raise <class>, <argument tuple>
- raise <class>, None
- raise <class>, <argument>
- raise <classinstance>, None
- raise <string>, <object>
- raise <string>, None
-
- An omitted second argument is the same as None.
-
- In addition, raise <tuple>, <anything> is the same as
- raising the tuple's first item (and it better have one!);
- this rule is applied recursively.
-
- Finally, an optional third argument can be supplied, which
- gives the traceback to be substituted (useful when
- re-raising an exception after examining it). */
-
- /* First, check the traceback argument, replacing None with
- NULL. */
- if (tb == Py_None) {
- Py_DECREF(tb);
- tb = NULL;
- }
- else if (tb != NULL && !PyTraceBack_Check(tb)) {
- PyErr_SetString(PyExc_TypeError,
- "raise: arg 3 must be a traceback or None");
- goto raise_error;
- }
-
- /* Next, replace a missing value with None */
- if (value == NULL) {
- value = Py_None;
- Py_INCREF(value);
- }
+ raise
+ raise <instance>
+ raise <type> */
- /* Next, repeatedly, replace a tuple exception with its first item */
- while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
- PyObject *tmp = type;
- type = PyTuple_GET_ITEM(type, 0);
+ if (PyExceptionClass_Check(exc)) {
+ type = exc;
+ value = PyObject_CallObject(exc, NULL);
+ if (value == NULL)
+ goto raise_error;
+ }
+ else if (PyExceptionInstance_Check(exc)) {
+ value = exc;
+ type = PyExceptionInstance_Class(exc);
Py_INCREF(type);
- Py_DECREF(tmp);
- }
-
- if (PyExceptionClass_Check(type))
- PyErr_NormalizeException(&type, &value, &tb);
-
- else if (PyExceptionInstance_Check(type)) {
- /* Raising an instance. The value should be a dummy. */
- if (value != Py_None) {
- PyErr_SetString(PyExc_TypeError,
- "instance exception may not have a separate value");
- goto raise_error;
- }
- else {
- /* Normalize to raise <class>, <instance> */
- Py_DECREF(value);
- value = type;
- type = PyExceptionInstance_Class(type);
- Py_INCREF(type);
- }
}
else {
/* Not something you can raise. You get an exception
anyway, just not what you specified :-) */
+ Py_DECREF(exc);
+ Py_XDECREF(cause);
PyErr_SetString(PyExc_TypeError,
"exceptions must derive from BaseException");
goto raise_error;
}
+
+ tb = PyException_GetTraceback(value);
+ if (cause) {
+ PyObject *fixed_cause;
+ if (PyExceptionClass_Check(cause)) {
+ fixed_cause = PyObject_CallObject(cause, NULL);
+ if (fixed_cause == NULL)
+ goto raise_error;
+ }
+ else if (PyExceptionInstance_Check(cause)) {
+ fixed_cause = cause;
+ }
+ else {
+ Py_DECREF(cause);
+ PyErr_SetString(PyExc_TypeError,
+ "exception causes must derive from BaseException");
+ goto raise_error;
+ }
+ PyException_SetCause(value, fixed_cause);
+ }
+
PyErr_Restore(type, value, tb);
- if (tb == NULL)
- return WHY_EXCEPTION;
- else
- return WHY_RERAISE;
- raise_error:
+ return WHY_EXCEPTION;
+
+raise_error:
Py_XDECREF(value);
Py_XDECREF(type);
Py_XDECREF(tb);
diff --git a/Python/compile.c b/Python/compile.c
index 3a21127..e569102 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -2187,11 +2187,9 @@ compiler_assert(struct compiler *c, stmt_ty s)
ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
if (s->v.Assert.msg) {
VISIT(c, expr, s->v.Assert.msg);
- ADDOP_I(c, RAISE_VARARGS, 2);
- }
- else {
- ADDOP_I(c, RAISE_VARARGS, 1);
+ ADDOP_I(c, CALL_FUNCTION, 1);
}
+ ADDOP_I(c, RAISE_VARARGS, 1);
compiler_use_next_block(c, end);
ADDOP(c, POP_TOP);
return 1;
@@ -2244,17 +2242,13 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s)
return compiler_if(c, s);
case Raise_kind:
n = 0;
- if (s->v.Raise.type) {
- VISIT(c, expr, s->v.Raise.type);
+ if (s->v.Raise.exc) {
+ VISIT(c, expr, s->v.Raise.exc);
n++;
- if (s->v.Raise.inst) {
- VISIT(c, expr, s->v.Raise.inst);
- n++;
- if (s->v.Raise.tback) {
- VISIT(c, expr, s->v.Raise.tback);
- n++;
- }
- }
+ if (s->v.Raise.cause) {
+ VISIT(c, expr, s->v.Raise.cause);
+ n++;
+ }
}
ADDOP_I(c, RAISE_VARARGS, n);
break;
diff --git a/Python/graminit.c b/Python/graminit.c
index 95905d7..081a072 100644
--- a/Python/graminit.c
+++ b/Python/graminit.c
@@ -503,34 +503,25 @@ static arc arcs_24_1[2] = {
{0, 1},
};
static arc arcs_24_2[2] = {
- {30, 3},
+ {71, 3},
{0, 2},
};
static arc arcs_24_3[1] = {
{24, 4},
};
-static arc arcs_24_4[2] = {
- {30, 5},
+static arc arcs_24_4[1] = {
{0, 4},
};
-static arc arcs_24_5[1] = {
- {24, 6},
-};
-static arc arcs_24_6[1] = {
- {0, 6},
-};
-static state states_24[7] = {
+static state states_24[5] = {
{1, arcs_24_0},
{2, arcs_24_1},
{2, arcs_24_2},
{1, arcs_24_3},
- {2, arcs_24_4},
- {1, arcs_24_5},
- {1, arcs_24_6},
+ {1, arcs_24_4},
};
static arc arcs_25_0[2] = {
- {71, 1},
{72, 1},
+ {73, 1},
};
static arc arcs_25_1[1] = {
{0, 1},
@@ -540,10 +531,10 @@ static state states_25[2] = {
{1, arcs_25_1},
};
static arc arcs_26_0[1] = {
- {73, 1},
+ {74, 1},
};
static arc arcs_26_1[1] = {
- {74, 2},
+ {75, 2},
};
static arc arcs_26_2[1] = {
{0, 2},
@@ -554,7 +545,7 @@ static state states_26[3] = {
{1, arcs_26_2},
};
static arc arcs_27_0[1] = {
- {75, 1},
+ {71, 1},
};
static arc arcs_27_1[3] = {
{76, 2},
@@ -565,10 +556,10 @@ static arc arcs_27_2[4] = {
{76, 2},
{77, 2},
{12, 3},
- {73, 4},
+ {74, 4},
};
static arc arcs_27_3[1] = {
- {73, 4},
+ {74, 4},
};
static arc arcs_27_4[3] = {
{31, 5},
@@ -1733,9 +1724,9 @@ static state states_80[3] = {
};
static dfa dfas[81] = {
{256, "single_input", 0, 3, states_0,
- "\004\050\060\200\000\000\000\050\170\052\034\144\011\040\004\000\200\041\224\017\101"},
+ "\004\050\060\200\000\000\000\050\370\044\034\144\011\040\004\000\200\041\224\017\101"},
{257, "file_input", 0, 2, states_1,
- "\204\050\060\200\000\000\000\050\170\052\034\144\011\040\004\000\200\041\224\017\101"},
+ "\204\050\060\200\000\000\000\050\370\044\034\144\011\040\004\000\200\041\224\017\101"},
{258, "eval_input", 0, 3, states_2,
"\000\040\040\200\000\000\000\000\000\040\000\000\000\040\004\000\200\041\224\017\000"},
{259, "decorator", 0, 7, states_3,
@@ -1757,11 +1748,11 @@ static dfa dfas[81] = {
{267, "vfpdef", 0, 2, states_11,
"\000\000\040\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"},
{268, "stmt", 0, 2, states_12,
- "\000\050\060\200\000\000\000\050\170\052\034\144\011\040\004\000\200\041\224\017\101"},
+ "\000\050\060\200\000\000\000\050\370\044\034\144\011\040\004\000\200\041\224\017\101"},
{269, "simple_stmt", 0, 4, states_13,
- "\000\040\040\200\000\000\000\050\170\052\034\000\000\040\004\000\200\041\224\017\100"},
+ "\000\040\040\200\000\000\000\050\370\044\034\000\000\040\004\000\200\041\224\017\100"},
{270, "small_stmt", 0, 2, states_14,
- "\000\040\040\200\000\000\000\050\170\052\034\000\000\040\004\000\200\041\224\017\100"},
+ "\000\040\040\200\000\000\000\050\370\044\034\000\000\040\004\000\200\041\224\017\100"},
{271, "expr_stmt", 0, 6, states_15,
"\000\040\040\200\000\000\000\000\000\040\000\000\000\040\004\000\200\041\224\017\000"},
{272, "augassign", 0, 2, states_16,
@@ -1780,14 +1771,14 @@ static dfa dfas[81] = {
"\000\000\000\000\000\000\000\000\040\000\000\000\000\000\000\000\000\000\000\000\000"},
{279, "yield_stmt", 0, 2, states_23,
"\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\100"},
- {280, "raise_stmt", 0, 7, states_24,
+ {280, "raise_stmt", 0, 5, states_24,
"\000\000\000\000\000\000\000\000\100\000\000\000\000\000\000\000\000\000\000\000\000"},
{281, "import_stmt", 0, 2, states_25,
- "\000\000\000\000\000\000\000\000\000\012\000\000\000\000\000\000\000\000\000\000\000"},
+ "\000\000\000\000\000\000\000\000\200\004\000\000\000\000\000\000\000\000\000\000\000"},
{282, "import_name", 0, 3, states_26,
- "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000\000\000\000\000\000\000"},
+ "\000\000\000\000\000\000\000\000\000\004\000\000\000\000\000\000\000\000\000\000\000"},
{283, "import_from", 0, 8, states_27,
- "\000\000\000\000\000\000\000\000\000\010\000\000\000\000\000\000\000\000\000\000\000"},
+ "\000\000\000\000\000\000\000\000\200\000\000\000\000\000\000\000\000\000\000\000\000"},
{284, "import_as_name", 0, 4, states_28,
"\000\000\040\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"},
{285, "dotted_as_name", 0, 4, states_29,
@@ -1821,7 +1812,7 @@ static dfa dfas[81] = {
{299, "except_clause", 0, 5, states_43,
"\000\000\000\000\000\000\000\000\000\000\000\000\100\000\000\000\000\000\000\000\000"},
{300, "suite", 0, 5, states_44,
- "\004\040\040\200\000\000\000\050\170\052\034\000\000\040\004\000\200\041\224\017\100"},
+ "\004\040\040\200\000\000\000\050\370\044\034\000\000\040\004\000\200\041\224\017\100"},
{301, "test", 0, 6, states_45,
"\000\040\040\200\000\000\000\000\000\040\000\000\000\040\004\000\200\041\224\017\000"},
{302, "test_nocond", 0, 2, states_46,
@@ -1967,11 +1958,11 @@ static label labels[167] = {
{1, "continue"},
{1, "return"},
{1, "raise"},
+ {1, "from"},
{282, 0},
{283, 0},
{1, "import"},
{287, 0},
- {1, "from"},
{23, 0},
{52, 0},
{286, 0},
diff --git a/Python/import.c b/Python/import.c
index 2ef6aec..33cafd0 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -74,6 +74,7 @@ extern time_t PyOS_GetLastModificationTime(char *, FILE *);
3040 (added signature annotations)
3050 (print becomes a function)
3060 (PEP 3115 metaclass syntax)
+ 3070 (PEP 3109 raise changes)
.
*/
#define MAGIC (3060 | ((long)'\r'<<16) | ((long)'\n'<<24))
diff --git a/Python/symtable.c b/Python/symtable.c
index 59eef02..83e571e 100644
--- a/Python/symtable.c
+++ b/Python/symtable.c
@@ -1101,13 +1101,11 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
VISIT_SEQ(st, stmt, s->v.If.orelse);
break;
case Raise_kind:
- if (s->v.Raise.type) {
- VISIT(st, expr, s->v.Raise.type);
- if (s->v.Raise.inst) {
- VISIT(st, expr, s->v.Raise.inst);
- if (s->v.Raise.tback)
- VISIT(st, expr, s->v.Raise.tback);
- }
+ if (s->v.Raise.exc) {
+ VISIT(st, expr, s->v.Raise.exc);
+ if (s->v.Raise.cause) {
+ VISIT(st, expr, s->v.Raise.cause);
+ }
}
break;
case TryExcept_kind: