[Python-checkins] CVS: python/dist/src/Python compile.c,2.184,2.185 pythonrun.c,2.125,2.126

Jeremy Hylton jhylton@users.sourceforge.net
Thu, 01 Mar 2001 14:59:16 -0800


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

Modified Files:
	compile.c pythonrun.c 
Log Message:
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(). 



Index: compile.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/compile.c,v
retrieving revision 2.184
retrieving revision 2.185
diff -C2 -r2.184 -r2.185
*** compile.c	2001/03/01 06:09:34	2.184
--- compile.c	2001/03/01 22:59:14	2.185
***************
*** 472,476 ****
  static void com_assign_name(struct compiling *, node *, int);
  static PyCodeObject *icompile(node *, struct compiling *);
! static PyCodeObject *jcompile(node *, char *, struct compiling *);
  static PyObject *parsestrplus(node *);
  static PyObject *parsestr(char *);
--- 472,477 ----
  static void com_assign_name(struct compiling *, node *, int);
  static PyCodeObject *icompile(node *, struct compiling *);
! static PyCodeObject *jcompile(node *, char *, struct compiling *,
! 			      PyCompilerFlags *);
  static PyObject *parsestrplus(node *);
  static PyObject *parsestr(char *);
***************
*** 3817,3823 ****
  PyNode_Compile(node *n, char *filename)
  {
! 	return jcompile(n, filename, NULL);
  }
  
  struct symtable *
  PyNode_CompileSymtable(node *n, char *filename)
--- 3818,3830 ----
  PyNode_Compile(node *n, char *filename)
  {
! 	return PyNode_CompileFlags(n, filename, NULL);
  }
  
+ PyCodeObject *
+ PyNode_CompileFlags(node *n, char *filename, PyCompilerFlags *flags)
+ {
+ 	return jcompile(n, filename, NULL, flags);
+ }
+ 
  struct symtable *
  PyNode_CompileSymtable(node *n, char *filename)
***************
*** 3845,3853 ****
  icompile(node *n, struct compiling *base)
  {
! 	return jcompile(n, base->c_filename, base);
  }
  
  static PyCodeObject *
! jcompile(node *n, char *filename, struct compiling *base)
  {
  	struct compiling sc;
--- 3852,3861 ----
  icompile(node *n, struct compiling *base)
  {
! 	return jcompile(n, base->c_filename, base, NULL);
  }
  
  static PyCodeObject *
! jcompile(node *n, char *filename, struct compiling *base,
! 	 PyCompilerFlags *flags)
  {
  	struct compiling sc;
***************
*** 3865,3872 ****
  		sc.c_private = NULL;
  		sc.c_future = PyNode_Future(n, filename);
! 		if (sc.c_future == NULL || symtable_build(&sc, n) < 0) {
  			com_free(&sc);
  			return NULL;
  		}
  	}
  	co = NULL;
--- 3873,3890 ----
  		sc.c_private = NULL;
  		sc.c_future = PyNode_Future(n, filename);
! 		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;
+ 		}
  	}
  	co = NULL;
***************
*** 4953,4962 ****
  	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;
