[Python-checkins] CVS: python/dist/src/Python bltinmodule.c,2.195,2.196 ceval.c,2.234,2.235 pythonrun.c,2.126,2.127

Jeremy Hylton jhylton@users.sourceforge.net
Wed, 21 Mar 2001 18:48:00 -0800


Update of /cvsroot/python/python/dist/src/Python
In directory usw-pr-cvs1:/tmp/cvs-serv30456/Python

Modified Files:
	bltinmodule.c ceval.c pythonrun.c 
Log Message:
Extend support for from __future__ import nested_scopes

If a module has a future statement enabling nested scopes, they are
also enable for the exec statement and the functions compile() and
execfile() if they occur in the module.

If Python is run with the -i option, which enters interactive mode
after executing a script, and the script it runs enables nested
scopes, they are also enabled in interactive mode.

XXX The use of -i with -c "from __future__ import nested_scopes" is
not supported.  What's the point?

To support these changes, many function variants have been added to
pythonrun.c.  All the variants names end with Flags and they take an
extra PyCompilerFlags * argument.  It is possible that this complexity
will be eliminated in a future version of the interpreter in which
nested scopes are not optional.




Index: bltinmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/bltinmodule.c,v
retrieving revision 2.195
retrieving revision 2.196
diff -C2 -r2.195 -r2.196
*** bltinmodule.c	2001/03/21 18:40:58	2.195
--- bltinmodule.c	2001/03/22 02:47:58	2.196
***************
*** 374,378 ****
  		return NULL;
  	}
! 	return Py_CompileString(str, filename, start);
  }
  
--- 374,383 ----
  		return NULL;
  	}
! 	if (PyEval_GetNestedScopes()) {
! 		PyCompilerFlags cf;
! 		cf.cf_nested_scopes = 1;
! 		return Py_CompileStringFlags(str, filename, start, &cf);
! 	} else
! 		return Py_CompileString(str, filename, start);
  }
  
***************
*** 809,813 ****
  		return NULL;
  	}
! 	res = PyRun_FileEx(fp, filename, Py_file_input, globals, locals, 1);
  	return res;
  }
--- 814,825 ----
  		return NULL;
  	}
! 	if (PyEval_GetNestedScopes()) {
! 		PyCompilerFlags cf;
! 		cf.cf_nested_scopes = 1;
! 		res = PyRun_FileExFlags(fp, filename, Py_file_input, globals, 
! 				   locals, 1, &cf);
! 	} else 
! 		res = PyRun_FileEx(fp, filename, Py_file_input, globals, 
! 				   locals, 1);
  	return res;
  }

Index: ceval.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/ceval.c,v
retrieving revision 2.234
retrieving revision 2.235
diff -C2 -r2.234 -r2.235
*** ceval.c	2001/03/22 02:32:48	2.234
--- ceval.c	2001/03/22 02:47:58	2.235
***************
*** 3457,3461 ****
  		FILE *fp = PyFile_AsFile(prog);
  		char *name = PyString_AsString(PyFile_Name(prog));
! 		v = PyRun_File(fp, name, Py_file_input, globals, locals); 
  	}
  	else {
--- 3457,3469 ----
  		FILE *fp = PyFile_AsFile(prog);
  		char *name = PyString_AsString(PyFile_Name(prog));
! 		if (PyEval_GetNestedScopes()) {
! 			PyCompilerFlags cf;
! 			cf.cf_nested_scopes = 1;
! 			v = PyRun_FileFlags(fp, name, Py_file_input, globals,
! 					    locals, &cf); 
! 		} else {
! 			v = PyRun_File(fp, name, Py_file_input, globals,
! 				       locals); 
! 		}
  	}
  	else {
***************
*** 3463,3467 ****
  		if (PyString_AsStringAndSize(prog, &str, NULL))
  			return -1;
! 		v = PyRun_String(str, Py_file_input, globals, locals);
  	}
  	if (plain)
--- 3471,3481 ----
  		if (PyString_AsStringAndSize(prog, &str, NULL))
  			return -1;
! 		if (PyEval_GetNestedScopes()) {
! 			PyCompilerFlags cf;
! 			cf.cf_nested_scopes = 1;
! 			v = PyRun_StringFlags(str, Py_file_input, globals, 
! 					      locals, &cf);
! 		} else
! 			v = PyRun_String(str, Py_file_input, globals, locals);
  	}
  	if (plain)

