[Python-checkins] CVS: python/dist/src/Python compile.c,2.130,2.131

Barry Warsaw python-dev@python.org
Mon, 21 Aug 2000 10:07:23 -0700


Update of /cvsroot/python/python/dist/src/Python
In directory slayer.i.sourceforge.net:/tmp/cvs-serv15747

Modified Files:
	compile.c 
Log Message:
com_print_stmt(): Guido rightly points out that the stream expression
in extended prints should only be evaluated once.  This patch plays
stack games (documented!) to fix this.


Index: compile.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/compile.c,v
retrieving revision 2.130
retrieving revision 2.131
diff -C2 -r2.130 -r2.131
*** compile.c	2000/08/21 15:38:56	2.130
--- compile.c	2000/08/21 17:07:20	2.131
***************
*** 2056,2059 ****
--- 2056,2062 ----
  	if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
  		stream = CHILD(n, 2);
+ 		com_node(c, stream);
+ 		/* stack: [...] => [... stream] */
+ 		com_push(c, 1);
  		if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA)
  			i = 4;
***************
*** 2063,2075 ****
  	for (; i < NCH(n); i += 2) {
  		if (stream != NULL) {
! 			/* stack: [...] => [... obj stream] */
  			com_node(c, CHILD(n, i));
! 			com_node(c, stream);
  			com_addbyte(c, PRINT_ITEM_TO);
  			com_pop(c, 2);
  		}
  		else {
- 			/* stack: [...] => [... obj] */
  			com_node(c, CHILD(n, i));
  			com_addbyte(c, PRINT_ITEM);
  			com_pop(c, 1);
--- 2066,2083 ----
  	for (; i < NCH(n); i += 2) {
  		if (stream != NULL) {
! 			com_addbyte(c, DUP_TOP);
! 			/* stack: [stream] => [stream stream] */
! 			com_push(c, 1);
  			com_node(c, CHILD(n, i));
! 			/* stack: [stream stream] => [stream stream obj] */
! 			com_addbyte(c, ROT_TWO);
! 			/* stack: [stream stream obj] => [stream obj stream] */
  			com_addbyte(c, PRINT_ITEM_TO);
+ 			/* stack: [stream obj stream] => [stream] */
  			com_pop(c, 2);
  		}
  		else {
  			com_node(c, CHILD(n, i));
+ 			/* stack: [...] => [... obj] */
  			com_addbyte(c, PRINT_ITEM);
  			com_pop(c, 1);
***************
*** 2077,2084 ****
  	}
  	/* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
! 	if (TYPE(CHILD(n, NCH(n)-1)) != COMMA) {
  		if (stream != NULL) {
! 			com_node(c, stream);
  			com_addbyte(c, PRINT_NEWLINE_TO);
  			com_pop(c, 1);
  		}
--- 2085,2101 ----
  	}
  	/* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
! 	if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) {
! 		if (stream != NULL) {
! 			/* must pop the extra stream object off the stack */
! 			com_addbyte(c, POP_TOP);
! 			/* stack: [... stream] => [...] */
! 			com_pop(c, 1);
! 		}
! 	}
! 	else {
  		if (stream != NULL) {
! 			/* this consumes the last stream object on stack */
  			com_addbyte(c, PRINT_NEWLINE_TO);
+ 			/* stack: [... stream] => [...] */
  			com_pop(c, 1);
  		}