diff options
author | Guido van Rossum <guido@python.org> | 1990-10-26 14:58:58 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1990-10-26 14:58:58 (GMT) |
commit | 8d15b5d036af79b26e98f6c909fd0de97369291f (patch) | |
tree | f9a58a973ae6b421851312094620daedeb885658 | |
parent | abbda16f5834c314c85ff495af277de744dc5587 (diff) | |
download | cpython-8d15b5d036af79b26e98f6c909fd0de97369291f.zip cpython-8d15b5d036af79b26e98f6c909fd0de97369291f.tar.gz cpython-8d15b5d036af79b26e98f6c909fd0de97369291f.tar.bz2 |
Added reload() functionality.
-rw-r--r-- | Python/import.c | 87 |
1 files changed, 66 insertions, 21 deletions
diff --git a/Python/import.c b/Python/import.c index 8e94170..d6d941f 100644 --- a/Python/import.c +++ b/Python/import.c @@ -65,28 +65,35 @@ new_module(name) return m; } -void +static void +use_module(ctx, d) + context *ctx; + object *d; +{ + INCREF(d); + DECREF(ctx->ctx_locals); + ctx->ctx_locals = d; + INCREF(d); + DECREF(ctx->ctx_globals); + ctx->ctx_globals = d; +} + +static void define_module(ctx, name) context *ctx; char *name; { - object *m, *d; + object *m; m = new_module(name); if (m == NULL) { puterrno(ctx); return; } - d = getmoduledict(m); - INCREF(d); - DECREF(ctx->ctx_locals); - ctx->ctx_locals = d; - INCREF(d); - DECREF(ctx->ctx_globals); - ctx->ctx_globals = d; + use_module(ctx, getmoduledict(m)); DECREF(m); } -object * +static object * parsepath(path, delim) char *path; int delim; @@ -179,7 +186,7 @@ load_module(ctx, name) object *m; char **p; FILE *fp; - node *n, *mh; + node *n; int err; object *mtab; object *save_locals, *save_globals; @@ -191,19 +198,9 @@ load_module(ctx, name) } 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) { name_error(ctx, name); return NULL; } -#ifdef DEBUG - fprintf(stderr, "Reading module %s from file '%s'\n", name, namebuf); -#endif err = parseinput(fp, file_input, &n); fclose(fp); if (err != E_DONE) { @@ -250,3 +247,51 @@ import_module(ctx, name) } 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; +} |