Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6135a87

Browse files
committed
__builtins__ mods (and sys_checkinterval for ceval.c)
1 parent 2565bff commit 6135a87

8 files changed

Lines changed: 90 additions & 45 deletions

File tree

Include/bltinmodule.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3030

3131
/* Built-in module interface */
3232

33-
extern object *getbuiltin PROTO((object *));
34-
extern object *getbuiltins PROTO((char *));
35-
extern int setbuiltin PROTO((char *, object *));
33+
extern object *getbuiltindict PROTO(());
3634

3735
#ifdef __cplusplus
3836
}

Include/ceval.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3232

3333
object *call_object PROTO((object *, object *));
3434

35+
object *getbuiltins PROTO((void));
3536
object *getglobals PROTO((void));
3637
object *getlocals PROTO((void));
3738
object *getowner PROTO((void));
3839
object *getframe PROTO((void));
40+
int getrestricted PROTO((void));
3941

4042
void flushline PROTO((void));
4143

Include/frameobject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ typedef struct _frame {
4040
OB_HEAD
4141
struct _frame *f_back; /* previous frame, or NULL */
4242
codeobject *f_code; /* code segment */
43+
object *f_builtins; /* builtin symbol table (dictobject) */
4344
object *f_globals; /* global symbol table (dictobject) */
4445
object *f_locals; /* local symbol table (dictobject) */
4546
object *f_owner; /* owner (e.g. class or module) or NULL */
@@ -52,6 +53,7 @@ typedef struct _frame {
5253
int f_iblock; /* index in f_blockstack */
5354
int f_lasti; /* Last instruction if called */
5455
int f_lineno; /* Current line number */
56+
int f_restricted; /* Flag set if restricted operations in this scope */
5557
object *f_trace; /* Trace function */
5658
} frameobject;
5759

Include/rename1.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ typedef struct methodlist PyMethodDef;
269269
#define PyEval_CallObject call_object
270270
#define PyEval_EvalCode eval_code
271271
#define Py_FlushLine flushline
272+
#define PyEval_GetBuiltins getbuiltins
272273
#define PyEval_GetGlobals getglobals
273274
#define PyEval_GetLocals getlocals
274275
#define PyEval_InitThreads init_save_thread
@@ -287,7 +288,7 @@ typedef struct methodlist PyMethodDef;
287288
#define PyImport_Init initimport
288289
#define PyImport_ReloadModule reload_module
289290
#define PyNumber_Coerce coerce
290-
#define PyBuiltin_GetObject getbuiltin
291+
#define PyBuiltin_GetDict getbuiltindict
291292
#define PyBuiltin_Init initbuiltin
292293
#define PyMarshal_Init initmarshal
293294
#define PyMarshal_ReadLongFromFile rd_long
@@ -317,7 +318,7 @@ typedef struct methodlist PyMethodDef;
317318
#define PyRun_InteractiveLoop run_tty_loop
318319
#define PyMember_Get getmember
319320
#define PyMember_Set setmember
320-
#define Py_InitModule initmodule
321+
#define Py_InitModule(name, methods) initmodule(name, methods)
321322
#define Py_BuildValue mkvalue
322323
#define Py_VaBuildValue vmkvalue
323324
#define PyArg_Parse getargs

Python/bltinmodule.c

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,17 @@ builtin_eval(self, args)
392392
&Mappingtype, &globals,
393393
&Mappingtype, &locals))
394394
return NULL;
395+
if (globals == NULL) {
396+
globals = getglobals();
397+
if (globals == NULL)
398+
return NULL;
399+
}
400+
if (locals == NULL)
401+
locals = globals;
402+
if (dictlookup(globals, "__builtins__") == NULL) {
403+
if (dictinsert(globals, "__builtins__", getbuiltins()) != 0)
404+
return NULL;
405+
}
395406
if (is_codeobject(cmd))
396407
return eval_code((codeobject *) cmd, globals, locals,
397408
(object *)NULL, (object *)NULL);
@@ -428,6 +439,17 @@ builtin_execfile(self, args)
428439
&Mappingtype, &globals,
429440
&Mappingtype, &locals))
430441
return NULL;
442+
if (globals == NULL) {
443+
globals = getglobals();
444+
if (globals == NULL)
445+
return NULL;
446+
}
447+
if (locals == NULL)
448+
locals = globals;
449+
if (dictlookup(globals, "__builtins__") == NULL) {
450+
if (dictinsert(globals, "__builtins__", getbuiltins()) != 0)
451+
return NULL;
452+
}
431453
BGN_SAVE
432454
fp = fopen(filename, "r");
433455
END_SAVE
@@ -725,6 +747,7 @@ builtin_input(self, args)
725747
object *line;
726748
char *str;
727749
object *res;
750+
object *globals, *locals;
728751

729752
line = builtin_raw_input(self, args);
730753
if (line == NULL)
@@ -733,7 +756,13 @@ builtin_input(self, args)
733756
return NULL;
734757
while (*str == ' ' || *str == '\t')
735758
str++;
736-
res = run_string(str, eval_input, (object *)NULL, (object *)NULL);
759+
globals = getglobals();
760+
locals = getlocals();
761+
if (dictlookup(globals, "__builtins__") == NULL) {
762+
if (dictinsert(globals, "__builtins__", getbuiltins()) != 0)
763+
return NULL;
764+
}
765+
res = run_string(str, eval_input, globals, locals);
737766
DECREF(line);
738767
return res;
739768
}
@@ -1363,25 +1392,9 @@ static struct methodlist builtin_methods[] = {
13631392
static object *builtin_dict;
13641393

13651394
object *
1366-
getbuiltin(name)
1367-
object *name;
1368-
{
1369-
return mappinglookup(builtin_dict, name);
1370-
}
1371-
1372-
object *
1373-
getbuiltins(name)
1374-
char *name;
1375-
{
1376-
return dictlookup(builtin_dict, name);
1377-
}
1378-
1379-
int
1380-
setbuiltin(cname, value)
1381-
char *cname;
1382-
object *value;
1395+
getbuiltindict()
13831396
{
1384-
return dictinsert(builtin_dict, cname, value);
1397+
return builtin_dict;
13851398
}
13861399

13871400
/* Predefined exceptions */

Python/ceval.c

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3333
#include "eval.h"
3434
#include "ceval.h"
3535
#include "opcode.h"
36-
#include "bltinmodule.h"
3736
#include "traceback.h"
3837
#include "graminit.h"
3938
#include "pythonrun.h"
@@ -285,7 +284,6 @@ eval_code(co, globals, locals, owner, arg)
285284
char *name; /* Name used by some instructions */
286285
int needmerge = 0; /* Set if need to merge locals back at end */
287286
int defmode = 0; /* Default access mode for new variables */
288-
int ticker_count = 10; /* Check for intr every Nth instruction */
289287
#ifdef LLTRACE
290288
int lltrace;
291289
#endif
@@ -325,16 +323,9 @@ eval_code(co, globals, locals, owner, arg)
325323
#define POP() BASIC_POP()
326324
#endif
327325

328-
if (globals == NULL) {
329-
globals = getglobals();
330-
if (locals == NULL) {
331-
locals = getlocals();
332-
needmerge = 1;
333-
}
334-
}
335-
else {
336-
if (locals == NULL)
337-
locals = globals;
326+
if (globals == NULL || locals == NULL) {
327+
err_setstr(SystemError, "eval_code: NULL globals or locals");
328+
return NULL;
338329
}
339330

340331
#ifdef LLTRACE
@@ -385,10 +376,6 @@ eval_code(co, globals, locals, owner, arg)
385376
}
386377
}
387378

388-
x = sysget("check_interval");
389-
if (x != NULL && is_intobject(x))
390-
ticker_count = getintvalue(x);
391-
392379
next_instr = GETUSTRINGVALUE(f->f_code->co_code);
393380
stack_pointer = f->f_valuestack;
394381

@@ -417,7 +404,7 @@ eval_code(co, globals, locals, owner, arg)
417404
}
418405

