diff Python/compile.c @ 5233:b749d2dd7582 legacy-trunk

Added assert statement.
author Guido van Rossum <guido@python.org>
date Wed, 02 Apr 1997 05:24:36 +0000
parents 74c12704a0bd
children 38878f1cd4f1
line wrap: on
line diff
--- a/Python/compile.c	Wed Apr 02 05:24:08 1997 +0000
+++ b/Python/compile.c	Wed Apr 02 05:24:36 1997 +0000
@@ -1926,6 +1926,48 @@
 }
 
 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 @@
 	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;