diff Python/compile.c @ 16916:b0456d3a0f1f legacy-trunk

Useful future statement support for the interactive interpreter (Also remove warning about module-level global decl, because we can't distinguish from code passed to exec.) Define PyCompilerFlags type contains a single element, cf_nested_scopes, that is true if a nested scopes future statement has been entered at the interactive prompt. New API functions: PyNode_CompileFlags() PyRun_InteractiveOneFlags() -- same as their non Flags counterparts except that the take an optional PyCompilerFlags pointer compile.c: In jcompile() use PyCompilerFlags argument. If cf_nested_scopes is true, compile code with nested scopes. If it is false, but the code has a valid future nested scopes statement, set it to true. pythonrun.c: Create a new PyCompilerFlags object in PyRun_InteractiveLoop() and thread it through to PyRun_InteractiveOneFlags().
author Jeremy Hylton <jeremy@alum.mit.edu>
date Thu, 01 Mar 2001 22:59:14 +0000
parents f27d3299df39
children b046a31af2e0
line wrap: on
line diff
--- a/Python/compile.c	Thu Mar 01 22:56:17 2001 +0000
+++ b/Python/compile.c	Thu Mar 01 22:59:14 2001 +0000
@@ -471,7 +471,8 @@
 static void com_assign(struct compiling *, node *, int, node *);
 static void com_assign_name(struct compiling *, node *, int);
 static PyCodeObject *icompile(node *, struct compiling *);
-static PyCodeObject *jcompile(node *, char *, struct compiling *);
+static PyCodeObject *jcompile(node *, char *, struct compiling *,
+			      PyCompilerFlags *);
 static PyObject *parsestrplus(node *);
 static PyObject *parsestr(char *);
 static node *get_rawdocstring(node *);
@@ -3816,7 +3817,13 @@
 PyCodeObject *
 PyNode_Compile(node *n, char *filename)
 {
-	return jcompile(n, filename, NULL);
+	return PyNode_CompileFlags(n, filename, NULL);
+}
+
+PyCodeObject *
+PyNode_CompileFlags(node *n, char *filename, PyCompilerFlags *flags)
+{
+	return jcompile(n, filename, NULL, flags);
 }
 
 struct symtable *
@@ -3844,11 +3851,12 @@
 static PyCodeObject *
 icompile(node *n, struct compiling *base)
 {
-	return jcompile(n, base->c_filename, base);
+	return jcompile(n, base->c_filename, base, NULL);
 }
 
 static PyCodeObject *
-jcompile(node *n, char *filename, struct compiling *base)
+jcompile(node *n, char *filename, struct compiling *base,
+	 PyCompilerFlags *flags)
 {
 	struct compiling sc;
 	PyCodeObject *co;
@@ -3864,7 +3872,17 @@
 	} else {
 		sc.c_private = NULL;
 		sc.c_future = PyNode_Future(n, filename);
-		if (sc.c_future == NULL || symtable_build(&sc, n) < 0) {
+		if (sc.c_future == NULL) {
+			com_free(&sc);
+			return NULL;
+		}
+		if (flags) {
+			if (flags->cf_nested_scopes)
+				sc.c_future->ff_nested_scopes = 1;
+			else if (sc.c_future->ff_nested_scopes)
+				flags->cf_nested_scopes = 1;
+		}
+		if (symtable_build(&sc, n) < 0) {
 			com_free(&sc);
 			return NULL;
 		}
@@ -4952,12 +4970,10 @@
 {
 	int i;
 
-	if (st->st_nscopes == 1) {
-		/* XXX must check that we are compiling file_input */
-		if (symtable_warn(st, 
-		  "global statement has no meaning at module level") < 0)
-			return;
-	}
+	/* XXX It might be helpful to warn about module-level global
+	   statements, but it's hard to tell the difference between
+	   module-level and a string passed to exec.
+	*/
 
 	for (i = 1; i < NCH(n); i += 2) {
 		char *name = STR(CHILD(n, i));