mirror of
https://github.com/python/cpython.git
synced 2025-10-07 23:51:16 +00:00
Added reload() functionality.
This commit is contained in:
parent
abbda16f58
commit
8d15b5d036
1 changed files with 67 additions and 22 deletions
|
@ -65,28 +65,35 @@ new_module(name)
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
static void
|
||||||
define_module(ctx, name)
|
use_module(ctx, d)
|
||||||
context *ctx;
|
context *ctx;
|
||||||
char *name;
|
object *d;
|
||||||
{
|
{
|
||||||
object *m, *d;
|
|
||||||
m = new_module(name);
|
|
||||||
if (m == NULL) {
|
|
||||||
puterrno(ctx);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
d = getmoduledict(m);
|
|
||||||
INCREF(d);
|
INCREF(d);
|
||||||
DECREF(ctx->ctx_locals);
|
DECREF(ctx->ctx_locals);
|
||||||
ctx->ctx_locals = d;
|
ctx->ctx_locals = d;
|
||||||
INCREF(d);
|
INCREF(d);
|
||||||
DECREF(ctx->ctx_globals);
|
DECREF(ctx->ctx_globals);
|
||||||
ctx->ctx_globals = d;
|
ctx->ctx_globals = d;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
define_module(ctx, name)
|
||||||
|
context *ctx;
|
||||||
|
char *name;
|
||||||
|
{
|
||||||
|
object *m;
|
||||||
|
m = new_module(name);
|
||||||
|
if (m == NULL) {
|
||||||
|
puterrno(ctx);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
use_module(ctx, getmoduledict(m));
|
||||||
DECREF(m);
|
DECREF(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
object *
|
static object *
|
||||||
parsepath(path, delim)
|
parsepath(path, delim)
|
||||||
char *path;
|
char *path;
|
||||||
int delim;
|
int delim;
|
||||||
|
@ -179,7 +186,7 @@ load_module(ctx, name)
|
||||||
object *m;
|
object *m;
|
||||||
char **p;
|
char **p;
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
node *n, *mh;
|
node *n;
|
||||||
int err;
|
int err;
|
||||||
object *mtab;
|
object *mtab;
|
||||||
object *save_locals, *save_globals;
|
object *save_locals, *save_globals;
|
||||||
|
@ -190,20 +197,10 @@ load_module(ctx, name)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
fp = open_module(name, ".py");
|
fp = open_module(name, ".py");
|
||||||
if (fp == NULL) {
|
|
||||||
/* XXX Compatibility hack */
|
|
||||||
fprintf(stderr,
|
|
||||||
"Can't find '%s.py' in sys.path, trying '%s'\n",
|
|
||||||
name, name);
|
|
||||||
fp = open_module(name, "");
|
|
||||||
}
|
|
||||||
if (fp == NULL) {
|
if (fp == NULL) {
|
||||||
name_error(ctx, name);
|
name_error(ctx, name);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
#ifdef DEBUG
|
|
||||||
fprintf(stderr, "Reading module %s from file '%s'\n", name, namebuf);
|
|
||||||
#endif
|
|
||||||
err = parseinput(fp, file_input, &n);
|
err = parseinput(fp, file_input, &n);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
if (err != E_DONE) {
|
if (err != E_DONE) {
|
||||||
|
@ -250,3 +247,51 @@ import_module(ctx, name)
|
||||||
}
|
}
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
object *
|
||||||
|
reload_module(ctx, m)
|
||||||
|
context *ctx;
|
||||||
|
object *m;
|
||||||
|
{
|
||||||
|
char *name;
|
||||||
|
FILE *fp;
|
||||||
|
node *n;
|
||||||
|
int err;
|
||||||
|
object *d;
|
||||||
|
object *save_locals, *save_globals;
|
||||||
|
if (m == NULL || !is_moduleobject(m)) {
|
||||||
|
type_error(ctx, "reload() argument must be module");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
/* XXX Ought to check for builtin module */
|
||||||
|
name = getmodulename(m);
|
||||||
|
fp = open_module(name, ".py");
|
||||||
|
if (fp == NULL) {
|
||||||
|
error(ctx, "reload() cannot find module source file");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
err = parseinput(fp, file_input, &n);
|
||||||
|
fclose(fp);
|
||||||
|
if (err != E_DONE) {
|
||||||
|
input_error(ctx, err);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
d = newdictobject();
|
||||||
|
if (d == NULL)
|
||||||
|
return NULL;
|
||||||
|
setmoduledict(m, d);
|
||||||
|
save_locals = ctx->ctx_locals;
|
||||||
|
INCREF(save_locals);
|
||||||
|
save_globals = ctx->ctx_globals;
|
||||||
|
INCREF(save_globals);
|
||||||
|
use_module(ctx, d);
|
||||||
|
exec_node(ctx, n);
|
||||||
|
DECREF(ctx->ctx_locals);
|
||||||
|
ctx->ctx_locals = save_locals;
|
||||||
|
DECREF(ctx->ctx_globals);
|
||||||
|
ctx->ctx_globals = save_globals;
|
||||||
|
if (ctx->ctx_exception)
|
||||||
|
return NULL;
|
||||||
|
INCREF(None);
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue