diff Python/bltinmodule.c @ 1263:65a1e4e9cd54 legacy-trunk

* Fixed some subtleties with fastlocals. You can no longer access f_fastlocals in a traceback object (this is a core dump hazard if there are <nil> entries), but instead eval_code() merges the fast locals back into the locals dictionary if it looks like the local variables will be retained. Also, the merge routines save exceptions since this is sometimes needed (alas!). * Added id() to bltinmodule.c, which returns an object's address (identity). Useful to walk arbitrary data structures containing cycles. * Added compile() to bltinmodule.c and compile_string() to pythonrun.[ch]: support to exec/eval arbitrary code objects. The code that defaults globals and locals is moved from run_node in pythonrun.c (which is now identical to eval_node) to eval_code in ceval.c. [XXX For elegance a clean-up session is necessary.]
author Guido van Rossum <guido@python.org>
date Tue, 30 Mar 1993 17:46:03 +0000
parents 1cabc2b6c970
children 9fed5726a36d
line wrap: on
line diff
--- a/Python/bltinmodule.c	Tue Mar 30 13:18:41 1993 +0000
+++ b/Python/bltinmodule.c	Tue Mar 30 17:46:03 1993 +0000
@@ -35,6 +35,8 @@
 #include "pythonrun.h"
 #include "ceval.h"
 #include "modsupport.h"
+#include "compile.h"
+#include "eval.h"
 
 static object *
 builtin_abs(self, v)
@@ -107,6 +109,29 @@
 }
 
 static object *
+builtin_compile(self, args)
+	object *self;
+	object *args;
+{
+	char *str;
+	char *filename;
+	char *startstr;
+	int start;
+	if (!getargs(args, "(sss)", &str, &filename, &startstr))
+		return NULL;
+	if (strcmp(startstr, "exec") == 0)
+		start = file_input;
+	else if (strcmp(startstr, "eval") == 0)
+		start = eval_input;
+	else {
+		err_setstr(ValueError,
+			   "compile() mode must be 'exec' or 'eval'");
+		return NULL;
+	}
+	return compile_string(str, filename, start);
+}
+
+static object *
 builtin_dir(self, v)
 	object *self;
 	object *v;
@@ -168,23 +193,26 @@
 	char *s;
 	int n;
 	if (v != NULL) {
-		if (is_stringobject(v))
-			str = v;
-		else if (is_tupleobject(v) &&
+		if (is_tupleobject(v) &&
 				((n = gettuplesize(v)) == 2 || n == 3)) {
 			str = gettupleitem(v, 0);
 			globals = gettupleitem(v, 1);
 			if (n == 3)
 				locals = gettupleitem(v, 2);
 		}
+		else
+			str = v;
 	}
-	if (str == NULL || !is_stringobject(str) ||
+	if (str == NULL || (!is_stringobject(str) && !is_codeobject(str)) ||
 			globals != NULL && !is_dictobject(globals) ||
 			locals != NULL && !is_dictobject(locals)) {
 		err_setstr(TypeError,
-		    "exec/eval arguments must be string[,dict[,dict]]");
+		    "exec/eval arguments must be (string|code)[,dict[,dict]]");
 		return NULL;
 	}
+	if (is_codeobject(str))
+		return eval_code((codeobject *) str, globals, locals,
+				 (object *)NULL);
 	s = getstringvalue(str);
 	if (strlen(s) != getstringsize(str)) {
 		err_setstr(ValueError, "embedded '\\0' in string arg");
@@ -306,6 +334,17 @@
 }
 
 static object *
+builtin_id(self, args)
+	object *self;
+	object *args;
+{
+	object *v;
+	if (!getargs(args, "O", &v))
+		return NULL;
+	return newintobject((long)v);
+}
+
+static object *
 builtin_setattr(self, args)
 	object *self;
 	object *args;
@@ -713,6 +752,7 @@
 	{"chr",		builtin_chr},
 	{"cmp",		builtin_cmp},
 	{"coerce",	builtin_coerce},
+	{"compile",	builtin_compile},
 	{"dir",		builtin_dir},
 	{"divmod",	builtin_divmod},
 	{"eval",	builtin_eval},
@@ -723,6 +763,7 @@
 	{"hasattr",	builtin_hasattr},
 	{"hash",	builtin_hash},
 	{"hex",		builtin_hex},
+	{"id",		builtin_id},
 	{"input",	builtin_input},
 	{"int",		builtin_int},
 	{"len",		builtin_len},