Added assert statement.

This commit is contained in:
Guido van Rossum 1997-04-02 05:24:36 +00:00
parent 8ecd1ad785
commit 228d7f3f67
1 changed files with 45 additions and 0 deletions

View File

@ -1925,6 +1925,48 @@ com_expr_stmt(c, n)
}
}
static void
com_assert_stmt(c, n)
struct compiling *c;
node *n;
{
int a = 0, b = 0;
int i;
REQ(n, assert_stmt); /* 'assert' test [',' test] */
/* Generate code like for
if __debug__:
if not <test>:
raise AssertionError [, <message>]
where <message> is the second test, if present.
*/
if (Py_OptimizeFlag)
return;
com_addopnamestr(c, LOAD_GLOBAL, "__debug__");
com_push(c, 1);
com_addfwref(c, JUMP_IF_FALSE, &a);
com_addbyte(c, POP_TOP);
com_pop(c, 1);
com_node(c, CHILD(n, 1));
com_addfwref(c, JUMP_IF_TRUE, &b);
com_addbyte(c, POP_TOP);
com_pop(c, 1);
/* Raise that exception! */
com_addopnamestr(c, LOAD_GLOBAL, "AssertionError");
com_push(c, 1);
i = NCH(n)/2; /* Either 2 or 4 */
if (i > 1)
com_node(c, CHILD(n, 3));
com_addoparg(c, RAISE_VARARGS, i);
com_pop(c, i);
/* The interpreter does not fall through */
/* All jumps converge here */
com_backpatch(c, a);
com_backpatch(c, b);
com_addbyte(c, POP_TOP);
}
static void
com_print_stmt(c, n)
struct compiling *c;
@ -2866,6 +2908,9 @@ com_node(c, n)
case exec_stmt:
com_exec_stmt(c, n);
break;
case assert_stmt:
com_assert_stmt(c, n);
break;
case if_stmt:
com_if_stmt(c, n);
break;