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