@@ -1057,7 +1057,8 @@ codegen_addop_j(instr_sequence *seq, location loc,
1057
1057
1058
1058
static int
1059
1059
compiler_enter_scope (struct compiler * c , identifier name , int scope_type ,
1060
- void * key , int lineno , PyObject * private )
1060
+ void * key , int lineno , PyObject * private ,
1061
+ _PyCompile_CodeUnitMetadata * umd )
1061
1062
{
1062
1063
location loc = LOCATION (lineno , lineno , 0 , 0 );
1063
1064
@@ -1069,9 +1070,14 @@ compiler_enter_scope(struct compiler *c, identifier name, int scope_type,
1069
1070
return ERROR ;
1070
1071
}
1071
1072
u -> u_scope_type = scope_type ;
1072
- u -> u_metadata .u_argcount = 0 ;
1073
- u -> u_metadata .u_posonlyargcount = 0 ;
1074
- u -> u_metadata .u_kwonlyargcount = 0 ;
1073
+ if (umd != NULL ) {
1074
+ u -> u_metadata = * umd ;
1075
+ }
1076
+ else {
1077
+ u -> u_metadata .u_argcount = 0 ;
1078
+ u -> u_metadata .u_posonlyargcount = 0 ;
1079
+ u -> u_metadata .u_kwonlyargcount = 0 ;
1080
+ }
1075
1081
u -> u_ste = _PySymtable_Lookup (c -> c_st , key );
1076
1082
if (!u -> u_ste ) {
1077
1083
compiler_unit_free (u );
@@ -1441,14 +1447,19 @@ compiler_unwind_fblock_stack(struct compiler *c, location *ploc,
1441
1447
}
1442
1448
1443
1449
static int
1444
- compiler_setup_annotations_scope (struct compiler * c , location loc ,
1445
- void * key , PyObject * name )
1450
+ codegen_setup_annotations_scope (struct compiler * c , location loc ,
1451
+ void * key , PyObject * name )
1446
1452
{
1447
- if (compiler_enter_scope (c , name , COMPILER_SCOPE_ANNOTATIONS ,
1448
- key , loc .lineno , NULL ) == -1 ) {
1449
- return ERROR ;
1450
- }
1451
- c -> u -> u_metadata .u_posonlyargcount = 1 ;
1453
+ _PyCompile_CodeUnitMetadata umd = {
1454
+ .u_posonlyargcount = 1 ,
1455
+ };
1456
+ RETURN_IF_ERROR (
1457
+ compiler_enter_scope (c , name , COMPILER_SCOPE_ANNOTATIONS ,
1458
+ key , loc .lineno , NULL , & umd ));
1459
+ assert (c -> u -> u_metadata .u_posonlyargcount == 1 );
1460
+ assert (c -> u -> u_metadata .u_argcount == 0 );
1461
+ assert (c -> u -> u_metadata .u_kwonlyargcount == 0 );
1462
+
1452
1463
// if .format != 1: raise NotImplementedError
1453
1464
_Py_DECLARE_STR (format , ".format" );
1454
1465
ADDOP_I (c , loc , LOAD_FAST , 0 );
@@ -1463,8 +1474,8 @@ compiler_setup_annotations_scope(struct compiler *c, location loc,
1463
1474
}
1464
1475
1465
1476
static int
1466
- compiler_leave_annotations_scope (struct compiler * c , location loc ,
1467
- Py_ssize_t annotations_len )
1477
+ codegen_leave_annotations_scope (struct compiler * c , location loc ,
1478
+ Py_ssize_t annotations_len )
1468
1479
{
1469
1480
ADDOP_I (c , loc , BUILD_MAP , annotations_len );
1470
1481
ADDOP_IN_SCOPE (c , loc , RETURN_VALUE );
@@ -1534,8 +1545,8 @@ compiler_body(struct compiler *c, location loc, asdl_stmt_seq *stmts)
1534
1545
assert (ste -> ste_annotation_block != NULL );
1535
1546
PyObject * deferred_anno = Py_NewRef (c -> u -> u_deferred_annotations );
1536
1547
void * key = (void * )((uintptr_t )ste -> ste_id + 1 );
1537
- if (compiler_setup_annotations_scope (c , loc , key ,
1538
- ste -> ste_annotation_block -> ste_name ) == -1 ) {
1548
+ if (codegen_setup_annotations_scope (c , loc , key ,
1549
+ ste -> ste_annotation_block -> ste_name ) == -1 ) {
1539
1550
Py_DECREF (deferred_anno );
1540
1551
return ERROR ;
1541
1552
}
@@ -1555,7 +1566,7 @@ compiler_body(struct compiler *c, location loc, asdl_stmt_seq *stmts)
1555
1566
Py_DECREF (deferred_anno );
1556
1567
1557
1568
RETURN_IF_ERROR (
1558
- compiler_leave_annotations_scope (c , loc , annotations_len )
1569
+ codegen_leave_annotations_scope (c , loc , annotations_len )
1559
1570
);
1560
1571
RETURN_IF_ERROR (
1561
1572
compiler_nameop (c , loc , & _Py_ID (__annotate__ ), Store )
@@ -1618,7 +1629,7 @@ compiler_enter_anonymous_scope(struct compiler* c, mod_ty mod)
1618
1629
_Py_DECLARE_STR (anon_module , "<module>" );
1619
1630
RETURN_IF_ERROR (
1620
1631
compiler_enter_scope (c , & _Py_STR (anon_module ), COMPILER_SCOPE_MODULE ,
1621
- mod , 1 , NULL ));
1632
+ mod , 1 , NULL , NULL ));
1622
1633
return SUCCESS ;
1623
1634
}
1624
1635
@@ -1828,7 +1839,7 @@ compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
1828
1839
}
1829
1840
1830
1841
static int
1831
- compiler_argannotation (struct compiler * c , identifier id ,
1842
+ codegen_argannotation (struct compiler * c , identifier id ,
1832
1843
expr_ty annotation , Py_ssize_t * annotations_len , location loc )
1833
1844
{
1834
1845
if (!annotation ) {
@@ -1862,14 +1873,14 @@ compiler_argannotation(struct compiler *c, identifier id,
1862
1873
}
1863
1874
1864
1875
static int
1865
- compiler_argannotations (struct compiler * c , asdl_arg_seq * args ,
1866
- Py_ssize_t * annotations_len , location loc )
1876
+ codegen_argannotations (struct compiler * c , asdl_arg_seq * args ,
1877
+ Py_ssize_t * annotations_len , location loc )
1867
1878
{
1868
1879
int i ;
1869
1880
for (i = 0 ; i < asdl_seq_LEN (args ); i ++ ) {
1870
1881
arg_ty arg = (arg_ty )asdl_seq_GET (args , i );
1871
1882
RETURN_IF_ERROR (
1872
- compiler_argannotation (
1883
+ codegen_argannotation (
1873
1884
c ,
1874
1885
arg -> arg ,
1875
1886
arg -> annotation ,
@@ -1880,40 +1891,40 @@ compiler_argannotations(struct compiler *c, asdl_arg_seq* args,
1880
1891
}
1881
1892
1882
1893
static int
1883
- compiler_annotations_in_scope (struct compiler * c , location loc ,
1884
- arguments_ty args , expr_ty returns ,
1885
- Py_ssize_t * annotations_len )
1894
+ codegen_annotations_in_scope (struct compiler * c , location loc ,
1895
+ arguments_ty args , expr_ty returns ,
1896
+ Py_ssize_t * annotations_len )
1886
1897
{
1887
1898
RETURN_IF_ERROR (
1888
- compiler_argannotations (c , args -> args , annotations_len , loc ));
1899
+ codegen_argannotations (c , args -> args , annotations_len , loc ));
1889
1900
1890
1901
RETURN_IF_ERROR (
1891
- compiler_argannotations (c , args -> posonlyargs , annotations_len , loc ));
1902
+ codegen_argannotations (c , args -> posonlyargs , annotations_len , loc ));
1892
1903
1893
1904
if (args -> vararg && args -> vararg -> annotation ) {
1894
1905
RETURN_IF_ERROR (
1895
- compiler_argannotation (c , args -> vararg -> arg ,
1896
- args -> vararg -> annotation , annotations_len , loc ));
1906
+ codegen_argannotation (c , args -> vararg -> arg ,
1907
+ args -> vararg -> annotation , annotations_len , loc ));
1897
1908
}
1898
1909
1899
1910
RETURN_IF_ERROR (
1900
- compiler_argannotations (c , args -> kwonlyargs , annotations_len , loc ));
1911
+ codegen_argannotations (c , args -> kwonlyargs , annotations_len , loc ));
1901
1912
1902
1913
if (args -> kwarg && args -> kwarg -> annotation ) {
1903
1914
RETURN_IF_ERROR (
1904
- compiler_argannotation (c , args -> kwarg -> arg ,
1905
- args -> kwarg -> annotation , annotations_len , loc ));
1915
+ codegen_argannotation (c , args -> kwarg -> arg ,
1916
+ args -> kwarg -> annotation , annotations_len , loc ));
1906
1917
}
1907
1918
1908
1919
RETURN_IF_ERROR (
1909
- compiler_argannotation (c , & _Py_ID (return ), returns , annotations_len , loc ));
1920
+ codegen_argannotation (c , & _Py_ID (return ), returns , annotations_len , loc ));
1910
1921
1911
1922
return 0 ;
1912
1923
}
1913
1924
1914
1925
static int
1915
- compiler_annotations (struct compiler * c , location loc ,
1916
- arguments_ty args , expr_ty returns )
1926
+ codegen_annotations (struct compiler * c , location loc ,
1927
+ arguments_ty args , expr_ty returns )
1917
1928
{
1918
1929
/* Push arg annotation names and values.
1919
1930
The expressions are evaluated separately from the rest of the source code.
@@ -1930,15 +1941,15 @@ compiler_annotations(struct compiler *c, location loc,
1930
1941
bool annotations_used = ste -> ste_annotations_used ;
1931
1942
1932
1943
if (annotations_used ) {
1933
- if (compiler_setup_annotations_scope (c , loc , (void * )args ,
1934
- ste -> ste_name ) < 0 ) {
1944
+ if (codegen_setup_annotations_scope (c , loc , (void * )args ,
1945
+ ste -> ste_name ) < 0 ) {
1935
1946
Py_DECREF (ste );
1936
1947
return ERROR ;
1937
1948
}
1938
1949
}
1939
1950
Py_DECREF (ste );
1940
1951
1941
- if (compiler_annotations_in_scope (c , loc , args , returns , & annotations_len ) < 0 ) {
1952
+ if (codegen_annotations_in_scope (c , loc , args , returns , & annotations_len ) < 0 ) {
1942
1953
if (annotations_used ) {
1943
1954
compiler_exit_scope (c );
1944
1955
}
@@ -1947,7 +1958,7 @@ compiler_annotations(struct compiler *c, location loc,
1947
1958
1948
1959
if (annotations_used ) {
1949
1960
RETURN_IF_ERROR (
1950
- compiler_leave_annotations_scope (c , loc , annotations_len )
1961
+ codegen_leave_annotations_scope (c , loc , annotations_len )
1951
1962
);
1952
1963
return MAKE_FUNCTION_ANNOTATE ;
1953
1964
}
@@ -2011,7 +2022,7 @@ compiler_type_param_bound_or_default(struct compiler *c, expr_ty e,
2011
2022
{
2012
2023
PyObject * defaults = PyTuple_Pack (1 , _PyLong_GetOne ());
2013
2024
ADDOP_LOAD_CONST_NEW (c , LOC (e ), defaults );
2014
- if (compiler_setup_annotations_scope (c , LOC (e ), key , name ) == -1 ) {
2025
+ if (codegen_setup_annotations_scope (c , LOC (e ), key , name ) == -1 ) {
2015
2026
return ERROR ;
2016
2027
}
2017
2028
if (allow_starred && e -> kind == Starred_kind ) {
@@ -2155,8 +2166,13 @@ compiler_function_body(struct compiler *c, stmt_ty s, int is_async, Py_ssize_t f
2155
2166
scope_type = COMPILER_SCOPE_FUNCTION ;
2156
2167
}
2157
2168
2169
+ _PyCompile_CodeUnitMetadata umd = {
2170
+ .u_argcount = asdl_seq_LEN (args -> args ),
2171
+ .u_posonlyargcount = asdl_seq_LEN (args -> posonlyargs ),
2172
+ .u_kwonlyargcount = asdl_seq_LEN (args -> kwonlyargs ),
2173
+ };
2158
2174
RETURN_IF_ERROR (
2159
- compiler_enter_scope (c , name , scope_type , (void * )s , firstlineno , NULL ));
2175
+ compiler_enter_scope (c , name , scope_type , (void * )s , firstlineno , NULL , & umd ));
2160
2176
2161
2177
Py_ssize_t first_instr = 0 ;
2162
2178
PyObject * docstring = _PyAST_GetDocString (body );
@@ -2181,9 +2197,9 @@ compiler_function_body(struct compiler *c, stmt_ty s, int is_async, Py_ssize_t f
2181
2197
}
2182
2198
Py_CLEAR (docstring );
2183
2199
2184
- c -> u -> u_metadata .u_argcount = asdl_seq_LEN (args -> args );
2185
- c -> u -> u_metadata .u_posonlyargcount = asdl_seq_LEN (args -> posonlyargs );
2186
- c -> u -> u_metadata .u_kwonlyargcount = asdl_seq_LEN (args -> kwonlyargs );
2200
+ assert ( c -> u -> u_metadata .u_argcount == asdl_seq_LEN (args -> args ) );
2201
+ assert ( c -> u -> u_metadata .u_posonlyargcount == asdl_seq_LEN (args -> posonlyargs ) );
2202
+ assert ( c -> u -> u_metadata .u_kwonlyargcount == asdl_seq_LEN (args -> kwonlyargs ) );
2187
2203
2188
2204
NEW_JUMP_TARGET_LABEL (c , start );
2189
2205
USE_LABEL (c , start );
@@ -2222,7 +2238,7 @@ compiler_function_body(struct compiler *c, stmt_ty s, int is_async, Py_ssize_t f
2222
2238
}
2223
2239
2224
2240
static int
2225
- compiler_function (struct compiler * c , stmt_ty s , int is_async )
2241
+ codegen_function (struct compiler * c , stmt_ty s , int is_async )
2226
2242
{
2227
2243
arguments_ty args ;
2228
2244
expr_ty returns ;
@@ -2282,8 +2298,11 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
2282
2298
if (!type_params_name ) {
2283
2299
return ERROR ;
2284
2300
}
2301
+ _PyCompile_CodeUnitMetadata umd = {
2302
+ .u_argcount = num_typeparam_args ,
2303
+ };
2285
2304
if (compiler_enter_scope (c , type_params_name , COMPILER_SCOPE_ANNOTATIONS ,
2286
- (void * )type_params , firstlineno , NULL ) == -1 ) {
2305
+ (void * )type_params , firstlineno , NULL , & umd ) == -1 ) {
2287
2306
Py_DECREF (type_params_name );
2288
2307
return ERROR ;
2289
2308
}
@@ -2294,7 +2313,7 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
2294
2313
}
2295
2314
}
2296
2315
2297
- int annotations_flag = compiler_annotations (c , loc , args , returns );
2316
+ int annotations_flag = codegen_annotations (c , loc , args , returns );
2298
2317
if (annotations_flag < 0 ) {
2299
2318
if (is_generic ) {
2300
2319
compiler_exit_scope (c );
@@ -2314,7 +2333,7 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
2314
2333
ADDOP_I_IN_SCOPE (c , loc , SWAP , 2 );
2315
2334
ADDOP_I_IN_SCOPE (c , loc , CALL_INTRINSIC_2 , INTRINSIC_SET_FUNCTION_TYPE_PARAMS );
2316
2335
2317
- c -> u -> u_metadata .u_argcount = num_typeparam_args ;
2336
+ assert ( c -> u -> u_metadata .u_argcount == num_typeparam_args ) ;
2318
2337
PyCodeObject * co = optimize_and_assemble (c , 0 );
2319
2338
compiler_exit_scope (c );
2320
2339
if (co == NULL ) {
@@ -2365,7 +2384,7 @@ compiler_class_body(struct compiler *c, stmt_ty s, int firstlineno)
2365
2384
/* 1. compile the class body into a code object */
2366
2385
RETURN_IF_ERROR (
2367
2386
compiler_enter_scope (c , s -> v .ClassDef .name , COMPILER_SCOPE_CLASS ,
2368
- (void * )s , firstlineno , s -> v .ClassDef .name ));
2387
+ (void * )s , firstlineno , s -> v .ClassDef .name , NULL ));
2369
2388
2370
2389
location loc = LOCATION (firstlineno , firstlineno , 0 , 0 );
2371
2390
/* load (global) __name__ ... */
@@ -2508,7 +2527,7 @@ compiler_class(struct compiler *c, stmt_ty s)
2508
2527
return ERROR ;
2509
2528
}
2510
2529
if (compiler_enter_scope (c , type_params_name , COMPILER_SCOPE_ANNOTATIONS ,
2511
- (void * )type_params , firstlineno , s -> v .ClassDef .name ) == -1 ) {
2530
+ (void * )type_params , firstlineno , s -> v .ClassDef .name , NULL ) == -1 ) {
2512
2531
Py_DECREF (type_params_name );
2513
2532
return ERROR ;
2514
2533
}
@@ -2592,7 +2611,7 @@ compiler_typealias_body(struct compiler *c, stmt_ty s)
2592
2611
PyObject * defaults = PyTuple_Pack (1 , _PyLong_GetOne ());
2593
2612
ADDOP_LOAD_CONST_NEW (c , loc , defaults );
2594
2613
RETURN_IF_ERROR (
2595
- compiler_setup_annotations_scope (c , LOC (s ), s , name ));
2614
+ codegen_setup_annotations_scope (c , LOC (s ), s , name ));
2596
2615
/* Make None the first constant, so the evaluate function can't have a
2597
2616
docstring. */
2598
2617
RETURN_IF_ERROR (compiler_add_const (c , Py_None ));
@@ -2627,7 +2646,7 @@ compiler_typealias(struct compiler *c, stmt_ty s)
2627
2646
return ERROR ;
2628
2647
}
2629
2648
if (compiler_enter_scope (c , type_params_name , COMPILER_SCOPE_ANNOTATIONS ,
2630
- (void * )type_params , loc .lineno , NULL ) == -1 ) {
2649
+ (void * )type_params , loc .lineno , NULL , NULL ) == -1 ) {
2631
2650
Py_DECREF (type_params_name );
2632
2651
return ERROR ;
2633
2652
}
@@ -2889,18 +2908,23 @@ compiler_lambda(struct compiler *c, expr_ty e)
2889
2908
return ERROR ;
2890
2909
}
2891
2910
2911
+ _PyCompile_CodeUnitMetadata umd = {
2912
+ .u_argcount = asdl_seq_LEN (args -> args ),
2913
+ .u_posonlyargcount = asdl_seq_LEN (args -> posonlyargs ),
2914
+ .u_kwonlyargcount = asdl_seq_LEN (args -> kwonlyargs ),
2915
+ };
2892
2916
_Py_DECLARE_STR (anon_lambda , "<lambda>" );
2893
2917
RETURN_IF_ERROR (
2894
2918
compiler_enter_scope (c , & _Py_STR (anon_lambda ), COMPILER_SCOPE_LAMBDA ,
2895
- (void * )e , e -> lineno , NULL ));
2919
+ (void * )e , e -> lineno , NULL , & umd ));
2896
2920
2897
2921
/* Make None the first constant, so the lambda can't have a
2898
2922
docstring. */
2899
2923
RETURN_IF_ERROR (compiler_add_const (c , Py_None ));
2900
2924
2901
- c -> u -> u_metadata .u_argcount = asdl_seq_LEN (args -> args );
2902
- c -> u -> u_metadata .u_posonlyargcount = asdl_seq_LEN (args -> posonlyargs );
2903
- c -> u -> u_metadata .u_kwonlyargcount = asdl_seq_LEN (args -> kwonlyargs );
2925
+ assert ( c -> u -> u_metadata .u_argcount == asdl_seq_LEN (args -> args ) );
2926
+ assert ( c -> u -> u_metadata .u_posonlyargcount == asdl_seq_LEN (args -> posonlyargs ) );
2927
+ assert ( c -> u -> u_metadata .u_kwonlyargcount == asdl_seq_LEN (args -> kwonlyargs ) );
2904
2928
VISIT_IN_SCOPE (c , expr , e -> v .Lambda .body );
2905
2929
if (SYMTABLE_ENTRY (c )-> ste_generator ) {
2906
2930
co = optimize_and_assemble (c , 0 );
@@ -3867,7 +3891,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s)
3867
3891
3868
3892
switch (s -> kind ) {
3869
3893
case FunctionDef_kind :
3870
- return compiler_function (c , s , 0 );
3894
+ return codegen_function (c , s , 0 );
3871
3895
case ClassDef_kind :
3872
3896
return compiler_class (c , s );
3873
3897
case TypeAlias_kind :
@@ -3949,7 +3973,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s)
3949
3973
case With_kind :
3950
3974
return compiler_with (c , s , 0 );
3951
3975
case AsyncFunctionDef_kind :
3952
- return compiler_function (c , s , 1 );
3976
+ return codegen_function (c , s , 1 );
3953
3977
case AsyncWith_kind :
3954
3978
return compiler_async_with (c , s , 0 );
3955
3979
case AsyncFor_kind :
@@ -5674,7 +5698,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
5674
5698
}
5675
5699
else {
5676
5700
if (compiler_enter_scope (c , name , COMPILER_SCOPE_COMPREHENSION ,
5677
- (void * )e , e -> lineno , NULL ) < 0 ) {
5701
+ (void * )e , e -> lineno , NULL , NULL ) < 0 ) {
5678
5702
goto error ;
5679
5703
}
5680
5704
}
0 commit comments