__builtins__ mods (and sys_checkinterval for ceval.c)

This commit is contained in:
Guido van Rossum 1995-01-09 17:53:26 +00:00
parent 2565bff40a
commit 6135a87f2b
8 changed files with 90 additions and 45 deletions

View File

@ -30,9 +30,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* Built-in module interface */ /* Built-in module interface */
extern object *getbuiltin PROTO((object *)); extern object *getbuiltindict PROTO(());
extern object *getbuiltins PROTO((char *));
extern int setbuiltin PROTO((char *, object *));
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -32,10 +32,12 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
object *call_object PROTO((object *, object *)); object *call_object PROTO((object *, object *));
object *getbuiltins PROTO((void));
object *getglobals PROTO((void)); object *getglobals PROTO((void));
object *getlocals PROTO((void)); object *getlocals PROTO((void));
object *getowner PROTO((void)); object *getowner PROTO((void));
object *getframe PROTO((void)); object *getframe PROTO((void));
int getrestricted PROTO((void));
void flushline PROTO((void)); void flushline PROTO((void));

View File

@ -40,6 +40,7 @@ typedef struct _frame {
OB_HEAD OB_HEAD
struct _frame *f_back; /* previous frame, or NULL */ struct _frame *f_back; /* previous frame, or NULL */
codeobject *f_code; /* code segment */ codeobject *f_code; /* code segment */
object *f_builtins; /* builtin symbol table (dictobject) */
object *f_globals; /* global symbol table (dictobject) */ object *f_globals; /* global symbol table (dictobject) */
object *f_locals; /* local symbol table (dictobject) */ object *f_locals; /* local symbol table (dictobject) */
object *f_owner; /* owner (e.g. class or module) or NULL */ object *f_owner; /* owner (e.g. class or module) or NULL */
@ -52,6 +53,7 @@ typedef struct _frame {
int f_iblock; /* index in f_blockstack */ int f_iblock; /* index in f_blockstack */
int f_lasti; /* Last instruction if called */ int f_lasti; /* Last instruction if called */
int f_lineno; /* Current line number */ int f_lineno; /* Current line number */
int f_restricted; /* Flag set if restricted operations in this scope */
object *f_trace; /* Trace function */ object *f_trace; /* Trace function */
} frameobject; } frameobject;

View File

@ -269,6 +269,7 @@ typedef struct methodlist PyMethodDef;
#define PyEval_CallObject call_object #define PyEval_CallObject call_object
#define PyEval_EvalCode eval_code #define PyEval_EvalCode eval_code
#define Py_FlushLine flushline #define Py_FlushLine flushline
#define PyEval_GetBuiltins getbuiltins
#define PyEval_GetGlobals getglobals #define PyEval_GetGlobals getglobals
#define PyEval_GetLocals getlocals #define PyEval_GetLocals getlocals
#define PyEval_InitThreads init_save_thread #define PyEval_InitThreads init_save_thread
@ -287,7 +288,7 @@ typedef struct methodlist PyMethodDef;
#define PyImport_Init initimport #define PyImport_Init initimport
#define PyImport_ReloadModule reload_module #define PyImport_ReloadModule reload_module
#define PyNumber_Coerce coerce #define PyNumber_Coerce coerce
#define PyBuiltin_GetObject getbuiltin #define PyBuiltin_GetDict getbuiltindict
#define PyBuiltin_Init initbuiltin #define PyBuiltin_Init initbuiltin
#define PyMarshal_Init initmarshal #define PyMarshal_Init initmarshal
#define PyMarshal_ReadLongFromFile rd_long #define PyMarshal_ReadLongFromFile rd_long
@ -317,7 +318,7 @@ typedef struct methodlist PyMethodDef;
#define PyRun_InteractiveLoop run_tty_loop #define PyRun_InteractiveLoop run_tty_loop
#define PyMember_Get getmember #define PyMember_Get getmember
#define PyMember_Set setmember #define PyMember_Set setmember
#define Py_InitModule initmodule #define Py_InitModule(name, methods) initmodule(name, methods)
#define Py_BuildValue mkvalue #define Py_BuildValue mkvalue
#define Py_VaBuildValue vmkvalue #define Py_VaBuildValue vmkvalue
#define PyArg_Parse getargs #define PyArg_Parse getargs

View File

