[Python-checkins] CVS: python/dist/src/Python compile.c,2.225,2.226

Jeremy Hylton jhylton@users.sourceforge.net
2001年10月17日 06:22:25 -0700


Update of /cvsroot/python/python/dist/src/Python
In directory usw-pr-cvs1:/tmp/cvs-serv13722/Python
Modified Files:
	compile.c 
Log Message:
Fix computation of stack depth for classdef and closures.
Also minor tweaks to internal routines.
Use PyCF_MASK instead of explicit list of flags.
For the MAKE_CLOSURE opcode, the number of items popped off the stack
depends on both the oparg and the number of free variables for the
code object. Fix the code so it accounts for the free variables.
In com_classdef(), record an extra pop to account for the STORE call
after the BUILD_CLASS.
Get rid of some commented out debugging code in com_push() and
com_pop().
Factor string resize logic into helper routine com_check_size().
In com_addbyte(), remove redudant if statement after assert. (They
test the same condition.)
In several routines, use string macros instead of string functions.
Index: compile.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/compile.c,v
retrieving revision 2.225
retrieving revision 2.226
diff -C2 -d -r2.225 -r2.226
*** compile.c	2001年10月15日 15:44:05	2.225
--- compile.c	2001年10月17日 13:22:22	2.226
***************
*** 669,674 ****
 {
 	c->c_stacklevel += n;
! 	if (c->c_stacklevel > c->c_maxstacklevel)
 		c->c_maxstacklevel = c->c_stacklevel;
 }
 
--- 669,680 ----
 {
 	c->c_stacklevel += n;
! 	if (c->c_stacklevel > c->c_maxstacklevel) {
 		c->c_maxstacklevel = c->c_stacklevel;
+ 		/*
+ 		fprintf(stderr, "%s:%s:%d max stack nexti=%d level=%d n=%d\n",
+ 			c->c_filename, c->c_name, c->c_lineno,
+ 			c->c_nexti, c->c_stacklevel, n);
+ 		*/
+ 	}
 }
 
***************
*** 676,686 ****
 com_pop(struct compiling *c, int n)
 {
! 	if (c->c_stacklevel < n) {
! 		/* fprintf(stderr,
! 			"%s:%d: underflow! nexti=%d, level=%d, n=%d\n",
! 			c->c_filename, c->c_lineno,
! 			c->c_nexti, c->c_stacklevel, n); */
 		c->c_stacklevel = 0;
- 	}
 	else
 		c->c_stacklevel -= n;
--- 682,687 ----
 com_pop(struct compiling *c, int n)
 {
! 	if (c->c_stacklevel < n) 
 		c->c_stacklevel = 0;
 	else
 		c->c_stacklevel -= n;
***************
*** 696,719 ****
 }
 
 static void
 com_addbyte(struct compiling *c, int byte)
 {
- 	int len;
 	/*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
 	assert(byte >= 0 && byte <= 255);
! 	if (byte < 0 || byte > 255) {
! 		com_error(c, PyExc_SystemError,
! 			 "com_addbyte: byte out of range");
! 	}
! 	if (c->c_code == NULL)
 		return;
- 	len = PyString_Size(c->c_code);
- 	if (c->c_nexti >= len) {
- 		if (_PyString_Resize(&c->c_code, len+1000) != 0) {
- 			c->c_errors++;
- 			return;
- 		}
 	}
! 	PyString_AsString(c->c_code)[c->c_nexti++] = byte;
 }
 
--- 697,720 ----
 }
 
+ static int
+ com_check_size(PyObject **s, int offset)
+ {
+ 	int len = PyString_GET_SIZE(*s);
+ 	if (offset >= len) 
+ 		return _PyString_Resize(s, len * 2);
+ 	return 0;
+ }
+ 
 static void
 com_addbyte(struct compiling *c, int byte)
 {
 	/*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
 	assert(byte >= 0 && byte <= 255);
! 	assert(c->c_code);
! 	if (com_check_size(&c->c_code, c->c_nexti)) {
! 		c->c_errors++;
 		return;
 	}
! 	PyString_AS_STRING(c->c_code)[c->c_nexti++] = byte;
 }
 
***************
*** 728,743 ****
 com_add_lnotab(struct compiling *c, int addr, int line)
 {
- 	int size;
 	char *p;
 	if (c->c_lnotab == NULL)
 		return;
! 	size = PyString_Size(c->c_lnotab);
! 	if (c->c_lnotab_next+2 > size) {
! 		if (_PyString_Resize(&c->c_lnotab, size + 1000) < 0) {
! 			c->c_errors++;
! 			return;
! 		}
 	}
! 	p = PyString_AsString(c->c_lnotab) + c->c_lnotab_next;
 	*p++ = addr;
 	*p++ = line;
--- 729,740 ----
 com_add_lnotab(struct compiling *c, int addr, int line)
 {
 	char *p;
 	if (c->c_lnotab == NULL)
 		return;
! 	if (com_check_size(&c->c_lnotab, c->c_lnotab_next + 2)) {
! 		c->c_errors++;
! 		return;
 	}
! 	p = PyString_AS_STRING(c->c_lnotab) + c->c_lnotab_next;
 	*p++ = addr;
 	*p++ = line;
***************
*** 805,809 ****
 com_backpatch(struct compiling *c, int anchor)
 {
! 	unsigned char *code = (unsigned char *) PyString_AsString(c->c_code);
 	int target = c->c_nexti;
 	int dist;
--- 802,806 ----
 com_backpatch(struct compiling *c, int anchor)
 {
! 	unsigned char *code = (unsigned char *) PyString_AS_STRING(c->c_code);
 	int target = c->c_nexti;
 	int dist;
***************
*** 2327,2336 ****
 	REQ(n, test); /* and_test ('or' and_test)* | lambdef */
 	if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
! 		PyObject *co;
 		int i, closure;
 		int ndefs = com_argdefs(c, CHILD(n, 0));
 		symtable_enter_scope(c->c_symtable, "lambda", lambdef,
 				 n->n_lineno);
! 		co = (PyObject *) icompile(CHILD(n, 0), c);
 		if (co == NULL) {
 			c->c_errors++;
--- 2324,2333 ----
 	REQ(n, test); /* and_test ('or' and_test)* | lambdef */
 	if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
! 		PyCodeObject *co;
 		int i, closure;
 		int ndefs = com_argdefs(c, CHILD(n, 0));
 		symtable_enter_scope(c->c_symtable, "lambda", lambdef,
 				 n->n_lineno);
! 		co = icompile(CHILD(n, 0), c);
 		if (co == NULL) {
 			c->c_errors++;
***************
*** 2338,2350 ****
 		}
 		symtable_exit_scope(c->c_symtable);
! 		i = com_addconst(c, co);
! 		closure = com_make_closure(c, (PyCodeObject *)co);
! 		Py_DECREF(co);
 		com_addoparg(c, LOAD_CONST, i);
 		com_push(c, 1);
! 		if (closure)
 			com_addoparg(c, MAKE_CLOSURE, ndefs);
! 		else
 			com_addoparg(c, MAKE_FUNCTION, ndefs);
 		com_pop(c, ndefs);
 	}
--- 2335,2348 ----
 		}
 		symtable_exit_scope(c->c_symtable);
! 		i = com_addconst(c, (PyObject *)co);
! 		closure = com_make_closure(c, co);
 		com_addoparg(c, LOAD_CONST, i);
 		com_push(c, 1);
! 		if (closure) {
 			com_addoparg(c, MAKE_CLOSURE, ndefs);
! 			com_pop(c, PyTuple_GET_SIZE(co->co_freevars));
! 		} else
 			com_addoparg(c, MAKE_FUNCTION, ndefs);
+ 		Py_DECREF(co);
 		com_pop(c, ndefs);
 	}
