diff Python/ceval.c @ 17104:a488a63cb867 legacy-trunk

Fix PyFrame_FastToLocals() and counterpart to deal with cells and frees. Note there doesn't seem to be any way to test LocalsToFast(), because the instructions that trigger it are illegal in nested scopes with free variables. Fix allocation strategy for cells that are also formal parameters. Instead of emitting LOAD_FAST / STORE_DEREF pairs for each parameter, have the argument handling code in eval_code2() do the right thing. A side-effect of this change is that cell variables that are also arguments are listed at the front of co_cellvars in the order they appear in the argument list.
author Jeremy Hylton <jeremy@alum.mit.edu>
date Wed, 21 Mar 2001 16:43:47 +0000
parents ba81d79dc3a3
children 6fe13d008e25
line wrap: on
line diff
--- a/Python/ceval.c	Wed Mar 21 15:57:54 2001 +0000
+++ b/Python/ceval.c	Wed Mar 21 16:43:47 2001 +0000
@@ -570,11 +570,52 @@
 			goto fail;
 		}
 	}
-	/* Allocate storage for cell vars and copy free vars into frame */ 
+	/* Allocate and initialize storage for cell vars, and copy free
+	   vars into frame.  This isn't too efficient right now. */
 	if (f->f_ncells) {
-		int i;
-		for (i = 0; i < f->f_ncells; ++i)
-			freevars[i] = PyCell_New(NULL);
+		int i = 0, j = 0, nargs, found;
+		char *cellname, *argname;
+		PyObject *c;
+
+		nargs = co->co_argcount;
+		if (co->co_flags & CO_VARARGS)
+			nargs++;
+		if (co->co_flags & CO_VARKEYWORDS)
+			nargs++;
+
+		/* Check for cells that shadow args */
+		for (i = 0; i < f->f_ncells && j < nargs; ++i) {
+			cellname = PyString_AS_STRING(
+				PyTuple_GET_ITEM(co->co_cellvars, i));
+			found = 0;
+			while (j < nargs) {
+				argname = PyString_AS_STRING(
+					PyTuple_GET_ITEM(co->co_varnames, j));
+				if (strcmp(cellname, argname) == 0) {
+					c = PyCell_New(GETLOCAL(j));
+					if (c == NULL)
+						goto fail;
+					GETLOCAL(f->f_nlocals + i) = c;
+					found = 1;
+					break;
+				}
+				j++;
+			}
+			if (found == 0) {
+				c = PyCell_New(NULL);
+				if (c == NULL)
+					goto fail;
+				SETLOCAL(f->f_nlocals + i, c);
+			}
+		}
+		/* Initialize any that are left */
+		while (i < f->f_ncells) {
+			c = PyCell_New(NULL);
+			if (c == NULL)
+				goto fail;
+			SETLOCAL(f->f_nlocals + i, c);
+			i++;
+		}
 	}
 	if (f->f_nfreevars) {
 		int i;