Index: pythonrun.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/pythonrun.c,v
retrieving revision 2.126
retrieving revision 2.127
diff -C2 -r2.126 -r2.127
*** pythonrun.c	2001/03/01 22:59:14	2.126
--- pythonrun.c	2001/03/22 02:47:58	2.127
***************
*** 41,45 ****
  static PyObject *run_node(node *, char *, PyObject *, PyObject *,
  			  PyCompilerFlags *);
! static PyObject *run_pyc_file(FILE *, char *, PyObject *, PyObject *);
  static void err_input(perrdetail *);
  static void initsigs(void);
--- 41,46 ----
  static PyObject *run_node(node *, char *, PyObject *, PyObject *,
  			  PyCompilerFlags *);
! static PyObject *run_pyc_file(FILE *, char *, PyObject *, PyObject *,
! 			      PyCompilerFlags *);
  static void err_input(perrdetail *);
  static void initsigs(void);
***************
*** 448,461 ****
  PyRun_AnyFile(FILE *fp, char *filename)
  {
! 	return PyRun_AnyFileEx(fp, filename, 0);
  }
  
  int
  PyRun_AnyFileEx(FILE *fp, char *filename, int closeit)
  {
  	if (filename == NULL)
  		filename = "???";
  	if (Py_FdIsInteractive(fp, filename)) {
! 		int err = PyRun_InteractiveLoop(fp, filename);
  		if (closeit)
  			fclose(fp);
--- 449,475 ----
  PyRun_AnyFile(FILE *fp, char *filename)
  {
! 	return PyRun_AnyFileExFlags(fp, filename, 0, NULL);
  }
  
  int
+ PyRun_AnyFileFlags(FILE *fp, char *filename, PyCompilerFlags *flags)
+ {
+ 	return PyRun_AnyFileExFlags(fp, filename, 0, flags);
+ }
+ 
+ int
  PyRun_AnyFileEx(FILE *fp, char *filename, int closeit)
  {
+ 	return PyRun_AnyFileExFlags(fp, filename, closeit, NULL);
+ }
+ 
+ int
+ PyRun_AnyFileExFlags(FILE *fp, char *filename, int closeit, 
+ 		     PyCompilerFlags *flags)
+ {
  	if (filename == NULL)
  		filename = "???";
  	if (Py_FdIsInteractive(fp, filename)) {
! 		int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
  		if (closeit)
  			fclose(fp);
***************
*** 463,467 ****
  	}
  	else
! 		return PyRun_SimpleFileEx(fp, filename, closeit);
  }
  
--- 477,481 ----
  	}
  	else
! 		return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
  }
  
