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

Skip to content

Commit 8d15b5d

Browse files
committed
Added reload() functionality.
1 parent abbda16 commit 8d15b5d

1 file changed

Lines changed: 66 additions & 21 deletions

File tree

Python/import.c

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -65,28 +65,35 @@ new_module(name)
6565
return m;
6666
}
6767

68-
void
68+
static void
69+
use_module(ctx, d)
70+
context *ctx;
71+
object *d;
72+
{
73+
INCREF(d);
74+
DECREF(ctx->ctx_locals);
75+
ctx->ctx_locals = d;
76+
INCREF(d);
77+
DECREF(ctx->ctx_globals);
78+
ctx->ctx_globals = d;
79+
}
80+
81+
static void
6982
define_module(ctx, name)
7083
context *ctx;
7184
char *name;
7285
{
73-
object *m, *d;
86+
object *m;
7487
m = new_module(name);
7588
if (m == NULL) {
7689
puterrno(ctx);
7790
return;
7891
}
79-
d = getmoduledict(m);
80-
INCREF(d);
81-
DECREF(ctx->ctx_locals);
82-
ctx->ctx_locals = d;
83-
INCREF(d);
84-
DECREF(ctx->ctx_globals);
85-
ctx->ctx_globals = d;
92+
use_module(ctx, getmoduledict(m));
8693
DECREF(m);
8794
}
8895

89-
object *
96+
static object *
9097
parsepath(path, delim)
9198
char *path;
9299
int delim;
@@ -179,7 +186,7 @@ load_module(ctx, name)
179186
object *m;
180187
char **p;
181188
FILE *fp;
182-
node *n, *mh;
189+
node *n;
183190
int err;
184191
object *mtab;
185192
object *save_locals, *save_globals;
@@ -190,20 +197,10 @@ load_module(ctx, name)
190197
return NULL;
191198
}
192199
fp = open_module(name, ".py");
193-
if (fp == NULL) {
194-
/* XXX Compatibility hack */
195-
fprintf(stderr,
196-
"Can't find '%s.py' in sys.path, trying '%s'\n",
197-
name, name);
198-
fp = open_module(name, "");
199-
}
200200
if (fp == NULL) {
201201
name_error(ctx, name);
202202
return NULL;
203203
}
204-
#ifdef DEBUG
205-
fprintf(stderr, "Reading module %s from file '%s'\n", name, namebuf);
206-
#endif
207204
err = parseinput(fp, file_input, &n);
208205
fclose(fp);
209206
if (err != E_DONE) {
@@ -250,3 +247,51 @@ import_module(ctx, name)
250247
}
251248
return m;
252249
}
250+
251+
object *
252+
reload_module(ctx, m)
253+
context *ctx;
254+
object *m;
255+
{
256+
char *name;
257+
FILE *fp;
258+
node *n;
259+
int err;
260+
object *d;
261+
object *save_locals, *save_globals;
262+
if (m == NULL || !is_moduleobject(m)) {
263+
type_error(ctx, "reload() argument must be module");
264+
return NULL;
265+
}
266+
/* XXX Ought to check for builtin module */
267+
name = getmodulename(m);
268+
fp = open_module(name, ".py");
269+
if (fp == NULL) {
270+
error(ctx, "reload() cannot find module source file");
271+
return NULL;
272+
}
273+
err = parseinput(fp, file_input, &n);
274+
fclose(fp);
275+
if (err != E_DONE) {
276+
input_error(ctx, err);
277+
return NULL;
278+
}
279+
d = newdictobject();
280+
if (d == NULL)
281+
return NULL;
282+
setmoduledict(m, d);
283+
save_locals = ctx->ctx_locals;
284+
INCREF(save_locals);
285+
save_globals = ctx->ctx_globals;
286+
INCREF(save_globals);
287+
use_module(ctx, d);
288+
exec_node(ctx, n);
289+
DECREF(ctx->ctx_locals);
290+
ctx->ctx_locals = save_locals;
291+
DECREF(ctx->ctx_globals);
292+
ctx->ctx_globals = save_globals;
293+
if (ctx->ctx_exception)
294+
return NULL;
295+
INCREF(None);
296+
return None;
297+
}

0 commit comments

Comments
 (0)