Skip to content

Commit dec11f3

Browse files
author
neal.norwitz
committed
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. ........ git-svn-id: http://svn.python.org/projects/python/branches/py3k@62078 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 524e678 commit dec11f3

7 files changed

Lines changed: 141 additions & 109 deletions

File tree

Include/Python-ast.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,17 @@ struct _comprehension {
342342
asdl_seq *ifs;
343343
};
344344

345+
enum _excepthandler_kind {ExceptHandler_kind=1};
345346
struct _excepthandler {
346-
expr_ty type;
347-
identifier name;
348-
asdl_seq *body;
347+
enum _excepthandler_kind kind;
348+
union {
349+
struct {
350+
expr_ty type;
351+
identifier name;
352+
asdl_seq *body;
353+
} ExceptHandler;
354+
355+
} v;
349356
int lineno;
350357
int col_offset;
351358
};
@@ -525,8 +532,8 @@ slice_ty _Py_Index(expr_ty value, PyArena *arena);
525532
#define comprehension(a0, a1, a2, a3) _Py_comprehension(a0, a1, a2, a3)
526533
comprehension_ty _Py_comprehension(expr_ty target, expr_ty iter, asdl_seq *
527534
ifs, PyArena *arena);
528-
#define excepthandler(a0, a1, a2, a3, a4, a5) _Py_excepthandler(a0, a1, a2, a3, a4, a5)
529-
excepthandler_ty _Py_excepthandler(expr_ty type, identifier name, asdl_seq *
535+
#define ExceptHandler(a0, a1, a2, a3, a4, a5) _Py_ExceptHandler(a0, a1, a2, a3, a4, a5)
536+
excepthandler_ty _Py_ExceptHandler(expr_ty type, identifier name, asdl_seq *
530537
body, int lineno, int col_offset, PyArena
531538
*arena);
532539
#define arguments(a0, a1, a2, a3, a4, a5, a6, a7, a8) _Py_arguments(a0, a1, a2, a3, a4, a5, a6, a7, a8)

Lib/test/test_ast.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def run_tests():
152152
#### EVERYTHING BELOW IS GENERATED #####
153153
exec_results = [
154154
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, [], None, None, [], []), [('Pass', (1, 9))], [], None)]),
155-
('Module', [('ClassDef', (1, 0), 'C', [], [], None, None, [('Pass', (1, 8))], [], )]),
155+
('Module', [('ClassDef', (1, 0), 'C', [], [], None, None, [('Pass', (1, 8))], [])]),
156156
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, [], None, None, [], []), [('Return', (1, 8), ('Num', (1, 15), 1))], [], None)]),
157157
('Module', [('Delete', (1, 0), [('Name', (1, 4), 'v', ('Del',))])]),
158158
('Module', [('Assign', (1, 0), [('Name', (1, 0), 'v', ('Store',))], ('Num', (1, 4), 1))]),
@@ -161,7 +161,7 @@ def run_tests():
161161
('Module', [('While', (1, 0), ('Name', (1, 6), 'v', ('Load',)), [('Pass', (1, 8))], [])]),
162162
('Module', [('If', (1, 0), ('Name', (1, 3), 'v', ('Load',)), [('Pass', (1, 5))], [])]),
163163
('Module', [('Raise', (1, 0), ('Call', (1, 6), ('Name', (1, 6), 'Exception', ('Load',)), [('Str', (1, 16), 'string')], [], None, None), None)]),
164-
('Module', [('TryExcept', (1, 0), [('Pass', (2, 2))], [('excepthandler', (3, 0), ('Name', (3, 7), 'Exception', ('Load',)), None, [('Pass', (4, 2))], 3, 0)], [])]),
164+
('Module', [('TryExcept', (1, 0), [('Pass', (2, 2))], [('ExceptHandler', (3, 0), ('Name', (3, 7), 'Exception', ('Load',)), None, [('Pass', (4, 2))])], [])]),
165165
('Module', [('TryFinally', (1, 0), [('Pass', (2, 2))], [('Pass', (4, 2))])]),
166166
('Module', [('Assert', (1, 0), ('Name', (1, 7), 'v', ('Load',)), None)]),
167167
('Module', [('Import', (1, 0), [('alias', 'sys', None)])]),

Parser/Python.asdl

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,8 @@ module Python version "$Revision$"
101101
comprehension = (expr target, expr iter, expr* ifs)
102102

103103
-- not sure what to call the first argument for raise and except
104-
-- TODO(jhylton): Figure out if there is a better way to handle
105-
-- lineno and col_offset fields, particularly when
106-
-- ast is exposed to Python.
107-
excepthandler = (expr? type, identifier? name, stmt* body, int lineno,
108-
int col_offset)
104+
excepthandler = ExceptHandler(expr? type, identifier? name, stmt* body)
105+
attributes (int lineno, int col_offset)
109106

110107
arguments = (arg* args, identifier? vararg, expr? varargannotation,
111108
arg* kwonlyargs, identifier? kwarg,

Python/Python-ast.c

Lines changed: 107 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -351,13 +351,16 @@ static char *comprehension_fields[]={
351351
"ifs",
352352
};
353353
static PyTypeObject *excepthandler_type;
354+
static char *excepthandler_attributes[] = {
355+
"lineno",
356+
"col_offset",
357+
};
354358
static PyObject* ast2obj_excepthandler(void*);
355-
static char *excepthandler_fields[]={
359+
static PyTypeObject *ExceptHandler_type;
360+
static char *ExceptHandler_fields[]={
356361
"type",
357362
"name",
358363
"body",
359-
"lineno",
360-
"col_offset",
361364
};
362365
static PyTypeObject *arguments_type;
363366
static PyObject* ast2obj_arguments(void*);
@@ -881,9 +884,13 @@ static int init_types(void)
881884
comprehension_type = make_type("comprehension", &AST_type,
882885
comprehension_fields, 3);
883886
if (!comprehension_type) return 0;
884-
excepthandler_type = make_type("excepthandler", &AST_type,
885-
excepthandler_fields, 5);
887+
excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
886888
if (!excepthandler_type) return 0;
889+
if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
890+
return 0;
891+
ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
892+
ExceptHandler_fields, 3);
893+
if (!ExceptHandler_type) return 0;
887894
arguments_type = make_type("arguments", &AST_type, arguments_fields, 8);
888895
if (!arguments_type) return 0;
889896
arg_type = make_type("arg", &AST_type, arg_fields, 2);
@@ -2002,16 +2009,17 @@ comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
20022009
}
20032010

20042011
excepthandler_ty
2005-
excepthandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
2012+
ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
20062013
col_offset, PyArena *arena)
20072014
{
20082015
excepthandler_ty p;
20092016
p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
20102017
if (!p)
20112018
return NULL;
2012-
p->type = type;
2013-
p->name = name;
2014-
p->body = body;
2019+
p->kind = ExceptHandler_kind;
2020+
p->v.ExceptHandler.type = type;
2021+
p->v.ExceptHandler.name = name;
2022+
p->v.ExceptHandler.body = body;
20152023
p->lineno = lineno;
20162024
p->col_offset = col_offset;
20172025
return p;
@@ -3144,31 +3152,35 @@ ast2obj_excepthandler(void* _o)
31443152
return Py_None;
31453153
}
31463154

3147-
result = PyType_GenericNew(excepthandler_type, NULL, NULL);
3148-
if (!result) return NULL;
3149-
value = ast2obj_expr(o->type);
3150-
if (!value) goto failed;
3151-
if (PyObject_SetAttrString(result, "type", value) == -1)
3152-
goto failed;
3153-
Py_DECREF(value);
3154-
value = ast2obj_identifier(o->name);
3155-
if (!value) goto failed;
3156-
if (PyObject_SetAttrString(result, "name", value) == -1)
3157-
goto failed;
3158-
Py_DECREF(value);
3159-
value = ast2obj_list(o->body, ast2obj_stmt);
3160-
if (!value) goto failed;
3161-
if (PyObject_SetAttrString(result, "body", value) == -1)
3162-
goto failed;
3163-
Py_DECREF(value);
3155+
switch (o->kind) {
3156+
case ExceptHandler_kind:
3157+
result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3158+
if (!result) goto failed;
3159+
value = ast2obj_expr(o->v.ExceptHandler.type);
3160+
if (!value) goto failed;
3161+
if (PyObject_SetAttrString(result, "type", value) == -1)
3162+
goto failed;
3163+
Py_DECREF(value);
3164+
value = ast2obj_identifier(o->v.ExceptHandler.name);
3165+
if (!value) goto failed;
3166+
if (PyObject_SetAttrString(result, "name", value) == -1)
3167+
goto failed;
3168+
Py_DECREF(value);
3169+
value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3170+
if (!value) goto failed;
3171+
if (PyObject_SetAttrString(result, "body", value) == -1)
3172+
goto failed;
3173+
Py_DECREF(value);
3174+
break;
3175+
}
31643176
value = ast2obj_int(o->lineno);
31653177
if (!value) goto failed;
3166-
if (PyObject_SetAttrString(result, "lineno", value) == -1)
3178+
if (PyObject_SetAttrString(result, "lineno", value) < 0)
31673179
goto failed;
31683180
Py_DECREF(value);
31693181
value = ast2obj_int(o->col_offset);
31703182
if (!value) goto failed;
3171-
if (PyObject_SetAttrString(result, "col_offset", value) == -1)
3183+
if (PyObject_SetAttrString(result, "col_offset", value) < 0)
31723184
goto failed;
31733185
Py_DECREF(value);
31743186
return result;
@@ -5976,58 +5988,13 @@ int
59765988
obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
59775989
{
59785990
PyObject* tmp = NULL;
5979-
expr_ty type;
5980-
identifier name;
5981-
asdl_seq* body;
5991+
59825992
int lineno;
59835993
int col_offset;
59845994

5985-
if (PyObject_HasAttrString(obj, "type")) {
5986-
int res;
5987-
tmp = PyObject_GetAttrString(obj, "type");
5988-
if (tmp == NULL) goto failed;
5989-
res = obj2ast_expr(tmp, &type, arena);
5990-
if (res != 0) goto failed;
5991-
Py_XDECREF(tmp);
5992-
tmp = NULL;
5993-
} else {
5994-
type = NULL;
5995-
}
5996-
if (PyObject_HasAttrString(obj, "name")) {
5997-
int res;
5998-
tmp = PyObject_GetAttrString(obj, "name");
5999-
if (tmp == NULL) goto failed;
6000-
res = obj2ast_identifier(tmp, &name, arena);
6001-
if (res != 0) goto failed;
6002-
Py_XDECREF(tmp);
6003-
tmp = NULL;
6004-
} else {
6005-
name = NULL;
6006-
}
6007-
if (PyObject_HasAttrString(obj, "body")) {
6008-
int res;
6009-
Py_ssize_t len;
6010-
Py_ssize_t i;
6011-
tmp = PyObject_GetAttrString(obj, "body");
6012-
if (tmp == NULL) goto failed;
6013-
if (!PyList_Check(tmp)) {
6014-
PyErr_Format(PyExc_TypeError, "excepthandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6015-
goto failed;
6016-
}
6017-
len = PyList_GET_SIZE(tmp);
6018-
body = asdl_seq_new(len, arena);
6019-
if (body == NULL) goto failed;
6020-
for (i = 0; i < len; i++) {
6021-
stmt_ty value;
6022-
res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
6023-
if (res != 0) goto failed;
6024-
asdl_seq_SET(body, i, value);
6025-
}
6026-
Py_XDECREF(tmp);
6027-
tmp = NULL;
6028-
} else {
6029-
PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from excepthandler");
6030-
return 1;
5995+
if (obj == Py_None) {
5996+
*out = NULL;
5997+
return 0;
60315998
}
60325999
if (PyObject_HasAttrString(obj, "lineno")) {
60336000
int res;
@@ -6053,8 +6020,67 @@ obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
60536020
PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
60546021
return 1;
60556022
}
6056-
*out = excepthandler(type, name, body, lineno, col_offset, arena);
6057-
return 0;
6023+
if (PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type)) {
6024+
expr_ty type;
6025+
identifier name;
6026+
asdl_seq* body;
6027+
6028+
if (PyObject_HasAttrString(obj, "type")) {
6029+
int res;
6030+
tmp = PyObject_GetAttrString(obj, "type");
6031+
if (tmp == NULL) goto failed;
6032+
res = obj2ast_expr(tmp, &type, arena);
6033+
if (res != 0) goto failed;
6034+
Py_XDECREF(tmp);
6035+
tmp = NULL;
6036+
} else {
6037+
type = NULL;
6038+
}
6039+
if (PyObject_HasAttrString(obj, "name")) {
6040+
int res;
6041+
tmp = PyObject_GetAttrString(obj, "name");
6042+
if (tmp == NULL) goto failed;
6043+
res = obj2ast_identifier(tmp, &name, arena);
6044+
if (res != 0) goto failed;
6045+
Py_XDECREF(tmp);
6046+
tmp = NULL;
6047+
} else {
6048+
name = NULL;
6049+
}
6050+
if (PyObject_HasAttrString(obj, "body")) {
6051+
int res;
6052+
Py_ssize_t len;
6053+
Py_ssize_t i;
6054+
tmp = PyObject_GetAttrString(obj, "body");
6055+
if (tmp == NULL) goto failed;
6056+
if (!PyList_Check(tmp)) {
6057+
PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6058+
goto failed;
6059+
}
6060+
len = PyList_GET_SIZE(tmp);
6061+
body = asdl_seq_new(len, arena);
6062+
if (body == NULL) goto failed;
6063+
for (i = 0; i < len; i++) {
6064+
stmt_ty value;
6065+
res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
6066+
if (res != 0) goto failed;
6067+
asdl_seq_SET(body, i, value);
6068+
}
6069+
Py_XDECREF(tmp);
6070+
tmp = NULL;
6071+
} else {
6072+
PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
6073+
return 1;
6074+
}
6075+
*out = ExceptHandler(type, name, body, lineno, col_offset,
6076+
arena);
6077+
if (*out == NULL) goto failed;
6078+
return 0;
6079+
}
6080+
6081+
tmp = PyObject_Repr(obj);
6082+
if (tmp == NULL) goto failed;
6083+
PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %.400s", PyString_AS_STRING(tmp));
60586084
failed:
60596085
Py_XDECREF(tmp);
60606086
return 1;
@@ -6493,6 +6519,8 @@ init_ast(void)
64936519
(PyObject*)comprehension_type) < 0) return;
64946520
if (PyDict_SetItemString(d, "excepthandler",
64956521
(PyObject*)excepthandler_type) < 0) return;
6522+
if (PyDict_SetItemString(d, "ExceptHandler",
6523+
(PyObject*)ExceptHandler_type) < 0) return;
64966524
if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
64976525
0) return;
64986526
if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return;

Python/ast.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2787,7 +2787,7 @@ ast_for_except_clause(struct compiling *c, const node *exc, node *body)
27872787
if (!suite_seq)
27882788
return NULL;
27892789

2790-
return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
2790+
return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
27912791
exc->n_col_offset, c->c_arena);
27922792
}
27932793
else if (NCH(exc) == 2) {
@@ -2801,7 +2801,7 @@ ast_for_except_clause(struct compiling *c, const node *exc, node *body)
28012801
if (!suite_seq)
28022802
return NULL;
28032803

2804-
return excepthandler(expression, NULL, suite_seq, LINENO(exc),
2804+
return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
28052805
exc->n_col_offset, c->c_arena);
28062806
}
28072807
else if (NCH(exc) == 4) {
@@ -2817,7 +2817,7 @@ ast_for_except_clause(struct compiling *c, const node *exc, node *body)
28172817
if (!suite_seq)
28182818
return NULL;
28192819

2820-
return excepthandler(expression, e, suite_seq, LINENO(exc),
2820+
return ExceptHandler(expression, e, suite_seq, LINENO(exc),
28212821
exc->n_col_offset, c->c_arena);
28222822
}
28232823

0 commit comments

Comments
 (0)