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

Skip to content

Commit 24c1374

Browse files
committed
call __import__() with 4 args instead of 1
1 parent becdbec commit 24c1374

2 files changed

Lines changed: 56 additions & 2 deletions

File tree

Python/bltinmodule.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@ builtin___import__(self, args)
4747
object *args;
4848
{
4949
char *name;
50+
object *globals = NULL;
51+
object *locals = NULL;
52+
object *fromlist = NULL;
5053

51-
if (!newgetargs(args, "s:__import__", &name))
54+
if (!newgetargs(args, "s|OOO:__import__",
55+
&name, &globals, &locals, &fromlist))
5256
return NULL;
5357
return import_module(name);
5458
}

Python/ceval.c

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ static int import_from PROTO((object *, object *, object *));
9393
static object *build_class PROTO((object *, object *, object *));
9494
static int access_statement PROTO((object *, object *, frameobject *));
9595
static int exec_statement PROTO((object *, object *, object *));
96+
static object *find_from_args PROTO((frameobject *, int));
9697

9798

9899
/* Pointer to current frame, used to link new frames to */
@@ -1338,7 +1339,19 @@ eval_code(co, globals, locals, owner, arg)
13381339
"__import__ not found");
13391340
break;
13401341
}
1341-
w = mkvalue("(O)", w);
1342+
if (is_methodobject(x)) {
1343+
u = None;
1344+
INCREF(u);
1345+
}
1346+
else {
1347+
u = find_from_args(f, INSTR_OFFSET());
1348+
if (u == NULL) {
1349+
x = u;
1350+
break;
1351+
}
1352+
}
1353+
w = mkvalue("(OOOO)", w, f->f_globals, f->f_locals, u);
1354+
DECREF(u);
13421355
if (w == NULL) {
13431356
x = NULL;
13441357
break;
@@ -1352,6 +1365,7 @@ eval_code(co, globals, locals, owner, arg)
13521365
case IMPORT_FROM:
13531366
w = GETNAMEV(oparg);
13541367
v = TOP();
1368+
fast_2_locals(f);
13551369
err = import_from(f->f_locals, v, w);
13561370
locals_2_fast(f, 0);
13571371
break;
@@ -2711,3 +2725,39 @@ exec_statement(prog, globals, locals)
27112725
DECREF(v);
27122726
return 0;
27132727
}
2728+
2729+
/* Hack for Ken Manheimer */
2730+
static object *
2731+
find_from_args(f, nexti)
2732+
frameobject *f;
2733+
int nexti;
2734+
{
2735+
int opcode;
2736+
int oparg;
2737+
object *list, *name;
2738+
unsigned char *next_instr;
2739+
2740+
next_instr = GETUSTRINGVALUE(f->f_code->co_code) + nexti;
2741+
opcode = (*next_instr++);
2742+
if (opcode != IMPORT_FROM) {
2743+
printf("next opcode: %d\n", opcode);
2744+
INCREF(None);
2745+
return None;
2746+
}
2747+
2748+
list = newlistobject(0);
2749+
if (list == NULL)
2750+
return NULL;
2751+
2752+
do {
2753+
oparg = (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2]);
2754+
name = Getnamev(f, oparg);
2755+
if (addlistitem(list, name) < 0) {
2756+
DECREF(list);
2757+
break;
2758+
}
2759+
opcode = (*next_instr++);
2760+
} while (opcode == IMPORT_FROM);
2761+
2762+
return list;
2763+
}

0 commit comments

Comments
 (0)