@@ -162,12 +162,14 @@ PyTypeObject PySTEntry_Type = {
162
162
};
163
163
164
164
static int symtable_analyze (struct symtable * st );
165
- static int symtable_warn (struct symtable * st , char * msg , int lineno );
165
+ static int symtable_warn (struct symtable * st ,
166
+ PyObject * warn , const char * msg , int lineno );
166
167
static int symtable_enter_block (struct symtable * st , identifier name ,
167
168
_Py_block_ty block , void * ast , int lineno );
168
169
static int symtable_exit_block (struct symtable * st , void * ast );
169
170
static int symtable_visit_stmt (struct symtable * st , stmt_ty s );
170
171
static int symtable_visit_expr (struct symtable * st , expr_ty s );
172
+ static int symtable_visit_listcomp (struct symtable * st , expr_ty e );
171
173
static int symtable_visit_genexp (struct symtable * st , expr_ty s );
172
174
static int symtable_visit_setcomp (struct symtable * st , expr_ty e );
173
175
static int symtable_visit_dictcomp (struct symtable * st , expr_ty e );
@@ -796,14 +798,18 @@ symtable_analyze(struct symtable *st)
796
798
797
799
798
800
static int
799
- symtable_warn (struct symtable * st , char * msg , int lineno )
801
+ symtable_warn (struct symtable * st , PyObject * warn , const char * msg , int lineno )
800
802
{
801
- if (PyErr_WarnExplicit (PyExc_SyntaxWarning , msg , st -> st_filename ,
802
- lineno , NULL , NULL ) < 0 ) {
803
- if (PyErr_ExceptionMatches (PyExc_SyntaxWarning )) {
803
+ if (lineno < 0 ) {
804
+ lineno = st -> st_cur -> ste_lineno ;
805
+ }
806
+ if (PyErr_WarnExplicit (warn , msg , st -> st_filename , lineno , NULL , NULL ) < 0 ) {
807
+ if (PyErr_ExceptionMatches (warn )) {
808
+ /* Replace the warning exception with a SyntaxError
809
+ to get a more accurate error report */
810
+ PyErr_Clear ();
804
811
PyErr_SetString (PyExc_SyntaxError , msg );
805
- PyErr_SyntaxLocation (st -> st_filename ,
806
- st -> st_cur -> ste_lineno );
812
+ PyErr_SyntaxLocation (st -> st_filename , lineno );
807
813
}
808
814
return 0 ;
809
815
}
@@ -1153,7 +1159,7 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
1153
1159
PyOS_snprintf (buf , sizeof (buf ),
1154
1160
GLOBAL_AFTER_USE ,
1155
1161
c_name );
1156
- if (!symtable_warn (st , buf , s -> lineno ))
1162
+ if (!symtable_warn (st , PyExc_SyntaxWarning , buf , s -> lineno ))
1157
1163
return 0 ;
1158
1164
}
1159
1165
if (!symtable_add_def (st , name , DEF_GLOBAL ))
@@ -1221,8 +1227,8 @@ symtable_visit_expr(struct symtable *st, expr_ty e)
1221
1227
VISIT_SEQ (st , expr , e -> v .Set .elts );
1222
1228
break ;
1223
1229
case ListComp_kind :
1224
- VISIT ( st , expr , e -> v . ListComp . elt );
1225
- VISIT_SEQ ( st , comprehension , e -> v . ListComp . generators ) ;
1230
+ if (! symtable_visit_listcomp ( st , e ))
1231
+ return 0 ;
1226
1232
break ;
1227
1233
case GeneratorExp_kind :
1228
1234
if (!symtable_visit_genexp (st , e ))
@@ -1420,12 +1426,11 @@ symtable_visit_alias(struct symtable *st, alias_ty a)
1420
1426
return r ;
1421
1427
}
1422
1428
else {
1423
- if (st -> st_cur -> ste_type != ModuleBlock ) {
1424
- int lineno = st -> st_cur -> ste_lineno ;
1425
- if (!symtable_warn (st , IMPORT_STAR_WARNING , lineno )) {
1426
- Py_DECREF (store_name );
1427
- return 0 ;
1428
- }
1429
+ if (st -> st_cur -> ste_type != ModuleBlock &&
1430
+ !symtable_warn (st , PyExc_SyntaxWarning , IMPORT_STAR_WARNING , -1 ))
1431
+ {
1432
+ Py_DECREF (store_name );
1433
+ return 0 ;
1429
1434
}
1430
1435
st -> st_cur -> ste_unoptimized |= OPT_IMPORT_STAR ;
1431
1436
Py_DECREF (store_name );
@@ -1509,7 +1514,10 @@ symtable_handle_comprehension(struct symtable *st, expr_ty e,
1509
1514
!symtable_enter_block (st , scope_name , FunctionBlock , (void * )e , 0 )) {
1510
1515
return 0 ;
1511
1516
}
1512
- st -> st_cur -> ste_generator = is_generator ;
1517
+ /* In order to check for yield expressions under '-3', we clear
1518
+ the generator flag, and restore it at the end */
1519
+ is_generator |= st -> st_cur -> ste_generator ;
1520
+ st -> st_cur -> ste_generator = 0 ;
1513
1521
/* Outermost iter is received as an argument */
1514
1522
if (!symtable_implicit_arg (st , 0 )) {
1515
1523
symtable_exit_block (st , (void * )e );
@@ -1527,9 +1535,55 @@ symtable_handle_comprehension(struct symtable *st, expr_ty e,
1527
1535
if (value )
1528
1536
VISIT_IN_BLOCK (st , expr , value , (void * )e );
1529
1537
VISIT_IN_BLOCK (st , expr , elt , (void * )e );
1538
+ if (Py_Py3kWarningFlag && st -> st_cur -> ste_generator ) {
1539
+ const char * msg = (
1540
+ (e -> kind == SetComp_kind ) ? "'yield' inside set comprehension" :
1541
+ (e -> kind == DictComp_kind ) ? "'yield' inside dict comprehension" :
1542
+ "'yield' inside generator expression" );
1543
+ if (!symtable_warn (st , PyExc_DeprecationWarning , msg , -1 )) {
1544
+ symtable_exit_block (st , (void * )e );
1545
+ return 0 ;
1546
+ }
1547
+ }
1548
+ st -> st_cur -> ste_generator |= is_generator ;
1530
1549
return symtable_exit_block (st , (void * )e );
1531
1550
}
1532
1551
1552
+ static int
1553
+ symtable_visit_listcomp (struct symtable * st , expr_ty e )
1554
+ {
1555
+ asdl_seq * generators = e -> v .ListComp .generators ;
1556
+ int i , is_generator ;
1557
+ /* In order to check for yield expressions under '-3', we clear
1558
+ the generator flag, and restore it at the end */
1559
+ is_generator = st -> st_cur -> ste_generator ;
1560
+ st -> st_cur -> ste_generator = 0 ;
1561
+ VISIT (st , expr , e -> v .ListComp .elt );
1562
+ for (i = 0 ; i < asdl_seq_LEN (generators ); i ++ ) {
1563
+ comprehension_ty lc = (comprehension_ty )asdl_seq_GET (generators , i );
1564
+ VISIT (st , expr , lc -> target );
1565
+ if (i == 0 && !st -> st_cur -> ste_generator ) {
1566
+ /* 'yield' in the outermost iterator doesn't cause a warning */
1567
+ VISIT (st , expr , lc -> iter );
1568
+ is_generator |= st -> st_cur -> ste_generator ;
1569
+ st -> st_cur -> ste_generator = 0 ;
1570
+ }
1571
+ else {
1572
+ VISIT (st , expr , lc -> iter );
1573
+ }
1574
+ VISIT_SEQ (st , expr , lc -> ifs );
1575
+ }
1576
+
1577
+ if (Py_Py3kWarningFlag && st -> st_cur -> ste_generator ) {
1578
+ const char * msg = "'yield' inside list comprehension" ;
1579
+ if (!symtable_warn (st , PyExc_DeprecationWarning , msg , -1 )) {
1580
+ return 0 ;
1581
+ }
1582
+ }
1583
+ st -> st_cur -> ste_generator |= is_generator ;
1584
+ return 1 ;
1585
+ }
1586
+
1533
1587
static int
1534
1588
symtable_visit_genexp (struct symtable * st , expr_ty e )
1535
1589
{
0 commit comments