! 	}
  
  	for (i = 1; i < NCH(n); i += 2) {
--- 4971,4978 ----
  	int i;
  
! 	/* 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) {

Index: pythonrun.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/pythonrun.c,v
retrieving revision 2.125
retrieving revision 2.126
diff -C2 -r2.125 -r2.126
*** pythonrun.c	2001/02/28 20:58:04	2.125
--- pythonrun.c	2001/03/01 22:59:14	2.126
***************
*** 37,46 ****
  static void initmain(void);
  static void initsite(void);
! static PyObject *run_err_node(node *n, char *filename,
! 			      PyObject *globals, PyObject *locals);
! static PyObject *run_node(node *n, char *filename,
! 			  PyObject *globals, PyObject *locals);
! static PyObject *run_pyc_file(FILE *fp, char *filename,
! 			      PyObject *globals, PyObject *locals);
  static void err_input(perrdetail *);
  static void initsigs(void);
--- 37,45 ----
  static void initmain(void);
  static void initsite(void);
! static PyObject *run_err_node(node *, char *, PyObject *, PyObject *,
! 			      PyCompilerFlags *);
! 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);
***************
*** 57,61 ****
  extern void _PyCodecRegistry_Fini(void);
  
- 
  int Py_DebugFlag; /* Needed by parser.c */
  int Py_VerboseFlag; /* Needed by import.c */
--- 56,59 ----
***************
*** 473,476 ****
--- 471,477 ----
  	PyObject *v;
  	int ret;
+ 	PyCompilerFlags flags;
+ 
+ 	flags.cf_nested_scopes = 0;
  	v = PySys_GetObject("ps1");
  	if (v == NULL) {
***************
*** 484,488 ****
  	}
  	for (;;) {
! 		ret = PyRun_InteractiveOne(fp, filename);
  #ifdef Py_REF_DEBUG
  		fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
--- 485,489 ----
  	}
  	for (;;) {
! 		ret = PyRun_InteractiveOneFlags(fp, filename, &flags);
  #ifdef Py_REF_DEBUG
  		fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
***************
*** 500,503 ****
--- 501,510 ----
  PyRun_InteractiveOne(FILE *fp, char *filename)
  {
+ 	return PyRun_InteractiveOneFlags(fp, filename, NULL);
+ }
+ 
+ int
+ PyRun_InteractiveOneFlags(FILE *fp, char *filename, PyCompilerFlags *flags)
+ {
  	PyObject *m, *d, *v, *w;
  	node *n;
***************
*** 538,542 ****
  		return -1;
  	d = PyModule_GetDict(m);
! 	v = run_node(n, filename, d, d);
  	if (v == NULL) {
  		PyErr_Print();
--- 545,549 ----
  		return -1;
  	d = PyModule_GetDict(m);
! 	v = run_node(n, filename, d, d, flags);
  	if (v == NULL) {
  		PyErr_Print();
***************
*** 908,912 ****
  {
  	return run_err_node(PyParser_SimpleParseString(str, start),
! 			    "<string>", globals, locals);
  }
  
--- 915,919 ----
  {
  	return run_err_node(PyParser_SimpleParseString(str, start),
! 			    "<string>", globals, locals, NULL);
  }
  
***************
*** 925,945 ****
  	if (closeit)
  		fclose(fp);
! 	return run_err_node(n, filename, globals, locals);
  }
  
  static PyObject *
! run_err_node(node *n, char *filename, PyObject *globals, PyObject *locals)
  {
  	if (n == NULL)
  		return  NULL;
! 	return run_node(n, filename, globals, locals);
  }
  
  static PyObject *
! run_node(node *n, char *filename, PyObject *globals, PyObject *locals)
  {
  	PyCodeObject *co;
  	PyObject *v;
! 	co = PyNode_Compile(n, filename);
  	PyNode_Free(n);
  	if (co == NULL)
--- 932,955 ----
  	if (closeit)
  		fclose(fp);
! 	return run_err_node(n, filename, globals, locals, NULL);
  }
  
  static PyObject *
! run_err_node(node *n, char *filename, PyObject *globals, PyObject *locals,
! 	     PyCompilerFlags *flags)
  {
  	if (n == NULL)
  		return  NULL;
! 	return run_node(n, filename, globals, locals, flags);
  }
  
  static PyObject *
! run_node(node *n, char *filename, PyObject *globals, PyObject *locals,
! 	 PyCompilerFlags *flags)
  {
  	PyCodeObject *co;
  	PyObject *v;
! 	/* XXX pass sess->ss_nested_scopes to PyNode_Compile */
! 	co = PyNode_CompileFlags(n, filename, flags);
  	PyNode_Free(n);
  	if (co == NULL)