summaryrefslogtreecommitdiffstats
path: root/Python/bltinmodule.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1993-03-30 17:46:03 (GMT)
committerGuido van Rossum <guido@python.org>1993-03-30 17:46:03 (GMT)
commit5b7221849e240c2a62ebbc48d58607417b42ef44 (patch)
tree28882cac1829c5da2258788ffdb274839f788589 /Python/bltinmodule.c
parent8b17d6bd89cd79820c76bd88bc064e44fc03a1bd (diff)
downloadcpython-5b7221849e240c2a62ebbc48d58607417b42ef44.zip
cpython-5b7221849e240c2a62ebbc48d58607417b42ef44.tar.gz
cpython-5b7221849e240c2a62ebbc48d58607417b42ef44.tar.bz2
* 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.]
Diffstat (limited to 'Python/bltinmodule.c')
-rw-r--r--Python/bltinmodule.c51
1 files changed, 46 insertions, 5 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 8fd12e4..b74b9cc 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -35,6 +35,8 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#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 @@ builtin_coerce(self, args)
}
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 @@ exec_eval(v, start)
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 @@ builtin_hasattr(self, args)
}
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 @@ static struct methodlist builtin_methods[] = {
{"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 @@ static struct methodlist builtin_methods[] = {
{"hasattr", builtin_hasattr},
{"hash", builtin_hash},
{"hex", builtin_hex},
+ {"id", builtin_id},
{"input", builtin_input},
{"int", builtin_int},
{"len", builtin_len},