1991-02-19 12:39:46 +00:00
|
|
|
/***********************************************************
|
1995-01-04 19:12:13 +00:00
|
|
|
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
|
The Netherlands.
|
1991-02-19 12:39:46 +00:00
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
1996-10-25 14:44:06 +00: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 12:39:46 +00:00
|
|
|
provided that the above copyright notice appear in all copies and that
|
1996-10-25 14:44:06 +00:00
|
|
|
both that copyright notice and this permission notice appear in
|
1991-02-19 12:39:46 +00:00
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
1996-10-25 14:44:06 +00: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 12:39:46 +00:00
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
1990-10-14 12:07:46 +00:00
|
|
|
/* Module definition and import implementation */
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
#include "Python.h"
|
1990-10-14 12:07:46 +00:00
|
|
|
|
|
|
|
#include "node.h"
|
|
|
|
#include "token.h"
|
|
|
|
#include "errcode.h"
|
1991-06-04 19:39:42 +00:00
|
|
|
#include "marshal.h"
|
|
|
|
#include "compile.h"
|
1992-08-05 19:58:53 +00:00
|
|
|
#include "eval.h"
|
1992-02-26 15:19:13 +00:00
|
|
|
#include "osdefs.h"
|
1995-01-02 19:04:15 +00:00
|
|
|
#include "importdl.h"
|
1995-02-15 22:57:06 +00:00
|
|
|
#ifdef macintosh
|
|
|
|
#include "macglue.h"
|
|
|
|
#endif
|
1991-06-04 19:39:42 +00:00
|
|
|
|
1996-12-05 23:27:02 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
extern long PyOS_GetLastModificationTime(); /* In getmtime.c */
|
1993-10-15 13:01:11 +00:00
|
|
|
|
1995-01-02 19:04:15 +00:00
|
|
|
/* Magic word to reject .pyc files generated by other Python versions */
|
1995-07-07 22:50:36 +00: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-17 21:06:11 +00:00
|
|
|
/* New way to come up with the magic number: (YEAR-1995), MONTH, DAY */
|
1997-01-24 03:44:53 +00:00
|
|
|
#define MAGIC (20121 | ((long)'\r'<<16) | ((long)'\n'<<24))
|
1992-01-19 16:28:21 +00:00
|
|
|
|
1997-08-02 03:10:38 +00:00
|
|
|
/* See _PyImport_FixupExtension() below */
|
|
|
|
static PyObject *extensions = NULL;
|
1994-08-29 12:54:38 +00:00
|
|
|
|
|
|
|
|
1995-01-02 19:04:15 +00:00
|
|
|
/* Initialize things */
|
1994-08-29 12:54:38 +00:00
|
|
|
|
1995-01-02 19:04:15 +00:00
|
|
|
void
|
1997-08-02 03:10:38 +00:00
|
|
|
_PyImport_Init()
|
1995-01-02 19:04:15 +00:00
|
|
|
{
|
1997-03-11 18:37:35 +00:00
|
|
|
if (Py_OptimizeFlag) {
|
|
|
|
/* Replace ".pyc" with ".pyo" in import_filetab */
|
|
|
|
struct filedescr *p;
|
1997-04-29 20:08:16 +00:00
|
|
|
for (p = _PyImport_Filetab; p->suffix != NULL; p++) {
|
1997-03-11 18:37:35 +00:00
|
|
|
if (strcmp(p->suffix, ".pyc") == 0)
|
|
|
|
p->suffix = ".pyo";
|
|
|
|
}
|
|
|
|
}
|
1995-01-02 19:04:15 +00:00
|
|
|
}
|
1994-08-29 12:54:38 +00:00
|
|
|
|
1997-08-02 03:10:38 +00:00
|
|
|
void
|
|
|
|
_PyImport_Fini()
|
|
|
|
{
|
|
|
|
Py_XDECREF(extensions);
|
|
|
|
extensions = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Helper for sys */
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyImport_GetModuleDict()
|
|
|
|
{
|
|
|
|
PyInterpreterState *interp = PyThreadState_Get()->interp;
|
|
|
|
if (interp->modules == NULL)
|
|
|
|
Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
|
|
|
|
return interp->modules;
|
|
|
|
}
|
|
|
|
|
1991-12-16 13:06:34 +00:00
|
|
|
|
1997-08-05 02:20:51 +00:00
|
|
|
/* Helper for PyImport_Cleanup */
|
|
|
|
|
|
|
|
static void
|
|
|
|
clear_carefully(d)
|
|
|
|
PyObject *d;
|
|
|
|
{
|
|
|
|
/* To make the execution order of destructors for global
|
|
|
|
objects a bit more predictable, we first zap all objects
|
|
|
|
whose name starts with a single underscore, before we clear
|
|
|
|
the entire dictionary. We zap them by replacing them with
|
|
|
|
None, rather than deleting them from the dictionary, to
|
|
|
|
avoid rehashing the dictionary (to some extent). */
|
|
|
|
|
|
|
|
int pos;
|
|
|
|
PyObject *key, *value;
|
|
|
|
|
|
|
|
pos = 0;
|
|
|
|
while (PyDict_Next(d, &pos, &key, &value)) {
|
|
|
|
if (value != Py_None && PyString_Check(key)) {
|
|
|
|
char *s = PyString_AsString(key);
|
|
|
|
if (s[0] == '_' && s[1] != '_')
|
|
|
|
PyDict_SetItem(d, key, Py_None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PyDict_Clear(d);
|
|
|
|
}
|
|
|
|
|
1995-01-02 19:04:15 +00:00
|
|
|
/* Un-initialize things, as good as we can */
|
1991-12-16 13:06:34 +00:00
|
|
|
|
1995-01-02 19:04:15 +00:00
|
|
|
void
|
1997-04-29 20:08:16 +00:00
|
|
|
PyImport_Cleanup()
|
1995-01-02 19:04:15 +00:00
|
|
|
{
|
1997-08-02 03:10:38 +00:00
|
|
|
PyInterpreterState *interp = PyThreadState_Get()->interp;
|
|
|
|
PyObject *tmp = interp->modules;
|
|
|
|
if (tmp != NULL) {
|
|
|
|
int pos;
|
|
|
|
PyObject *key, *value;
|
|
|
|
interp->modules = NULL;
|
|
|
|
pos = 0;
|
|
|
|
while (PyDict_Next(tmp, &pos, &key, &value)) {
|
|
|
|
if (PyModule_Check(value)) {
|
|
|
|
PyObject *d = PyModule_GetDict(value);
|
1997-08-05 02:20:51 +00:00
|
|
|
clear_carefully(d);
|
1997-08-02 03:10:38 +00:00
|
|
|
}
|
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
PyDict_Clear(tmp);
|
|
|
|
Py_DECREF(tmp);
|
1995-01-02 19:04:15 +00:00
|
|
|
}
|
|
|
|
}
|
1990-12-20 15:06:42 +00:00
|
|
|
|
1991-04-03 19:03:52 +00:00
|
|
|
|
1995-01-02 19:04:15 +00:00
|
|
|
/* Helper for pythonrun.c -- return magic number */
|
1994-09-14 13:31:04 +00:00
|
|
|
|
|
|
|
long
|
1997-04-29 20:08:16 +00:00
|
|
|
PyImport_GetMagicNumber()
|
1994-09-14 13:31:04 +00:00
|
|
|
{
|
|
|
|
return MAGIC;
|
|
|
|
}
|
|
|
|
|
1990-10-14 12:07:46 +00:00
|
|
|
|
1997-08-02 03:10:38 +00:00
|
|
|
/* Magic for extension modules (built-in as well as dynamically
|
|
|
|
loaded). To prevent initializing an extension module more than
|
|
|
|
once, we keep a static dictionary 'extensions' keyed by module name
|
|
|
|
(for built-in modules) or by filename (for dynamically loaded
|
|
|
|
modules), containing these modules. A copy od the module's
|
|
|
|
dictionary is stored by calling _PyImport_FixupExtension()
|
|
|
|
immediately after the module initialization function succeeds. A
|
|
|
|
copy can be retrieved from there by calling
|
|
|
|
_PyImport_FindExtension(). */
|
1990-10-14 12:07:46 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *
|
1997-08-02 03:10:38 +00:00
|
|
|
_PyImport_FixupExtension(name, filename)
|
|
|
|
char *name;
|
|
|
|
char *filename;
|
|
|
|
{
|
|
|
|
PyObject *modules, *mod, *dict, *copy;
|
|
|
|
if (extensions == NULL) {
|
|
|
|
extensions = PyDict_New();
|
|
|
|
if (extensions == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
modules = PyImport_GetModuleDict();
|
|
|
|
mod = PyDict_GetItemString(modules, name);
|
|
|
|
if (mod == NULL || !PyModule_Check(mod)) {
|
|
|
|
PyErr_SetString(PyExc_SystemError,
|
|
|
|
"_PyImport_FixupExtension: module not loaded");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
dict = PyModule_GetDict(mod);
|
|
|
|
if (dict == NULL)
|
|
|
|
return NULL;
|
|
|
|
copy = PyObject_CallMethod(dict, "copy", "");
|
|
|
|
if (copy == NULL)
|
|
|
|
return NULL;
|
|
|
|
PyDict_SetItemString(extensions, filename, copy);
|
|
|
|
Py_DECREF(copy);
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
_PyImport_FindExtension(name, filename)
|
|
|
|
char *name;
|
|
|
|
char *filename;
|
1990-12-20 15:06:42 +00:00
|
|
|
{
|
1997-08-02 03:10:38 +00:00
|
|
|
PyObject *dict, *mod, *mdict, *result;
|
|
|
|
if (extensions == NULL)
|
|
|
|
return NULL;
|
|
|
|
dict = PyDict_GetItemString(extensions, filename);
|
|
|
|
if (dict == NULL)
|
|
|
|
return NULL;
|
|
|
|
mod = PyImport_AddModule(name);
|
|
|
|
if (mod == NULL)
|
|
|
|
return NULL;
|
|
|
|
mdict = PyModule_GetDict(mod);
|
|
|
|
if (mdict == NULL)
|
|
|
|
return NULL;
|
|
|
|
result = PyObject_CallMethod(mdict, "update", "O", dict);
|
|
|
|
if (result == NULL)
|
|
|
|
return NULL;
|
|
|
|
Py_DECREF(result);
|
|
|
|
if (Py_VerboseFlag)
|
|
|
|
fprintf(stderr, "import %s # previously loaded (%s)\n",
|
|
|
|
name, filename);
|
|
|
|
return mod;
|
1990-12-20 15:06:42 +00:00
|
|
|
}
|
|
|
|
|
1995-01-02 19:04:15 +00: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-20 16:53:12 +00:00
|
|
|
Because the former action is most common, THIS DOES NOT RETURN A
|
|
|
|
'NEW' REFERENCE! */
|
1995-01-02 19:04:15 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *
|
|
|
|
PyImport_AddModule(name)
|
1990-10-14 12:07:46 +00:00
|
|
|
char *name;
|
|
|
|
{
|
1997-08-02 03:10:38 +00:00
|
|
|
PyObject *modules = PyImport_GetModuleDict();
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *m;
|
1995-01-02 19:04:15 +00:00
|
|
|
|
1997-08-02 03:10:38 +00:00
|
|
|
if ((m = PyDict_GetItemString(modules, name)) != NULL &&
|
1997-04-29 20:08:16 +00:00
|
|
|
PyModule_Check(m))
|
1990-12-20 15:06:42 +00:00
|
|
|
return m;
|
1997-04-29 20:08:16 +00:00
|
|
|
m = PyModule_New(name);
|
1990-10-14 12:07:46 +00:00
|
|
|
if (m == NULL)
|
|
|
|
return NULL;
|
1997-08-02 03:10:38 +00:00
|
|
|
if (PyDict_SetItemString(modules, name, m) != 0) {
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_DECREF(m);
|
1990-10-14 12:07:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_DECREF(m); /* Yes, it still exists, in modules! */
|
1995-01-02 19:04:15 +00:00
|
|
|
|
1990-10-14 12:07:46 +00:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
1992-01-19 16:28:21 +00:00
|
|
|
|
1995-01-20 16:53:12 +00:00
|
|
|
/* Execute a code object in a module and return the module object
|
|
|
|
WITH INCREMENTED REFERENCE COUNT */
|
1992-01-19 16:28:21 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *
|
|
|
|
PyImport_ExecCodeModule(name, co)
|
1994-08-29 12:54:38 +00:00
|
|
|
char *name;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *co;
|
1994-08-29 12:54:38 +00:00
|
|
|
{
|
1997-08-02 03:10:38 +00:00
|
|
|
PyObject *modules = PyImport_GetModuleDict();
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *m, *d, *v;
|
1995-01-02 19:04:15 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
m = PyImport_AddModule(name);
|
1995-01-02 19:04:15 +00:00
|
|
|
if (m == NULL)
|
1994-08-29 12:54:38 +00:00
|
|
|
return NULL;
|
1997-04-29 20:08:16 +00:00
|
|
|
d = PyModule_GetDict(m);
|
|
|
|
if (PyDict_GetItemString(d, "__builtins__") == NULL) {
|
|
|
|
if (PyDict_SetItemString(d, "__builtins__",
|
|
|
|
PyEval_GetBuiltins()) != 0)
|
1995-01-09 17:53:26 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1996-05-16 20:43:40 +00:00
|
|
|
/* Remember the filename as the __file__ attribute */
|
1997-04-29 20:08:16 +00:00
|
|
|
if (PyDict_SetItemString(d, "__file__",
|
|
|
|
((PyCodeObject *)co)->co_filename) != 0)
|
|
|
|
PyErr_Clear(); /* Not important enough to report */
|
|
|
|
v = PyEval_EvalCode((PyCodeObject *)co, d, d); /* XXX owner? */
|
1995-01-02 19:04:15 +00:00
|
|
|
if (v == NULL)
|
|
|
|
return NULL;
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_DECREF(v);
|
1997-07-10 18:00:45 +00:00
|
|
|
|
1997-08-02 03:10:38 +00:00
|
|
|
if ((m = PyDict_GetItemString(modules, name)) == NULL) {
|
1997-07-10 18:00:45 +00:00
|
|
|
PyErr_SetString(PyExc_SystemError,
|
|
|
|
"loaded module not found in sys.modules");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_INCREF(m);
|
1995-01-02 19:04:15 +00:00
|
|
|
|
|
|
|
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);
|
1997-03-11 18:37:35 +00:00
|
|
|
strcpy(buf+len, Py_OptimizeFlag ? "o" : "c");
|
1995-01-02 19:04:15 +00:00
|
|
|
|
|
|
|
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;
|
1997-04-29 20:08:16 +00:00
|
|
|
magic = PyMarshal_ReadLongFromFile(fp);
|
1995-01-02 19:04:15 +00:00
|
|
|
if (magic != MAGIC) {
|
1997-04-29 20:08:16 +00:00
|
|
|
if (Py_VerboseFlag)
|
1995-01-02 19:04:15 +00:00
|
|
|
fprintf(stderr, "# %s has bad magic\n", cpathname);
|
|
|
|
fclose(fp);
|
1994-08-29 12:54:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
pyc_mtime = PyMarshal_ReadLongFromFile(fp);
|
1995-01-02 19:04:15 +00:00
|
|
|
if (pyc_mtime != mtime) {
|
1997-04-29 20:08:16 +00:00
|
|
|
if (Py_VerboseFlag)
|
1995-01-02 19:04:15 +00:00
|
|
|
fprintf(stderr, "# %s has bad mtime\n", cpathname);
|
|
|
|
fclose(fp);
|
1994-08-29 12:54:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
if (Py_VerboseFlag)
|
1995-01-02 19:04:15 +00:00
|
|
|
fprintf(stderr, "# %s matches %s\n", cpathname, pathname);
|
|
|
|
return fp;
|
|
|
|
}
|
1994-08-29 12:54:38 +00:00
|
|
|
|
|
|
|
|
1995-01-02 19:04:15 +00:00
|
|
|
/* Read a code object from a file and check it for validity */
|
1994-08-29 12:54:38 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyCodeObject *
|
1995-01-02 19:04:15 +00:00
|
|
|
read_compiled_module(fp)
|
|
|
|
FILE *fp;
|
|
|
|
{
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *co;
|
1994-08-29 12:54:38 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
co = PyMarshal_ReadObjectFromFile(fp);
|
1995-01-02 19:04:15 +00:00
|
|
|
/* Ugly: rd_object() may return NULL with or without error */
|
1997-04-29 20:08:16 +00:00
|
|
|
if (co == NULL || !PyCode_Check(co)) {
|
|
|
|
if (!PyErr_Occurred())
|
|
|
|
PyErr_SetString(PyExc_ImportError,
|
1995-01-02 19:04:15 +00:00
|
|
|
"Non-code object in .pyc file");
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_XDECREF(co);
|
1995-01-02 19:04:15 +00:00
|
|
|
return NULL;
|
1994-09-12 10:39:56 +00:00
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
return (PyCodeObject *)co;
|
1995-01-02 19:04:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Load a module from a compiled file, execute it, and return its
|
1995-01-20 16:53:12 +00:00
|
|
|
module object WITH INCREMENTED REFERENCE COUNT */
|
1995-01-02 19:04:15 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyObject *
|
1995-01-02 19:04:15 +00:00
|
|
|
load_compiled_module(name, cpathname, fp)
|
|
|
|
char *name;
|
|
|
|
char *cpathname;
|
|
|
|
FILE *fp;
|
|
|
|
{
|
|
|
|
long magic;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyCodeObject *co;
|
|
|
|
PyObject *m;
|
1995-01-02 19:04:15 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
magic = PyMarshal_ReadLongFromFile(fp);
|
1995-01-02 19:04:15 +00:00
|
|
|
if (magic != MAGIC) {
|
1997-04-29 20:08:16 +00:00
|
|
|
PyErr_SetString(PyExc_ImportError,
|
|
|
|
"Bad magic number in .pyc file");
|
1994-08-29 12:54:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
(void) PyMarshal_ReadLongFromFile(fp);
|
1995-01-02 19:04:15 +00:00
|
|
|
co = read_compiled_module(fp);
|
|
|
|
if (co == NULL)
|
|
|
|
return NULL;
|
1997-04-29 20:08:16 +00:00
|
|
|
if (Py_VerboseFlag)
|
1995-01-02 19:04:15 +00:00
|
|
|
fprintf(stderr, "import %s # precompiled from %s\n",
|
|
|
|
name, cpathname);
|
1997-04-29 20:08:16 +00:00
|
|
|
m = PyImport_ExecCodeModule(name, (PyObject *)co);
|
|
|
|
Py_DECREF(co);
|
1995-01-02 19:04:15 +00:00
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse a source file and return the corresponding code object */
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyCodeObject *
|
1995-01-02 19:04:15 +00:00
|
|
|
parse_source_module(pathname, fp)
|
|
|
|
char *pathname;
|
|
|
|
FILE *fp;
|
|
|
|
{
|
1997-04-29 20:08:16 +00:00
|
|
|
PyCodeObject *co;
|
1995-01-02 19:04:15 +00:00
|
|
|
node *n;
|
1994-08-29 12:54:38 +00:00
|
|
|
|
1997-05-07 17:46:13 +00:00
|
|
|
n = PyParser_SimpleParseFile(fp, pathname, Py_file_input);
|
1995-01-02 19:04:15 +00:00
|
|
|
if (n == NULL)
|
1994-08-29 12:54:38 +00:00
|
|
|
return NULL;
|
1997-04-29 20:08:16 +00:00
|
|
|
co = PyNode_Compile(n, pathname);
|
|
|
|
PyNode_Free(n);
|
1995-01-02 19:04:15 +00:00
|
|
|
|
|
|
|
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)
|
1997-04-29 20:08:16 +00:00
|
|
|
PyCodeObject *co;
|
1995-01-02 19:04:15 +00:00
|
|
|
char *cpathname;
|
|
|
|
long mtime;
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
fp = fopen(cpathname, "wb");
|
|
|
|
if (fp == NULL) {
|
1997-04-29 20:08:16 +00:00
|
|
|
if (Py_VerboseFlag)
|
1995-01-02 19:04:15 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"# can't create %s\n", cpathname);
|
|
|
|
return;
|
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
PyMarshal_WriteLongToFile(MAGIC, fp);
|
1995-01-02 19:04:15 +00:00
|
|
|
/* First write a 0 for mtime */
|
1997-04-29 20:08:16 +00:00
|
|
|
PyMarshal_WriteLongToFile(0L, fp);
|
|
|
|
PyMarshal_WriteObjectToFile((PyObject *)co, fp);
|
1995-01-02 19:04:15 +00:00
|
|
|
if (ferror(fp)) {
|
1997-04-29 20:08:16 +00:00
|
|
|
if (Py_VerboseFlag)
|
1995-01-02 19:04:15 +00:00
|
|
|
fprintf(stderr, "# can't write %s\n", cpathname);
|
|
|
|
/* Don't keep partial file */
|
|
|
|
fclose(fp);
|
|
|
|
(void) unlink(cpathname);
|
|
|
|
return;
|
1994-08-29 12:54:38 +00:00
|
|
|
}
|
1995-01-02 19:04:15 +00:00
|
|
|
/* Now write the true mtime */
|
|
|
|
fseek(fp, 4L, 0);
|
1997-04-29 20:08:16 +00:00
|
|
|
PyMarshal_WriteLongToFile(mtime, fp);
|
1995-01-02 19:04:15 +00:00
|
|
|
fflush(fp);
|
|
|
|
fclose(fp);
|
1997-04-29 20:08:16 +00:00
|
|
|
if (Py_VerboseFlag)
|
1995-01-02 19:04:15 +00:00
|
|
|
fprintf(stderr, "# wrote %s\n", cpathname);
|
|
|
|
#ifdef macintosh
|
1996-02-21 15:29:20 +00:00
|
|
|
setfiletype(cpathname, 'Pyth', 'PYC ');
|
1995-01-02 19:04:15 +00:00
|
|
|
#endif
|
1994-08-29 12:54:38 +00:00
|
|
|
}
|
1995-01-02 19:04:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Load a source module from a given file and return its module
|
1995-01-20 16:53:12 +00:00
|
|
|
object WITH INCREMENTED REFERENCE COUNT. If there's a matching
|
|
|
|
byte-compiled file, use that instead. */
|
1994-08-29 12:54:38 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyObject *
|
1995-01-02 19:04:15 +00:00
|
|
|
load_source_module(name, pathname, fp)
|
1990-10-14 12:07:46 +00:00
|
|
|
char *name;
|
1995-01-02 19:04:15 +00:00
|
|
|
char *pathname;
|
|
|
|
FILE *fp;
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
1995-01-02 19:04:15 +00:00
|
|
|
long mtime;
|
|
|
|
FILE *fpc;
|
|
|
|
char buf[MAXPATHLEN+1];
|
|
|
|
char *cpathname;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyCodeObject *co;
|
|
|
|
PyObject *m;
|
1995-01-02 19:04:15 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
mtime = PyOS_GetLastModificationTime(pathname);
|
1995-01-02 19:04:15 +00:00
|
|
|
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;
|
1997-04-29 20:08:16 +00:00
|
|
|
if (Py_VerboseFlag)
|
1995-01-02 19:04:15 +00:00
|
|
|
fprintf(stderr, "import %s # precompiled from %s\n",
|
|
|
|
name, cpathname);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
co = parse_source_module(pathname, fp);
|
|
|
|
if (co == NULL)
|
|
|
|
return NULL;
|
1997-04-29 20:08:16 +00:00
|
|
|
if (Py_VerboseFlag)
|
1995-01-02 19:04:15 +00:00
|
|
|
fprintf(stderr, "import %s # from %s\n",
|
|
|
|
name, pathname);
|
|
|
|
write_compiled_module(co, cpathname, mtime);
|
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
m = PyImport_ExecCodeModule(name, (PyObject *)co);
|
|
|
|
Py_DECREF(co);
|
1995-01-02 19:04:15 +00:00
|
|
|
|
|
|
|
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;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *path;
|
1995-01-02 19:04:15 +00:00
|
|
|
/* Output parameters: */
|
|
|
|
char *buf;
|
|
|
|
int buflen;
|
|
|
|
FILE **p_fp;
|
|
|
|
{
|
|
|
|
int i, npath, len, namelen;
|
1996-12-05 23:27:02 +00:00
|
|
|
struct filedescr *fdp = NULL;
|
1995-08-04 04:08:57 +00:00
|
|
|
FILE *fp = NULL;
|
1993-10-15 13:01:11 +00:00
|
|
|
|
1996-08-22 23:10:58 +00:00
|
|
|
#ifdef MS_COREDLL
|
1997-04-11 20:37:35 +00:00
|
|
|
extern FILE *PyWin_FindRegisteredModule();
|
1996-04-09 02:39:59 +00:00
|
|
|
if ((fp=PyWin_FindRegisteredModule(name, &fdp, buf, buflen))!=NULL) {
|
|
|
|
*p_fp = fp;
|
|
|
|
return fdp;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1995-01-02 19:04:15 +00:00
|
|
|
if (path == NULL)
|
1997-04-29 20:08:16 +00:00
|
|
|
path = PySys_GetObject("path");
|
|
|
|
if (path == NULL || !PyList_Check(path)) {
|
|
|
|
PyErr_SetString(PyExc_ImportError,
|
1995-08-04 04:08:57 +00:00
|
|
|
"sys.path must be a list of directory names");
|
1993-11-17 22:58:56 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
npath = PyList_Size(path);
|
1994-09-26 15:47:17 +00:00
|
|
|
namelen = strlen(name);
|
1993-11-17 22:58:56 +00:00
|
|
|
for (i = 0; i < npath; i++) {
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *v = PyList_GetItem(path, i);
|
|
|
|
if (!PyString_Check(v))
|
1993-11-17 22:58:56 +00:00
|
|
|
continue;
|
1997-04-29 20:08:16 +00:00
|
|
|
len = PyString_Size(v);
|
1997-07-21 14:54:36 +00:00
|
|
|
if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen)
|
1994-09-26 15:47:17 +00:00
|
|
|
continue; /* Too long */
|
1997-04-29 20:08:16 +00:00
|
|
|
strcpy(buf, PyString_AsString(v));
|
1997-04-11 20:37:35 +00:00
|
|
|
if ((int)strlen(buf) != len)
|
1994-09-26 15:47:17 +00:00
|
|
|
continue; /* v contains '\0' */
|
1995-02-15 22:57:06 +00:00
|
|
|
#ifdef macintosh
|
|
|
|
if ( PyMac_FindResourceModule(name, buf) ) {
|
1997-04-29 20:08:16 +00:00
|
|
|
static struct filedescr resfiledescr =
|
|
|
|
{"", "", PY_RESOURCE};
|
1995-02-15 22:57:06 +00:00
|
|
|
|
|
|
|
return &resfiledescr;
|
|
|
|
}
|
|
|
|
#endif
|
1995-01-02 19:04:15 +00:00
|
|
|
if (len > 0 && buf[len-1] != SEP)
|
|
|
|
buf[len++] = SEP;
|
1996-05-23 22:51:04 +00: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;
|
1997-04-29 20:08:16 +00:00
|
|
|
char ch; /* limit name to 8 lower-case characters */
|
1996-05-23 22:51:04 +00:00
|
|
|
for (j = 0; (ch = name[j]) && j < 8; j++)
|
|
|
|
if (isupper(ch))
|
|
|
|
buf[len++] = tolower(ch);
|
|
|
|
else
|
|
|
|
buf[len++] = ch;
|
|
|
|
}
|
1996-06-20 14:18:34 +00:00
|
|
|
else /* Not in dos_8x3, use the full name */
|
1996-06-28 20:15:15 +00:00
|
|
|
#endif
|
1996-06-20 14:18:34 +00:00
|
|
|
{
|
1996-05-23 22:51:04 +00:00
|
|
|
strcpy(buf+len, name);
|
|
|
|
len += namelen;
|
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
|
1995-01-02 19:04:15 +00:00
|
|
|
strcpy(buf+len, fdp->suffix);
|
1997-04-29 20:08:16 +00:00
|
|
|
if (Py_VerboseFlag > 1)
|
1995-01-02 19:04:15 +00:00
|
|
|
fprintf(stderr, "# trying %s\n", buf);
|
|
|
|
fp = fopen(buf, fdp->mode);
|
1993-11-17 22:58:56 +00:00
|
|
|
if (fp != NULL)
|
|
|
|
break;
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
1993-11-17 22:58:56 +00:00
|
|
|
if (fp != NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (fp == NULL) {
|
1995-01-02 19:04:15 +00:00
|
|
|
char buf[256];
|
|
|
|
sprintf(buf, "No module named %.200s", name);
|
1997-04-29 20:08:16 +00:00
|
|
|
PyErr_SetString(PyExc_ImportError, buf);
|
1993-11-17 22:58:56 +00:00
|
|
|
return NULL;
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
1995-01-02 19:04:15 +00:00
|
|
|
*p_fp = fp;
|
|
|
|
return fdp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Load an external module using the default search path and return
|
1995-01-20 16:53:12 +00:00
|
|
|
its module object WITH INCREMENTED REFERENCE COUNT */
|
1995-01-02 19:04:15 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyObject *
|
1995-01-02 19:04:15 +00:00
|
|
|
load_module(name)
|
|
|
|
char *name;
|
|
|
|
{
|
|
|
|
char buf[MAXPATHLEN+1];
|
|
|
|
struct filedescr *fdp;
|
|
|
|
FILE *fp = NULL;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *m;
|
1995-01-02 19:04:15 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
fdp = find_module(name, (PyObject *)NULL, buf, MAXPATHLEN+1, &fp);
|
1995-01-02 19:04:15 +00:00
|
|
|
if (fdp == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
1993-11-17 22:58:56 +00:00
|
|
|
switch (fdp->type) {
|
|
|
|
|
|
|
|
case PY_SOURCE:
|
1995-01-02 19:04:15 +00:00
|
|
|
m = load_source_module(name, buf, fp);
|
1993-11-17 22:58:56 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PY_COMPILED:
|
1995-01-02 19:04:15 +00:00
|
|
|
m = load_compiled_module(name, buf, fp);
|
1993-11-17 22:58:56 +00:00
|
|
|
break;
|
1993-10-15 13:01:11 +00:00
|
|
|
|
1993-11-17 22:58:56 +00:00
|
|
|
case C_EXTENSION:
|
1997-04-29 20:08:16 +00:00
|
|
|
m = _PyImport_LoadDynamicModule(name, buf, fp);
|
1995-01-02 19:04:15 +00:00
|
|
|
break;
|
1993-11-17 22:58:56 +00:00
|
|
|
|
1995-02-15 22:57:06 +00:00
|
|
|
#ifdef macintosh
|
|
|
|
case PY_RESOURCE:
|
|
|
|
m = PyMac_LoadResourceModule(name, buf);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
1993-11-17 22:58:56 +00:00
|
|
|
default:
|
1997-04-29 20:08:16 +00:00
|
|
|
PyErr_SetString(PyExc_SystemError,
|
1995-01-02 19:04:15 +00:00
|
|
|
"find_module returned unexpected result");
|
1995-01-20 16:53:12 +00:00
|
|
|
m = NULL;
|
1993-11-17 22:58:56 +00:00
|
|
|
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
1995-02-15 22:57:06 +00:00
|
|
|
if ( fp )
|
|
|
|
fclose(fp);
|
1993-11-17 22:58:56 +00:00
|
|
|
|
1995-01-02 19:04:15 +00: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;
|
|
|
|
{
|
1997-08-02 03:10:38 +00:00
|
|
|
PyInterpreterState *interp = PyThreadState_Get()->interp;
|
|
|
|
struct _inittab *p;
|
|
|
|
PyObject *mod;
|
|
|
|
|
|
|
|
if ((mod = _PyImport_FindExtension(name, name)) != NULL)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
for (p = _PyImport_Inittab; p->name != NULL; p++) {
|
|
|
|
if (strcmp(name, p->name) == 0) {
|
|
|
|
if (p->initfunc == NULL) {
|
1997-04-29 20:08:16 +00:00
|
|
|
PyErr_SetString(PyExc_ImportError,
|
1995-08-04 04:08:57 +00:00
|
|
|
"Cannot re-init internal module");
|
1995-01-02 19:04:15 +00:00
|
|
|
return -1;
|
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
if (Py_VerboseFlag)
|
1997-08-02 03:10:38 +00:00
|
|
|
fprintf(stderr, "import %s # builtin\n", name);
|
|
|
|
(*p->initfunc)();
|
1997-04-29 20:08:16 +00:00
|
|
|
if (PyErr_Occurred())
|
1995-01-02 19:04:15 +00:00
|
|
|
return -1;
|
1997-08-02 03:10:38 +00:00
|
|
|
if (_PyImport_FixupExtension(name, name) == NULL)
|
|
|
|
return -1;
|
1995-01-02 19:04:15 +00:00
|
|
|
return 1;
|
1990-12-20 15:06:42 +00:00
|
|
|
}
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
1995-01-02 19:04:15 +00:00
|
|
|
return 0;
|
1990-12-20 15:06:42 +00:00
|
|
|
}
|
|
|
|
|
1995-01-02 19:04:15 +00:00
|
|
|
|
1995-08-04 04:08:57 +00:00
|
|
|
/* Frozen modules */
|
1995-01-02 19:04:15 +00:00
|
|
|
|
1996-06-17 17:06:34 +00:00
|
|
|
static struct _frozen *
|
1995-08-04 04:08:57 +00:00
|
|
|
find_frozen(name)
|
1990-12-20 15:06:42 +00:00
|
|
|
char *name;
|
|
|
|
{
|
1996-06-17 17:06:34 +00:00
|
|
|
struct _frozen *p;
|
1995-08-04 04:08:57 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
for (p = PyImport_FrozenModules; ; p++) {
|
1995-01-02 19:04:15 +00:00
|
|
|
if (p->name == NULL)
|
1995-08-04 04:08:57 +00:00
|
|
|
return NULL;
|
1995-01-02 19:04:15 +00:00
|
|
|
if (strcmp(p->name, name) == 0)
|
|
|
|
break;
|
|
|
|
}
|
1995-08-04 04:08:57 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyObject *
|
1995-08-04 04:08:57 +00:00
|
|
|
get_frozen_object(name)
|
|
|
|
char *name;
|
|
|
|
{
|
1996-06-17 17:06:34 +00:00
|
|
|
struct _frozen *p = find_frozen(name);
|
1995-08-04 04:08:57 +00:00
|
|
|
|
|
|
|
if (p == NULL) {
|
1997-04-29 20:08:16 +00:00
|
|
|
PyErr_SetString(PyExc_ImportError, "No such frozen object");
|
1995-08-04 04:08:57 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
return PyMarshal_ReadObjectFromString((char *)p->code, p->size);
|
1995-08-04 04:08:57 +00: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
|
1997-04-29 20:08:16 +00:00
|
|
|
PyImport_ImportFrozenModule(name)
|
1995-08-04 04:08:57 +00:00
|
|
|
char *name;
|
|
|
|
{
|
1996-06-17 17:06:34 +00:00
|
|
|
struct _frozen *p = find_frozen(name);
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *co;
|
|
|
|
PyObject *m;
|
1995-08-04 04:08:57 +00:00
|
|
|
|
|
|
|
if (p == NULL)
|
|
|
|
return 0;
|
1997-04-29 20:08:16 +00:00
|
|
|
if (Py_VerboseFlag)
|
1995-01-02 19:04:15 +00:00
|
|
|
fprintf(stderr, "import %s # frozen\n", name);
|
1997-04-29 20:08:16 +00:00
|
|
|
co = PyMarshal_ReadObjectFromString((char *)p->code, p->size);
|
1995-01-02 19:04:15 +00:00
|
|
|
if (co == NULL)
|
|
|
|
return -1;
|
1997-04-29 20:08:16 +00:00
|
|
|
if (!PyCode_Check(co)) {
|
|
|
|
Py_DECREF(co);
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"frozen object is not a code object");
|
1995-01-02 19:04:15 +00:00
|
|
|
return -1;
|
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
m = PyImport_ExecCodeModule(name, co);
|
|
|
|
Py_DECREF(co);
|
1995-01-20 16:53:12 +00:00
|
|
|
if (m == NULL)
|
|
|
|
return -1;
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_DECREF(m);
|
1995-01-20 16:53:12 +00:00
|
|
|
return 1;
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
1995-01-02 19:04:15 +00:00
|
|
|
|
|
|
|
/* Import a module, either built-in, frozen, or external, and return
|
1995-01-20 16:53:12 +00:00
|
|
|
its module object WITH INCREMENTED REFERENCE COUNT */
|
1995-01-02 19:04:15 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *
|
|
|
|
PyImport_ImportModule(name)
|
1990-10-14 12:07:46 +00:00
|
|
|
char *name;
|
|
|
|
{
|
1997-08-02 03:10:38 +00:00
|
|
|
PyObject *modules = PyImport_GetModuleDict();
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *m;
|
1995-01-02 19:04:15 +00:00
|
|
|
|
1997-08-02 03:10:38 +00:00
|
|
|
if ((m = PyDict_GetItemString(modules, name)) != NULL) {
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_INCREF(m);
|
1995-01-20 16:53:12 +00:00
|
|
|
}
|
|
|
|
else {
|
1995-01-02 19:04:15 +00:00
|
|
|
int i;
|
1997-04-29 20:08:16 +00:00
|
|
|
if ((i = init_builtin(name)) ||
|
|
|
|
(i = PyImport_ImportFrozenModule(name))) {
|
1995-01-02 19:04:15 +00:00
|
|
|
if (i < 0)
|
1993-04-01 20:59:32 +00:00
|
|
|
return NULL;
|
1997-08-02 03:10:38 +00:00
|
|
|
if ((m = PyDict_GetItemString(modules,
|
1997-04-29 20:08:16 +00:00
|
|
|
name)) == NULL) {
|
|
|
|
if (PyErr_Occurred() == NULL)
|
|
|
|
PyErr_SetString(PyExc_SystemError,
|
1995-01-02 19:04:15 +00:00
|
|
|
"built-in module not initialized properly");
|
1994-08-29 12:54:38 +00:00
|
|
|
}
|
1995-01-20 16:53:12 +00:00
|
|
|
else
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_INCREF(m);
|
1991-02-19 12:23:57 +00:00
|
|
|
}
|
1995-01-02 19:04:15 +00:00
|
|
|
else
|
1991-02-19 12:23:57 +00:00
|
|
|
m = load_module(name);
|
|
|
|
}
|
1995-01-02 19:04:15 +00:00
|
|
|
|
1990-10-14 12:07:46 +00:00
|
|
|
return m;
|
|
|
|
}
|
1990-10-26 14:58:58 +00:00
|
|
|
|
1995-01-02 19:04:15 +00:00
|
|
|
|
|
|
|
/* Re-import a module of any kind and return its module object, WITH
|
|
|
|
INCREMENTED REFERENCE COUNT */
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *
|
|
|
|
PyImport_ReloadModule(m)
|
|
|
|
PyObject *m;
|
1990-10-26 14:58:58 +00:00
|
|
|
{
|
1997-08-02 03:10:38 +00:00
|
|
|
PyObject *modules = PyImport_GetModuleDict();
|
1993-11-17 22:58:56 +00:00
|
|
|
char *name;
|
1994-08-29 12:54:38 +00:00
|
|
|
int i;
|
1995-01-02 19:04:15 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
if (m == NULL || !PyModule_Check(m)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"reload() argument must be module");
|
1990-10-26 14:58:58 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
name = PyModule_GetName(m);
|
1993-11-17 22:58:56 +00:00
|
|
|
if (name == NULL)
|
|
|
|
return NULL;
|
1997-08-02 03:10:38 +00:00
|
|
|
if (m != PyDict_GetItemString(modules, name)) {
|
1997-04-29 20:08:16 +00:00
|
|
|
PyErr_SetString(PyExc_ImportError,
|
|
|
|
"reload() module not in sys.modules");
|
1995-01-02 19:04:15 +00:00
|
|
|
return NULL;
|
1994-08-29 12:54:38 +00:00
|
|
|
}
|
1995-01-02 19:04:15 +00:00
|
|
|
/* Check for built-in and frozen modules */
|
1997-04-29 20:08:16 +00:00
|
|
|
if ((i = init_builtin(name)) ||
|
|
|
|
(i = PyImport_ImportFrozenModule(name))) {
|
1994-08-29 12:54:38 +00:00
|
|
|
if (i < 0)
|
|
|
|
return NULL;
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_INCREF(m);
|
1994-08-29 12:54:38 +00:00
|
|
|
}
|
1995-01-02 19:04:15 +00:00
|
|
|
else
|
|
|
|
m = load_module(name);
|
|
|
|
return m;
|
1990-12-20 15:06:42 +00:00
|
|
|
}
|
|
|
|
|
1995-01-02 19:04:15 +00:00
|
|
|
|
|
|
|
/* Module 'imp' provides Python access to the primitives used for
|
|
|
|
importing modules.
|
|
|
|
*/
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyObject *
|
1995-01-02 19:04:15 +00:00
|
|
|
imp_get_magic(self, args)
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1990-12-20 15:06:42 +00:00
|
|
|
{
|
1995-01-02 19:04:15 +00:00
|
|
|
char buf[4];
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
if (!PyArg_ParseTuple(args, ""))
|
1995-01-02 19:04:15 +00:00
|
|
|
return NULL;
|
1997-04-11 20:37:35 +00:00
|
|
|
buf[0] = (char) ((MAGIC >> 0) & 0xff);
|
|
|
|
buf[1] = (char) ((MAGIC >> 8) & 0xff);
|
|
|
|
buf[2] = (char) ((MAGIC >> 16) & 0xff);
|
|
|
|
buf[3] = (char) ((MAGIC >> 24) & 0xff);
|
1995-01-02 19:04:15 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
return PyString_FromStringAndSize(buf, 4);
|
1995-01-02 19:04:15 +00:00
|
|
|
}
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyObject *
|
1995-01-02 19:04:15 +00:00
|
|
|
imp_get_suffixes(self, args)
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1995-01-02 19:04:15 +00:00
|
|
|
{
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *list;
|
1995-01-02 19:04:15 +00:00
|
|
|
struct filedescr *fdp;
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
if (!PyArg_ParseTuple(args, ""))
|
1995-01-02 19:04:15 +00:00
|
|
|
return NULL;
|
1997-04-29 20:08:16 +00:00
|
|
|
list = PyList_New(0);
|
1995-01-02 19:04:15 +00:00
|
|
|
if (list == NULL)
|
|
|
|
return NULL;
|
1997-04-29 20:08:16 +00:00
|
|
|
for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
|
|
|
|
PyObject *item = Py_BuildValue("ssi",
|
1995-01-02 19:04:15 +00:00
|
|
|
fdp->suffix, fdp->mode, fdp->type);
|
|
|
|
if (item == NULL) {
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_DECREF(list);
|
1995-01-02 19:04:15 +00:00
|
|
|
return NULL;
|
1990-12-20 15:06:42 +00:00
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
if (PyList_Append(list, item) < 0) {
|
|
|
|
Py_DECREF(list);
|
|
|
|
Py_DECREF(item);
|
1995-01-02 19:04:15 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_DECREF(item);
|
1990-10-26 14:58:58 +00:00
|
|
|
}
|
1995-01-02 19:04:15 +00:00
|
|
|
return list;
|
1990-10-26 14:58:58 +00:00
|
|
|
}
|
1991-02-19 12:23:57 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyObject *
|
1995-01-02 19:04:15 +00:00
|
|
|
imp_find_module(self, args)
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1995-01-02 19:04:15 +00:00
|
|
|
{
|
1997-04-29 20:08:16 +00:00
|
|
|
extern int fclose Py_PROTO((FILE *));
|
1995-01-02 19:04:15 +00:00
|
|
|
char *name;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *path = NULL;
|
|
|
|
PyObject *fob, *ret;
|
1995-01-02 19:04:15 +00:00
|
|
|
struct filedescr *fdp;
|
|
|
|
char pathname[MAXPATHLEN+1];
|
|
|
|
FILE *fp;
|
1997-04-29 20:08:16 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "s|O!", &name, &PyList_Type, &path))
|
1995-01-02 19:04:15 +00:00
|
|
|
return NULL;
|
|
|
|
fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
|
|
|
|
if (fdp == NULL)
|
|
|
|
return NULL;
|
1997-04-29 20:08:16 +00:00
|
|
|
fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
|
1995-01-02 19:04:15 +00:00
|
|
|
if (fob == NULL) {
|
|
|
|
fclose(fp);
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
ret = Py_BuildValue("Os(ssi)",
|
1995-01-02 19:04:15 +00:00
|
|
|
fob, pathname, fdp->suffix, fdp->mode, fdp->type);
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_DECREF(fob);
|
1995-01-02 19:04:15 +00:00
|
|
|
return ret;
|
|
|
|
}
|
1991-02-19 12:23:57 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyObject *
|
1995-01-02 19:04:15 +00:00
|
|
|
imp_init_builtin(self, args)
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1995-01-02 19:04:15 +00:00
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
int ret;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *m;
|
|
|
|
if (!PyArg_ParseTuple(args, "s", &name))
|
1995-01-02 19:04:15 +00:00
|
|
|
return NULL;
|
|
|
|
ret = init_builtin(name);
|
|
|
|
if (ret < 0)
|
|
|
|
return NULL;
|
|
|
|
if (ret == 0) {
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1995-01-02 19:04:15 +00:00
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
m = PyImport_AddModule(name);
|
|
|
|
Py_XINCREF(m);
|
1995-01-02 19:04:15 +00:00
|
|
|
return m;
|
|
|
|
}
|
1991-02-19 12:23:57 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyObject *
|
1995-01-02 19:04:15 +00:00
|
|
|
imp_init_frozen(self, args)
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1995-01-02 19:04:15 +00:00
|
|
|
{
|
1991-02-19 12:23:57 +00:00
|
|
|
char *name;
|
1995-01-02 19:04:15 +00:00
|
|
|
int ret;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *m;
|
|
|
|
if (!PyArg_ParseTuple(args, "s", &name))
|
1995-01-02 19:04:15 +00:00
|
|
|
return NULL;
|
1997-04-29 20:08:16 +00:00
|
|
|
ret = PyImport_ImportFrozenModule(name);
|
1995-01-02 19:04:15 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return NULL;
|
|
|
|
if (ret == 0) {
|
1997-04-29 20:08:16 +00:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1995-01-02 19:04:15 +00:00
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
m = PyImport_AddModule(name);
|
|
|
|
Py_XINCREF(m);
|
1995-01-02 19:04:15 +00:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyObject *
|
1995-08-04 04:08:57 +00:00
|
|
|
imp_get_frozen_object(self, args)
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1995-08-04 04:08:57 +00:00
|
|
|
{
|
|
|
|
char *name;
|
1995-10-03 14:38:41 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "s", &name))
|
1995-08-04 04:08:57 +00:00
|
|
|
return NULL;
|
|
|
|
return get_frozen_object(name);
|
|
|
|
}
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyObject *
|
1995-01-02 19:04:15 +00:00
|
|
|
imp_is_builtin(self, args)
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1991-02-19 12:23:57 +00:00
|
|
|
{
|
|
|
|
int i;
|
1995-01-02 19:04:15 +00:00
|
|
|
char *name;
|
1997-04-29 20:08:16 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "s", &name))
|
1995-01-02 19:04:15 +00:00
|
|
|
return NULL;
|
1997-04-29 20:24:10 +00:00
|
|
|
for (i = 0; _PyImport_Inittab[i].name != NULL; i++) {
|
|
|
|
if (strcmp(name, _PyImport_Inittab[i].name) == 0) {
|
|
|
|
if (_PyImport_Inittab[i].initfunc == NULL)
|
1997-04-29 20:08:16 +00:00
|
|
|
return PyInt_FromLong(-1);
|
1995-01-02 19:04:15 +00:00
|
|
|
else
|
1997-04-29 20:08:16 +00:00
|
|
|
return PyInt_FromLong(1);
|
1991-02-19 12:23:57 +00:00
|
|
|
}
|
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
return PyInt_FromLong(0);
|
1991-02-19 12:23:57 +00:00
|
|
|
}
|
1993-04-01 20:59:32 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyObject *
|
1995-01-02 19:04:15 +00:00
|
|
|
imp_is_frozen(self, args)
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1993-04-01 20:59:32 +00:00
|
|
|
{
|
1996-06-17 17:06:34 +00:00
|
|
|
struct _frozen *p;
|
1995-01-02 19:04:15 +00:00
|
|
|
char *name;
|
1997-04-29 20:08:16 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "s", &name))
|
1995-01-02 19:04:15 +00:00
|
|
|
return NULL;
|
1997-04-29 20:08:16 +00:00
|
|
|
for (p = PyImport_FrozenModules; ; p++) {
|
1993-04-01 20:59:32 +00:00
|
|
|
if (p->name == NULL)
|
|
|
|
break;
|
1995-01-02 19:04:15 +00:00
|
|
|
if (strcmp(p->name, name) == 0)
|
1997-04-29 20:08:16 +00:00
|
|
|
return PyInt_FromLong(1);
|
1993-04-01 20:59:32 +00:00
|
|
|
}
|
1997-04-29 20:08:16 +00:00
|
|
|
return PyInt_FromLong(0);
|
1995-01-02 19:04:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static FILE *
|
|
|
|
get_file(pathname, fob, mode)
|
|
|
|
char *pathname;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *fob;
|
1995-01-02 19:04:15 +00:00
|
|
|
char *mode;
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
if (fob == NULL) {
|
|
|
|
fp = fopen(pathname, mode);
|
|
|
|
if (fp == NULL)
|
1997-04-29 20:08:16 +00:00
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
1993-04-01 20:59:32 +00:00
|
|
|
}
|
1995-01-02 19:04:15 +00:00
|
|
|
else {
|
1997-04-29 20:08:16 +00:00
|
|
|
fp = PyFile_AsFile(fob);
|
1995-01-02 19:04:15 +00:00
|
|
|
if (fp == NULL)
|
1997-04-29 20:08:16 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"bad/closed file object");
|
1995-01-02 19:04:15 +00:00
|
|
|
}
|
|
|
|
return fp;
|
1993-04-01 20:59:32 +00:00
|
|
|
}
|
1994-08-29 12:54:38 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyObject *
|
1995-01-02 19:04:15 +00:00
|
|
|
imp_load_compiled(self, args)
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1995-01-02 19:04:15 +00:00
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
char *pathname;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *fob = NULL;
|
|
|
|
PyObject *m;
|
1995-01-02 19:04:15 +00:00
|
|
|
FILE *fp;
|
1997-04-29 20:08:16 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "ssO!", &name, &pathname,
|
|
|
|
&PyFile_Type, &fob))
|
1995-01-02 19:04:15 +00: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 12:54:38 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyObject *
|
1995-01-02 19:04:15 +00:00
|
|
|
imp_load_dynamic(self, args)
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1995-01-02 19:04:15 +00:00
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
char *pathname;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *fob = NULL;
|
|
|
|
PyObject *m;
|
1995-07-07 22:50:36 +00:00
|
|
|
FILE *fp = NULL;
|
1997-04-29 20:08:16 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "ss|O!", &name, &pathname,
|
|
|
|
&PyFile_Type, &fob))
|
1995-01-02 19:04:15 +00:00
|
|
|
return NULL;
|
1995-07-07 22:50:36 +00:00
|
|
|
if (fob)
|
|
|
|
fp = get_file(pathname, fob, "r");
|
1997-04-29 20:08:16 +00:00
|
|
|
m = _PyImport_LoadDynamicModule(name, pathname, fp);
|
1995-07-07 22:50:36 +00:00
|
|
|
return m;
|
1995-01-02 19:04:15 +00:00
|
|
|
}
|
1994-08-29 12:54:38 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyObject *
|
1995-01-02 19:04:15 +00:00
|
|
|
imp_load_source(self, args)
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1995-01-02 19:04:15 +00:00
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
char *pathname;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *fob = NULL;
|
|
|
|
PyObject *m;
|
1995-01-02 19:04:15 +00:00
|
|
|
FILE *fp;
|
1997-04-29 20:08:16 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "ssO!", &name, &pathname,
|
|
|
|
&PyFile_Type, &fob))
|
1995-01-02 19:04:15 +00: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 12:54:38 +00:00
|
|
|
|
1995-02-15 22:57:06 +00:00
|
|
|
#ifdef macintosh
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyObject *
|
1995-02-15 22:57:06 +00:00
|
|
|
imp_load_resource(self, args)
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1995-02-15 22:57:06 +00:00
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
char *pathname;
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *m;
|
1995-02-15 22:57:06 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "ss", &name, &pathname))
|
1995-02-15 22:57:06 +00:00
|
|
|
return NULL;
|
|
|
|
m = PyMac_LoadResourceModule(name, pathname);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
#endif /* macintosh */
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyObject *
|
1995-01-02 19:04:15 +00:00
|
|
|
imp_new_module(self, args)
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1994-08-29 12:54:38 +00:00
|
|
|
{
|
1995-01-02 19:04:15 +00:00
|
|
|
char *name;
|
1997-04-29 20:08:16 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "s", &name))
|
1995-01-02 19:04:15 +00:00
|
|
|
return NULL;
|
1997-04-29 20:08:16 +00:00
|
|
|
return PyModule_New(name);
|
1995-01-02 19:04:15 +00:00
|
|
|
}
|
1994-08-29 12:54:38 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
static PyMethodDef imp_methods[] = {
|
1995-08-04 04:08:57 +00:00
|
|
|
{"get_frozen_object", imp_get_frozen_object, 1},
|
1995-01-02 19:04:15 +00: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-15 22:57:06 +00:00
|
|
|
#ifdef macintosh
|
|
|
|
{"load_resource", imp_load_resource, 1},
|
|
|
|
#endif
|
1995-01-02 19:04:15 +00:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
1994-08-29 12:54:38 +00:00
|
|
|
|
1995-01-02 19:04:15 +00:00
|
|
|
void
|
|
|
|
initimp()
|
|
|
|
{
|
1997-04-29 20:08:16 +00:00
|
|
|
PyObject *m, *d, *v;
|
1994-08-29 12:54:38 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
m = Py_InitModule("imp", imp_methods);
|
|
|
|
d = PyModule_GetDict(m);
|
1994-08-29 12:54:38 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
v = PyInt_FromLong(SEARCH_ERROR);
|
|
|
|
PyDict_SetItemString(d, "SEARCH_ERROR", v);
|
|
|
|
Py_XDECREF(v);
|
1994-08-29 12:54:38 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
v = PyInt_FromLong(PY_SOURCE);
|
|
|
|
PyDict_SetItemString(d, "PY_SOURCE", v);
|
|
|
|
Py_XDECREF(v);
|
1994-08-29 12:54:38 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
v = PyInt_FromLong(PY_COMPILED);
|
|
|
|
PyDict_SetItemString(d, "PY_COMPILED", v);
|
|
|
|
Py_XDECREF(v);
|
1995-01-02 19:04:15 +00:00
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
v = PyInt_FromLong(C_EXTENSION);
|
|
|
|
PyDict_SetItemString(d, "C_EXTENSION", v);
|
|
|
|
Py_XDECREF(v);
|
1995-01-02 19:04:15 +00:00
|
|
|
|
1995-06-18 20:06:44 +00:00
|
|
|
#ifdef macintosh
|
1997-04-29 20:08:16 +00:00
|
|
|
v = PyInt_FromLong(PY_RESOURCE);
|
|
|
|
PyDict_SetItemString(d, "PY_RESOURCE", v);
|
|
|
|
Py_XDECREF(v);
|
1995-06-18 20:06:44 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1997-04-29 20:08:16 +00:00
|
|
|
if (PyErr_Occurred())
|
|
|
|
Py_FatalError("imp module initialization failed");
|
1995-01-02 19:04:15 +00:00
|
|
|
}
|