[Python-checkins] CVS: python/dist/src/Python compile.c,2.153,2.154

Jeremy Hylton jhylton@users.sourceforge.net
Mon, 29 Jan 2001 17:24:45 -0800


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

Modified Files:
	compile.c 
Log Message:
Enforce two illegal import statements that were outlawed in the
reference manual but not checked: Names bound by import statemants may
not occur in global statements in the same scope. The from ... import *
form may only occur in a module scope.

I guess these changes could break code, but the reference manual
warned about them.

Several other small changes

If a variable is declared global in the nearest enclosing scope of a
free variable, then treat it is a global in the nested scope too.

Get rid of com_mangle and symtable_mangle functions and call mangle
directly. 

If errors occur during symtable table creation, return -1 from
symtable_build().

Do not increment st_errors in assignment to lambda, because exception
is not set.

Add extra argument to symtable_assign(); the argument, flag, is ORed
with DEF_LOCAL for each symtable_add_def() call.


Index: compile.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/compile.c,v
retrieving revision 2.153
retrieving revision 2.154
diff -C2 -r2.153 -r2.154
*** compile.c	2001/01/29 22:42:28	2.153
--- compile.c	2001/01/30 01:24:43	2.154
***************
*** 59,64 ****
  
  #define DEL_CLOSURE_ERROR \
! "can not delete variable '%s' referenced in nested scope"
  
  #define MANGLE_LEN 256
  
--- 59,73 ----
  
  #define DEL_CLOSURE_ERROR \
! "can not delete variable '%.400s' referenced in nested scope"
  
+ #define DUPLICATE_ARGUMENT \
+ "duplicate argument '%s' in function definition"
+ 
+ #define ILLEGAL_IMPORT_STAR \
+ "'from ... import *' may only occur in a module scope"
+ 
+ #define ILLEGAL_IMPORT_GLOBAL \
+ "may not import name '%.400s' because it is declared global"
+ 
  #define MANGLE_LEN 256
  
***************
*** 430,433 ****
--- 439,443 ----
  #define DEF_FREE_GLOBAL 2<<7   /* free variable is actually implicit global */
  #define DEF_FREE_CLASS 2<<8    /* free variable from class's method */
+ #define DEF_IMPORT 2<<9        /* assignment occurred via import */
  
  int is_free(int v)
***************
*** 564,568 ****
  static void symtable_global(struct symtable *, node *);
  static void symtable_import(struct symtable *, node *);
! static void symtable_assign(struct symtable *, node *);
  static void symtable_list_comprehension(struct symtable *, node *);
  
--- 574,578 ----
  static void symtable_global(struct symtable *, node *);
  static void symtable_import(struct symtable *, node *);
! static void symtable_assign(struct symtable *, node *, int);
  static void symtable_list_comprehension(struct symtable *, node *);
  
***************
*** 908,917 ****
  }
  
- static int
- com_mangle(struct compiling *c, char *name, char *buffer, size_t maxlen)
- {
- 	return mangle(c->c_private, name, buffer, maxlen);
- }
- 
  static void
  com_addop_name(struct compiling *c, int op, char *name)
--- 918,921 ----
***************
*** 920,927 ****
  	int i;
  	char buffer[MANGLE_LEN];
! /*	fprintf(stderr, "com_addop_name(%s, %d, %s)\n",
! 		c->c_name, op, name);
! */
! 	if (com_mangle(c, name, buffer, sizeof(buffer)))
  		name = buffer;
  	if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
--- 924,929 ----
  	int i;
  	char buffer[MANGLE_LEN];
! 
! 	if (mangle(c->c_private, name, buffer, sizeof(buffer)))
  		name = buffer;
  	if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
***************
*** 960,964 ****
  	char buffer[MANGLE_LEN];
  
! 	if (com_mangle(c, name, buffer, sizeof(buffer)))
  		name = buffer;
  	if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
--- 962,966 ----
  	char buffer[MANGLE_LEN];
  
! 	if (mangle(c->c_private, name, buffer, sizeof(buffer)))
  		name = buffer;
  	if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
