[Python-checkins] python/dist/src/Python compile.c,2.285,2.286 symtable.c,2.10,2.11

jhylton@users.sourceforge.net jhylton@users.sourceforge.net
Wed, 21 May 2003 10:34:52 -0700


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

Modified Files:
	compile.c symtable.c 
Log Message:
Fix for SF [ 734869 ] Lambda functions in list comprehensions

The compiler was reseting the list comprehension tmpname counter for each function, but the symtable was using the same counter for the entire module.  Repair by move tmpname into the symtable entry.

Bugfix candidate.


Index: compile.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/compile.c,v
retrieving revision 2.285
retrieving revision 2.286
diff -C2 -d -r2.285 -r2.286
*** compile.c	12 May 2003 19:16:52 -0000	2.285
--- compile.c	21 May 2003 17:34:50 -0000	2.286
***************
*** 732,735 ****
--- 732,736 ----
  static void symtable_assign(struct symtable *, node *, int);
  static void symtable_list_comprehension(struct symtable *, node *);
+ static void symtable_list_for(struct symtable *, node *);
  
  static int symtable_update_free_vars(struct symtable *);
***************
*** 1603,1606 ****
--- 1604,1609 ----
  	/* listmaker: test list_for */
  	char tmpname[30];
+ 
+ 	REQ(n, listmaker);
  	PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->c_tmpname);
  	com_addoparg(c, BUILD_LIST, 0);
***************
*** 4922,4926 ****
  	st->st_nscopes = 0;
  	st->st_errors = 0;
- 	st->st_tmpname = 0;
  	st->st_private = NULL;
  	return st;
--- 4925,4928 ----
***************
*** 5124,5130 ****
  		prev = st->st_cur;
  		if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
- 			/* Py_DECREF(st->st_cur); */
- 			/* I believe the previous line would lead to a
- 			   double-DECREF when st is disposed - JRH */
  			st->st_errors++;
  			return;
--- 5126,5129 ----
***************
*** 5396,5405 ****
  		goto loop;
  	case list_iter:
  		n = CHILD(n, 0);
! 		if (TYPE(n) == list_for) {
! 			st->st_tmpname++;
! 			symtable_list_comprehension(st, n);
! 			st->st_tmpname--;
! 		} else {
  			REQ(n, list_if);
  			symtable_node(st, CHILD(n, 1));
--- 5395,5404 ----
  		goto loop;
  	case list_iter:
+ 		/* only occurs when there are multiple for loops
+ 		   in a list comprehension */
  		n = CHILD(n, 0);
! 		if (TYPE(n) == list_for)
! 			symtable_list_for(st, n);
! 		else {
  			REQ(n, list_if);
  			symtable_node(st, CHILD(n, 1));
***************
*** 5429,5436 ****
  	case listmaker:
  		if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
! 			st->st_tmpname++;
! 			symtable_list_comprehension(st, CHILD(n, 1));
! 			symtable_node(st, CHILD(n, 0));
! 			st->st_tmpname--;
  			break;
  		}
--- 5428,5432 ----
  	case listmaker:
  		if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
! 			symtable_list_comprehension(st, n);
  			break;
  		}
***************
*** 5630,5637 ****
  symtable_list_comprehension(struct symtable *st, node *n)
  {
  	char tmpname[30];
  
! 	PyOS_snprintf(tmpname, sizeof(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));
--- 5626,5646 ----
  symtable_list_comprehension(struct symtable *st, node *n)
  {
+ 	/* listmaker: test list_for */
  	char tmpname[30];
  
! 	REQ(n, listmaker);
! 	PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", 
! 		      ++st->st_cur->ste_tmpname);
  	symtable_add_def(st, tmpname, DEF_LOCAL);
+ 	symtable_list_for(st, CHILD(n, 1));
+ 	symtable_node(st, CHILD(n, 0));
+ 	--st->st_cur->ste_tmpname;
+ }
+ 
+ static void
+ symtable_list_for(struct symtable *st, node *n)
+ {
+ 	REQ(n, list_for);
+ 	/* list_for: for v in expr [list_iter] */
  	symtable_assign(st, CHILD(n, 1), 0);
  	symtable_node(st, CHILD(n, 3));

Index: symtable.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/symtable.c,v
retrieving revision 2.10
retrieving revision 2.11
diff -C2 -d -r2.10 -r2.11
*** symtable.c	10 Dec 2001 00:53:18 -0000	2.10
--- symtable.c	21 May 2003 17:34:50 -0000	2.11
***************
*** 62,65 ****
--- 62,66 ----
  	ste->ste_optimized = 0;
  	ste->ste_opt_lineno = 0;
+ 	ste->ste_tmpname = 0;
  	ste->ste_lineno = lineno;
  	switch (type) {