diff options
author | Guido van Rossum <guido@python.org> | 1997-04-29 20:08:16 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1997-04-29 20:08:16 (GMT) |
commit | 79f25d9a7b853a9f491a0cfe4b81eeb9e2d19569 (patch) | |
tree | 5e456453855bd2906af42f998d70f17e8b834cba /Python/import.c | |
parent | 65bf9f265e4052c3eee95cd73f8de66bc92e53bf (diff) | |
download | cpython-79f25d9a7b853a9f491a0cfe4b81eeb9e2d19569.zip cpython-79f25d9a7b853a9f491a0cfe4b81eeb9e2d19569.tar.gz cpython-79f25d9a7b853a9f491a0cfe4b81eeb9e2d19569.tar.bz2 |
Quickly renamed the remaining files -- this directory is done.
Diffstat (limited to 'Python/import.c')
-rw-r--r-- | Python/import.c | 552 |
1 files changed, 282 insertions, 270 deletions
diff --git a/Python/import.c b/Python/import.c index 8dadc0e..64f4e29 100644 --- a/Python/import.c +++ b/Python/import.c @@ -31,17 +31,12 @@ PERFORMANCE OF THIS SOFTWARE. /* Module definition and import implementation */ -#include "allobjects.h" +#include "Python.h" -/* XXX Some of the following are duplicate with allobjects.h... */ #include "node.h" #include "token.h" #include "graminit.h" -#include "import.h" #include "errcode.h" -#include "sysmodule.h" -#include "bltinmodule.h" -#include "pythonrun.h" #include "marshal.h" #include "compile.h" #include "eval.h" @@ -57,7 +52,7 @@ PERFORMANCE OF THIS SOFTWARE. #include <unistd.h> #endif -extern long getmtime(); /* In getmtime.c */ +extern long PyOS_GetLastModificationTime(); /* In getmtime.c */ /* Magic word to reject .pyc files generated by other Python versions */ /* Change for each incompatible change */ @@ -69,22 +64,22 @@ extern long getmtime(); /* In getmtime.c */ /* New way to come up with the magic number: (YEAR-1995), MONTH, DAY */ #define MAGIC (20121 | ((long)'\r'<<16) | ((long)'\n'<<24)) -object *import_modules; /* This becomes sys.modules */ +PyObject *import_modules; /* This becomes sys.modules */ /* Initialize things */ void -initimport() +PyImport_Init() { if (import_modules != NULL) - fatal("duplicate initimport() call"); - if ((import_modules = newdictobject()) == NULL) - fatal("no mem for dictionary of modules"); + Py_FatalError("duplicate initimport() call"); + if ((import_modules = PyDict_New()) == NULL) + Py_FatalError("no mem for dictionary of modules"); if (Py_OptimizeFlag) { /* Replace ".pyc" with ".pyo" in import_filetab */ struct filedescr *p; - for (p = import_filetab; p->suffix != NULL; p++) { + for (p = _PyImport_Filetab; p->suffix != NULL; p++) { if (strcmp(p->suffix, ".pyc") == 0) p->suffix = ".pyo"; } @@ -95,19 +90,19 @@ initimport() /* Un-initialize things, as good as we can */ void -doneimport() +PyImport_Cleanup() { if (import_modules != NULL) { - object *tmp = import_modules; + PyObject *tmp = import_modules; import_modules = NULL; /* This deletes all modules from sys.modules. - When a module is deallocated, it in turn clears its dictionary, - thus hopefully breaking any circular references between modules - and between a module's dictionary and its functions. - Note that "import" will fail while we are cleaning up. - */ - mappingclear(tmp); - DECREF(tmp); + 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); } } @@ -115,7 +110,7 @@ doneimport() /* Helper for pythonrun.c -- return magic number */ long -get_pyc_magic() +PyImport_GetMagicNumber() { return MAGIC; } @@ -123,8 +118,8 @@ get_pyc_magic() /* Helper for sysmodule.c -- return modules dictionary */ -object * -get_modules() +PyObject * +PyImport_GetModuleDict() { return import_modules; } @@ -136,27 +131,28 @@ get_modules() Because the former action is most common, THIS DOES NOT RETURN A 'NEW' REFERENCE! */ -object * -add_module(name) +PyObject * +PyImport_AddModule(name) char *name; { - object *m; + PyObject *m; if (import_modules == NULL) { - err_setstr(SystemError, "sys.modules has been deleted"); + PyErr_SetString(PyExc_SystemError, + "sys.modules has been deleted"); return NULL; } - if ((m = dictlookup(import_modules, name)) != NULL && - is_moduleobject(m)) + if ((m = PyDict_GetItemString(import_modules, name)) != NULL && + PyModule_Check(m)) return m; - m = newmoduleobject(name); + m = PyModule_New(name); if (m == NULL) return NULL; - if (dictinsert(import_modules, name, m) != 0) { - DECREF(m); + if (PyDict_SetItemString(import_modules, name, m) != 0) { + Py_DECREF(m); return NULL; } - DECREF(m); /* Yes, it still exists, in modules! */ + Py_DECREF(m); /* Yes, it still exists, in modules! */ return m; } @@ -165,29 +161,31 @@ add_module(name) /* Execute a code object in a module and return the module object WITH INCREMENTED REFERENCE COUNT */ -object * -exec_code_module(name, co) +PyObject * +PyImport_ExecCodeModule(name, co) char *name; - object *co; + PyObject *co; { - object *m, *d, *v; + PyObject *m, *d, *v; - m = add_module(name); + m = PyImport_AddModule(name); if (m == NULL) return NULL; - d = getmoduledict(m); - if (dictlookup(d, "__builtins__") == NULL) { - if (dictinsert(d, "__builtins__", getbuiltins()) != 0) + d = PyModule_GetDict(m); + if (PyDict_GetItemString(d, "__builtins__") == NULL) { + if (PyDict_SetItemString(d, "__builtins__", + PyEval_GetBuiltins()) != 0) return NULL; } /* Remember the filename as the __file__ attribute */ - if (dictinsert(d, "__file__", ((codeobject *)co)->co_filename) != 0) - err_clear(); /* Not important enough to report */ - v = eval_code((codeobject *)co, d, d); /* XXX owner? */ + 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? */ if (v == NULL) return NULL; - DECREF(v); - INCREF(m); + Py_DECREF(v); + Py_INCREF(m); return m; } @@ -236,21 +234,21 @@ check_compiled_module(pathname, mtime, cpathname) fp = fopen(cpathname, "rb"); if (fp == NULL) return NULL; - magic = rd_long(fp); + magic = PyMarshal_ReadLongFromFile(fp); if (magic != MAGIC) { - if (verbose) + if (Py_VerboseFlag) fprintf(stderr, "# %s has bad magic\n", cpathname); fclose(fp); return NULL; } - pyc_mtime = rd_long(fp); + pyc_mtime = PyMarshal_ReadLongFromFile(fp); if (pyc_mtime != mtime) { - if (verbose) + if (Py_VerboseFlag) fprintf(stderr, "# %s has bad mtime\n", cpathname); fclose(fp); return NULL; } - if (verbose) + if (Py_VerboseFlag) fprintf(stderr, "# %s matches %s\n", cpathname, pathname); return fp; } @@ -258,71 +256,72 @@ check_compiled_module(pathname, mtime, cpathname) /* Read a code object from a file and check it for validity */ -static codeobject * +static PyCodeObject * read_compiled_module(fp) FILE *fp; { - object *co; + PyObject *co; - co = rd_object(fp); + co = PyMarshal_ReadObjectFromFile(fp); /* Ugly: rd_object() may return NULL with or without error */ - if (co == NULL || !is_codeobject(co)) { - if (!err_occurred()) - err_setstr(ImportError, + if (co == NULL || !PyCode_Check(co)) { + if (!PyErr_Occurred()) + PyErr_SetString(PyExc_ImportError, "Non-code object in .pyc file"); - XDECREF(co); + Py_XDECREF(co); return NULL; } - return (codeobject *)co; + return (PyCodeObject *)co; } /* Load a module from a compiled file, execute it, and return its module object WITH INCREMENTED REFERENCE COUNT */ -static object * +static PyObject * load_compiled_module(name, cpathname, fp) char *name; char *cpathname; FILE *fp; { long magic; - codeobject *co; - object *m; + PyCodeObject *co; + PyObject *m; - magic = rd_long(fp); + magic = PyMarshal_ReadLongFromFile(fp); if (magic != MAGIC) { - err_setstr(ImportError, "Bad magic number in .pyc file"); + PyErr_SetString(PyExc_ImportError, + "Bad magic number in .pyc file"); return NULL; } - (void) rd_long(fp); + (void) PyMarshal_ReadLongFromFile(fp); co = read_compiled_module(fp); if (co == NULL) return NULL; - if (verbose) + if (Py_VerboseFlag) fprintf(stderr, "import %s # precompiled from %s\n", name, cpathname); - m = exec_code_module(name, (object *)co); - DECREF(co); + m = PyImport_ExecCodeModule(name, (PyObject *)co); + Py_DECREF(co); return m; } /* Parse a source file and return the corresponding code object */ -static codeobject * +static PyCodeObject * parse_source_module(pathname, fp) char *pathname; FILE *fp; { - codeobject *co; + PyCodeObject *co; node *n; - n = parse_file(fp, pathname, file_input); + n = PyParser_SimpleParseFile(fp, pathname, file_input); if (n == NULL) return NULL; - co = compile(n, pathname); - freetree(n); + co = PyNode_Compile(n, pathname); + PyNode_Free(n); return co; } @@ -335,7 +334,7 @@ parse_source_module(pathname, fp) static void write_compiled_module(co, cpathname, mtime) - codeobject *co; + PyCodeObject *co; char *cpathname; long mtime; { @@ -343,17 +342,17 @@ write_compiled_module(co, cpathname, mtime) fp = fopen(cpathname, "wb"); if (fp == NULL) { - if (verbose) + if (Py_VerboseFlag) fprintf(stderr, "# can't create %s\n", cpathname); return; } - wr_long(MAGIC, fp); + PyMarshal_WriteLongToFile(MAGIC, fp); /* First write a 0 for mtime */ - wr_long(0L, fp); - wr_object((object *)co, fp); + PyMarshal_WriteLongToFile(0L, fp); + PyMarshal_WriteObjectToFile((PyObject *)co, fp); if (ferror(fp)) { - if (verbose) + if (Py_VerboseFlag) fprintf(stderr, "# can't write %s\n", cpathname); /* Don't keep partial file */ fclose(fp); @@ -362,10 +361,10 @@ write_compiled_module(co, cpathname, mtime) } /* Now write the true mtime */ fseek(fp, 4L, 0); - wr_long(mtime, fp); + PyMarshal_WriteLongToFile(mtime, fp); fflush(fp); fclose(fp); - if (verbose) + if (Py_VerboseFlag) fprintf(stderr, "# wrote %s\n", cpathname); #ifdef macintosh setfiletype(cpathname, 'Pyth', 'PYC '); @@ -377,7 +376,7 @@ write_compiled_module(co, cpathname, mtime) object WITH INCREMENTED REFERENCE COUNT. If there's a matching byte-compiled file, use that instead. */ -static object * +static PyObject * load_source_module(name, pathname, fp) char *name; char *pathname; @@ -387,10 +386,10 @@ load_source_module(name, pathname, fp) FILE *fpc; char buf[MAXPATHLEN+1]; char *cpathname; - codeobject *co; - object *m; + PyCodeObject *co; + PyObject *m; - mtime = getmtime(pathname); + mtime = PyOS_GetLastModificationTime(pathname); cpathname = make_compiled_pathname(pathname, buf, MAXPATHLEN+1); if (cpathname != NULL && (fpc = check_compiled_module(pathname, mtime, cpathname))) { @@ -398,7 +397,7 @@ load_source_module(name, pathname, fp) fclose(fpc); if (co == NULL) return NULL; - if (verbose) + if (Py_VerboseFlag) fprintf(stderr, "import %s # precompiled from %s\n", name, cpathname); } @@ -406,13 +405,13 @@ load_source_module(name, pathname, fp) co = parse_source_module(pathname, fp); if (co == NULL) return NULL; - if (verbose) + if (Py_VerboseFlag) fprintf(stderr, "import %s # from %s\n", name, pathname); write_compiled_module(co, cpathname, mtime); } - m = exec_code_module(name, (object *)co); - DECREF(co); + m = PyImport_ExecCodeModule(name, (PyObject *)co); + Py_DECREF(co); return m; } @@ -425,7 +424,7 @@ load_source_module(name, pathname, fp) static struct filedescr * find_module(name, path, buf, buflen, p_fp) char *name; - object *path; + PyObject *path; /* Output parameters: */ char *buf; int buflen; @@ -445,27 +444,28 @@ find_module(name, path, buf, buflen, p_fp) if (path == NULL) - path = sysget("path"); - if (path == NULL || !is_listobject(path)) { - err_setstr(ImportError, + path = PySys_GetObject("path"); + if (path == NULL || !PyList_Check(path)) { + PyErr_SetString(PyExc_ImportError, "sys.path must be a list of directory names"); return NULL; } - npath = getlistsize(path); + npath = PyList_Size(path); namelen = strlen(name); for (i = 0; i < npath; i++) { - object *v = getlistitem(path, i); - if (!is_stringobject(v)) + PyObject *v = PyList_GetItem(path, i); + if (!PyString_Check(v)) continue; - len = getstringsize(v); - if (len + 2 + namelen + import_maxsuffixsize >= buflen) + len = PyString_Size(v); + if (len + 2 + namelen + _PyImport_MaxSuffixSize >= buflen) continue; /* Too long */ - strcpy(buf, getstringvalue(v)); + strcpy(buf, PyString_AsString(v)); if ((int)strlen(buf) != len) continue; /* v contains '\0' */ #ifdef macintosh if ( PyMac_FindResourceModule(name, buf) ) { - static struct filedescr resfiledescr = { "", "", PY_RESOURCE}; + static struct filedescr resfiledescr = + {"", "", PY_RESOURCE}; return &resfiledescr; } @@ -476,7 +476,7 @@ find_module(name, path, buf, buflen, p_fp) /* see if we are searching in directory dos_8x3 */ if (len > 7 && !strncmp(buf + len - 8, "dos_8x3", 7)){ int j; - char ch; /* limit name to eight lower-case characters */ + char ch; /* limit name to 8 lower-case characters */ for (j = 0; (ch = name[j]) && j < 8; j++) if (isupper(ch)) buf[len++] = tolower(ch); @@ -489,9 +489,9 @@ find_module(name, path, buf, buflen, p_fp) strcpy(buf+len, name); len += namelen; } - for (fdp = import_filetab; fdp->suffix != NULL; fdp++) { + for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) { strcpy(buf+len, fdp->suffix); - if (verbose > 1) + if (Py_VerboseFlag > 1) fprintf(stderr, "# trying %s\n", buf); fp = fopen(buf, fdp->mode); if (fp != NULL) @@ -503,7 +503,7 @@ find_module(name, path, buf, buflen, p_fp) if (fp == NULL) { char buf[256]; sprintf(buf, "No module named %.200s", name); - err_setstr(ImportError, buf); + PyErr_SetString(PyExc_ImportError, buf); return NULL; } @@ -515,16 +515,16 @@ find_module(name, path, buf, buflen, p_fp) /* Load an external module using the default search path and return its module object WITH INCREMENTED REFERENCE COUNT */ -static object * +static PyObject * load_module(name) char *name; { char buf[MAXPATHLEN+1]; struct filedescr *fdp; FILE *fp = NULL; - object *m; + PyObject *m; - fdp = find_module(name, (object *)NULL, buf, MAXPATHLEN+1, &fp); + fdp = find_module(name, (PyObject *)NULL, buf, MAXPATHLEN+1, &fp); if (fdp == NULL) return NULL; @@ -539,7 +539,7 @@ load_module(name) break; case C_EXTENSION: - m = load_dynamic_module(name, buf, fp); + m = _PyImport_LoadDynamicModule(name, buf, fp); break; #ifdef macintosh @@ -549,7 +549,7 @@ load_module(name) #endif default: - err_setstr(SystemError, + PyErr_SetString(PyExc_SystemError, "find_module returned unexpected result"); m = NULL; @@ -573,15 +573,15 @@ init_builtin(name) for (i = 0; inittab[i].name != NULL; i++) { if (strcmp(name, inittab[i].name) == 0) { if (inittab[i].initfunc == NULL) { - err_setstr(ImportError, + PyErr_SetString(PyExc_ImportError, "Cannot re-init internal module"); return -1; } - if (verbose) + if (Py_VerboseFlag) fprintf(stderr, "import %s # builtin\n", name); (*inittab[i].initfunc)(); - if (err_occurred()) + if (PyErr_Occurred()) return -1; return 1; } @@ -598,7 +598,7 @@ find_frozen(name) { struct _frozen *p; - for (p = frozen_modules; ; p++) { + for (p = PyImport_FrozenModules; ; p++) { if (p->name == NULL) return NULL; if (strcmp(p->name, name) == 0) @@ -607,17 +607,17 @@ find_frozen(name) return p; } -static object * +static PyObject * get_frozen_object(name) char *name; { struct _frozen *p = find_frozen(name); if (p == NULL) { - err_setstr(ImportError, "No such frozen object"); + PyErr_SetString(PyExc_ImportError, "No such frozen object"); return NULL; } - return rds_object((char *)p->code, p->size); + return PyMarshal_ReadObjectFromString((char *)p->code, p->size); } /* Initialize a frozen module. @@ -626,30 +626,31 @@ get_frozen_object(name) This function is also used from frozenmain.c */ int -init_frozen(name) +PyImport_ImportFrozenModule(name) char *name; { struct _frozen *p = find_frozen(name); - object *co; - object *m; + PyObject *co; + PyObject *m; if (p == NULL) return 0; - if (verbose) + if (Py_VerboseFlag) fprintf(stderr, "import %s # frozen\n", name); - co = rds_object((char *)p->code, p->size); + co = PyMarshal_ReadObjectFromString((char *)p->code, p->size); if (co == NULL) return -1; - if (!is_codeobject(co)) { - DECREF(co); - err_setstr(TypeError, "frozen object is not a code object"); + if (!PyCode_Check(co)) { + Py_DECREF(co); + PyErr_SetString(PyExc_TypeError, + "frozen object is not a code object"); return -1; } - m = exec_code_module(name, co); - DECREF(co); + m = PyImport_ExecCodeModule(name, co); + Py_DECREF(co); if (m == NULL) return -1; - DECREF(m); + Py_DECREF(m); return 1; } @@ -657,31 +658,34 @@ init_frozen(name) /* Import a module, either built-in, frozen, or external, and return its module object WITH INCREMENTED REFERENCE COUNT */ -object * -import_module(name) +PyObject * +PyImport_ImportModule(name) char *name; { - object *m; + PyObject *m; if (import_modules == NULL) { - err_setstr(SystemError, "sys.modules has been deleted"); + PyErr_SetString(PyExc_SystemError, + "sys.modules has been deleted"); return NULL; } - if ((m = dictlookup(import_modules, name)) != NULL) { - INCREF(m); + if ((m = PyDict_GetItemString(import_modules, name)) != NULL) { + Py_INCREF(m); } else { int i; - if ((i = init_builtin(name)) || (i = init_frozen(name))) { + if ((i = init_builtin(name)) || + (i = PyImport_ImportFrozenModule(name))) { if (i < 0) return NULL; - if ((m = dictlookup(import_modules, name)) == NULL) { - if (err_occurred() == NULL) - err_setstr(SystemError, + if ((m = PyDict_GetItemString(import_modules, + name)) == NULL) { + if (PyErr_Occurred() == NULL) + PyErr_SetString(PyExc_SystemError, "built-in module not initialized properly"); } else - INCREF(m); + Py_INCREF(m); } else m = load_module(name); @@ -694,33 +698,37 @@ import_module(name) /* Re-import a module of any kind and return its module object, WITH INCREMENTED REFERENCE COUNT */ -object * -reload_module(m) - object *m; +PyObject * +PyImport_ReloadModule(m) + PyObject *m; { char *name; int i; - if (m == NULL || !is_moduleobject(m)) { - err_setstr(TypeError, "reload() argument must be module"); + if (m == NULL || !PyModule_Check(m)) { + PyErr_SetString(PyExc_TypeError, + "reload() argument must be module"); return NULL; } - name = getmodulename(m); + name = PyModule_GetName(m); if (name == NULL) return NULL; if (import_modules == NULL) { - err_setstr(SystemError, "sys.modules has been deleted"); + PyErr_SetString(PyExc_SystemError, + "sys.modules has been deleted"); return NULL; } - if (m != dictlookup(import_modules, name)) { - err_setstr(ImportError, "reload() module not in sys.modules"); + if (m != PyDict_GetItemString(import_modules, name)) { + PyErr_SetString(PyExc_ImportError, + "reload() module not in sys.modules"); return NULL; } /* Check for built-in and frozen modules */ - if ((i = init_builtin(name)) || (i = init_frozen(name))) { + if ((i = init_builtin(name)) || + (i = PyImport_ImportFrozenModule(name))) { if (i < 0) return NULL; - INCREF(m); + Py_INCREF(m); } else m = load_module(name); @@ -732,206 +740,208 @@ reload_module(m) importing modules. */ -static object * +static PyObject * imp_get_magic(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { char buf[4]; - if (!newgetargs(args, "")) + if (!PyArg_ParseTuple(args, "")) return NULL; buf[0] = (char) ((MAGIC >> 0) & 0xff); buf[1] = (char) ((MAGIC >> 8) & 0xff); buf[2] = (char) ((MAGIC >> 16) & 0xff); buf[3] = (char) ((MAGIC >> 24) & 0xff); - return newsizedstringobject(buf, 4); + return PyString_FromStringAndSize(buf, 4); } -static object * +static PyObject * imp_get_suffixes(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *list; + PyObject *list; struct filedescr *fdp; - if (!newgetargs(args, "")) + if (!PyArg_ParseTuple(args, "")) return NULL; - list = newlistobject(0); + list = PyList_New(0); if (list == NULL) return NULL; - for (fdp = import_filetab; fdp->suffix != NULL; fdp++) { - object *item = mkvalue("ssi", + for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) { + PyObject *item = Py_BuildValue("ssi", fdp->suffix, fdp->mode, fdp->type); if (item == NULL) { - DECREF(list); + Py_DECREF(list); return NULL; } - if (addlistitem(list, item) < 0) { - DECREF(list); - DECREF(item); + if (PyList_Append(list, item) < 0) { + Py_DECREF(list); + Py_DECREF(item); return NULL; } - DECREF(item); + Py_DECREF(item); } return list; } -static object * +static PyObject * imp_find_module(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - extern int fclose PROTO((FILE *)); + extern int fclose Py_PROTO((FILE *)); char *name; - object *path = NULL; - object *fob, *ret; + PyObject *path = NULL; + PyObject *fob, *ret; struct filedescr *fdp; char pathname[MAXPATHLEN+1]; FILE *fp; - if (!newgetargs(args, "s|O!", &name, &Listtype, &path)) + if (!PyArg_ParseTuple(args, "s|O!", &name, &PyList_Type, &path)) return NULL; fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp); if (fdp == NULL) return NULL; - fob = newopenfileobject(fp, pathname, fdp->mode, fclose); + fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose); if (fob == NULL) { fclose(fp); return NULL; } - ret = mkvalue("Os(ssi)", + ret = Py_BuildValue("Os(ssi)", fob, pathname, fdp->suffix, fdp->mode, fdp->type); - DECREF(fob); + Py_DECREF(fob); return ret; } -static object * +static PyObject * imp_init_builtin(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { char *name; int ret; - object *m; - if (!newgetargs(args, "s", &name)) + PyObject *m; + if (!PyArg_ParseTuple(args, "s", &name)) return NULL; ret = init_builtin(name); if (ret < 0) return NULL; if (ret == 0) { - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } - m = add_module(name); - XINCREF(m); + m = PyImport_AddModule(name); + Py_XINCREF(m); return m; } -static object * +static PyObject * imp_init_frozen(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { char *name; int ret; - object *m; - if (!newgetargs(args, "s", &name)) + PyObject *m; + if (!PyArg_ParseTuple(args, "s", &name)) return NULL; - ret = init_frozen(name); + ret = PyImport_ImportFrozenModule(name); if (ret < 0) return NULL; if (ret == 0) { - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } - m = add_module(name); - XINCREF(m); + m = PyImport_AddModule(name); + Py_XINCREF(m); return m; } -static object * +static PyObject * imp_get_frozen_object(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { char *name; - if (!newgetargs(args, "s", &name)) + if (!PyArg_ParseTuple(args, "s", &name)) return NULL; return get_frozen_object(name); } -static object * +static PyObject * imp_is_builtin(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { int i; char *name; - if (!newgetargs(args, "s", &name)) + if (!PyArg_ParseTuple(args, "s", &name)) return NULL; for (i = 0; inittab[i].name != NULL; i++) { if (strcmp(name, inittab[i].name) == 0) { if (inittab[i].initfunc == NULL) - return newintobject(-1); + return PyInt_FromLong(-1); else - return newintobject(1); + return PyInt_FromLong(1); } } - return newintobject(0); + return PyInt_FromLong(0); } -static object * +static PyObject * imp_is_frozen(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { struct _frozen *p; char *name; - if (!newgetargs(args, "s", &name)) + if (!PyArg_ParseTuple(args, "s", &name)) return NULL; - for (p = frozen_modules; ; p++) { + for (p = PyImport_FrozenModules; ; p++) { if (p->name == NULL) break; if (strcmp(p->name, name) == 0) - return newintobject(1); + return PyInt_FromLong(1); } - return newintobject(0); + return PyInt_FromLong(0); } static FILE * get_file(pathname, fob, mode) char *pathname; - object *fob; + PyObject *fob; char *mode; { FILE *fp; if (fob == NULL) { fp = fopen(pathname, mode); if (fp == NULL) - err_errno(IOError); + PyErr_SetFromErrno(PyExc_IOError); } else { - fp = getfilefile(fob); + fp = PyFile_AsFile(fob); if (fp == NULL) - err_setstr(ValueError, "bad/closed file object"); + PyErr_SetString(PyExc_ValueError, + "bad/closed file object"); } return fp; } -static object * +static PyObject * imp_load_compiled(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { char *name; char *pathname; - object *fob = NULL; - object *m; + PyObject *fob = NULL; + PyObject *m; FILE *fp; - if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob)) + if (!PyArg_ParseTuple(args, "ssO!", &name, &pathname, + &PyFile_Type, &fob)) return NULL; fp = get_file(pathname, fob, "rb"); if (fp == NULL) @@ -940,35 +950,37 @@ imp_load_compiled(self, args) return m; } -static object * +static PyObject * imp_load_dynamic(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { char *name; char *pathname; - object *fob = NULL; - object *m; + PyObject *fob = NULL; + PyObject *m; FILE *fp = NULL; - if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob)) + if (!PyArg_ParseTuple(args, "ss|O!", &name, &pathname, + &PyFile_Type, &fob)) return NULL; if (fob) fp = get_file(pathname, fob, "r"); - m = load_dynamic_module(name, pathname, fp); + m = _PyImport_LoadDynamicModule(name, pathname, fp); return m; } -static object * +static PyObject * imp_load_source(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { char *name; char *pathname; - object *fob = NULL; - object *m; + PyObject *fob = NULL; + PyObject *m; FILE *fp; - if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob)) + if (!PyArg_ParseTuple(args, "ssO!", &name, &pathname, + &PyFile_Type, &fob)) return NULL; fp = get_file(pathname, fob, "r"); if (fp == NULL) @@ -978,34 +990,34 @@ imp_load_source(self, args) } #ifdef macintosh -static object * +static PyObject * imp_load_resource(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { char *name; char *pathname; - object *m; + PyObject *m; - if (!newgetargs(args, "ss", &name, &pathname)) + if (!PyArg_ParseTuple(args, "ss", &name, &pathname)) return NULL; m = PyMac_LoadResourceModule(name, pathname); return m; } #endif /* macintosh */ -static object * +static PyObject * imp_new_module(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { char *name; - if (!newgetargs(args, "s", &name)) + if (!PyArg_ParseTuple(args, "s", &name)) return NULL; - return newmoduleobject(name); + return PyModule_New(name); } -static struct methodlist imp_methods[] = { +static PyMethodDef imp_methods[] = { {"get_frozen_object", imp_get_frozen_object, 1}, {"get_magic", imp_get_magic, 1}, {"get_suffixes", imp_get_suffixes, 1}, @@ -1027,34 +1039,34 @@ static struct methodlist imp_methods[] = { void initimp() { - object *m, *d, *v; + PyObject *m, *d, *v; - m = initmodule("imp", imp_methods); - d = getmoduledict(m); + m = Py_InitModule("imp", imp_methods); + d = PyModule_GetDict(m); - v = newintobject(SEARCH_ERROR); - dictinsert(d, "SEARCH_ERROR", v); - XDECREF(v); + v = PyInt_FromLong(SEARCH_ERROR); + PyDict_SetItemString(d, "SEARCH_ERROR", v); + Py_XDECREF(v); - v = newintobject(PY_SOURCE); - dictinsert(d, "PY_SOURCE", v); - XDECREF(v); + v = PyInt_FromLong(PY_SOURCE); + PyDict_SetItemString(d, "PY_SOURCE", v); + Py_XDECREF(v); - v = newintobject(PY_COMPILED); - dictinsert(d, "PY_COMPILED", v); - XDECREF(v); + v = PyInt_FromLong(PY_COMPILED); + PyDict_SetItemString(d, "PY_COMPILED", v); + Py_XDECREF(v); - v = newintobject(C_EXTENSION); - dictinsert(d, "C_EXTENSION", v); - XDECREF(v); + v = PyInt_FromLong(C_EXTENSION); + PyDict_SetItemString(d, "C_EXTENSION", v); + Py_XDECREF(v); #ifdef macintosh - v = newintobject(PY_RESOURCE); - dictinsert(d, "PY_RESOURCE", v); - XDECREF(v); + v = PyInt_FromLong(PY_RESOURCE); + PyDict_SetItemString(d, "PY_RESOURCE", v); + Py_XDECREF(v); #endif - if (err_occurred()) - fatal("imp module initialization failed"); + if (PyErr_Occurred()) + Py_FatalError("imp module initialization failed"); } |