419406
if (--ticker < 0) {
420-
ticker = ticker_count;
407+
ticker = sys_checkinterval;
421408
if (sigcheck()) {
422409
why = WHY_EXCEPTION;
423410
goto on_error;
@@ -745,7 +732,7 @@ eval_code(co, globals, locals, owner, arg)
745732
/* Print value except if procedure result */
746733
/* Before printing, also assign to '_' */
747734
if (v != None &&
748-
(err = setbuiltin("_", v)) == 0 &&
735+
(err = dictinsert(f->f_builtins, "_", v)) == 0 &&
749736
!suppress_print) {
750737
flushline();
751738
x = sysget("stdout");
@@ -1157,7 +1144,7 @@ eval_code(co, globals, locals, owner, arg)
11571144
x = dict2lookup(f->f_globals, w);
11581145
if (x == NULL) {
11591146
err_clear();
1160-
x = getbuiltin(w);
1147+
x = dict2lookup(f->f_builtins, w);
11611148
if (x == NULL) {
11621149
err_setval(NameError, w);
11631150
break;
@@ -1179,7 +1166,7 @@ eval_code(co, globals, locals, owner, arg)
11791166
x = dict2lookup(f->f_globals, w);
11801167
if (x == NULL) {
11811168
err_clear();
1182-
x = getbuiltin(w);
1169+
x = dict2lookup(f->f_builtins, w);
11831170
if (x == NULL) {
11841171
err_setval(NameError, w);
11851172
break;
@@ -1324,7 +1311,7 @@ eval_code(co, globals, locals, owner, arg)
13241311

13251312
case IMPORT_NAME:
13261313
w = GETNAMEV(oparg);
1327-
x = getbuiltins("__import__");
1314+
x = dictlookup(f->f_builtins, "__import__");
13281315
if (x == NULL) {
13291316
err_setstr(ImportError,
13301317
"__import__ not found");
@@ -1700,6 +1687,15 @@ call_trace(p_trace, p_newtrace, f, msg, arg)
17001687
}
17011688
}
17021689

1690+
object *
1691+
getbuiltins()
1692+
{
1693+
if (current_frame == NULL)
1694+
return NULL;
1695+
else
1696+
return current_frame->f_builtins;
1697+
}
1698+
17031699
object *
17041700
getlocals()
17051701
{
@@ -1733,6 +1729,12 @@ getframe()
17331729
return (object *)current_frame;
17341730
}
17351731

1732+
int
1733+
getrestricted()
1734+
{
1735+
return current_frame == NULL ? 0 : current_frame->f_restricted;
1736+
}
1737+
17361738
void
17371739
flushline()
17381740
{
@@ -2660,6 +2662,8 @@ exec_statement(prog, globals, locals)
26602662
"exec 2nd/3rd args must be dict or None");
26612663
return -1;
26622664
}
2665+
if (dictlookup(globals, "__builtins__") == NULL)
2666+
dictinsert(globals, "__builtins__", current_frame->f_builtins);
26632667
if (is_codeobject(prog)) {
26642668
if (eval_code((codeobject *) prog, globals, locals,
26652669
(object *)NULL, (object *)NULL) == NULL)

Python/import.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3232
#include "import.h"
3333
#include "errcode.h"
3434
#include "sysmodule.h"
35+
#include "bltinmodule.h"
3536
#include "pythonrun.h"
3637
#include "marshal.h"
3738
#include "compile.h"
@@ -147,6 +148,10 @@ exec_code_module(name, co)
147148
if (m == NULL)
148149
return NULL;
149150
d = getmoduledict(m);
151+
if (dictlookup(d, "__builtins__") == NULL) {
152+
if (dictinsert(d, "__builtins__", getbuiltindict()) != 0)
153+
return NULL;
154+
}
150155
v = eval_code((codeobject *)co, d, d, d, (object *)NULL);
151156
if (v == NULL)
152157
return NULL;

Python/pythonrun.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3232
#include "graminit.h"
3333
#include "errcode.h"
3434
#include "sysmodule.h"
35+
#include "bltinmodule.h"
3536
#include "compile.h"
3637
#include "eval.h"
3738
#include "ceval.h"
@@ -48,6 +49,7 @@ extern char *getpythonpath();
4849
extern grammar gram; /* From graminit.c */
4950

5051
/* Forward */
52+
static void initmain PROTO((void));
5153
static object *run_err_node PROTO((node *n, char *filename,
5254
object *globals, object *locals));
5355
static object *run_node PROTO((node *n, char *filename,
@@ -83,6 +85,24 @@ initall()
8385
setpythonpath(getpythonpath());
8486

8587
initsigs(); /* Signal handling stuff, including initintr() */
88+
89+
initmain();
90+
}
91+
92+
/* Create __main__ module */
93+
94+
static void
95+
initmain()
96+
{
97+
object *m, *d;
98+
m = add_module("__main__");
99+
if (m == NULL)
100+
fatal("can't create __main__ module");
101+
d = getmoduledict(m);
102+
if (dictlookup(d, "__builtins__") == NULL) {
103+
if (dictinsert(d, "__builtins__", getbuiltindict()))
104+
fatal("can't add __builtins__ to __main__");
105+
}
86106
}
87107

88108
/* Parse input from a file and execute it */

0 commit comments

Comments
 (0)