summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1997-04-02 05:24:36 (GMT)
committerGuido van Rossum <guido@python.org>1997-04-02 05:24:36 (GMT)
commit228d7f3f67f399a456383890c3447efe92afac88 (patch)
tree8512c124f21bd81391926ed3c48dfb1587eed8ff /Python
parent8ecd1ad785c59edc64d1ed0760b8356bb28a566c (diff)
downloadcpython-228d7f3f67f399a456383890c3447efe92afac88.zip
cpython-228d7f3f67f399a456383890c3447efe92afac88.tar.gz
cpython-228d7f3f67f399a456383890c3447efe92afac88.tar.bz2
Added assert statement.
Diffstat (limited to 'Python')
-rw-r--r--Python/compile.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/Python/compile.c b/Python/compile.c
index 3bdd994..0338d6f 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -1926,6 +1926,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;
node *n;
@@ -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;