1
1
#include "Python.h"
2
2
#include "pycore_ast.h" // identifier, stmt_ty
3
- #include "pycore_compile.h" // _Py_Mangle()
3
+ #include "pycore_compile.h" // _Py_Mangle(), _PyFuture_FromAST()
4
4
#include "pycore_parser.h" // _PyParser_ASTFromString()
5
5
#include "pycore_pystate.h" // _PyThreadState_GET()
6
6
#include "pycore_symtable.h" // PySTEntryObject
45
45
#define NAMED_EXPR_COMP_ITER_EXPR \
46
46
"assignment expression cannot be used in a comprehension iterable expression"
47
47
48
+ #define ANNOTATION_NOT_ALLOWED \
49
+ "'%s' can not be used within an annotation"
50
+
51
+
48
52
static PySTEntryObject *
49
53
ste_new (struct symtable * st , identifier name , _Py_block_ty block ,
50
54
void * key , int lineno , int col_offset ,
@@ -209,17 +213,19 @@ static int symtable_visit_alias(struct symtable *st, alias_ty);
209
213
static int symtable_visit_comprehension (struct symtable * st , comprehension_ty );
210
214
static int symtable_visit_keyword (struct symtable * st , keyword_ty );
211
215
static int symtable_visit_params (struct symtable * st , asdl_arg_seq * args );
216
+ static int symtable_visit_annotation (struct symtable * st , expr_ty annotation );
212
217
static int symtable_visit_argannotations (struct symtable * st , asdl_arg_seq * args );
213
218
static int symtable_implicit_arg (struct symtable * st , int pos );
214
- static int symtable_visit_annotations (struct symtable * st , arguments_ty , expr_ty );
219
+ static int symtable_visit_annotations (struct symtable * st , stmt_ty , arguments_ty , expr_ty );
215
220
static int symtable_visit_withitem (struct symtable * st , withitem_ty item );
216
221
static int symtable_visit_match_case (struct symtable * st , match_case_ty m );
217
222
static int symtable_visit_pattern (struct symtable * st , pattern_ty s );
223
+ static int symtable_raise_if_annotation_block (struct symtable * st , const char * , expr_ty );
218
224
219
225
220
226
static identifier top = NULL , lambda = NULL , genexpr = NULL ,
221
227
listcomp = NULL , setcomp = NULL , dictcomp = NULL ,
222
- __class__ = NULL ;
228
+ __class__ = NULL , _annotation = NULL ;
223
229
224
230
#define GET_IDENTIFIER (VAR ) \
225
231
((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
@@ -987,8 +993,17 @@ symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
987
993
/* The entry is owned by the stack. Borrow it for st_cur. */
988
994
Py_DECREF (ste );
989
995
st -> st_cur = ste ;
996
+
997
+ /* Annotation blocks shouldn't have any affect on the symbol table since in
998
+ * the compilation stage, they will all be transformed to strings. They are
999
+ * only created if future 'annotations' feature is activated. */
1000
+ if (block == AnnotationBlock ) {
1001
+ return 1 ;
1002
+ }
1003
+
990
1004
if (block == ModuleBlock )
991
1005
st -> st_global = st -> st_cur -> ste_symbols ;
1006
+
992
1007
if (prev ) {
993
1008
if (PyList_Append (prev -> ste_children , (PyObject * )ste ) < 0 ) {
994
1009
return 0 ;
@@ -1190,7 +1205,7 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
1190
1205
VISIT_SEQ (st , expr , s -> v .FunctionDef .args -> defaults );
1191
1206
if (s -> v .FunctionDef .args -> kw_defaults )
1192
1207
VISIT_SEQ_WITH_NULL (st , expr , s -> v .FunctionDef .args -> kw_defaults );
1193
- if (!symtable_visit_annotations (st , s -> v .FunctionDef .args ,
1208
+ if (!symtable_visit_annotations (st , s , s -> v .FunctionDef .args ,
1194
1209
s -> v .FunctionDef .returns ))
1195
1210
VISIT_QUIT (st , 0 );
1196
1211
if (s -> v .FunctionDef .decorator_list )
@@ -1273,7 +1288,10 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
1273
1288
else {
1274
1289
VISIT (st , expr , s -> v .AnnAssign .target );
1275
1290
}
1276
- VISIT (st , expr , s -> v .AnnAssign .annotation );
1291
+ if (!symtable_visit_annotation (st , s -> v .AnnAssign .annotation )) {
1292
+ VISIT_QUIT (st , 0 );
1293
+ }
1294
+
1277
1295
if (s -> v .AnnAssign .value ) {
1278
1296
VISIT (st , expr , s -> v .AnnAssign .value );
1279
1297
}
@@ -1422,7 +1440,7 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
1422
1440
if (s -> v .AsyncFunctionDef .args -> kw_defaults )
1423
1441
VISIT_SEQ_WITH_NULL (st , expr ,
1424
1442
s -> v .AsyncFunctionDef .args -> kw_defaults );
1425
- if (!symtable_visit_annotations (st , s -> v .AsyncFunctionDef .args ,
1443
+ if (!symtable_visit_annotations (st , s , s -> v .AsyncFunctionDef .args ,
1426
1444
s -> v .AsyncFunctionDef .returns ))
1427
1445
VISIT_QUIT (st , 0 );
1428
1446
if (s -> v .AsyncFunctionDef .decorator_list )
@@ -1564,6 +1582,9 @@ symtable_visit_expr(struct symtable *st, expr_ty e)
1564
1582
}
1565
1583
switch (e -> kind ) {
1566
1584
case NamedExpr_kind :
1585
+ if (!symtable_raise_if_annotation_block (st , "named expression" , e )) {
1586
+ VISIT_QUIT (st , 0 );
1587
+ }
1567
1588
if (!symtable_handle_namedexpr (st , e ))
1568
1589
VISIT_QUIT (st , 0 );
1569
1590
break ;
@@ -1624,15 +1645,24 @@ symtable_visit_expr(struct symtable *st, expr_ty e)
1624
1645
VISIT_QUIT (st , 0 );
1625
1646
break ;
1626
1647
case Yield_kind :
1648
+ if (!symtable_raise_if_annotation_block (st , "yield expression" , e )) {
1649
+ VISIT_QUIT (st , 0 );
1650
+ }
1627
1651
if (e -> v .Yield .value )
1628
1652
VISIT (st , expr , e -> v .Yield .value );
1629
1653
st -> st_cur -> ste_generator = 1 ;
1630
1654
break ;
1631
1655
case YieldFrom_kind :
1656
+ if (!symtable_raise_if_annotation_block (st , "yield expression" , e )) {
1657
+ VISIT_QUIT (st , 0 );
1658
+ }
1632
1659
VISIT (st , expr , e -> v .YieldFrom .value );
1633
1660
st -> st_cur -> ste_generator = 1 ;
1634
1661
break ;
1635
1662
case Await_kind :
1663
+ if (!symtable_raise_if_annotation_block (st , "await expression" , e )) {
1664
+ VISIT_QUIT (st , 0 );
1665
+ }
1636
1666
VISIT (st , expr , e -> v .Await .value );
1637
1667
st -> st_cur -> ste_coroutine = 1 ;
1638
1668
break ;
@@ -1780,6 +1810,24 @@ symtable_visit_params(struct symtable *st, asdl_arg_seq *args)
1780
1810
return 1 ;
1781
1811
}
1782
1812
1813
+ static int
1814
+ symtable_visit_annotation (struct symtable * st , expr_ty annotation )
1815
+ {
1816
+ int future_annotations = st -> st_future -> ff_features & CO_FUTURE_ANNOTATIONS ;
1817
+ if (future_annotations &&
1818
+ !symtable_enter_block (st , GET_IDENTIFIER (_annotation ), AnnotationBlock ,
1819
+ (void * )annotation , annotation -> lineno ,
1820
+ annotation -> col_offset , annotation -> end_lineno ,
1821
+ annotation -> end_col_offset )) {
1822
+ VISIT_QUIT (st , 0 );
1823
+ }
1824
+ VISIT (st , expr , annotation );
1825
+ if (future_annotations && !symtable_exit_block (st )) {
1826
+ VISIT_QUIT (st , 0 );
1827
+ }
1828
+ return 1 ;
1829
+ }
1830
+
1783
1831
static int
1784
1832
symtable_visit_argannotations (struct symtable * st , asdl_arg_seq * args )
1785
1833
{
@@ -1798,8 +1846,15 @@ symtable_visit_argannotations(struct symtable *st, asdl_arg_seq *args)
1798
1846
}
1799
1847
1800
1848
static int
1801
- symtable_visit_annotations (struct symtable * st , arguments_ty a , expr_ty returns )
1849
+ symtable_visit_annotations (struct symtable * st , stmt_ty o , arguments_ty a , expr_ty returns )
1802
1850
{
1851
+ int future_annotations = st -> st_future -> ff_features & CO_FUTURE_ANNOTATIONS ;
1852
+ if (future_annotations &&
1853
+ !symtable_enter_block (st , GET_IDENTIFIER (_annotation ), AnnotationBlock ,
1854
+ (void * )o , o -> lineno , o -> col_offset , o -> end_lineno ,
1855
+ o -> end_col_offset )) {
1856
+ VISIT_QUIT (st , 0 );
1857
+ }
1803
1858
if (a -> posonlyargs && !symtable_visit_argannotations (st , a -> posonlyargs ))
1804
1859
return 0 ;
1805
1860
if (a -> args && !symtable_visit_argannotations (st , a -> args ))
@@ -1810,8 +1865,12 @@ symtable_visit_annotations(struct symtable *st, arguments_ty a, expr_ty returns)
1810
1865
VISIT (st , expr , a -> kwarg -> annotation );
1811
1866
if (a -> kwonlyargs && !symtable_visit_argannotations (st , a -> kwonlyargs ))
1812
1867
return 0 ;
1813
- if (returns )
1814
- VISIT (st , expr , returns );
1868
+ if (future_annotations && !symtable_exit_block (st )) {
1869
+ VISIT_QUIT (st , 0 );
1870
+ }
1871
+ if (returns && !symtable_visit_annotation (st , returns )) {
1872
+ VISIT_QUIT (st , 0 );
1873
+ }
1815
1874
return 1 ;
1816
1875
}
1817
1876
@@ -2033,6 +2092,21 @@ symtable_visit_dictcomp(struct symtable *st, expr_ty e)
2033
2092
e -> v .DictComp .value );
2034
2093
}
2035
2094
2095
+ static int
2096
+ symtable_raise_if_annotation_block (struct symtable * st , const char * name , expr_ty e )
2097
+ {
2098
+ if (st -> st_cur -> ste_type != AnnotationBlock ) {
2099
+ return 1 ;
2100
+ }
2101
+
2102
+ PyErr_Format (PyExc_SyntaxError , ANNOTATION_NOT_ALLOWED , name );
2103
+ PyErr_RangedSyntaxLocationObject (st -> st_filename ,
2104
+ e -> lineno ,
2105
+ e -> col_offset + 1 ,
2106
+ e -> end_lineno ,
2107
+ e -> end_col_offset + 1 );
2108
+ return 0 ;
2109
+ }
2036
2110
2037
2111
struct symtable *
2038
2112
_Py_SymtableStringObjectFlags (const char * str , PyObject * filename ,
@@ -2051,7 +2125,14 @@ _Py_SymtableStringObjectFlags(const char *str, PyObject *filename,
2051
2125
_PyArena_Free (arena );
2052
2126
return NULL ;
2053
2127
}
2054
- st = _PySymtable_Build (mod , filename , 0 );
2128
+ PyFutureFeatures * future = _PyFuture_FromAST (mod , filename );
2129
+ if (future == NULL ) {
2130
+ _PyArena_Free (arena );
2131
+ return NULL ;
2132
+ }
2133
+ future -> ff_features |= flags -> cf_flags ;
2134
+ st = _PySymtable_Build (mod , filename , future );
2135
+ PyObject_Free ((void * )future );
2055
2136
_PyArena_Free (arena );
2056
2137
return st ;
2057
2138
}
0 commit comments