mirror of
https://github.com/python/cpython.git
synced 2025-09-11 19:27:07 +00:00
Added support for ``if __debug__:'' -- if -O is given, this form is
recognized by the code generator and code generation for the test and the subsequent suite is suppressed. One must write *exactly* ``if __debug__:'' or ``elif __debug__:'' -- no parentheses or operators must be present, or the optimization is not carried through. Whitespace doesn't matter. Other uses of __debug__ will find __debug__ defined as 0 or 1 in the __builtin__ module.
This commit is contained in:
parent
0824f63cfc
commit
7c53111d5b
1 changed files with 90 additions and 2 deletions
|
@ -1891,6 +1891,7 @@ com_assign(c, n, assigning)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Forward */ static node *get_rawdocstring PROTO((node *));
|
/* Forward */ static node *get_rawdocstring PROTO((node *));
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -2170,6 +2171,90 @@ com_exec_stmt(c, n)
|
||||||
com_pop(c, 3);
|
com_pop(c, 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
is_constant_false(c, n)
|
||||||
|
struct compiling *c;
|
||||||
|
node *n;
|
||||||
|
{
|
||||||
|
object *v;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
/* Label to avoid tail recursion */
|
||||||
|
next:
|
||||||
|
switch (TYPE(n)) {
|
||||||
|
|
||||||
|
case suite:
|
||||||
|
if (NCH(n) == 1) {
|
||||||
|
n = CHILD(n, 0);
|
||||||
|
goto next;
|
||||||
|
}
|
||||||
|
/* Fall through */
|
||||||
|
case file_input:
|
||||||
|
for (i = 0; i < NCH(n); i++) {
|
||||||
|
node *ch = CHILD(n, i);
|
||||||
|
if (TYPE(ch) == stmt) {
|
||||||
|
n = ch;
|
||||||
|
goto next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case stmt:
|
||||||
|
case simple_stmt:
|
||||||
|
case small_stmt:
|
||||||
|
n = CHILD(n, 0);
|
||||||
|
goto next;
|
||||||
|
|
||||||
|
case expr_stmt:
|
||||||
|
case testlist:
|
||||||
|
case test:
|
||||||
|
case and_test:
|
||||||
|
case not_test:
|
||||||
|
case comparison:
|
||||||
|
case expr:
|
||||||
|
case xor_expr:
|
||||||
|
case and_expr:
|
||||||
|
case shift_expr:
|
||||||
|
case arith_expr:
|
||||||
|
case term:
|
||||||
|
case factor:
|
||||||
|
case power:
|
||||||
|
case atom:
|
||||||
|
if (NCH(n) == 1) {
|
||||||
|
n = CHILD(n, 0);
|
||||||
|
goto next;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case NAME:
|
||||||
|
if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0)
|
||||||
|
return 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case NUMBER:
|
||||||
|
v = parsenumber(c, STR(n));
|
||||||
|
if (v == NULL) {
|
||||||
|
err_clear();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i = testbool(v);
|
||||||
|
DECREF(v);
|
||||||
|
return i == 0;
|
||||||
|
|
||||||
|
case STRING:
|
||||||
|
v = parsestrplus(n);
|
||||||
|
if (v == NULL) {
|
||||||
|
err_clear();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i = testbool(v);
|
||||||
|
DECREF(v);
|
||||||
|
return i == 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
com_if_stmt(c, n)
|
com_if_stmt(c, n)
|
||||||
struct compiling *c;
|
struct compiling *c;
|
||||||
|
@ -2182,9 +2267,11 @@ com_if_stmt(c, n)
|
||||||
for (i = 0; i+3 < NCH(n); i+=4) {
|
for (i = 0; i+3 < NCH(n); i+=4) {
|
||||||
int a = 0;
|
int a = 0;
|
||||||
node *ch = CHILD(n, i+1);
|
node *ch = CHILD(n, i+1);
|
||||||
|
if (is_constant_false(c, ch))
|
||||||
|
continue;
|
||||||
if (i > 0)
|
if (i > 0)
|
||||||
com_addoparg(c, SET_LINENO, ch->n_lineno);
|
com_addoparg(c, SET_LINENO, ch->n_lineno);
|
||||||
com_node(c, CHILD(n, i+1));
|
com_node(c, ch);
|
||||||
com_addfwref(c, JUMP_IF_FALSE, &a);
|
com_addfwref(c, JUMP_IF_FALSE, &a);
|
||||||
com_addbyte(c, POP_TOP);
|
com_addbyte(c, POP_TOP);
|
||||||
com_pop(c, 1);
|
com_pop(c, 1);
|
||||||
|
@ -2196,6 +2283,7 @@ com_if_stmt(c, n)
|
||||||
}
|
}
|
||||||
if (i+2 < NCH(n))
|
if (i+2 < NCH(n))
|
||||||
com_node(c, CHILD(n, i+2));
|
com_node(c, CHILD(n, i+2));
|
||||||
|
if (anchor)
|
||||||
com_backpatch(c, anchor);
|
com_backpatch(c, anchor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue