1991-02-19 20:39:46 +08:00
|
|
|
/***********************************************************
|
1995-01-05 03:12:13 +08:00
|
|
|
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
|
The Netherlands.
|
1991-02-19 20:39:46 +08:00
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
1996-10-25 22:44:06 +08:00
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
1991-02-19 20:39:46 +08:00
|
|
|
provided that the above copyright notice appear in all copies and that
|
1996-10-25 22:44:06 +08:00
|
|
|
both that copyright notice and this permission notice appear in
|
1991-02-19 20:39:46 +08:00
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
1996-10-25 22:44:06 +08:00
|
|
|
Centrum or CWI or Corporation for National Research Initiatives or
|
|
|
|
CNRI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior
|
|
|
|
permission.
|
|
|
|
|
|
|
|
While CWI is the initial source for this software, a modified version
|
|
|
|
is made available by the Corporation for National Research Initiatives
|
|
|
|
(CNRI) at the Internet address ftp://ftp.python.org.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
|
|
|
|
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
|
|
|
|
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
|
|
|
|
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
PERFORMANCE OF THIS SOFTWARE.
|
1991-02-19 20:39:46 +08:00
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
1990-10-14 20:07:46 +08:00
|
|
|
/* Module definition and import implementation */
|
|
|
|
|
1990-12-20 23:06:42 +08:00
|
|
|
#include "allobjects.h"
|
1990-10-14 20:07:46 +08:00
|
|
|
|
1996-05-24 06:51:04 +08:00
|
|
|
/* XXX Some of the following are duplicate with allobjects.h... */
|
1990-10-14 20:07:46 +08:00
|
|
|
#include "node.h"
|
|
|
|
#include "token.h"
|
|
|
|
#include "graminit.h"
|
|
|
|
#include "import.h"
|
|
|
|
#include "errcode.h"
|
|
|
|
#include "sysmodule.h"
|
1995-01-10 01:53:26 +08:00
|
|
|
#include "bltinmodule.h"
|
1990-12-20 23:06:42 +08:00
|
|
|
#include "pythonrun.h"
|
1991-06-05 04:39:42 +09:00
|
|
|
#include "marshal.h"
|
|
|
|
#include "compile.h"
|
1992-08-06 03:58:53 +08:00
|
|
|
#include "eval.h"
|
1992-02-26 23:19:13 +08:00
|
|
|
#include "osdefs.h"
|
1995-01-03 03:04:15 +08:00
|
|
|
#include "importdl.h"
|
1995-02-16 06:57:06 +08:00
|
|
|
#ifdef macintosh
|
1995-07-28 19:28:14 +08:00
|
|
|
/* 'argument' is a grammar symbol, but also used in some mac header files */
|
|
|
|
#undef argument
|
1995-02-16 06:57:06 +08:00
|
|
|
#include "macglue.h"
|
|
|
|
#endif
|
1991-06-05 04:39:42 +09:00
|
|
|
|
1996-12-06 07:27:02 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
1994-08-29 20:54:38 +08:00
|
|
|
extern long getmtime(); /* In getmtime.c */
|
1993-10-15 21:01:11 +08:00
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
/* Magic word to reject .pyc files generated by other Python versions */
|
1995-07-08 06:50:36 +08:00
|
|
|
/* Change for each incompatible change */
|
|
|
|
/* The value of CR and LF is incorporated so if you ever read or write
|
|
|
|
a .pyc file in text mode the magic number will be wrong; also, the
|
|
|
|
Apple MPW compiler swaps their values, botching string constants */
|
|
|
|
/* XXX Perhaps the magic number should be frozen and a version field
|
|
|
|
added to the .pyc file header? */
|
1997-01-18 05:06:11 +08:00
|
|
|
/* New way to come up with the magic number: (YEAR-1995), MONTH, DAY */
|
|
|
|
#define MAGIC (20117 | ((long)'\r'<<16) | ((long)'\n'<<24))
|
1992-01-20 00:28:21 +08:00
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
object *import_modules; /* This becomes sys.modules */
|
1994-08-29 20:54:38 +08:00
|
|
|
|
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
/* Initialize things */
|
1994-08-29 20:54:38 +08:00
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
void
|
|
|
|
initimport()
|
|
|
|
{
|
|
|
|
if (import_modules != NULL)
|
|
|
|
fatal("duplicate initimport() call");
|
|
|
|
if ((import_modules = newdictobject()) == NULL)
|
|
|
|
fatal("no mem for dictionary of modules");
|
|
|
|
}
|
1994-08-29 20:54:38 +08:00
|
|
|
|
1991-12-16 21:06:34 +08:00
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
/* Un-initialize things, as good as we can */
|
1991-12-16 21:06:34 +08:00
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
void
|
|
|
|
doneimport()
|
|
|
|
{
|
|
|
|
if (import_modules != NULL) {
|
1995-01-26 08:41:28 +08:00
|
|
|
object *tmp = import_modules;
|
|
|
|
import_modules = NULL;
|
|
|
|
/* This deletes all modules from sys.modules.
|
|
|
|
When a module is deallocated, it in turn clears its dictionary,
|
|
|
|
thus hopefully breaking any circular references between modules
|
|
|
|
and between a module's dictionary and its functions.
|
|
|
|
Note that "import" will fail while we are cleaning up.
|
|
|
|
*/
|
|
|
|
mappingclear(tmp);
|
|
|
|
DECREF(tmp);
|
1995-01-03 03:04:15 +08:00
|
|
|
}
|
|
|
|
}
|
1990-12-20 23:06:42 +08:00
|
|
|
|
1991-04-04 03:03:52 +08:00
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
/* Helper for pythonrun.c -- return magic number */
|
1994-09-14 21:31:04 +08:00
|
|
|
|
|
|
|
long
|
|
|
|
get_pyc_magic()
|
|
|
|
{
|
|
|
|
return MAGIC;
|
|
|
|
}
|
|
|
|
|
1990-10-14 20:07:46 +08:00
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
/* Helper for sysmodule.c -- return modules dictionary */
|
1990-10-14 20:07:46 +08:00
|
|
|
|
|
|
|
object *
|
1990-12-20 23:06:42 +08:00
|
|
|
get_modules()
|
|
|
|
{
|
1995-01-03 03:04:15 +08:00
|
|
|
return import_modules;
|
1990-12-20 23:06:42 +08:00
|
|
|
}
|
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
|
|
|
|
/* Get the module object corresponding to a module name.
|
|
|
|
First check the modules dictionary if there's one there,
|
|
|
|
if not, create a new one and insert in in the modules dictionary.
|
1995-01-21 00:53:12 +08:00
|
|
|
Because the former action is most common, THIS DOES NOT RETURN A
|
|
|
|
'NEW' REFERENCE! */
|
1995-01-03 03:04:15 +08:00
|
|
|
|
1990-12-20 23:06:42 +08:00
|
|
|
object *
|
|
|
|
add_module(name)
|
1990-10-14 20:07:46 +08:00
|
|
|
char *name;
|
|
|
|
{
|
|
|
|
object *m;
|
1995-01-03 03:04:15 +08:00
|
|
|
|
1995-01-26 08:41:28 +08:00
|
|
|
if (import_modules == NULL) {
|
|
|
|
err_setstr(SystemError, "sys.modules has been deleted");
|
|
|
|
return NULL;
|
|
|
|
}
|
1995-01-03 03:04:15 +08:00
|
|
|
if ((m = dictlookup(import_modules, name)) != NULL &&
|
|
|
|
is_moduleobject(m))
|
1990-12-20 23:06:42 +08:00
|
|
|
return m;
|
1990-10-14 20:07:46 +08:00
|
|
|
m = newmoduleobject(name);
|
|
|
|
if (m == NULL)
|
|
|
|
return NULL;
|
1995-01-03 03:04:15 +08:00
|
|
|
if (dictinsert(import_modules, name, m) != 0) {
|
1990-10-14 20:07:46 +08:00
|
|
|
DECREF(m);
|
|
|
|
return NULL;
|
|
|
|
}
|
1990-12-20 23:06:42 +08:00
|
|
|
DECREF(m); /* Yes, it still exists, in modules! */
|
1995-01-03 03:04:15 +08:00
|
|
|
|
1990-10-14 20:07:46 +08:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
1992-01-20 00:28:21 +08:00
|
|
|
|
1995-01-21 00:53:12 +08:00
|
|
|
/* Execute a code object in a module and return the module object
|
|
|
|
WITH INCREMENTED REFERENCE COUNT */
|
1992-01-20 00:28:21 +08:00
|
|
|
|
1995-02-16 06:57:06 +08:00
|
|
|
object *
|
1995-01-03 03:04:15 +08:00
|
|
|
exec_code_module(name, co)
|
1994-08-29 20:54:38 +08:00
|
|
|
char *name;
|
1995-02-16 06:57:06 +08:00
|
|
|
object *co;
|
1994-08-29 20:54:38 +08:00
|
|
|
{
|
1995-01-03 03:04:15 +08:00
|
|
|
object *m, *d, *v;
|
|
|
|
|
|
|
|
m = add_module(name);
|
|
|
|
if (m == NULL)
|
1994-08-29 20:54:38 +08:00
|
|
|
return NULL;
|
1995-01-03 03:04:15 +08:00
|
|
|
d = getmoduledict(m);
|
1995-01-10 01:53:26 +08:00
|
|
|
if (dictlookup(d, "__builtins__") == NULL) {
|
1995-01-12 19:37:57 +08:00
|
|
|
if (dictinsert(d, "__builtins__", getbuiltins()) != 0)
|
1995-01-10 01:53:26 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
1996-05-17 04:43:40 +08:00
|
|
|
/* Remember the filename as the __file__ attribute */
|
|
|
|
if (dictinsert(d, "__file__", ((codeobject *)co)->co_filename) != 0)
|
|
|
|
err_clear(); /* Not important enough to report */
|
1995-07-18 22:51:37 +08:00
|
|
|
v = eval_code((codeobject *)co, d, d); /* XXX owner? */
|
1995-01-03 03:04:15 +08:00
|
|
|
if (v == NULL)
|
|
|
|
return NULL;
|
|
|
|
DECREF(v);
|
|
|
|
INCREF(m);
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Given a pathname for a Python source file, fill a buffer with the
|
|
|
|
pathname for the corresponding compiled file. Return the pathname
|
|
|
|
for the compiled file, or NULL if there's no space in the buffer.
|
|
|
|
Doesn't set an exception. */
|
|
|
|
|
|
|
|
static char *
|
|
|
|
make_compiled_pathname(pathname, buf, buflen)
|
|
|
|
char *pathname;
|
|
|
|
char *buf;
|
|
|
|
int buflen;
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = strlen(pathname);
|
|
|
|
if (len+2 > buflen)
|
|
|
|
return NULL;
|
|
|
|
strcpy(buf, pathname);
|
|
|
|
strcpy(buf+len, "c");
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Given a pathname for a Python source file, its time of last
|
|
|
|
modification, and a pathname for a compiled file, check whether the
|
|
|
|
compiled file represents the same version of the source. If so,
|
|
|
|
return a FILE pointer for the compiled file, positioned just after
|
|
|
|
the header; if not, return NULL.
|
|
|
|
Doesn't set an exception. */
|
|
|
|
|
|
|
|
static FILE *
|
|
|
|
check_compiled_module(pathname, mtime, cpathname)
|
|
|
|
char *pathname;
|
|
|
|
long mtime;
|
|
|
|
char *cpathname;
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
long magic;
|
|
|
|
long pyc_mtime;
|
|
|
|
|
|
|
|
fp = fopen(cpathname, "rb");
|
|
|
|
if (fp == NULL)
|
|
|
|
return NULL;
|
|
|
|
magic = rd_long(fp);
|
|
|
|
if (magic != MAGIC) {
|
1994-08-29 20:54:38 +08:00
|
|
|
if (verbose)
|
1995-01-03 03:04:15 +08:00
|
|
|
fprintf(stderr, "# %s has bad magic\n", cpathname);
|
|
|
|
fclose(fp);
|
1994-08-29 20:54:38 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
1995-01-03 03:04:15 +08:00
|
|
|
pyc_mtime = rd_long(fp);
|
|
|
|
if (pyc_mtime != mtime) {
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "# %s has bad mtime\n", cpathname);
|
|
|
|
fclose(fp);
|
1994-08-29 20:54:38 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
1995-01-03 03:04:15 +08:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "# %s matches %s\n", cpathname, pathname);
|
|
|
|
return fp;
|
|
|
|
}
|
1994-08-29 20:54:38 +08:00
|
|
|
|
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
/* Read a code object from a file and check it for validity */
|
1994-08-29 20:54:38 +08:00
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
static codeobject *
|
|
|
|
read_compiled_module(fp)
|
|
|
|
FILE *fp;
|
|
|
|
{
|
|
|
|
object *co;
|
1994-08-29 20:54:38 +08:00
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
co = rd_object(fp);
|
|
|
|
/* Ugly: rd_object() may return NULL with or without error */
|
|
|
|
if (co == NULL || !is_codeobject(co)) {
|
|
|
|
if (!err_occurred())
|
|
|
|
err_setstr(ImportError,
|
|
|
|
"Non-code object in .pyc file");
|
|
|
|
XDECREF(co);
|
|
|
|
return NULL;
|
1994-09-12 18:39:56 +08:00
|
|
|
}
|
1995-01-03 03:04:15 +08:00
|
|
|
return (codeobject *)co;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Load a module from a compiled file, execute it, and return its
|
1995-01-21 00:53:12 +08:00
|
|
|
module object WITH INCREMENTED REFERENCE COUNT */
|
1995-01-03 03:04:15 +08:00
|
|
|
|
|
|
|
static object *
|
|
|
|
load_compiled_module(name, cpathname, fp)
|
|
|
|
char *name;
|
|
|
|
char *cpathname;
|
|
|
|
FILE *fp;
|
|
|
|
{
|
|
|
|
long magic;
|
|
|
|
codeobject *co;
|
|
|
|
object *m;
|
|
|
|
|
|
|
|
magic = rd_long(fp);
|
|
|
|
if (magic != MAGIC) {
|
|
|
|
err_setstr(ImportError, "Bad magic number in .pyc file");
|
1994-08-29 20:54:38 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
1995-01-03 03:04:15 +08:00
|
|
|
(void) rd_long(fp);
|
|
|
|
co = read_compiled_module(fp);
|
|
|
|
if (co == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "import %s # precompiled from %s\n",
|
|
|
|
name, cpathname);
|
1995-02-16 06:57:06 +08:00
|
|
|
m = exec_code_module(name, (object *)co);
|
1995-01-03 03:04:15 +08:00
|
|
|
DECREF(co);
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse a source file and return the corresponding code object */
|
|
|
|
|
|
|
|
static codeobject *
|
|
|
|
parse_source_module(pathname, fp)
|
|
|
|
char *pathname;
|
|
|
|
FILE *fp;
|
|
|
|
{
|
|
|
|
codeobject *co;
|
|
|
|
node *n;
|
1994-08-29 20:54:38 +08:00
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
n = parse_file(fp, pathname, file_input);
|
|
|
|
if (n == NULL)
|
1994-08-29 20:54:38 +08:00
|
|
|
return NULL;
|
1995-01-03 03:04:15 +08:00
|
|
|
co = compile(n, pathname);
|
|
|
|
freetree(n);
|
|
|
|
|
|
|
|
return co;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Write a compiled module to a file, placing the time of last
|
|
|
|
modification of its source into the header.
|
|
|
|
Errors are ignored, if a write error occurs an attempt is made to
|
|
|
|
remove the file. */
|
|
|
|
|
|
|
|
static void
|
|
|
|
write_compiled_module(co, cpathname, mtime)
|
|
|
|
codeobject *co;
|
|
|
|
char *cpathname;
|
|
|
|
long mtime;
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
fp = fopen(cpathname, "wb");
|
|
|
|
if (fp == NULL) {
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr,
|
|
|
|
"# can't create %s\n", cpathname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
wr_long(MAGIC, fp);
|
|
|
|
/* First write a 0 for mtime */
|
|
|
|
wr_long(0L, fp);
|
|
|
|
wr_object((object *)co, fp);
|
|
|
|
if (ferror(fp)) {
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "# can't write %s\n", cpathname);
|
|
|
|
/* Don't keep partial file */
|
|
|
|
fclose(fp);
|
|
|
|
(void) unlink(cpathname);
|
|
|
|
return;
|
1994-08-29 20:54:38 +08:00
|
|
|
}
|
1995-01-03 03:04:15 +08:00
|
|
|
/* Now write the true mtime */
|
|
|
|
fseek(fp, 4L, 0);
|
|
|
|
wr_long(mtime, fp);
|
|
|
|
fflush(fp);
|
|
|
|
fclose(fp);
|
1994-08-29 20:54:38 +08:00
|
|
|
if (verbose)
|
1995-01-03 03:04:15 +08:00
|
|
|
fprintf(stderr, "# wrote %s\n", cpathname);
|
|
|
|
#ifdef macintosh
|
1996-02-21 23:29:20 +08:00
|
|
|
setfiletype(cpathname, 'Pyth', 'PYC ');
|
1995-01-03 03:04:15 +08:00
|
|
|
#endif
|
1994-08-29 20:54:38 +08:00
|
|
|
}
|
1995-01-03 03:04:15 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* Load a source module from a given file and return its module
|
1995-01-21 00:53:12 +08:00
|
|
|
object WITH INCREMENTED REFERENCE COUNT. If there's a matching
|
|
|
|
byte-compiled file, use that instead. */
|
1994-08-29 20:54:38 +08:00
|
|
|
|
1993-11-18 06:58:56 +08:00
|
|
|
static object *
|
1995-01-03 03:04:15 +08:00
|
|
|
load_source_module(name, pathname, fp)
|
1990-10-14 20:07:46 +08:00
|
|
|
char *name;
|
1995-01-03 03:04:15 +08:00
|
|
|
char *pathname;
|
|
|
|
FILE *fp;
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
1995-01-03 03:04:15 +08:00
|
|
|
long mtime;
|
|
|
|
FILE *fpc;
|
|
|
|
char buf[MAXPATHLEN+1];
|
|
|
|
char *cpathname;
|
|
|
|
codeobject *co;
|
|
|
|
object *m;
|
|
|
|
|
|
|
|
mtime = getmtime(pathname);
|
|
|
|
cpathname = make_compiled_pathname(pathname, buf, MAXPATHLEN+1);
|
|
|
|
if (cpathname != NULL &&
|
|
|
|
(fpc = check_compiled_module(pathname, mtime, cpathname))) {
|
|
|
|
co = read_compiled_module(fpc);
|
|
|
|
fclose(fpc);
|
|
|
|
if (co == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "import %s # precompiled from %s\n",
|
|
|
|
name, cpathname);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
co = parse_source_module(pathname, fp);
|
|
|
|
if (co == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "import %s # from %s\n",
|
|
|
|
name, pathname);
|
|
|
|
write_compiled_module(co, cpathname, mtime);
|
|
|
|
}
|
1995-02-16 06:57:06 +08:00
|
|
|
m = exec_code_module(name, (object *)co);
|
1995-01-03 03:04:15 +08:00
|
|
|
DECREF(co);
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Search the path (default sys.path) for a module. Return the
|
|
|
|
corresponding filedescr struct, and (via return arguments) the
|
|
|
|
pathname and an open file. Return NULL if the module is not found. */
|
|
|
|
|
|
|
|
static struct filedescr *
|
|
|
|
find_module(name, path, buf, buflen, p_fp)
|
|
|
|
char *name;
|
|
|
|
object *path;
|
|
|
|
/* Output parameters: */
|
|
|
|
char *buf;
|
|
|
|
int buflen;
|
|
|
|
FILE **p_fp;
|
|
|
|
{
|
|
|
|
int i, npath, len, namelen;
|
1996-12-06 07:27:02 +08:00
|
|
|
struct filedescr *fdp = NULL;
|
1995-08-04 12:08:57 +08:00
|
|
|
FILE *fp = NULL;
|
1993-10-15 21:01:11 +08:00
|
|
|
|
1996-08-23 07:10:58 +08:00
|
|
|
#ifdef MS_COREDLL
|
1996-04-09 10:39:59 +08:00
|
|
|
if ((fp=PyWin_FindRegisteredModule(name, &fdp, buf, buflen))!=NULL) {
|
|
|
|
*p_fp = fp;
|
|
|
|
return fdp;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
if (path == NULL)
|
|
|
|
path = sysget("path");
|
1990-10-14 20:07:46 +08:00
|
|
|
if (path == NULL || !is_listobject(path)) {
|
1993-11-18 06:58:56 +08:00
|
|
|
err_setstr(ImportError,
|
1995-08-04 12:08:57 +08:00
|
|
|
"sys.path must be a list of directory names");
|
1993-11-18 06:58:56 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
npath = getlistsize(path);
|
1994-09-26 23:47:17 +08:00
|
|
|
namelen = strlen(name);
|
1993-11-18 06:58:56 +08:00
|
|
|
for (i = 0; i < npath; i++) {
|
1995-01-03 03:04:15 +08:00
|
|
|
object *v = getlistitem(path, i);
|
1993-11-18 06:58:56 +08:00
|
|
|
if (!is_stringobject(v))
|
|
|
|
continue;
|
|
|
|
len = getstringsize(v);
|
1995-01-03 03:04:15 +08:00
|
|
|
if (len + 2 + namelen + import_maxsuffixsize >= buflen)
|
1994-09-26 23:47:17 +08:00
|
|
|
continue; /* Too long */
|
1995-01-03 03:04:15 +08:00
|
|
|
strcpy(buf, getstringvalue(v));
|
|
|
|
if (strlen(buf) != len)
|
1994-09-26 23:47:17 +08:00
|
|
|
continue; /* v contains '\0' */
|
1995-02-16 06:57:06 +08:00
|
|
|
#ifdef macintosh
|
|
|
|
if ( PyMac_FindResourceModule(name, buf) ) {
|
|
|
|
static struct filedescr resfiledescr = { "", "", PY_RESOURCE};
|
|
|
|
|
|
|
|
return &resfiledescr;
|
|
|
|
}
|
|
|
|
#endif
|
1995-01-03 03:04:15 +08:00
|
|
|
if (len > 0 && buf[len-1] != SEP)
|
|
|
|
buf[len++] = SEP;
|
1996-05-24 06:51:04 +08:00
|
|
|
#ifdef IMPORT_8x3_NAMES
|
|
|
|
/* see if we are searching in directory dos_8x3 */
|
|
|
|
if (len > 7 && !strncmp(buf + len - 8, "dos_8x3", 7)){
|
|
|
|
int j;
|
|
|
|
char ch; /* limit name to eight lower-case characters */
|
|
|
|
for (j = 0; (ch = name[j]) && j < 8; j++)
|
|
|
|
if (isupper(ch))
|
|
|
|
buf[len++] = tolower(ch);
|
|
|
|
else
|
|
|
|
buf[len++] = ch;
|
|
|
|
}
|
1996-06-20 22:18:34 +08:00
|
|
|
else /* Not in dos_8x3, use the full name */
|
1996-06-29 04:15:15 +08:00
|
|
|
#endif
|
1996-06-20 22:18:34 +08:00
|
|
|
{
|
1996-05-24 06:51:04 +08:00
|
|
|
strcpy(buf+len, name);
|
|
|
|
len += namelen;
|
|
|
|
}
|
1995-01-03 03:04:15 +08:00
|
|
|
for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
|
|
|
|
strcpy(buf+len, fdp->suffix);
|
1993-11-18 06:58:56 +08:00
|
|
|
if (verbose > 1)
|
1995-01-03 03:04:15 +08:00
|
|
|
fprintf(stderr, "# trying %s\n", buf);
|
|
|
|
fp = fopen(buf, fdp->mode);
|
1993-11-18 06:58:56 +08:00
|
|
|
if (fp != NULL)
|
|
|
|
break;
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
1993-11-18 06:58:56 +08:00
|
|
|
if (fp != NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (fp == NULL) {
|
1995-01-03 03:04:15 +08:00
|
|
|
char buf[256];
|
|
|
|
sprintf(buf, "No module named %.200s", name);
|
|
|
|
err_setstr(ImportError, buf);
|
1993-11-18 06:58:56 +08:00
|
|
|
return NULL;
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
*p_fp = fp;
|
|
|
|
return fdp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Load an external module using the default search path and return
|
1995-01-21 00:53:12 +08:00
|
|
|
its module object WITH INCREMENTED REFERENCE COUNT */
|
1995-01-03 03:04:15 +08:00
|
|
|
|
|
|
|
static object *
|
|
|
|
load_module(name)
|
|
|
|
char *name;
|
|
|
|
{
|
|
|
|
char buf[MAXPATHLEN+1];
|
|
|
|
struct filedescr *fdp;
|
|
|
|
FILE *fp = NULL;
|
1995-01-21 00:53:12 +08:00
|
|
|
object *m;
|
1995-01-03 03:04:15 +08:00
|
|
|
|
|
|
|
fdp = find_module(name, (object *)NULL, buf, MAXPATHLEN+1, &fp);
|
|
|
|
if (fdp == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
1993-11-18 06:58:56 +08:00
|
|
|
switch (fdp->type) {
|
|
|
|
|
|
|
|
case PY_SOURCE:
|
1995-01-03 03:04:15 +08:00
|
|
|
m = load_source_module(name, buf, fp);
|
1993-11-18 06:58:56 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PY_COMPILED:
|
1995-01-03 03:04:15 +08:00
|
|
|
m = load_compiled_module(name, buf, fp);
|
1993-11-18 06:58:56 +08:00
|
|
|
break;
|
1993-10-15 21:01:11 +08:00
|
|
|
|
1993-11-18 06:58:56 +08:00
|
|
|
case C_EXTENSION:
|
1995-06-12 23:51:34 +08:00
|
|
|
m = load_dynamic_module(name, buf, fp);
|
1995-01-03 03:04:15 +08:00
|
|
|
break;
|
1993-11-18 06:58:56 +08:00
|
|
|
|
1995-02-16 06:57:06 +08:00
|
|
|
#ifdef macintosh
|
|
|
|
case PY_RESOURCE:
|
|
|
|
m = PyMac_LoadResourceModule(name, buf);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
1993-11-18 06:58:56 +08:00
|
|
|
default:
|
|
|
|
err_setstr(SystemError,
|
1995-01-03 03:04:15 +08:00
|
|
|
"find_module returned unexpected result");
|
1995-01-21 00:53:12 +08:00
|
|
|
m = NULL;
|
1993-11-18 06:58:56 +08:00
|
|
|
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
1995-02-16 06:57:06 +08:00
|
|
|
if ( fp )
|
|
|
|
fclose(fp);
|
1993-11-18 06:58:56 +08:00
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Initialize a built-in module.
|
|
|
|
Return 1 for succes, 0 if the module is not found, and -1 with
|
|
|
|
an exception set if the initialization failed. */
|
|
|
|
|
|
|
|
static int
|
|
|
|
init_builtin(name)
|
|
|
|
char *name;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; inittab[i].name != NULL; i++) {
|
|
|
|
if (strcmp(name, inittab[i].name) == 0) {
|
|
|
|
if (inittab[i].initfunc == NULL) {
|
|
|
|
err_setstr(ImportError,
|
1995-08-04 12:08:57 +08:00
|
|
|
"Cannot re-init internal module");
|
1995-01-03 03:04:15 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "import %s # builtin\n",
|
|
|
|
name);
|
|
|
|
(*inittab[i].initfunc)();
|
|
|
|
if (err_occurred())
|
|
|
|
return -1;
|
|
|
|
return 1;
|
1990-12-20 23:06:42 +08:00
|
|
|
}
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
1995-01-03 03:04:15 +08:00
|
|
|
return 0;
|
1990-12-20 23:06:42 +08:00
|
|
|
}
|
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
|
1995-08-04 12:08:57 +08:00
|
|
|
/* Frozen modules */
|
1995-01-03 03:04:15 +08:00
|
|
|
|
1996-06-18 01:06:34 +08:00
|
|
|
static struct _frozen *
|
1995-08-04 12:08:57 +08:00
|
|
|
find_frozen(name)
|
1990-12-20 23:06:42 +08:00
|
|
|
char *name;
|
|
|
|
{
|
1996-06-18 01:06:34 +08:00
|
|
|
struct _frozen *p;
|
1995-08-04 12:08:57 +08:00
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
for (p = frozen_modules; ; p++) {
|
|
|
|
if (p->name == NULL)
|
1995-08-04 12:08:57 +08:00
|
|
|
return NULL;
|
1995-01-03 03:04:15 +08:00
|
|
|
if (strcmp(p->name, name) == 0)
|
|
|
|
break;
|
|
|
|
}
|
1995-08-04 12:08:57 +08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
|
|
|
get_frozen_object(name)
|
|
|
|
char *name;
|
|
|
|
{
|
1996-06-18 01:06:34 +08:00
|
|
|
struct _frozen *p = find_frozen(name);
|
1995-08-04 12:08:57 +08:00
|
|
|
|
|
|
|
if (p == NULL) {
|
|
|
|
err_setstr(ImportError, "No such frozen object");
|
|
|
|
return NULL;
|
|
|
|
}
|
1996-08-09 02:52:59 +08:00
|
|
|
return rds_object((char *)p->code, p->size);
|
1995-08-04 12:08:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize a frozen module.
|
|
|
|
Return 1 for succes, 0 if the module is not found, and -1 with
|
|
|
|
an exception set if the initialization failed.
|
|
|
|
This function is also used from frozenmain.c */
|
|
|
|
|
|
|
|
int
|
|
|
|
init_frozen(name)
|
|
|
|
char *name;
|
|
|
|
{
|
1996-06-18 01:06:34 +08:00
|
|
|
struct _frozen *p = find_frozen(name);
|
1995-08-04 12:08:57 +08:00
|
|
|
object *co;
|
|
|
|
object *m;
|
|
|
|
|
|
|
|
if (p == NULL)
|
|
|
|
return 0;
|
1995-01-03 03:04:15 +08:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "import %s # frozen\n", name);
|
1996-08-09 02:52:59 +08:00
|
|
|
co = rds_object((char *)p->code, p->size);
|
1995-01-03 03:04:15 +08:00
|
|
|
if (co == NULL)
|
|
|
|
return -1;
|
|
|
|
if (!is_codeobject(co)) {
|
|
|
|
DECREF(co);
|
1995-08-04 12:08:57 +08:00
|
|
|
err_setstr(TypeError, "frozen object is not a code object");
|
1995-01-03 03:04:15 +08:00
|
|
|
return -1;
|
|
|
|
}
|
1995-02-16 06:57:06 +08:00
|
|
|
m = exec_code_module(name, co);
|
1995-01-03 03:04:15 +08:00
|
|
|
DECREF(co);
|
1995-01-21 00:53:12 +08:00
|
|
|
if (m == NULL)
|
|
|
|
return -1;
|
|
|
|
DECREF(m);
|
|
|
|
return 1;
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
|
|
|
|
/* Import a module, either built-in, frozen, or external, and return
|
1995-01-21 00:53:12 +08:00
|
|
|
its module object WITH INCREMENTED REFERENCE COUNT */
|
1995-01-03 03:04:15 +08:00
|
|
|
|
1990-10-14 20:07:46 +08:00
|
|
|
object *
|
1990-12-20 23:06:42 +08:00
|
|
|
import_module(name)
|
1990-10-14 20:07:46 +08:00
|
|
|
char *name;
|
|
|
|
{
|
|
|
|
object *m;
|
1995-01-03 03:04:15 +08:00
|
|
|
|
1995-01-26 08:41:28 +08:00
|
|
|
if (import_modules == NULL) {
|
|
|
|
err_setstr(SystemError, "sys.modules has been deleted");
|
|
|
|
return NULL;
|
|
|
|
}
|
1995-01-21 00:53:12 +08:00
|
|
|
if ((m = dictlookup(import_modules, name)) != NULL) {
|
|
|
|
INCREF(m);
|
|
|
|
}
|
|
|
|
else {
|
1995-01-03 03:04:15 +08:00
|
|
|
int i;
|
|
|
|
if ((i = init_builtin(name)) || (i = init_frozen(name))) {
|
|
|
|
if (i < 0)
|
1993-04-02 04:59:32 +08:00
|
|
|
return NULL;
|
1995-01-03 03:04:15 +08:00
|
|
|
if ((m = dictlookup(import_modules, name)) == NULL) {
|
|
|
|
if (err_occurred() == NULL)
|
|
|
|
err_setstr(SystemError,
|
|
|
|
"built-in module not initialized properly");
|
1994-08-29 20:54:38 +08:00
|
|
|
}
|
1995-01-21 00:53:12 +08:00
|
|
|
else
|
|
|
|
INCREF(m);
|
1991-02-19 20:23:57 +08:00
|
|
|
}
|
1995-01-03 03:04:15 +08:00
|
|
|
else
|
1991-02-19 20:23:57 +08:00
|
|
|
m = load_module(name);
|
|
|
|
}
|
1995-01-03 03:04:15 +08:00
|
|
|
|
1990-10-14 20:07:46 +08:00
|
|
|
return m;
|
|
|
|
}
|
1990-10-26 22:58:58 +08:00
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
|
|
|
|
/* Re-import a module of any kind and return its module object, WITH
|
|
|
|
INCREMENTED REFERENCE COUNT */
|
|
|
|
|
1990-10-26 22:58:58 +08:00
|
|
|
object *
|
1990-12-20 23:06:42 +08:00
|
|
|
reload_module(m)
|
1990-10-26 22:58:58 +08:00
|
|
|
object *m;
|
|
|
|
{
|
1993-11-18 06:58:56 +08:00
|
|
|
char *name;
|
1994-08-29 20:54:38 +08:00
|
|
|
int i;
|
1995-01-03 03:04:15 +08:00
|
|
|
|
1990-10-26 22:58:58 +08:00
|
|
|
if (m == NULL || !is_moduleobject(m)) {
|
1990-12-20 23:06:42 +08:00
|
|
|
err_setstr(TypeError, "reload() argument must be module");
|
1990-10-26 22:58:58 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
1993-11-18 06:58:56 +08:00
|
|
|
name = getmodulename(m);
|
|
|
|
if (name == NULL)
|
|
|
|
return NULL;
|
1995-01-26 08:41:28 +08:00
|
|
|
if (import_modules == NULL) {
|
|
|
|
err_setstr(SystemError, "sys.modules has been deleted");
|
|
|
|
return NULL;
|
|
|
|
}
|
1995-01-03 03:04:15 +08:00
|
|
|
if (m != dictlookup(import_modules, name)) {
|
|
|
|
err_setstr(ImportError, "reload() module not in sys.modules");
|
|
|
|
return NULL;
|
1994-08-29 20:54:38 +08:00
|
|
|
}
|
1995-01-03 03:04:15 +08:00
|
|
|
/* Check for built-in and frozen modules */
|
|
|
|
if ((i = init_builtin(name)) || (i = init_frozen(name))) {
|
1994-08-29 20:54:38 +08:00
|
|
|
if (i < 0)
|
|
|
|
return NULL;
|
1995-01-21 00:53:12 +08:00
|
|
|
INCREF(m);
|
1994-08-29 20:54:38 +08:00
|
|
|
}
|
1995-01-03 03:04:15 +08:00
|
|
|
else
|
|
|
|
m = load_module(name);
|
|
|
|
return m;
|
1990-12-20 23:06:42 +08:00
|
|
|
}
|
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
|
|
|
|
/* Module 'imp' provides Python access to the primitives used for
|
|
|
|
importing modules.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static object *
|
|
|
|
imp_get_magic(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
1990-12-20 23:06:42 +08:00
|
|
|
{
|
1995-01-03 03:04:15 +08:00
|
|
|
char buf[4];
|
|
|
|
|
|
|
|
if (!newgetargs(args, ""))
|
|
|
|
return NULL;
|
|
|
|
buf[0] = (MAGIC >> 0) & 0xff;
|
|
|
|
buf[1] = (MAGIC >> 8) & 0xff;
|
1995-01-30 20:53:06 +08:00
|
|
|
buf[2] = (MAGIC >> 16) & 0xff;
|
|
|
|
buf[3] = (MAGIC >> 24) & 0xff;
|
1995-01-03 03:04:15 +08:00
|
|
|
|
|
|
|
return newsizedstringobject(buf, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
|
|
|
imp_get_suffixes(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
object *list;
|
|
|
|
struct filedescr *fdp;
|
|
|
|
|
|
|
|
if (!newgetargs(args, ""))
|
|
|
|
return NULL;
|
|
|
|
list = newlistobject(0);
|
|
|
|
if (list == NULL)
|
|
|
|
return NULL;
|
|
|
|
for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
|
|
|
|
object *item = mkvalue("ssi",
|
|
|
|
fdp->suffix, fdp->mode, fdp->type);
|
|
|
|
if (item == NULL) {
|
|
|
|
DECREF(list);
|
|
|
|
return NULL;
|
1990-12-20 23:06:42 +08:00
|
|
|
}
|
1995-01-03 03:04:15 +08:00
|
|
|
if (addlistitem(list, item) < 0) {
|
|
|
|
DECREF(list);
|
|
|
|
DECREF(item);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
DECREF(item);
|
1990-10-26 22:58:58 +08:00
|
|
|
}
|
1995-01-03 03:04:15 +08:00
|
|
|
return list;
|
1990-10-26 22:58:58 +08:00
|
|
|
}
|
1991-02-19 20:23:57 +08:00
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
static object *
|
|
|
|
imp_find_module(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
extern int fclose PROTO((FILE *));
|
|
|
|
char *name;
|
|
|
|
object *path = NULL;
|
|
|
|
object *fob, *ret;
|
|
|
|
struct filedescr *fdp;
|
|
|
|
char pathname[MAXPATHLEN+1];
|
|
|
|
FILE *fp;
|
|
|
|
if (!newgetargs(args, "s|O!", &name, &Listtype, &path))
|
|
|
|
return NULL;
|
|
|
|
fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
|
|
|
|
if (fdp == NULL)
|
|
|
|
return NULL;
|
|
|
|
fob = newopenfileobject(fp, pathname, fdp->mode, fclose);
|
|
|
|
if (fob == NULL) {
|
|
|
|
fclose(fp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ret = mkvalue("Os(ssi)",
|
|
|
|
fob, pathname, fdp->suffix, fdp->mode, fdp->type);
|
|
|
|
DECREF(fob);
|
|
|
|
return ret;
|
|
|
|
}
|
1991-02-19 20:23:57 +08:00
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
static object *
|
|
|
|
imp_init_builtin(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
int ret;
|
|
|
|
object *m;
|
|
|
|
if (!newgetargs(args, "s", &name))
|
|
|
|
return NULL;
|
|
|
|
ret = init_builtin(name);
|
|
|
|
if (ret < 0)
|
|
|
|
return NULL;
|
|
|
|
if (ret == 0) {
|
|
|
|
INCREF(None);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
m = add_module(name);
|
|
|
|
XINCREF(m);
|
|
|
|
return m;
|
|
|
|
}
|
1991-02-19 20:23:57 +08:00
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
static object *
|
|
|
|
imp_init_frozen(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
1991-02-19 20:23:57 +08:00
|
|
|
char *name;
|
1995-01-03 03:04:15 +08:00
|
|
|
int ret;
|
|
|
|
object *m;
|
|
|
|
if (!newgetargs(args, "s", &name))
|
|
|
|
return NULL;
|
|
|
|
ret = init_frozen(name);
|
|
|
|
if (ret < 0)
|
|
|
|
return NULL;
|
|
|
|
if (ret == 0) {
|
|
|
|
INCREF(None);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
m = add_module(name);
|
|
|
|
XINCREF(m);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
1995-08-04 12:08:57 +08:00
|
|
|
static object *
|
|
|
|
imp_get_frozen_object(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
char *name;
|
1995-10-03 22:38:41 +08:00
|
|
|
|
1995-08-04 12:08:57 +08:00
|
|
|
if (!newgetargs(args, "s", &name))
|
|
|
|
return NULL;
|
|
|
|
return get_frozen_object(name);
|
|
|
|
}
|
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
static object *
|
|
|
|
imp_is_builtin(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
1991-02-19 20:23:57 +08:00
|
|
|
{
|
|
|
|
int i;
|
1995-01-03 03:04:15 +08:00
|
|
|
char *name;
|
|
|
|
if (!newgetargs(args, "s", &name))
|
|
|
|
return NULL;
|
1991-02-19 20:23:57 +08:00
|
|
|
for (i = 0; inittab[i].name != NULL; i++) {
|
|
|
|
if (strcmp(name, inittab[i].name) == 0) {
|
1995-01-03 03:04:15 +08:00
|
|
|
if (inittab[i].initfunc == NULL)
|
|
|
|
return newintobject(-1);
|
|
|
|
else
|
|
|
|
return newintobject(1);
|
1991-02-19 20:23:57 +08:00
|
|
|
}
|
|
|
|
}
|
1995-01-03 03:04:15 +08:00
|
|
|
return newintobject(0);
|
1991-02-19 20:23:57 +08:00
|
|
|
}
|
1993-04-02 04:59:32 +08:00
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
static object *
|
|
|
|
imp_is_frozen(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
1993-04-02 04:59:32 +08:00
|
|
|
{
|
1996-06-18 01:06:34 +08:00
|
|
|
struct _frozen *p;
|
1995-01-03 03:04:15 +08:00
|
|
|
char *name;
|
|
|
|
if (!newgetargs(args, "s", &name))
|
|
|
|
return NULL;
|
1993-04-02 04:59:32 +08:00
|
|
|
for (p = frozen_modules; ; p++) {
|
|
|
|
if (p->name == NULL)
|
|
|
|
break;
|
1995-01-03 03:04:15 +08:00
|
|
|
if (strcmp(p->name, name) == 0)
|
|
|
|
return newintobject(1);
|
1993-04-02 04:59:32 +08:00
|
|
|
}
|
1995-01-03 03:04:15 +08:00
|
|
|
return newintobject(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static FILE *
|
|
|
|
get_file(pathname, fob, mode)
|
|
|
|
char *pathname;
|
|
|
|
object *fob;
|
|
|
|
char *mode;
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
if (fob == NULL) {
|
|
|
|
fp = fopen(pathname, mode);
|
|
|
|
if (fp == NULL)
|
|
|
|
err_errno(IOError);
|
1993-04-02 04:59:32 +08:00
|
|
|
}
|
1995-01-03 03:04:15 +08:00
|
|
|
else {
|
|
|
|
fp = getfilefile(fob);
|
|
|
|
if (fp == NULL)
|
|
|
|
err_setstr(ValueError, "bad/closed file object");
|
|
|
|
}
|
|
|
|
return fp;
|
1993-04-02 04:59:32 +08:00
|
|
|
}
|
1994-08-29 20:54:38 +08:00
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
static object *
|
|
|
|
imp_load_compiled(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
char *pathname;
|
|
|
|
object *fob = NULL;
|
|
|
|
object *m;
|
|
|
|
FILE *fp;
|
1995-07-08 06:50:36 +08:00
|
|
|
if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob))
|
1995-01-03 03:04:15 +08:00
|
|
|
return NULL;
|
|
|
|
fp = get_file(pathname, fob, "rb");
|
|
|
|
if (fp == NULL)
|
|
|
|
return NULL;
|
|
|
|
m = load_compiled_module(name, pathname, fp);
|
|
|
|
return m;
|
|
|
|
}
|
1994-08-29 20:54:38 +08:00
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
static object *
|
|
|
|
imp_load_dynamic(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
char *pathname;
|
1995-07-08 06:50:36 +08:00
|
|
|
object *fob = NULL;
|
|
|
|
object *m;
|
|
|
|
FILE *fp = NULL;
|
|
|
|
if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob))
|
1995-01-03 03:04:15 +08:00
|
|
|
return NULL;
|
1995-07-08 06:50:36 +08:00
|
|
|
if (fob)
|
|
|
|
fp = get_file(pathname, fob, "r");
|
|
|
|
m = load_dynamic_module(name, pathname, fp);
|
|
|
|
return m;
|
1995-01-03 03:04:15 +08:00
|
|
|
}
|
1994-08-29 20:54:38 +08:00
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
static object *
|
|
|
|
imp_load_source(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
char *pathname;
|
|
|
|
object *fob = NULL;
|
|
|
|
object *m;
|
|
|
|
FILE *fp;
|
1995-07-08 06:50:36 +08:00
|
|
|
if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob))
|
1995-01-03 03:04:15 +08:00
|
|
|
return NULL;
|
|
|
|
fp = get_file(pathname, fob, "r");
|
|
|
|
if (fp == NULL)
|
|
|
|
return NULL;
|
|
|
|
m = load_source_module(name, pathname, fp);
|
|
|
|
return m;
|
|
|
|
}
|
1994-08-29 20:54:38 +08:00
|
|
|
|
1995-02-16 06:57:06 +08:00
|
|
|
#ifdef macintosh
|
|
|
|
static object *
|
|
|
|
imp_load_resource(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
char *pathname;
|
|
|
|
object *m;
|
|
|
|
|
|
|
|
if (!newgetargs(args, "ss", &name, &pathname))
|
|
|
|
return NULL;
|
|
|
|
m = PyMac_LoadResourceModule(name, pathname);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
#endif /* macintosh */
|
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
static object *
|
|
|
|
imp_new_module(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
1994-08-29 20:54:38 +08:00
|
|
|
{
|
1995-01-03 03:04:15 +08:00
|
|
|
char *name;
|
|
|
|
if (!newgetargs(args, "s", &name))
|
|
|
|
return NULL;
|
|
|
|
return newmoduleobject(name);
|
|
|
|
}
|
1994-08-29 20:54:38 +08:00
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
static struct methodlist imp_methods[] = {
|
1995-08-04 12:08:57 +08:00
|
|
|
{"get_frozen_object", imp_get_frozen_object, 1},
|
1995-01-03 03:04:15 +08:00
|
|
|
{"get_magic", imp_get_magic, 1},
|
|
|
|
{"get_suffixes", imp_get_suffixes, 1},
|
|
|
|
{"find_module", imp_find_module, 1},
|
|
|
|
{"init_builtin", imp_init_builtin, 1},
|
|
|
|
{"init_frozen", imp_init_frozen, 1},
|
|
|
|
{"is_builtin", imp_is_builtin, 1},
|
|
|
|
{"is_frozen", imp_is_frozen, 1},
|
|
|
|
{"load_compiled", imp_load_compiled, 1},
|
|
|
|
{"load_dynamic", imp_load_dynamic, 1},
|
|
|
|
{"load_source", imp_load_source, 1},
|
|
|
|
{"new_module", imp_new_module, 1},
|
1995-02-16 06:57:06 +08:00
|
|
|
#ifdef macintosh
|
|
|
|
{"load_resource", imp_load_resource, 1},
|
|
|
|
#endif
|
1995-01-03 03:04:15 +08:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
1994-08-29 20:54:38 +08:00
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
void
|
|
|
|
initimp()
|
|
|
|
{
|
|
|
|
object *m, *d, *v;
|
1994-08-29 20:54:38 +08:00
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
m = initmodule("imp", imp_methods);
|
|
|
|
d = getmoduledict(m);
|
1994-08-29 20:54:38 +08:00
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
v = newintobject(SEARCH_ERROR);
|
|
|
|
dictinsert(d, "SEARCH_ERROR", v);
|
|
|
|
XDECREF(v);
|
1994-08-29 20:54:38 +08:00
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
v = newintobject(PY_SOURCE);
|
|
|
|
dictinsert(d, "PY_SOURCE", v);
|
|
|
|
XDECREF(v);
|
1994-08-29 20:54:38 +08:00
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
v = newintobject(PY_COMPILED);
|
|
|
|
dictinsert(d, "PY_COMPILED", v);
|
|
|
|
XDECREF(v);
|
|
|
|
|
|
|
|
v = newintobject(C_EXTENSION);
|
|
|
|
dictinsert(d, "C_EXTENSION", v);
|
|
|
|
XDECREF(v);
|
|
|
|
|
1995-06-19 04:06:44 +08:00
|
|
|
#ifdef macintosh
|
|
|
|
v = newintobject(PY_RESOURCE);
|
|
|
|
dictinsert(d, "PY_RESOURCE", v);
|
|
|
|
XDECREF(v);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1995-01-03 03:04:15 +08:00
|
|
|
if (err_occurred())
|
|
|
|
fatal("imp module initialization failed");
|
|
|
|
}
|