📄 import.c
字号:
/* NOTREACHED */
}
static PyObject *
import_submodule(PyObject *mod, char *subname, char *fullname)
{
PyObject *modules = PyImport_GetModuleDict();
PyObject *m, *res = NULL;
/* Require:
if mod == None: subname == fullname
else: mod.__name__ + "." + subname == fullname
*/
if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Py_INCREF(m);
}
else {
PyObject *path;
char buf[MAXPATHLEN+1];
struct filedescr *fdp;
FILE *fp = NULL;
if (mod == Py_None)
path = NULL;
else {
path = PyObject_GetAttrString(mod, "__path__");
if (path == NULL) {
PyErr_Clear();
Py_INCREF(Py_None);
return Py_None;
}
}
buf[0] = '\0';
fdp = find_module(subname, path, buf, MAXPATHLEN+1, &fp);
Py_XDECREF(path);
if (fdp == NULL) {
if (!PyErr_ExceptionMatches(PyExc_ImportError))
return NULL;
PyErr_Clear();
Py_INCREF(Py_None);
return Py_None;
}
m = load_module(fullname, fp, buf, fdp->type);
if (fp)
fclose(fp);
if (mod != Py_None) {
/* Irrespective of the success of this load, make a
reference to it in the parent package module.
A copy gets saved in the modules dictionary
under the full name, so get a reference from
there, if need be. (The exception is when
the load failed with a SyntaxError -- then
there's no trace in sys.modules. In that case,
of course, do nothing extra.) */
res = m;
if (res == NULL)
res = PyDict_GetItemString(modules, fullname);
if (res != NULL &&
PyObject_SetAttrString(mod, subname, res) < 0) {
Py_XDECREF(m);
m = NULL;
}
}
}
return m;
}
/* Re-import a module of any kind and return its module object, WITH
INCREMENTED REFERENCE COUNT */
DL_EXPORT(PyObject *)
PyImport_ReloadModule(PyObject *m)
{
PyObject *modules = PyImport_GetModuleDict();
PyObject *path = NULL;
char *name, *subname;
char buf[MAXPATHLEN+1];
struct filedescr *fdp;
FILE *fp = NULL;
if (m == NULL || !PyModule_Check(m)) {
PyErr_SetString(PyExc_TypeError,
"reload() argument must be module");
return NULL;
}
name = PyModule_GetName(m);
if (name == NULL)
return NULL;
if (m != PyDict_GetItemString(modules, name)) {
PyErr_Format(PyExc_ImportError,
"reload(): module %.200s not in sys.modules",
name);
return NULL;
}
subname = strrchr(name, '.');
if (subname == NULL)
subname = name;
else {
PyObject *parentname, *parent;
parentname = PyString_FromStringAndSize(name, (subname-name));
if (parentname == NULL)
return NULL;
parent = PyDict_GetItem(modules, parentname);
Py_DECREF(parentname);
if (parent == NULL) {
PyErr_Format(PyExc_ImportError,
"reload(): parent %.200s not in sys.modules",
name);
return NULL;
}
subname++;
path = PyObject_GetAttrString(parent, "__path__");
if (path == NULL)
PyErr_Clear();
}
buf[0] = '\0';
fdp = find_module(subname, path, buf, MAXPATHLEN+1, &fp);
Py_XDECREF(path);
if (fdp == NULL)
return NULL;
m = load_module(name, fp, buf, fdp->type);
if (fp)
fclose(fp);
return m;
}
/* Higher-level import emulator which emulates the "import" statement
more accurately -- it invokes the __import__() function from the
builtins of the current globals. This means that the import is
done using whatever import hooks are installed in the current
environment, e.g. by "rexec".
A dummy list ["__doc__"] is passed as the 4th argument so that
e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
will return <module "gencache"> instead of <module "win32com">. */
DL_EXPORT(PyObject *)
PyImport_Import(PyObject *module_name)
{
#ifndef SYMBIAN
static PyObject *silly_list = NULL;
static PyObject *builtins_str = NULL;
static PyObject *import_str = NULL;
#else
#define silly_list (pyglobals->imp_silly_list)
#define builtins_str (pyglobals->imp_builtins_str)
#define import_str (pyglobals->imp_import_str)
#endif
PyObject *globals = NULL;
PyObject *import = NULL;
PyObject *builtins = NULL;
PyObject *r = NULL;
#ifdef SYMBIAN
SPy_Python_globals* pyglobals = PYTHON_GLOBALS; // avoid TLS calls
#endif
/* Initialize constant string objects */
if (silly_list == NULL) {
import_str = PyString_InternFromString("__import__");
if (import_str == NULL)
return NULL;
builtins_str = PyString_InternFromString("__builtins__");
if (builtins_str == NULL)
return NULL;
silly_list = Py_BuildValue("[s]", "__doc__");
if (silly_list == NULL)
return NULL;
}
/* Get the builtins from current globals */
globals = PyEval_GetGlobals();
if (globals != NULL) {
Py_INCREF(globals);
builtins = PyObject_GetItem(globals, builtins_str);
if (builtins == NULL)
goto err;
}
else {
/* No globals -- use standard builtins, and fake globals */
PyErr_Clear();
builtins = PyImport_ImportModuleEx("__builtin__",
NULL, NULL, NULL);
if (builtins == NULL)
return NULL;
globals = Py_BuildValue("{OO}", builtins_str, builtins);
if (globals == NULL)
goto err;
}
/* Get the __import__ function from the builtins */
if (PyDict_Check(builtins)) {
import = PyObject_GetItem(builtins, import_str);
if (import == NULL)
PyErr_SetObject(PyExc_KeyError, import_str);
}
else
import = PyObject_GetAttr(builtins, import_str);
if (import == NULL)
goto err;
/* Call the _import__ function with the proper argument list */
r = PyObject_CallFunction(import, "OOOO",
module_name, globals, globals, silly_list);
err:
Py_XDECREF(globals);
Py_XDECREF(builtins);
Py_XDECREF(import);
return r;
}
/* Module 'imp' provides Python access to the primitives used for
importing modules.
*/
static PyObject *
imp_get_magic(PyObject *self, PyObject *args)
{
char buf[4];
if (!PyArg_ParseTuple(args, ":get_magic"))
return NULL;
buf[0] = (char) ((pyc_magic >> 0) & 0xff);
buf[1] = (char) ((pyc_magic >> 8) & 0xff);
buf[2] = (char) ((pyc_magic >> 16) & 0xff);
buf[3] = (char) ((pyc_magic >> 24) & 0xff);
return PyString_FromStringAndSize(buf, 4);
}
static PyObject *
imp_get_suffixes(PyObject *self, PyObject *args)
{
PyObject *list;
struct filedescr *fdp;
if (!PyArg_ParseTuple(args, ":get_suffixes"))
return NULL;
list = PyList_New(0);
if (list == NULL)
return NULL;
for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
PyObject *item = Py_BuildValue("ssi",
fdp->suffix, fdp->mode, fdp->type);
if (item == NULL) {
Py_DECREF(list);
return NULL;
}
if (PyList_Append(list, item) < 0) {
Py_DECREF(list);
Py_DECREF(item);
return NULL;
}
Py_DECREF(item);
}
return list;
}
static PyObject *
call_find_module(char *name, PyObject *path)
{
extern int fclose(FILE *);
PyObject *fob, *ret;
struct filedescr *fdp;
char pathname[MAXPATHLEN+1];
FILE *fp = NULL;
pathname[0] = '\0';
if (path == Py_None)
path = NULL;
fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
if (fdp == NULL)
return NULL;
if (fp != NULL) {
fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
if (fob == NULL) {
fclose(fp);
return NULL;
}
}
else {
fob = Py_None;
Py_INCREF(fob);
}
ret = Py_BuildValue("Os(ssi)",
fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Py_DECREF(fob);
return ret;
}
static PyObject *
imp_find_module(PyObject *self, PyObject *args)
{
char *name;
PyObject *path = NULL;
if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
return NULL;
return call_find_module(name, path);
}
static PyObject *
imp_init_builtin(PyObject *self, PyObject *args)
{
char *name;
int ret;
PyObject *m;
if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
return NULL;
ret = init_builtin(name);
if (ret < 0)
return NULL;
if (ret == 0) {
Py_INCREF(Py_None);
return Py_None;
}
m = PyImport_AddModule(name);
Py_XINCREF(m);
return m;
}
static PyObject *
imp_init_frozen(PyObject *self, PyObject *args)
{
char *name;
int ret;
PyObject *m;
if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
return NULL;
ret = PyImport_ImportFrozenModule(name);
if (ret < 0)
return NULL;
if (ret == 0) {
Py_INCREF(Py_None);
return Py_None;
}
m = PyImport_AddModule(name);
Py_XINCREF(m);
return m;
}
static PyObject *
imp_get_frozen_object(PyObject *self, PyObject *args)
{
char *name;
if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
return NULL;
return get_frozen_object(name);
}
static PyObject *
imp_is_builtin(PyObject *self, PyObject *args)
{
char *name;
if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
return NULL;
return PyInt_FromLong(is_builtin(name));
}
static PyObject *
imp_is_frozen(PyObject *self, PyObject *args)
{
char *name;
struct _frozen *p;
if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
return NULL;
p = find_frozen(name);
return PyInt_FromLong((long) (p == NULL ? 0 : p->size));
}
static FILE *
get_file(char *pathname, PyObject *fob, char *mode)
{
FILE *fp;
if (fob == NULL) {
fp = fopen(pathname, mode);
if (fp == NULL)
PyErr_SetFromErrno(PyExc_IOError);
}
else {
fp = PyFile_AsFile(fob);
if (fp == NULL)
PyErr_SetString(PyExc_ValueError,
"bad/closed file object");
}
return fp;
}
static PyObject *
imp_load_compiled(PyObject *self, PyObject *args)
{
char *name;
char *pathname;
PyObject *fob = NULL;
PyObject *m;
FILE *fp;
if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
&PyFile_Type, &fob))
return NULL;
fp = get_file(pathname, fob, "rb");
if (fp == NULL)
return NULL;
m = load_compiled_module(name, pathname, fp);
if (fob == NULL)
fclose(fp);
return m;
}
#ifdef HAVE_DYNAMIC_LOADING
static PyObject *
imp_load_dynamic(PyObject *self, PyObject *args)
{
char *name;
char *pathname;
PyObject *fob = NULL;
PyObject *m;
FILE *fp = NULL;
if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
&PyFile_Type, &fob))
return NULL;
if (fob) {
fp = get_file(pathname, fob, "r");
if (fp == NULL)
return NULL;
}
m = _PyImport_LoadDynamicModule(name, pathname, fp);
return m;
}
#endif /* HAVE_DYNAMIC_LOADING */
static PyObject *
imp_load_source(PyObject *self, PyObject *args)
{
char *name;
char *pathname;
PyObject *fob = NULL;
PyObject *m;
FILE *fp;
if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
&PyFile_Type, &fob))
return NULL;
fp = get_file(pathname, fob, "r");
if (fp == NULL)
return NULL;
m = load_source_module(name, pathname, fp);
if (fob == NULL)
fclose(fp);
return m;
}
#ifdef macintosh
static PyObject *
imp_load_resource(PyObject *self, PyObject *args)
{
char *name;
char *pathname;
PyObject *m;
if (!PyArg_ParseTuple(args, "ss:load_resource", &name, &pathname))
return NULL;
m = PyMac_LoadResourceModule(name, pathname);
return m;
}
#endif /* macintosh */
static PyObject *
imp_load_module(PyObject *self, PyObject *args)
{
char *name;
PyObject *fob;
char *pathname;
char *suffix; /* Unused */
char *mode;
int type;
FILE *fp;
if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
&name, &fob, &pathname,
&suffix, &mode, &type))
return NULL;
if (*mode && (*mode != 'r' || strchr(mode, '+') != NULL)) {
PyErr_Format(PyExc_ValueError,
"invalid file open mode %.200s", mode);
return NULL;
}
if (fob == Py_None)
fp = NULL;
else {
if (!PyFile_Check(fob)) {
PyErr_SetString(PyExc_ValueError,
"load_module arg#2 should be a file or None");
return NULL;
}
fp = get_file(pathname, fob, mode);
if (fp == NULL)
return NULL;
}
return load_module(name, fp, pathname, type);
}
static PyObject *
imp_load_package(PyObj
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -