[Python-3000] Ripping out exec

Georg Brandl g.brandl at gmx.net
Fri Sep 1 20:34:09 CEST 2006


Hi,

in process of ripping out the exec statement, I stumbled over the
following function in symtable.c (line 468ff):

------------------------------------------------------------------------------------
/* Check for illegal statements in unoptimized namespaces */
static int
check_unoptimized(const PySTEntryObject* ste) {
	char buf[300];
	const char* trailer;

	if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
	    || !(ste->ste_free || ste->ste_child_free))
		return 1;

	trailer = (ste->ste_child_free ?
		       "contains a nested function with free variables" :
			       "is a nested function");

	switch (ste->ste_unoptimized) {
	case OPT_TOPLEVEL: /* exec / import * at top-level is fine */
	case OPT_EXEC: /* qualified exec is fine */
		return 1;
	case OPT_IMPORT_STAR:
		PyOS_snprintf(buf, sizeof(buf),
			      "import * is not allowed in function '%.100s' "
			      "because it is %s",
			      PyString_AS_STRING(ste->ste_name), trailer);
		break;
	case OPT_BARE_EXEC:
		PyOS_snprintf(buf, sizeof(buf),
			      "unqualified exec is not allowed in function "
			      "'%.100s' it %s",
			      PyString_AS_STRING(ste->ste_name), trailer);
		break;
	default:
		PyOS_snprintf(buf, sizeof(buf),
			      "function '%.100s' uses import * and bare exec, "
			      "which are illegal because it %s",
			      PyString_AS_STRING(ste->ste_name), trailer);
		break;
	}

	PyErr_SetString(PyExc_SyntaxError, buf);
	PyErr_SyntaxLocation(ste->ste_table->st_filename,
			     ste->ste_opt_lineno);
	return 0;
}
--------------------------------------------------------------------------------------

Of course, this check can't be made at compile time if exec() is a function.
(You can even outsmart it currently by giving explicit None arguments to the
exec statement)

So my question is: is this check required, and can it be done at execution time
instead?

Comparing the exec code to execfile(), only this can be the cause for the
extra precaution:
(from Python/ceval.c, function exec_statement)

	if (plain)
		PyFrame_LocalsToFast(f, 0);

Georg



More information about the Python-3000 mailing list