***************
*** 3565,3569 ****
 {
 	int i;
! 	PyObject *co, *v;
 	char *name;
 
--- 3563,3568 ----
 {
 	int i;
! 	PyObject *v;
! 	PyCodeObject *co;
 	char *name;
 
***************
*** 3588,3603 ****
 	name = STR(CHILD(n, 1));
 	symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno);
! 	co = (PyObject *)icompile(n, c);
 	symtable_exit_scope(c->c_symtable);
 	if (co == NULL)
 		c->c_errors++;
 	else {
! 		int closure = com_make_closure(c, (PyCodeObject *)co);
! 		i = com_addconst(c, co);
 		com_addoparg(c, LOAD_CONST, i);
 		com_push(c, 1);
! 		if (closure)
 			com_addoparg(c, MAKE_CLOSURE, 0);
! 		else
 			com_addoparg(c, MAKE_FUNCTION, 0);
 		com_addoparg(c, CALL_FUNCTION, 0);
--- 3587,3603 ----
 	name = STR(CHILD(n, 1));
 	symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno);
! 	co = icompile(n, c);
 	symtable_exit_scope(c->c_symtable);
 	if (co == NULL)
 		c->c_errors++;
 	else {
! 		int closure = com_make_closure(c, co);
! 		i = com_addconst(c, (PyObject *)co);
 		com_addoparg(c, LOAD_CONST, i);
 		com_push(c, 1);
! 		if (closure) {
 			com_addoparg(c, MAKE_CLOSURE, 0);
! 			com_pop(c, PyTuple_GET_SIZE(co->co_freevars));
! 		} else
 			com_addoparg(c, MAKE_FUNCTION, 0);
 		com_addoparg(c, CALL_FUNCTION, 0);
***************
*** 3605,3608 ****
--- 3605,3609 ----
 		com_pop(c, 2);
 		com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
+ 		com_pop(c, 1);
 		Py_DECREF(co);
 	}
***************
*** 4083,4088 ****
 		 || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION))
 			sc.c_nested = 1;
! 		sc.c_flags |= base->c_flags & (CO_GENERATOR_ALLOWED |
! 					 CO_FUTURE_DIVISION);
 	} else {
 		sc.c_private = NULL;
--- 4084,4088 ----
 		 || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION))
 			sc.c_nested = 1;
! 		sc.c_flags |= base->c_flags & PyCF_MASK;
 	} else {
 		sc.c_private = NULL;

AltStyle によって変換されたページ (->オリジナル) /