diff options
author | Guido van Rossum <guido@python.org> | 1993-03-30 17:46:03 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1993-03-30 17:46:03 (GMT) |
commit | 5b7221849e240c2a62ebbc48d58607417b42ef44 (patch) | |
tree | 28882cac1829c5da2258788ffdb274839f788589 /Python | |
parent | 8b17d6bd89cd79820c76bd88bc064e44fc03a1bd (diff) | |
download | cpython-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')
-rw-r--r-- | Python/bltinmodule.c | 51 | ||||
-rw-r--r-- | Python/ceval.c | 53 | ||||
-rw-r--r-- | Python/pythonrun.c | 45 |
3 files changed, 114 insertions, 35 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}, diff --git a/Python/ceval.c b/Python/ceval.c index 45d0a6a..cdd71a8 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -45,6 +45,9 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #define CHECKEXC 1 /* Double-check exception checking */ #endif +#define DEBUG + + /* Forward declarations */ #ifdef LLTRACE @@ -82,6 +85,7 @@ static object *cmp_outcome PROTO((int, object *, object *)); static int import_from PROTO((object *, object *, object *)); static object *build_class PROTO((object *, object *)); static void locals_2_fast PROTO((frameobject *, int)); +static void fast_2_locals PROTO((frameobject *)); /* Pointer to current frame, used to link new frames to */ @@ -178,6 +182,7 @@ eval_code(co, globals, locals, arg) object *trace = NULL; /* Trace function or NULL */ object *retval; /* Return value iff why == WHY_RETURN */ char *name; /* Name used by some instructions */ + int needmerge = 0; #ifdef LLTRACE int lltrace = dictlookup(globals, "__lltrace__") != NULL; #endif @@ -217,6 +222,18 @@ eval_code(co, globals, locals, arg) #define POP() BASIC_POP() #endif + if (globals == NULL) { + globals = getglobals(); + if (locals == NULL) { + locals = getlocals(); + needmerge = 1; + } + } + else { + if (locals == NULL) + locals = globals; + } + f = newframeobject( current_frame, /*back*/ co, /*code*/ @@ -1328,11 +1345,17 @@ eval_code(co, globals, locals, arg) why = WHY_EXCEPTION; } } + + if (fastlocals && (f->ob_refcnt > 1 || f->f_locals->ob_refcnt > 2)) + fast_2_locals(f); /* Restore previous frame and release the current one */ current_frame = f->f_back; DECREF(f); + + if (needmerge) + locals_2_fast(current_frame, 1); return retval; } @@ -1417,7 +1440,9 @@ call_trace(p_trace, p_newtrace, f, msg, arg) INCREF(arg); settupleitem(arglist, 2, arg); tracing++; + fast_2_locals(f); res = call_object(*p_trace, arglist); + locals_2_fast(f, 1); tracing--; cleanup: XDECREF(arglist); @@ -1447,24 +1472,25 @@ call_trace(p_trace, p_newtrace, f, msg, arg) } } -object * -getlocals() -{ - /* Merge f->f_fastlocals into f->f_locals, then return the latter */ +static void +fast_2_locals(f) frameobject *f; +{ + /* Merge f->f_fastlocals into f->f_locals */ object *locals, *fast, *map; + object *error_type, *error_value; int i; - f = current_frame; if (f == NULL) - return NULL; + return; locals = f->f_locals; fast = f->f_fastlocals; map = f->f_localmap; if (locals == NULL || fast == NULL || map == NULL) - return locals; + return; if (!is_dictobject(locals) || !is_listobject(fast) || !is_dictobject(map)) - return locals; + return; + err_get(&error_type, &error_value); i = getdictsize(map); while (--i >= 0) { object *key; @@ -1488,7 +1514,7 @@ getlocals() err_clear(); } } - return locals; + err_setval(error_type, error_value); } static void @@ -1539,6 +1565,15 @@ mergelocals() } object * +getlocals() +{ + if (current_frame == NULL) + return NULL; + fast_2_locals(current_frame); + return current_frame->f_locals; +} + +object * getglobals() { if (current_frame == NULL) diff --git a/Python/pythonrun.c b/Python/pythonrun.c index c387c62..98008b4 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -263,7 +263,7 @@ object * run_string(str, start, globals, locals) char *str; int start; - /*dict*/object *globals, *locals; + object *globals, *locals; { node *n; int err; @@ -276,7 +276,7 @@ run_file(fp, filename, start, globals, locals) FILE *fp; char *filename; int start; - /*dict*/object *globals, *locals; + object *globals, *locals; { node *n; int err; @@ -289,7 +289,7 @@ run_err_node(err, n, filename, globals, locals) int err; node *n; char *filename; - /*dict*/object *globals, *locals; + object *globals, *locals; { if (err != E_DONE) { err_input(err); @@ -302,25 +302,9 @@ object * run_node(n, filename, globals, locals) node *n; char *filename; - /*dict*/object *globals, *locals; + object *globals, *locals; { - object *res; - int needmerge = 0; - if (globals == NULL) { - globals = getglobals(); - if (locals == NULL) { - locals = getlocals(); - needmerge = 1; - } - } - else { - if (locals == NULL) - locals = globals; - } - res = eval_node(n, filename, globals, locals); - if (needmerge) - mergelocals(); - return res; + return eval_node(n, filename, globals, locals); } object * @@ -341,6 +325,25 @@ eval_node(n, filename, globals, locals) return v; } +object * +compile_string(str, filename, start) + char *str; + char *filename; + int start; +{ + node *n; + int err; + codeobject *co; + err = parse_string(str, start, &n); + if (err != E_DONE) { + err_input(err); + return NULL; + } + co = compile(n, filename); + freetree(n); + return (object *)co; +} + /* Simplified interface to parsefile */ int |