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

Skip to content

Commit 0f61f8a

Browse files
committed
Added execfile().
1 parent 465c499 commit 0f61f8a

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

Python/bltinmodule.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,42 @@ builtin_exec(self, v)
186186
return exec_eval(v, file_input);
187187
}
188188

189+
static object *
190+
builtin_execfile(self, v)
191+
object *self;
192+
object *v;
193+
{
194+
object *str = NULL, *globals = NULL, *locals = NULL, *w;
195+
FILE* fp;
196+
int n;
197+
if (v != NULL) {
198+
if (is_stringobject(v))
199+
str = v;
200+
else if (is_tupleobject(v) &&
201+
((n = gettuplesize(v)) == 2 || n == 3)) {
202+
str = gettupleitem(v, 0);
203+
globals = gettupleitem(v, 1);
204+
if (n == 3)
205+
locals = gettupleitem(v, 2);
206+
}
207+
}
208+
if (str == NULL || !is_stringobject(str) ||
209+
globals != NULL && !is_dictobject(globals) ||
210+
locals != NULL && !is_dictobject(locals)) {
211+
err_setstr(TypeError,
212+
"execfile arguments must be filename[,dict[,dict]]");
213+
return NULL;
214+
}
215+
fp = fopen(getstringvalue(str), "r");
216+
if (fp == NULL) {
217+
err_setstr(IOError, "execfile cannot open the file argument");
218+
return NULL;
219+
}
220+
w = run_file(fp, getstringvalue(str), file_input, globals, locals);
221+
fclose(fp);
222+
return w;
223+
}
224+
189225
static object *
190226
builtin_float(self, v)
191227
object *self;
@@ -603,6 +639,7 @@ static struct methodlist builtin_methods[] = {
603639
{"divmod", builtin_divmod},
604640
{"eval", builtin_eval},
605641
{"exec", builtin_exec},
642+
{"execfile", builtin_execfile},
606643
{"float", builtin_float},
607644
{"getattr", builtin_getattr},
608645
{"hex", builtin_hex},

0 commit comments

Comments
 (0)