diff Python/compile.c @ 14038:18385172fac0 legacy-trunk

Apply SF patch #101135, adding 'import module as m' and 'from module import name as n'. By doing some twists and turns, "as" is not a reserved word. There is a slight change in semantics for 'from module import name' (it will now honour the 'global' keyword) but only in cases that are explicitly undocumented.
author Thomas Wouters <thomas@python.org>
date Thu, 17 Aug 2000 22:55:00 +0000
parents 143ffbf87e51
children 920fc2868543
line wrap: on
line diff
--- a/Python/compile.c	Thu Aug 17 22:37:32 2000 +0000
+++ b/Python/compile.c	Thu Aug 17 22:55:00 2000 +0000
@@ -2096,6 +2096,22 @@
 }
 
 static void
+com_from_import(struct compiling *c, node *n)
+{
+	com_addopname(c, IMPORT_FROM, CHILD(n, 0));
+	com_push(c, 1);
+	if (NCH(n) > 1) {
+		if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
+			com_error(c, PyExc_SyntaxError, "invalid syntax");
+			return;
+		}
+		com_addopname(c, STORE_NAME, CHILD(n, 2));
+	} else
+		com_addopname(c, STORE_NAME, CHILD(n, 0));
+	com_pop(c, 1);
+}
+
+static void
 com_import_stmt(struct compiling *c, node *n)
 {
 	int i;
@@ -2107,18 +2123,32 @@
 		REQ(CHILD(n, 1), dotted_name);
 		com_addopname(c, IMPORT_NAME, CHILD(n, 1));
 		com_push(c, 1);
-		for (i = 3; i < NCH(n); i += 2)
-			com_addopname(c, IMPORT_FROM, CHILD(n, i));
-		com_addbyte(c, POP_TOP);
+		if (TYPE(CHILD(n, 3)) == STAR) 
+			com_addbyte(c, IMPORT_STAR);
+		else {
+			for (i = 3; i < NCH(n); i += 2) 
+				com_from_import(c, CHILD(n, i));
+			com_addbyte(c, POP_TOP);
+		}
 		com_pop(c, 1);
 	}
 	else {
 		/* 'import' ... */
 		for (i = 1; i < NCH(n); i += 2) {
-			REQ(CHILD(n, i), dotted_name);
-			com_addopname(c, IMPORT_NAME, CHILD(n, i));
+			node *subn = CHILD(n, i);
+			REQ(subn, dotted_as_name);
+			com_addopname(c, IMPORT_NAME, CHILD(subn, 0));
 			com_push(c, 1);
-			com_addopname(c, STORE_NAME, CHILD(CHILD(n, i), 0));
+			if (NCH(subn) > 1) {
+				if (strcmp(STR(CHILD(subn, 1)), "as") != 0) {
+					com_error(c, PyExc_SyntaxError,
+						  "invalid syntax");
+					return;
+				}
+				com_addopname(c, STORE_NAME, CHILD(subn, 2));
+			} else
+				com_addopname(c, STORE_NAME,
+					      CHILD(CHILD(subn, 0),0));
 			com_pop(c, 1);
 		}
 	}
@@ -3295,12 +3325,14 @@
 		case IMPORT_FROM:
 			com_addlocal_o(c, GETNAMEOBJ(oparg));
 			break;
+		case IMPORT_STAR:
 		case EXEC_STMT:
 			c->c_flags &= ~CO_OPTIMIZED;
 			break;
 		}
 	}
 	
+	/* TBD: Is this still necessary ? */
 	if (PyDict_GetItemString(c->c_locals, "*") != NULL)
 		c->c_flags &= ~CO_OPTIMIZED;