***************
*** 469,477 ****
  PyRun_InteractiveLoop(FILE *fp, char *filename)
  {
  	PyObject *v;
  	int ret;
! 	PyCompilerFlags flags;
  
! 	flags.cf_nested_scopes = 0;
  	v = PySys_GetObject("ps1");
  	if (v == NULL) {
--- 483,500 ----
  PyRun_InteractiveLoop(FILE *fp, char *filename)
  {
+ 	return PyRun_InteractiveLoopFlags(fp, filename, NULL);
+ }
+ 
+ int
+ PyRun_InteractiveLoopFlags(FILE *fp, char *filename, PyCompilerFlags *flags)
+ {
  	PyObject *v;
  	int ret;
! 	PyCompilerFlags local_flags;
  
! 	if (flags == NULL) {
! 		flags = &local_flags;
! 		local_flags.cf_nested_scopes = 0;
! 	}
  	v = PySys_GetObject("ps1");
  	if (v == NULL) {
***************
*** 485,489 ****
  	}
  	for (;;) {
! 		ret = PyRun_InteractiveOneFlags(fp, filename, &flags);
  #ifdef Py_REF_DEBUG
  		fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
--- 508,512 ----
  	}
  	for (;;) {
! 		ret = PyRun_InteractiveOneFlags(fp, filename, flags);
  #ifdef Py_REF_DEBUG
  		fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
***************
*** 612,615 ****
--- 635,645 ----
  PyRun_SimpleFileEx(FILE *fp, char *filename, int closeit)
  {
+ 	return PyRun_SimpleFileExFlags(fp, filename, closeit, NULL);
+ }
+ 
+ int
+ PyRun_SimpleFileExFlags(FILE *fp, char *filename, int closeit,
+ 			PyCompilerFlags *flags)
+ {
  	PyObject *m, *d, *v;
  	char *ext;
***************
*** 631,637 ****
  		if (strcmp(ext, ".pyo") == 0)
  			Py_OptimizeFlag = 1;
! 		v = run_pyc_file(fp, filename, d, d);
  	} else {
! 		v = PyRun_FileEx(fp, filename, Py_file_input, d, d, closeit);
  	}
  	if (v == NULL) {
--- 661,668 ----
  		if (strcmp(ext, ".pyo") == 0)
  			Py_OptimizeFlag = 1;
! 		v = run_pyc_file(fp, filename, d, d, flags);
  	} else {
! 		v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d, 
! 				      closeit, flags);
  	}
  	if (v == NULL) {
***************
*** 927,931 ****
  PyObject *
  PyRun_FileEx(FILE *fp, char *filename, int start, PyObject *globals,
! 	   PyObject *locals, int closeit)
  {
  	node *n = PyParser_SimpleParseFile(fp, filename, start);
--- 958,962 ----
  PyObject *
  PyRun_FileEx(FILE *fp, char *filename, int start, PyObject *globals,
! 	     PyObject *locals, int closeit)
  {
  	node *n = PyParser_SimpleParseFile(fp, filename, start);
***************
*** 935,938 ****
--- 966,995 ----
  }
  
+ PyObject *
+ PyRun_StringFlags(char *str, int start, PyObject *globals, PyObject *locals,
+ 		  PyCompilerFlags *flags)
+ {
+ 	return run_err_node(PyParser_SimpleParseString(str, start),
+ 			    "<string>", globals, locals, flags);
+ }
+ 
+ PyObject *
+ PyRun_FileFlags(FILE *fp, char *filename, int start, PyObject *globals,
+ 		PyObject *locals, PyCompilerFlags *flags)
+ {
+ 	return PyRun_FileExFlags(fp, filename, start, globals, locals, 0,
+ 				 flags); 
+ }
+ 
+ PyObject *
+ PyRun_FileExFlags(FILE *fp, char *filename, int start, PyObject *globals,
+ 		  PyObject *locals, int closeit, PyCompilerFlags *flags)
+ {
+ 	node *n = PyParser_SimpleParseFile(fp, filename, start);
+ 	if (closeit)
+ 		fclose(fp);
+ 	return run_err_node(n, filename, globals, locals, flags);
+ }
+ 
  static PyObject *
  run_err_node(node *n, char *filename, PyObject *globals, PyObject *locals,
***************
*** 950,954 ****
  	PyCodeObject *co;
  	PyObject *v;
- 	/* XXX pass sess->ss_nested_scopes to PyNode_Compile */
  	co = PyNode_CompileFlags(n, filename, flags);
  	PyNode_Free(n);
--- 1007,1010 ----
***************
*** 961,965 ****
  
  static PyObject *
! run_pyc_file(FILE *fp, char *filename, PyObject *globals, PyObject *locals)
  {
  	PyCodeObject *co;
--- 1017,1022 ----
  
  static PyObject *
! run_pyc_file(FILE *fp, char *filename, PyObject *globals, PyObject *locals,
! 	     PyCompilerFlags *flags)
  {
  	PyCodeObject *co;
***************
*** 985,988 ****
--- 1042,1051 ----
  	co = (PyCodeObject *)v;
  	v = PyEval_EvalCode(co, globals, locals);
+ 	if (v && flags) {
+ 		if (co->co_flags & CO_NESTED)
+ 			flags->cf_nested_scopes = 1;
+ 		fprintf(stderr, "run_pyc_file: nested_scopes: %d\n",
+ 			flags->cf_nested_scopes);			
+ 	}
  	Py_DECREF(co);
  	return v;
***************
*** 991,994 ****
--- 1054,1064 ----
  PyObject *
  Py_CompileString(char *str, char *filename, int start)
+ {
+ 	return Py_CompileStringFlags(str, filename, start, NULL);
+ }
+ 
+ PyObject *
+ Py_CompileStringFlags(char *str, char *filename, int start, 
+ 		      PyCompilerFlags *flags)
  {
  	node *n;