diff Python/symtable.c @ 36695:b6d2f6fd3e11 legacy-trunk

PEP 343 -- the with-statement. This was started by Mike Bland and completed by Guido (with help from Neal). This still needs a __future__ statement added; Thomas is working on Michael's patch for that aspect. There's a small amount of code cleanup and refactoring in ast.c, compile.c and ceval.c (I fixed the lltrace behavior when EXT_POP is used -- however I had to make lltrace a static global).
author Guido van Rossum <guido@python.org>
date Mon, 27 Feb 2006 22:32:47 +0000
parents 6165927431d6
children 796d150e45a9
line wrap: on
line diff
--- a/Python/symtable.c	Mon Feb 27 21:41:03 2006 +0000
+++ b/Python/symtable.c	Mon Feb 27 22:32:47 2006 +0000
@@ -891,6 +891,21 @@
 }
 
 static int
+symtable_new_tmpname(struct symtable *st)
+{
+	char tmpname[256];
+	identifier tmp;
+
+	PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
+		      ++st->st_cur->ste_tmpname);
+	tmp = PyString_InternFromString(tmpname);
+	if (!symtable_add_def(st, tmp, DEF_LOCAL))
+		return 0;
+	Py_DECREF(tmp);
+	return 1;
+}
+
+static int
 symtable_visit_stmt(struct symtable *st, stmt_ty s)
 {
 	switch (s->kind) {
@@ -1051,6 +1066,17 @@
         case Continue_kind:
 		/* nothing to do here */
 		break;
+        case With_kind:
+		if (!symtable_new_tmpname(st))
+			return 0;
+                VISIT(st, expr, s->v.With.context_expr);
+                if (s->v.With.optional_vars) {
+			if (!symtable_new_tmpname(st))
+				return 0;
+                        VISIT(st, expr, s->v.With.optional_vars);
+                }
+                VISIT_SEQ(st, stmt, s->v.With.body);
+                break;
 	}
 	return 1;
 }
@@ -1093,26 +1119,16 @@
 		VISIT_SEQ(st, expr, e->v.Dict.keys);
 		VISIT_SEQ(st, expr, e->v.Dict.values);
 		break;
-        case ListComp_kind: {
-		char tmpname[256];
-		identifier tmp;
-
-		PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
-			      ++st->st_cur->ste_tmpname);
-		tmp = PyString_InternFromString(tmpname);
-		if (!symtable_add_def(st, tmp, DEF_LOCAL))
+        case ListComp_kind:
+		if (!symtable_new_tmpname(st))
 			return 0;
-		Py_DECREF(tmp);
 		VISIT(st, expr, e->v.ListComp.elt);
 		VISIT_SEQ(st, comprehension, e->v.ListComp.generators);
 		break;
-	}
-        case GeneratorExp_kind: {
-		if (!symtable_visit_genexp(st, e)) {
+        case GeneratorExp_kind:
+		if (!symtable_visit_genexp(st, e))
 			return 0;
-		}
 		break;
-	}
         case Yield_kind:
 		if (e->v.Yield.value)
 			VISIT(st, expr, e->v.Yield.value);