@ -392,6 +392,17 @@ builtin_eval(self, args)
&Mappingtype, &globals, &Mappingtype, &globals,
&Mappingtype, &locals)) &Mappingtype, &locals))
return NULL; return NULL;
if (globals == NULL) {
globals = getglobals();
if (globals == NULL)
return NULL;
}
if (locals == NULL)
locals = globals;
if (dictlookup(globals, "__builtins__") == NULL) {
if (dictinsert(globals, "__builtins__", getbuiltins()) != 0)
return NULL;
}
if (is_codeobject(cmd)) if (is_codeobject(cmd))
return eval_code((codeobject *) cmd, globals, locals, return eval_code((codeobject *) cmd, globals, locals,
(object *)NULL, (object *)NULL); (object *)NULL, (object *)NULL);
@ -428,6 +439,17 @@ builtin_execfile(self, args)
&Mappingtype, &globals, &Mappingtype, &globals,
&Mappingtype, &locals)) &Mappingtype, &locals))
return NULL; return NULL;
if (globals == NULL) {
globals = getglobals();
if (globals == NULL)
return NULL;
}
if (locals == NULL)
locals = globals;
if (dictlookup(globals, "__builtins__") == NULL) {
if (dictinsert(globals, "__builtins__", getbuiltins()) != 0)
return NULL;
}
BGN_SAVE BGN_SAVE
fp = fopen(filename, "r"); fp = fopen(filename, "r");
END_SAVE END_SAVE
@ -725,6 +747,7 @@ builtin_input(self, args)
object *line; object *line;
char *str; char *str;
object *res; object *res;
object *globals, *locals;
line = builtin_raw_input(self, args); line = builtin_raw_input(self, args);
if (line == NULL) if (line == NULL)
@ -733,7 +756,13 @@ builtin_input(self, args)
return NULL; return NULL;
while (*str == ' ' || *str == '\t') while (*str == ' ' || *str == '\t')
str++; str++;
res = run_string(str, eval_input, (object *)NULL, (object *)NULL); globals = getglobals();
locals = getlocals();
if (dictlookup(globals, "__builtins__") == NULL) {
if (dictinsert(globals, "__builtins__", getbuiltins()) != 0)
return NULL;
}
res = run_string(str, eval_input, globals, locals);
DECREF(line); DECREF(line);
return res; return res;
} }
@ -1363,25 +1392,9 @@ static struct methodlist builtin_methods[] = {
static object *builtin_dict; static object *builtin_dict;
object * object *
getbuiltin(name) getbuiltindict()
object *name;
{ {
return mappinglookup(builtin_dict, name); return builtin_dict;
}
object *
getbuiltins(name)
char *name;
{
return dictlookup(builtin_dict, name);
}
int
setbuiltin(cname, value)
char *cname;
object *value;
{
return dictinsert(builtin_dict, cname, value);
} }
/* Predefined exceptions */ /* Predefined exceptions */

View File

@ -33,7 +33,6 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "eval.h" #include "eval.h"
#include "ceval.h" #include "ceval.h"
#include "opcode.h" #include "opcode.h"
#include "bltinmodule.h"
#include "traceback.h" #include "traceback.h"
#include "graminit.h" #include "graminit.h"
#include "pythonrun.h" #include "pythonrun.h"
@ -285,7 +284,6 @@ eval_code(co, globals, locals, owner, arg)
char *name; /* Name used by some instructions */ char *name; /* Name used by some instructions */
int needmerge = 0; /* Set if need to merge locals back at end */ int needmerge = 0; /* Set if need to merge locals back at end */
int defmode = 0; /* Default access mode for new variables */ int defmode = 0; /* Default access mode for new variables */
int ticker_count = 10; /* Check for intr every Nth instruction */
#ifdef LLTRACE #ifdef LLTRACE
int lltrace; int lltrace;
#endif #endif
@ -325,16 +323,9 @@ eval_code(co, globals, locals, owner, arg)
#define POP() BASIC_POP() #define POP() BASIC_POP()
#endif #endif
if (globals == NULL) { if (globals == NULL || locals == NULL) {
globals = getglobals(); err_setstr(SystemError, "eval_code: NULL globals or locals");
if (locals == NULL) { return NULL;
locals = getlocals();
needmerge = 1;
}
}
else {
if (locals == NULL)
locals = globals;
} }
#ifdef LLTRACE #ifdef LLTRACE
@ -385,10 +376,6 @@ eval_code(co, globals, locals, owner, arg)
} }
} }
x = sysget("check_interval");
if (x != NULL && is_intobject(x))
ticker_count = getintvalue(x);
next_instr = GETUSTRINGVALUE(f->f_code->co_code); next_instr = GETUSTRINGVALUE(f->f_code->co_code);
stack_pointer = f->f_valuestack; stack_pointer = f->f_valuestack;
@ -417,7 +404,7 @@ eval_code(co, globals, locals, owner, arg)
} }
if (--ticker < 0) { if (--ticker < 0) {
ticker = ticker_count; ticker = sys_checkinterval;
if (sigcheck()) { if (sigcheck()) {
why = WHY_EXCEPTION; why = WHY_EXCEPTION;
goto on_error; goto on_error;
@ -745,7 +732,7 @@ eval_code(co, globals, locals, owner, arg)
/* Print value except if procedure result */ /* Print value except if procedure result */
/* Before printing, also assign to '_' */ /* Before printing, also assign to '_' */
if (v != None && if (v != None &&
(err = setbuiltin("_", v)) == 0 && (err = dictinsert(f->f_builtins, "_", v)) == 0 &&
!suppress_print) { !suppress_print) {
flushline(); flushline();
x = sysget("stdout"); x = sysget("stdout");
@ -1157,7 +1144,7 @@ eval_code(co, globals, locals, owner, arg)
x = dict2lookup(f->f_globals, w); x = dict2lookup(f->f_globals, w);
if (x == NULL) { if (x == NULL) {
err_clear(); err_clear();
x = getbuiltin(w); x = dict2lookup(f->f_builtins, w);
if (x == NULL) { if (x == NULL) {
err_setval(NameError, w); err_setval(NameError, w);
break; break;
@ -1179,7 +1166,7 @@ eval_code(co, globals, locals, owner, arg)
x = dict2lookup(f->f_globals, w); x = dict2lookup(f->f_globals, w);
if (x == NULL) { if (x == NULL) {
err_clear(); err_clear();
x = getbuiltin(w); x = dict2lookup(f->f_builtins, w);
if (x == NULL) { if (x == NULL) {
err_setval(NameError, w); err_setval(NameError, w);
break; break;
@ -1324,7 +1311,7 @@ eval_code(co, globals, locals, owner, arg)
case IMPORT_NAME: case IMPORT_NAME:
w = GETNAMEV(oparg); w = GETNAMEV(oparg);
x = getbuiltins("__import__"); x = dictlookup(f->f_builtins, "__import__");
if (x == NULL) { if (x == NULL) {
err_setstr(ImportError, err_setstr(ImportError,
"__import__ not found"); "__import__ not found");
@ -1700,6 +1687,15 @@ call_trace(p_trace, p_newtrace, f, msg, arg)
} }
} }
object *
getbuiltins()
{
if (current_frame == NULL)
return NULL;
else
return current_frame->f_builtins;
}
object * object *
getlocals() getlocals()
{ {
@ -1733,6 +1729,12 @@ getframe()
return (object *)current_frame; return (object *)current_frame;
} }
int
getrestricted()
{
return current_frame == NULL ? 0 : current_frame->f_restricted;
}
void void
flushline() flushline()
{ {
@ -2660,6 +2662,8 @@ exec_statement(prog, globals, locals)
"exec 2nd/3rd args must be dict or None"); "exec 2nd/3rd args must be dict or None");
return -1; return -1;
} }
if (dictlookup(globals, "__builtins__") == NULL)
dictinsert(globals, "__builtins__", current_frame->f_builtins);
if (is_codeobject(prog)) { if (is_codeobject(prog)) {
if (eval_code((codeobject *) prog, globals, locals, if (eval_code((codeobject *) prog, globals, locals,
(object *)NULL, (object *)NULL) == NULL) (object *)NULL, (object *)NULL) == NULL)

View File

@ -32,6 +32,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "import.h" #include "import.h"
#include "errcode.h" #include "errcode.h"
#include "sysmodule.h" #include "sysmodule.h"
#include "bltinmodule.h"
#include "pythonrun.h" #include "pythonrun.h"
#include "marshal.h" #include "marshal.h"
#include "compile.h" #include "compile.h"
@ -147,6 +148,10 @@ exec_code_module(name, co)
if (m == NULL) if (m == NULL)
return NULL; return NULL;
d = getmoduledict(m); d = getmoduledict(m);
if (dictlookup(d, "__builtins__") == NULL) {
if (dictinsert(d, "__builtins__", getbuiltindict()) != 0)
return NULL;
}
v = eval_code((codeobject *)co, d, d, d, (object *)NULL); v = eval_code((codeobject *)co, d, d, d, (object *)NULL);
if (v == NULL) if (v == NULL)
return NULL; return NULL;

View File

@ -32,6 +32,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "graminit.h" #include "graminit.h"
#include "errcode.h" #include "errcode.h"
#include "sysmodule.h" #include "sysmodule.h"
#include "bltinmodule.h"
#include "compile.h" #include "compile.h"
#include "eval.h" #include "eval.h"
#include "ceval.h" #include "ceval.h"
@ -48,6 +49,7 @@ extern char *getpythonpath();
extern grammar gram; /* From graminit.c */ extern grammar gram; /* From graminit.c */
/* Forward */ /* Forward */
static void initmain PROTO((void));
static object *run_err_node PROTO((node *n, char *filename, static object *run_err_node PROTO((node *n, char *filename,
object *globals, object *locals)); object *globals, object *locals));
static object *run_node PROTO((node *n, char *filename, static object *run_node PROTO((node *n, char *filename,
@ -83,6 +85,24 @@ initall()
setpythonpath(getpythonpath()); setpythonpath(getpythonpath());
initsigs(); /* Signal handling stuff, including initintr() */ initsigs(); /* Signal handling stuff, including initintr() */
initmain();
}
/* Create __main__ module */
static void
initmain()
{
object *m, *d;
m = add_module("__main__");
if (m == NULL)
fatal("can't create __main__ module");
d = getmoduledict(m);
if (dictlookup(d, "__builtins__") == NULL) {
if (dictinsert(d, "__builtins__", getbuiltindict()))
fatal("can't add __builtins__ to __main__");
}
} }
/* Parse input from a file and execute it */ /* Parse input from a file and execute it */