@@ -161,6 +161,9 @@ struct compiler {
161
161
int c_optimize ; /* optimization level */
162
162
int c_interactive ; /* true if in interactive mode */
163
163
int c_nestlevel ;
164
+ int c_emit_bytecode ; /* The compiler won't emmit any bytecode
165
+ if this flag is false. This can be used
166
+ to visit nodes to check only errors. */
164
167
165
168
PyObject * c_const_cache ; /* Python dict holding all constants,
166
169
including names tuple */
@@ -340,6 +343,7 @@ PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
340
343
c .c_flags = flags ;
341
344
c .c_optimize = (optimize == -1 ) ? config -> optimization_level : optimize ;
342
345
c .c_nestlevel = 0 ;
346
+ c .c_emit_bytecode = 1 ;
343
347
344
348
if (!_PyAST_Optimize (mod , arena , c .c_optimize )) {
345
349
goto finally ;
@@ -1152,6 +1156,9 @@ compiler_addop(struct compiler *c, int opcode)
1152
1156
struct instr * i ;
1153
1157
int off ;
1154
1158
assert (!HAS_ARG (opcode ));
1159
+ if (!c -> c_emit_bytecode ) {
1160
+ return 1 ;
1161
+ }
1155
1162
off = compiler_next_instr (c , c -> u -> u_curblock );
1156
1163
if (off < 0 )
1157
1164
return 0 ;
@@ -1359,6 +1366,10 @@ compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
1359
1366
struct instr * i ;
1360
1367
int off ;
1361
1368
1369
+ if (!c -> c_emit_bytecode ) {
1370
+ return 1 ;
1371
+ }
1372
+
1362
1373
/* oparg value is unsigned, but a signed C int is usually used to store
1363
1374
it in the C code (like Python/ceval.c).
1364
1375
@@ -1385,6 +1396,10 @@ compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1385
1396
struct instr * i ;
1386
1397
int off ;
1387
1398
1399
+ if (!c -> c_emit_bytecode ) {
1400
+ return 1 ;
1401
+ }
1402
+
1388
1403
assert (HAS_ARG (opcode ));
1389
1404
assert (b != NULL );
1390
1405
off = compiler_next_instr (c , c -> u -> u_curblock );
@@ -1519,6 +1534,17 @@ compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1519
1534
} \
1520
1535
}
1521
1536
1537
+ /* These macros allows to check only for errors and not emmit bytecode
1538
+ * while visiting nodes.
1539
+ */
1540
+
1541
+ #define BEGIN_DO_NOT_EMIT_BYTECODE { \
1542
+ c->c_emit_bytecode = 0;
1543
+
1544
+ #define END_DO_NOT_EMIT_BYTECODE \
1545
+ c->c_emit_bytecode = 1; \
1546
+ }
1547
+
1522
1548
/* Search if variable annotations are present statically in a block. */
1523
1549
1524
1550
static int
@@ -2546,12 +2572,18 @@ compiler_if(struct compiler *c, stmt_ty s)
2546
2572
return 0 ;
2547
2573
2548
2574
constant = expr_constant (s -> v .If .test );
2549
- /* constant = 0: "if 0" Leave the optimizations to
2550
- * the pephole optimizer to check for syntax errors
2551
- * in the block.
2575
+ /* constant = 0: "if 0"
2552
2576
* constant = 1: "if 1", "if 2", ...
2553
2577
* constant = -1: rest */
2554
- if (constant == 1 ) {
2578
+ if (constant == 0 ) {
2579
+ if (s -> v .If .orelse ) {
2580
+ VISIT_SEQ (c , stmt , s -> v .If .orelse );
2581
+ } else {
2582
+ BEGIN_DO_NOT_EMIT_BYTECODE
2583
+ VISIT_SEQ (c , stmt , s -> v .If .body );
2584
+ END_DO_NOT_EMIT_BYTECODE
2585
+ }
2586
+ } else if (constant == 1 ) {
2555
2587
VISIT_SEQ (c , stmt , s -> v .If .body );
2556
2588
} else {
2557
2589
if (asdl_seq_LEN (s -> v .If .orelse )) {
0 commit comments