diff Python/ceval.c @ 21398:de125792bfaa legacy-trunk

Fix for SF bug [ #492403 ] exec() segfaults on closure's func_code Based on the patch from Danny Yoo. The fix is in exec_statement() in ceval.c. There are also changes to introduce use of PyCode_GetNumFree() in several places.
author Jeremy Hylton <jeremy@alum.mit.edu>
date Thu, 13 Dec 2001 19:51:56 +0000
parents 97709fd8fda0
children 30efe46f8e3c
line wrap: on
line diff
--- a/Python/ceval.c	Thu Dec 13 19:47:51 2001 +0000
+++ b/Python/ceval.c	Thu Dec 13 19:51:56 2001 +0000
@@ -2094,7 +2094,7 @@
 			int nfree;
 			v = POP(); /* code object */
 			x = PyFunction_New(v, f->f_globals);
-			nfree = PyTuple_GET_SIZE(((PyCodeObject *)v)->co_freevars);
+			nfree = PyCode_GetNumFree((PyCodeObject *)v);
 			Py_DECREF(v);
 			/* XXX Maybe this should be a separate opcode? */
 			if (x != NULL && nfree > 0) {
@@ -3631,6 +3631,11 @@
 	if (PyDict_GetItemString(globals, "__builtins__") == NULL)
 		PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
 	if (PyCode_Check(prog)) {
+		if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
+			PyErr_SetString(PyExc_TypeError,
+		"code object passed to exec may not contain free variables");
+			return -1;
+		}
 		v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
 	}
 	else if (PyFile_Check(prog)) {