***************
*** 1046,1050 ****
  			break;
  		case NAME_CLOSURE: {
! 			char buf[256];
  			sprintf(buf, DEL_CLOSURE_ERROR, name);
  			com_error(c, PyExc_SyntaxError, buf);
--- 1048,1052 ----
  			break;
  		case NAME_CLOSURE: {
! 			char buf[500];
  			sprintf(buf, DEL_CLOSURE_ERROR, name);
  			com_error(c, PyExc_SyntaxError, buf);
***************
*** 4008,4011 ****
--- 4010,4015 ----
  		return -1;
  	symtable_node(c->c_symtable, n);
+ 	if (c->c_symtable->st_errors > 0)
+ 		return -1;
  	/* reset for second pass */
  	c->c_symtable->st_scopes = 1;
***************
*** 4114,4117 ****
--- 4118,4128 ----
  				goto fail;
  			}
+ 			if (info & DEF_IMPORT) {
+ 				char buf[500];
+ 				sprintf(buf, ILLEGAL_IMPORT_GLOBAL,
+ 					PyString_AS_STRING(name));
+ 				com_error(c, PyExc_SyntaxError, buf);
+ 				goto fail;
+ 			}
  			if (PyDict_SetItem(c->c_globals, name, Py_None) < 0)
  				goto fail;
***************
*** 4335,4339 ****
  		return symtable_undo_free(st, child, name);
  	v = PyInt_AS_LONG(o);
! 	if (is_free(v)) 
  		return symtable_undo_free(st, child, name);
  	else
--- 4346,4350 ----
  		return symtable_undo_free(st, child, name);
  	v = PyInt_AS_LONG(o);
! 	if (is_free(v) || (v & DEF_GLOBAL)) 
  		return symtable_undo_free(st, child, name);
  	else
***************
*** 4494,4503 ****
  
  static int
- symtable_mangle(struct symtable *st, char *name, char *buffer, size_t maxlen)
- {
- 	return mangle(st->st_private, name, buffer, maxlen);
- }
- 
- static int
  symtable_add_def(struct symtable *st, char *name, int flag)
  {
--- 4505,4508 ----
***************
*** 4505,4509 ****
  	char buffer[MANGLE_LEN];
  
! 	if (symtable_mangle(st, name, buffer, sizeof(buffer)))
  		name = buffer;
  	if ((s = PyString_InternFromString(name)) == NULL)
--- 4510,4514 ----
  	char buffer[MANGLE_LEN];
  
! 	if (mangle(st->st_private, name, buffer, sizeof(buffer)))
  		name = buffer;
  	if ((s = PyString_InternFromString(name)) == NULL)
***************
*** 4521,4530 ****
  	int val;
  
- /*	fprintf(stderr, "def(%s, %d)\n", REPR(name), flag);  */
  	if ((o = PyDict_GetItem(dict, name))) {
  	    val = PyInt_AS_LONG(o);
  	    if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
! 		    PyErr_Format(PyExc_SyntaxError,
! 			 "duplicate argument '%s' in function definition",
  				 name);
  		    return -1;
--- 4526,4533 ----
  	int val;
  
  	if ((o = PyDict_GetItem(dict, name))) {
  	    val = PyInt_AS_LONG(o);
  	    if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
! 		    PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
  				 name);
  		    return -1;
***************
*** 4635,4639 ****
  	case except_clause:
  		if (NCH(n) == 4)
! 			symtable_assign(st, CHILD(n, 3));
  		if (NCH(n) > 1) {
  			n = CHILD(n, 1);
--- 4638,4642 ----
  	case except_clause:
  		if (NCH(n) == 4)
! 			symtable_assign(st, CHILD(n, 3), 0);
  		if (NCH(n) > 1) {
  			n = CHILD(n, 1);
***************
*** 4642,4646 ****
  		break;
  	case del_stmt:
! 		symtable_assign(st, CHILD(n, 1));
  		break;
  	case expr_stmt:
--- 4645,4649 ----
  		break;
  	case del_stmt:
! 		symtable_assign(st, CHILD(n, 1), 0);
  		break;
  	case expr_stmt:
***************
*** 4649,4653 ****
  		else {
  			if (TYPE(CHILD(n, 1)) == augassign) {
! 				symtable_assign(st, CHILD(n, 0));
  				symtable_node(st, CHILD(n, 2));
  				break;
--- 4652,4656 ----
  		else {
  			if (TYPE(CHILD(n, 1)) == augassign) {
! 				symtable_assign(st, CHILD(n, 0), 0);
  				symtable_node(st, CHILD(n, 2));
  				break;
***************
*** 4655,4659 ****
  				int i;
  				for (i = 0; i < NCH(n) - 2; i += 2) 
! 					symtable_assign(st, CHILD(n, i));
  				n = CHILD(n, NCH(n) - 1);
  			}
--- 4658,4662 ----
  				int i;
  				for (i = 0; i < NCH(n) - 2; i += 2) 
! 					symtable_assign(st, CHILD(n, i), 0);
  				n = CHILD(n, NCH(n) - 1);
  			}
***************
*** 4679,4683 ****
  	case for_stmt:
  		if (TYPE(n) == for_stmt) {
! 			symtable_assign(st, CHILD(n, 1));
  			start = 3;
  		}
--- 4682,4686 ----
  	case for_stmt:
  		if (TYPE(n) == for_stmt) {
! 			symtable_assign(st, CHILD(n, 1), 0);
  			start = 3;
  		}
***************
*** 4835,4839 ****
  	sprintf(tmpname, "[%d]", ++st->st_tmpname);
  	symtable_add_def(st, tmpname, DEF_LOCAL);
! 	symtable_assign(st, CHILD(n, 1));
  	symtable_node(st, CHILD(n, 3));
  	if (NCH(n) == 5)
--- 4838,4842 ----
  	sprintf(tmpname, "[%d]", ++st->st_tmpname);
  	symtable_add_def(st, tmpname, DEF_LOCAL);
! 	symtable_assign(st, CHILD(n, 1), 0);
  	symtable_node(st, CHILD(n, 3));
  	if (NCH(n) == 5)
***************
*** 4855,4858 ****
--- 4858,4867 ----
  	if (STR(CHILD(n, 0))[0] == 'f') {  /* from */
  		if (TYPE(CHILD(n, 3)) == STAR) {
+ 			if (st->st_cur_type != TYPE_MODULE) {
+ 				PyErr_SetString(PyExc_SyntaxError,
+ 						ILLEGAL_IMPORT_STAR);
+ 				st->st_errors++;
+ 				return;
+ 			}
  			if (PyDict_SetItemString(st->st_cur, NOOPT,
  						 Py_None) < 0)
***************
*** 4862,4873 ****
  				node *c = CHILD(n, i);
  				if (NCH(c) > 1) /* import as */
! 					symtable_assign(st, CHILD(c, 2));
  				else
! 					symtable_assign(st, CHILD(c, 0));
  			}
  		}
  	} else {
  		for (i = 1; i < NCH(n); i += 2) {
! 			symtable_assign(st, CHILD(n, i));
  		}
  	}
--- 4871,4884 ----
  				node *c = CHILD(n, i);
  				if (NCH(c) > 1) /* import as */
! 					symtable_assign(st, CHILD(c, 2),
! 							DEF_IMPORT);
  				else
! 					symtable_assign(st, CHILD(c, 0),
! 							DEF_IMPORT);
  			}
  		}
  	} else {
  		for (i = 1; i < NCH(n); i += 2) {
! 			symtable_assign(st, CHILD(n, i), DEF_IMPORT);
  		}
  	}
***************
*** 4875,4879 ****
  
  static void 
! symtable_assign(struct symtable *st, node *n)
  {
  	node *tmp;
--- 4886,4890 ----
  
  static void 
! symtable_assign(struct symtable *st, node *n, int flag)
  {
  	node *tmp;
***************
*** 4883,4888 ****
  	switch (TYPE(n)) {
  	case lambdef:
! 		/* invalid assignment, e.g. lambda x:x=2 */
! 		st->st_errors++;
  		return;
  	case power:
--- 4894,4899 ----
  	switch (TYPE(n)) {
  	case lambdef:
! 		/* invalid assignment, e.g. lambda x:x=2.  The next
! 		   pass will catch this error. */
  		return;
  	case power:
***************
*** 4905,4909 ****
  		else {
  			for (i = 0; i < NCH(n); i += 2)
! 				symtable_assign(st, CHILD(n, i));
  		}
  		return;
--- 4916,4920 ----
  		else {
  			for (i = 0; i < NCH(n); i += 2)
! 				symtable_assign(st, CHILD(n, i), flag);
  		}
  		return;
***************
*** 4917,4921 ****
  			int i;
  			for (i = 0; i < NCH(n); i += 2)
! 				symtable_assign(st, CHILD(n, i));
  			return;
  		}
--- 4928,4932 ----
  			int i;
  			for (i = 0; i < NCH(n); i += 2)
! 				symtable_assign(st, CHILD(n, i), flag);
  			return;
  		}
***************
*** 4927,4947 ****
  			goto loop;
  		} else if (TYPE(tmp) == NAME)
! 			symtable_add_def(st, STR(tmp), DEF_LOCAL);
  		return;
  	case dotted_as_name:
  		if (NCH(n) == 3)
  			symtable_add_def(st, STR(CHILD(n, 2)),
! 					 DEF_LOCAL);
  		else
  			symtable_add_def(st,
  					 STR(CHILD(CHILD(n,
  							 0), 0)),
! 					 DEF_LOCAL);
  		return;
  	case dotted_name:
! 		symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL);
  		return;
  	case NAME:
! 		symtable_add_def(st, STR(n), DEF_LOCAL);
  		return;
  	default:
--- 4938,4958 ----
  			goto loop;
  		} else if (TYPE(tmp) == NAME)
! 			symtable_add_def(st, STR(tmp), DEF_LOCAL | flag);
  		return;
  	case dotted_as_name:
  		if (NCH(n) == 3)
  			symtable_add_def(st, STR(CHILD(n, 2)),
! 					 DEF_LOCAL | flag);
  		else
  			symtable_add_def(st,
  					 STR(CHILD(CHILD(n,
  							 0), 0)),
! 					 DEF_LOCAL | flag);
  		return;
  	case dotted_name:
! 		symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | flag);
  		return;
  	case NAME:
! 		symtable_add_def(st, STR(n), DEF_LOCAL | flag);
  		return;
  	default: