Commit 187cb64a authored by Guido van Rossum's avatar Guido van Rossum

* 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.]
parent 3873f1e0
...@@ -42,6 +42,8 @@ object *run_file PROTO((FILE *, char *, int, object *, object *)); ...@@ -42,6 +42,8 @@ object *run_file PROTO((FILE *, char *, int, object *, object *));
object *run_err_node PROTO((int, struct _node *, char *, object *, object *)); object *run_err_node PROTO((int, struct _node *, char *, object *, object *));
object *run_node PROTO((struct _node *, char *, object *, object *)); object *run_node PROTO((struct _node *, char *, object *, object *));
object *compile_string PROTO((char *, char *, int));
void print_error PROTO((void)); void print_error PROTO((void));
void goaway PROTO((int)); void goaway PROTO((int));
...@@ -38,7 +38,7 @@ static struct memberlist frame_memberlist[] = { ...@@ -38,7 +38,7 @@ static struct memberlist frame_memberlist[] = {
{"f_code", T_OBJECT, OFF(f_code)}, {"f_code", T_OBJECT, OFF(f_code)},
{"f_globals", T_OBJECT, OFF(f_globals)}, {"f_globals", T_OBJECT, OFF(f_globals)},
{"f_locals", T_OBJECT, OFF(f_locals)}, {"f_locals", T_OBJECT, OFF(f_locals)},
{"f_fastlocals",T_OBJECT, OFF(f_fastlocals)}, /* {"f_fastlocals",T_OBJECT, OFF(f_fastlocals)}, /* XXX Unsafe */
{"f_localmap", T_OBJECT, OFF(f_localmap)}, {"f_localmap", T_OBJECT, OFF(f_localmap)},
{"f_lasti", T_INT, OFF(f_lasti)}, {"f_lasti", T_INT, OFF(f_lasti)},
{"f_lineno", T_INT, OFF(f_lineno)}, {"f_lineno", T_INT, OFF(f_lineno)},
......
...@@ -35,6 +35,8 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ...@@ -35,6 +35,8 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "pythonrun.h" #include "pythonrun.h"
#include "ceval.h" #include "ceval.h"
#include "modsupport.h" #include "modsupport.h"
#include "compile.h"
#include "eval.h"
static object * static object *
builtin_abs(self, v) builtin_abs(self, v)
...@@ -106,6 +108,29 @@ builtin_coerce(self, args) ...@@ -106,6 +108,29 @@ builtin_coerce(self, args)
return res; return res;
} }
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 * static object *
builtin_dir(self, v) builtin_dir(self, v)
object *self; object *self;
...@@ -168,23 +193,26 @@ exec_eval(v, start) ...@@ -168,23 +193,26 @@ exec_eval(v, start)
char *s; char *s;
int n; int n;
if (v != NULL) { if (v != NULL) {
if (is_stringobject(v)) if (is_tupleobject(v) &&
str = v;
else if (is_tupleobject(v) &&
((n = gettuplesize(v)) == 2 || n == 3)) { ((n = gettuplesize(v)) == 2 || n == 3)) {
str = gettupleitem(v, 0); str = gettupleitem(v, 0);
globals = gettupleitem(v, 1); globals = gettupleitem(v, 1);
if (n == 3) if (n == 3)
locals = gettupleitem(v, 2); 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) || globals != NULL && !is_dictobject(globals) ||
locals != NULL && !is_dictobject(locals)) { locals != NULL && !is_dictobject(locals)) {
err_setstr(TypeError, err_setstr(TypeError,
"exec/eval arguments must be string[,dict[,dict]]"); "exec/eval arguments must be (string|code)[,dict[,dict]]");
return NULL; return NULL;
} }
if (is_codeobject(str))
return eval_code((codeobject *) str, globals, locals,
(object *)NULL);
s = getstringvalue(str); s = getstringvalue(str);
if (strlen(s) != getstringsize(str)) { if (strlen(s) != getstringsize(str)) {
err_setstr(ValueError, "embedded '\\0' in string arg"); err_setstr(ValueError, "embedded '\\0' in string arg");
...@@ -305,6 +333,17 @@ builtin_hasattr(self, args) ...@@ -305,6 +333,17 @@ builtin_hasattr(self, args)
return newintobject(1L); return newintobject(1L);
} }
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 * static object *
builtin_setattr(self, args) builtin_setattr(self, args)
object *self; object *self;
...@@ -713,6 +752,7 @@ static struct methodlist builtin_methods[] = { ...@@ -713,6 +752,7 @@ static struct methodlist builtin_methods[] = {
{"chr", builtin_chr}, {"chr", builtin_chr},
{"cmp", builtin_cmp}, {"cmp", builtin_cmp},
{"coerce", builtin_coerce}, {"coerce", builtin_coerce},
{"compile", builtin_compile},
{"dir", builtin_dir}, {"dir", builtin_dir},
{"divmod", builtin_divmod}, {"divmod", builtin_divmod},
{"eval", builtin_eval}, {"eval", builtin_eval},
...@@ -723,6 +763,7 @@ static struct methodlist builtin_methods[] = { ...@@ -723,6 +763,7 @@ static struct methodlist builtin_methods[] = {
{"hasattr", builtin_hasattr}, {"hasattr", builtin_hasattr},
{"hash", builtin_hash}, {"hash", builtin_hash},
{"hex", builtin_hex}, {"hex", builtin_hex},
{"id", builtin_id},
{"input", builtin_input}, {"input", builtin_input},
{"int", builtin_int}, {"int", builtin_int},
{"len", builtin_len}, {"len", builtin_len},
......
...@@ -45,6 +45,9 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ...@@ -45,6 +45,9 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#define CHECKEXC 1 /* Double-check exception checking */ #define CHECKEXC 1 /* Double-check exception checking */
#endif #endif
#define DEBUG
/* Forward declarations */ /* Forward declarations */
#ifdef LLTRACE #ifdef LLTRACE
...@@ -82,6 +85,7 @@ static object *cmp_outcome PROTO((int, object *, object *)); ...@@ -82,6 +85,7 @@ static object *cmp_outcome PROTO((int, object *, object *));
static int import_from PROTO((object *, object *, object *)); static int import_from PROTO((object *, object *, object *));
static object *build_class PROTO((object *, object *)); static object *build_class PROTO((object *, object *));
static void locals_2_fast PROTO((frameobject *, int)); 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 */ /* Pointer to current frame, used to link new frames to */
...@@ -178,6 +182,7 @@ eval_code(co, globals, locals, arg) ...@@ -178,6 +182,7 @@ eval_code(co, globals, locals, arg)
object *trace = NULL; /* Trace function or NULL */ object *trace = NULL; /* Trace function or NULL */
object *retval; /* Return value iff why == WHY_RETURN */ object *retval; /* Return value iff why == WHY_RETURN */
char *name; /* Name used by some instructions */ char *name; /* Name used by some instructions */
int needmerge = 0;
#ifdef LLTRACE #ifdef LLTRACE
int lltrace = dictlookup(globals, "__lltrace__") != NULL; int lltrace = dictlookup(globals, "__lltrace__") != NULL;
#endif #endif
...@@ -217,6 +222,18 @@ eval_code(co, globals, locals, arg) ...@@ -217,6 +222,18 @@ eval_code(co, globals, locals, arg)
#define POP() BASIC_POP() #define POP() BASIC_POP()
#endif #endif
if (globals == NULL) {
globals = getglobals();
if (locals == NULL) {
locals = getlocals();
needmerge = 1;
}
}
else {
if (locals == NULL)
locals = globals;
}
f = newframeobject( f = newframeobject(
current_frame, /*back*/ current_frame, /*back*/
co, /*code*/ co, /*code*/
...@@ -1328,11 +1345,17 @@ eval_code(co, globals, locals, arg) ...@@ -1328,11 +1345,17 @@ eval_code(co, globals, locals, arg)
why = WHY_EXCEPTION; 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 */ /* Restore previous frame and release the current one */
current_frame = f->f_back; current_frame = f->f_back;
DECREF(f); DECREF(f);
if (needmerge)
locals_2_fast(current_frame, 1);
return retval; return retval;
} }
...@@ -1417,7 +1440,9 @@ call_trace(p_trace, p_newtrace, f, msg, arg) ...@@ -1417,7 +1440,9 @@ call_trace(p_trace, p_newtrace, f, msg, arg)
INCREF(arg); INCREF(arg);
settupleitem(arglist, 2, arg); settupleitem(arglist, 2, arg);
tracing++; tracing++;
fast_2_locals(f);
res = call_object(*p_trace, arglist); res = call_object(*p_trace, arglist);
locals_2_fast(f, 1);
tracing--; tracing--;
cleanup: cleanup:
XDECREF(arglist); XDECREF(arglist);
...@@ -1447,24 +1472,25 @@ call_trace(p_trace, p_newtrace, f, msg, arg) ...@@ -1447,24 +1472,25 @@ call_trace(p_trace, p_newtrace, f, msg, arg)
} }
} }
object * static void
getlocals() fast_2_locals(f)
{
/* Merge f->f_fastlocals into f->f_locals, then return the latter */
frameobject *f; frameobject *f;
{
/* Merge f->f_fastlocals into f->f_locals */
object *locals, *fast, *map; object *locals, *fast, *map;
object *error_type, *error_value;
int i; int i;
f = current_frame;
if (f == NULL) if (f == NULL)
return NULL; return;
locals = f->f_locals; locals = f->f_locals;
fast = f->f_fastlocals; fast = f->f_fastlocals;
map = f->f_localmap; map = f->f_localmap;
if (locals == NULL || fast == NULL || map == NULL) if (locals == NULL || fast == NULL || map == NULL)
return locals; return;
if (!is_dictobject(locals) || !is_listobject(fast) || if (!is_dictobject(locals) || !is_listobject(fast) ||
!is_dictobject(map)) !is_dictobject(map))
return locals; return;
err_get(&error_type, &error_value);
i = getdictsize(map); i = getdictsize(map);
while (--i >= 0) { while (--i >= 0) {
object *key; object *key;
...@@ -1488,7 +1514,7 @@ getlocals() ...@@ -1488,7 +1514,7 @@ getlocals()
err_clear(); err_clear();
} }
} }
return locals; err_setval(error_type, error_value);
} }
static void static void
...@@ -1538,6 +1564,15 @@ mergelocals() ...@@ -1538,6 +1564,15 @@ mergelocals()
locals_2_fast(current_frame, 1); locals_2_fast(current_frame, 1);
} }
object *
getlocals()
{
if (current_frame == NULL)
return NULL;
fast_2_locals(current_frame);
return current_frame->f_locals;
}
object * object *
getglobals() getglobals()
{ {
......
...@@ -263,7 +263,7 @@ object * ...@@ -263,7 +263,7 @@ object *
run_string(str, start, globals, locals) run_string(str, start, globals, locals)
char *str; char *str;
int start; int start;
/*dict*/object *globals, *locals; object *globals, *locals;
{ {
node *n; node *n;
int err; int err;
...@@ -276,7 +276,7 @@ run_file(fp, filename, start, globals, locals) ...@@ -276,7 +276,7 @@ run_file(fp, filename, start, globals, locals)
FILE *fp; FILE *fp;
char *filename; char *filename;
int start; int start;
/*dict*/object *globals, *locals; object *globals, *locals;
{ {
node *n; node *n;
int err; int err;
...@@ -289,7 +289,7 @@ run_err_node(err, n, filename, globals, locals) ...@@ -289,7 +289,7 @@ run_err_node(err, n, filename, globals, locals)
int err; int err;
node *n; node *n;
char *filename; char *filename;
/*dict*/object *globals, *locals; object *globals, *locals;
{ {
if (err != E_DONE) { if (err != E_DONE) {
err_input(err); err_input(err);
...@@ -302,25 +302,9 @@ object * ...@@ -302,25 +302,9 @@ object *
run_node(n, filename, globals, locals) run_node(n, filename, globals, locals)
node *n; node *n;
char *filename; char *filename;
/*dict*/object *globals, *locals; object *globals, *locals;
{ {
object *res; return eval_node(n, filename, globals, locals);
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;
} }
object * object *
...@@ -341,6 +325,25 @@ eval_node(n, filename, globals, locals) ...@@ -341,6 +325,25 @@ eval_node(n, filename, globals, locals)
return v; 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 */ /* Simplified interface to parsefile */
int int
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment