Kaydet (Commit) ad74aa8d authored tarafından Neal Norwitz's avatar Neal Norwitz

Merged revisions 62047 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/trunk

........
  r62047 | georg.brandl | 2008-03-29 23:40:17 -0700 (Sat, 29 Mar 2008) | 2 lines

  Patch #2511: Give the "excepthandler" AST item proper attributes by making it a Sum.
........
üst d3575938
...@@ -342,10 +342,17 @@ struct _comprehension { ...@@ -342,10 +342,17 @@ struct _comprehension {
asdl_seq *ifs; asdl_seq *ifs;
}; };
enum _excepthandler_kind {ExceptHandler_kind=1};
struct _excepthandler { struct _excepthandler {
expr_ty type; enum _excepthandler_kind kind;
identifier name; union {
asdl_seq *body; struct {
expr_ty type;
identifier name;
asdl_seq *body;
} ExceptHandler;
} v;
int lineno; int lineno;
int col_offset; int col_offset;
}; };
...@@ -525,8 +532,8 @@ slice_ty _Py_Index(expr_ty value, PyArena *arena); ...@@ -525,8 +532,8 @@ slice_ty _Py_Index(expr_ty value, PyArena *arena);
#define comprehension(a0, a1, a2, a3) _Py_comprehension(a0, a1, a2, a3) #define comprehension(a0, a1, a2, a3) _Py_comprehension(a0, a1, a2, a3)
comprehension_ty _Py_comprehension(expr_ty target, expr_ty iter, asdl_seq * comprehension_ty _Py_comprehension(expr_ty target, expr_ty iter, asdl_seq *
ifs, PyArena *arena); ifs, PyArena *arena);
#define excepthandler(a0, a1, a2, a3, a4, a5) _Py_excepthandler(a0, a1, a2, a3, a4, a5) #define ExceptHandler(a0, a1, a2, a3, a4, a5) _Py_ExceptHandler(a0, a1, a2, a3, a4, a5)
excepthandler_ty _Py_excepthandler(expr_ty type, identifier name, asdl_seq * excepthandler_ty _Py_ExceptHandler(expr_ty type, identifier name, asdl_seq *
body, int lineno, int col_offset, PyArena body, int lineno, int col_offset, PyArena
*arena); *arena);
#define arguments(a0, a1, a2, a3, a4, a5, a6, a7, a8) _Py_arguments(a0, a1, a2, a3, a4, a5, a6, a7, a8) #define arguments(a0, a1, a2, a3, a4, a5, a6, a7, a8) _Py_arguments(a0, a1, a2, a3, a4, a5, a6, a7, a8)
......
...@@ -152,7 +152,7 @@ def run_tests(): ...@@ -152,7 +152,7 @@ def run_tests():
#### EVERYTHING BELOW IS GENERATED ##### #### EVERYTHING BELOW IS GENERATED #####
exec_results = [ exec_results = [
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, [], None, None, [], []), [('Pass', (1, 9))], [], None)]), ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, [], None, None, [], []), [('Pass', (1, 9))], [], None)]),
('Module', [('ClassDef', (1, 0), 'C', [], [], None, None, [('Pass', (1, 8))], [], )]), ('Module', [('ClassDef', (1, 0), 'C', [], [], None, None, [('Pass', (1, 8))], [])]),
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, [], None, None, [], []), [('Return', (1, 8), ('Num', (1, 15), 1))], [], None)]), ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, [], None, None, [], []), [('Return', (1, 8), ('Num', (1, 15), 1))], [], None)]),
('Module', [('Delete', (1, 0), [('Name', (1, 4), 'v', ('Del',))])]), ('Module', [('Delete', (1, 0), [('Name', (1, 4), 'v', ('Del',))])]),
('Module', [('Assign', (1, 0), [('Name', (1, 0), 'v', ('Store',))], ('Num', (1, 4), 1))]), ('Module', [('Assign', (1, 0), [('Name', (1, 0), 'v', ('Store',))], ('Num', (1, 4), 1))]),
...@@ -161,7 +161,7 @@ exec_results = [ ...@@ -161,7 +161,7 @@ exec_results = [
('Module', [('While', (1, 0), ('Name', (1, 6), 'v', ('Load',)), [('Pass', (1, 8))], [])]), ('Module', [('While', (1, 0), ('Name', (1, 6), 'v', ('Load',)), [('Pass', (1, 8))], [])]),
('Module', [('If', (1, 0), ('Name', (1, 3), 'v', ('Load',)), [('Pass', (1, 5))], [])]), ('Module', [('If', (1, 0), ('Name', (1, 3), 'v', ('Load',)), [('Pass', (1, 5))], [])]),
('Module', [('Raise', (1, 0), ('Call', (1, 6), ('Name', (1, 6), 'Exception', ('Load',)), [('Str', (1, 16), 'string')], [], None, None), None)]), ('Module', [('Raise', (1, 0), ('Call', (1, 6), ('Name', (1, 6), 'Exception', ('Load',)), [('Str', (1, 16), 'string')], [], None, None), None)]),
('Module', [('TryExcept', (1, 0), [('Pass', (2, 2))], [('excepthandler', (3, 0), ('Name', (3, 7), 'Exception', ('Load',)), None, [('Pass', (4, 2))], 3, 0)], [])]), ('Module', [('TryExcept', (1, 0), [('Pass', (2, 2))], [('ExceptHandler', (3, 0), ('Name', (3, 7), 'Exception', ('Load',)), None, [('Pass', (4, 2))])], [])]),
('Module', [('TryFinally', (1, 0), [('Pass', (2, 2))], [('Pass', (4, 2))])]), ('Module', [('TryFinally', (1, 0), [('Pass', (2, 2))], [('Pass', (4, 2))])]),
('Module', [('Assert', (1, 0), ('Name', (1, 7), 'v', ('Load',)), None)]), ('Module', [('Assert', (1, 0), ('Name', (1, 7), 'v', ('Load',)), None)]),
('Module', [('Import', (1, 0), [('alias', 'sys', None)])]), ('Module', [('Import', (1, 0), [('alias', 'sys', None)])]),
......
...@@ -101,11 +101,8 @@ module Python version "$Revision$" ...@@ -101,11 +101,8 @@ module Python version "$Revision$"
comprehension = (expr target, expr iter, expr* ifs) comprehension = (expr target, expr iter, expr* ifs)
-- not sure what to call the first argument for raise and except -- not sure what to call the first argument for raise and except
-- TODO(jhylton): Figure out if there is a better way to handle excepthandler = ExceptHandler(expr? type, identifier? name, stmt* body)
-- lineno and col_offset fields, particularly when attributes (int lineno, int col_offset)
-- ast is exposed to Python.
excepthandler = (expr? type, identifier? name, stmt* body, int lineno,
int col_offset)
arguments = (arg* args, identifier? vararg, expr? varargannotation, arguments = (arg* args, identifier? vararg, expr? varargannotation,
arg* kwonlyargs, identifier? kwarg, arg* kwonlyargs, identifier? kwarg,
......
This diff is collapsed.
...@@ -2787,7 +2787,7 @@ ast_for_except_clause(struct compiling *c, const node *exc, node *body) ...@@ -2787,7 +2787,7 @@ ast_for_except_clause(struct compiling *c, const node *exc, node *body)
if (!suite_seq) if (!suite_seq)
return NULL; return NULL;
return excepthandler(NULL, NULL, suite_seq, LINENO(exc), return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
exc->n_col_offset, c->c_arena); exc->n_col_offset, c->c_arena);
} }
else if (NCH(exc) == 2) { else if (NCH(exc) == 2) {
...@@ -2801,7 +2801,7 @@ ast_for_except_clause(struct compiling *c, const node *exc, node *body) ...@@ -2801,7 +2801,7 @@ ast_for_except_clause(struct compiling *c, const node *exc, node *body)
if (!suite_seq) if (!suite_seq)
return NULL; return NULL;
return excepthandler(expression, NULL, suite_seq, LINENO(exc), return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
exc->n_col_offset, c->c_arena); exc->n_col_offset, c->c_arena);
} }
else if (NCH(exc) == 4) { else if (NCH(exc) == 4) {
...@@ -2817,7 +2817,7 @@ ast_for_except_clause(struct compiling *c, const node *exc, node *body) ...@@ -2817,7 +2817,7 @@ ast_for_except_clause(struct compiling *c, const node *exc, node *body)
if (!suite_seq) if (!suite_seq)
return NULL; return NULL;
return excepthandler(expression, e, suite_seq, LINENO(exc), return ExceptHandler(expression, e, suite_seq, LINENO(exc),
exc->n_col_offset, c->c_arena); exc->n_col_offset, c->c_arena);
} }
......
...@@ -1984,22 +1984,22 @@ compiler_try_except(struct compiler *c, stmt_ty s) ...@@ -1984,22 +1984,22 @@ compiler_try_except(struct compiler *c, stmt_ty s)
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
s->v.TryExcept.handlers, i); s->v.TryExcept.handlers, i);
if (!handler->type && i < n-1) if (!handler->v.ExceptHandler.type && i < n-1)
return compiler_error(c, "default 'except:' must be last"); return compiler_error(c, "default 'except:' must be last");
c->u->u_lineno_set = 0; c->u->u_lineno_set = 0;
c->u->u_lineno = handler->lineno; c->u->u_lineno = handler->lineno;
except = compiler_new_block(c); except = compiler_new_block(c);
if (except == NULL) if (except == NULL)
return 0; return 0;
if (handler->type) { if (handler->v.ExceptHandler.type) {
ADDOP(c, DUP_TOP); ADDOP(c, DUP_TOP);
VISIT(c, expr, handler->type); VISIT(c, expr, handler->v.ExceptHandler.type);
ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH); ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
ADDOP_JREL(c, JUMP_IF_FALSE, except); ADDOP_JREL(c, JUMP_IF_FALSE, except);
ADDOP(c, POP_TOP); ADDOP(c, POP_TOP);
} }
ADDOP(c, POP_TOP); ADDOP(c, POP_TOP);
if (handler->name) { if (handler->v.ExceptHandler.name) {
basicblock *cleanup_end, *cleanup_body; basicblock *cleanup_end, *cleanup_body;
cleanup_end = compiler_new_block(c); cleanup_end = compiler_new_block(c);
...@@ -2007,7 +2007,7 @@ compiler_try_except(struct compiler *c, stmt_ty s) ...@@ -2007,7 +2007,7 @@ compiler_try_except(struct compiler *c, stmt_ty s)
if(!(cleanup_end || cleanup_body)) if(!(cleanup_end || cleanup_body))
return 0; return 0;
compiler_nameop(c, handler->name, Store); compiler_nameop(c, handler->v.ExceptHandler.name, Store);
ADDOP(c, POP_TOP); ADDOP(c, POP_TOP);
/* /*
...@@ -2028,7 +2028,7 @@ compiler_try_except(struct compiler *c, stmt_ty s) ...@@ -2028,7 +2028,7 @@ compiler_try_except(struct compiler *c, stmt_ty s)
return 0; return 0;
/* second # body */ /* second # body */
VISIT_SEQ(c, stmt, handler->body); VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
ADDOP(c, POP_BLOCK); ADDOP(c, POP_BLOCK);
compiler_pop_fblock(c, FINALLY_TRY, cleanup_body); compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
...@@ -2040,10 +2040,10 @@ compiler_try_except(struct compiler *c, stmt_ty s) ...@@ -2040,10 +2040,10 @@ compiler_try_except(struct compiler *c, stmt_ty s)
/* name = None */ /* name = None */
ADDOP_O(c, LOAD_CONST, Py_None, consts); ADDOP_O(c, LOAD_CONST, Py_None, consts);
compiler_nameop(c, handler->name, Store); compiler_nameop(c, handler->v.ExceptHandler.name, Store);
/* del name */ /* del name */
compiler_nameop(c, handler->name, Del); compiler_nameop(c, handler->v.ExceptHandler.name, Del);
ADDOP(c, END_FINALLY); ADDOP(c, END_FINALLY);
compiler_pop_fblock(c, FINALLY_END, cleanup_end); compiler_pop_fblock(c, FINALLY_END, cleanup_end);
...@@ -2051,11 +2051,11 @@ compiler_try_except(struct compiler *c, stmt_ty s) ...@@ -2051,11 +2051,11 @@ compiler_try_except(struct compiler *c, stmt_ty s)
else { else {
ADDOP(c, POP_TOP); ADDOP(c, POP_TOP);
ADDOP(c, POP_TOP); ADDOP(c, POP_TOP);
VISIT_SEQ(c, stmt, handler->body); VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
} }
ADDOP_JREL(c, JUMP_FORWARD, end); ADDOP_JREL(c, JUMP_FORWARD, end);
compiler_use_next_block(c, except); compiler_use_next_block(c, except);
if (handler->type) if (handler->v.ExceptHandler.type)
ADDOP(c, POP_TOP); ADDOP(c, POP_TOP);
} }
ADDOP(c, END_FINALLY); ADDOP(c, END_FINALLY);
......
...@@ -1431,12 +1431,12 @@ symtable_visit_arguments(struct symtable *st, arguments_ty a) ...@@ -1431,12 +1431,12 @@ symtable_visit_arguments(struct symtable *st, arguments_ty a)
static int static int
symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh) symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
{ {
if (eh->type) if (eh->v.ExceptHandler.type)
VISIT(st, expr, eh->type); VISIT(st, expr, eh->v.ExceptHandler.type);
if (eh->name) if (eh->v.ExceptHandler.name)
if (!symtable_add_def(st, eh->name, DEF_LOCAL)) if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
return 0; return 0;
VISIT_SEQ(st, stmt, eh->body); VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
return 1; return 1;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment