@@ -351,13 +351,16 @@ static char *comprehension_fields[]={
351351 "ifs" ,
352352};
353353static PyTypeObject * excepthandler_type ;
354+ static char * excepthandler_attributes [] = {
355+ "lineno" ,
356+ "col_offset" ,
357+ };
354358static 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};
362365static PyTypeObject * arguments_type ;
363366static 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
20042011excepthandler_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
59765988obj2ast_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 ));
60586084failed :
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 ;
0 commit comments