2002-12-30 18:08:05 -04:00
|
|
|
#include "Python.h"
|
|
|
|
#include "structmember.h"
|
|
|
|
#include "osdefs.h"
|
|
|
|
#include "marshal.h"
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
|
|
|
|
#define IS_SOURCE 0x0
|
|
|
|
#define IS_BYTECODE 0x1
|
|
|
|
#define IS_PACKAGE 0x2
|
|
|
|
|
|
|
|
struct st_zip_searchorder {
|
2010-05-09 12:52:27 -03:00
|
|
|
char suffix[14];
|
|
|
|
int type;
|
2002-12-30 18:08:05 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/* zip_searchorder defines how we search for a module in the Zip
|
|
|
|
archive: we first search for a package __init__, then for
|
|
|
|
non-package .pyc, .pyo and .py entries. The .pyc and .pyo entries
|
|
|
|
are swapped by initzipimport() if we run in optimized mode. Also,
|
|
|
|
'/' is replaced by SEP there. */
|
2003-03-23 09:21:03 -04:00
|
|
|
static struct st_zip_searchorder zip_searchorder[] = {
|
2010-05-09 12:52:27 -03:00
|
|
|
{"/__init__.pyc", IS_PACKAGE | IS_BYTECODE},
|
|
|
|
{"/__init__.pyo", IS_PACKAGE | IS_BYTECODE},
|
|
|
|
{"/__init__.py", IS_PACKAGE | IS_SOURCE},
|
|
|
|
{".pyc", IS_BYTECODE},
|
|
|
|
{".pyo", IS_BYTECODE},
|
|
|
|
{".py", IS_SOURCE},
|
|
|
|
{"", 0}
|
2002-12-30 18:08:05 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/* zipimporter object definition and support */
|
|
|
|
|
|
|
|
typedef struct _zipimporter ZipImporter;
|
|
|
|
|
|
|
|
struct _zipimporter {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject_HEAD
|
2010-10-18 19:34:46 -03:00
|
|
|
PyObject *archive; /* pathname of the Zip archive,
|
|
|
|
decoded from the filesystem encoding */
|
2010-10-18 08:44:21 -03:00
|
|
|
PyObject *prefix; /* file prefix: "a/sub/directory/",
|
|
|
|
encoded to the filesystem encoding */
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *files; /* dict with file info {path: toc_entry} */
|
2002-12-30 18:08:05 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject *ZipImportError;
|
2010-10-18 08:39:05 -03:00
|
|
|
/* read_directory() cache */
|
2002-12-30 18:08:05 -04:00
|
|
|
static PyObject *zip_directory_cache = NULL;
|
|
|
|
|
|
|
|
/* forward decls */
|
2010-08-16 14:54:28 -03:00
|
|
|
static PyObject *read_directory(PyObject *archive);
|
2010-08-16 20:48:11 -03:00
|
|
|
static PyObject *get_data(PyObject *archive, PyObject *toc_entry);
|
2002-12-30 18:08:05 -04:00
|
|
|
static PyObject *get_module_code(ZipImporter *self, char *fullname,
|
2010-10-18 09:09:02 -03:00
|
|
|
int *p_ispackage, PyObject **p_modpath);
|
2002-12-30 18:08:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
#define ZipImporter_Check(op) PyObject_TypeCheck(op, &ZipImporter_Type)
|
|
|
|
|
|
|
|
|
|
|
|
/* zipimporter.__init__
|
|
|
|
Split the "subdirectory" from the Zip archive path, lookup a matching
|
|
|
|
entry in sys.path_importer_cache, fetch the file directory from there
|
|
|
|
if found, or else read it from the archive. */
|
|
|
|
static int
|
|
|
|
zipimporter_init(ZipImporter *self, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
2010-08-16 14:54:28 -03:00
|
|
|
PyObject *pathobj, *files;
|
2010-08-14 11:54:10 -03:00
|
|
|
Py_UNICODE *path, *p, *prefix, buf[MAXPATHLEN+2];
|
|
|
|
Py_ssize_t len;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
if (!_PyArg_NoKeywords("zipimporter()", kwds))
|
|
|
|
return -1;
|
|
|
|
|
2010-08-14 11:54:10 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "O&:zipimporter",
|
2010-10-18 19:34:46 -03:00
|
|
|
PyUnicode_FSDecoder, &pathobj))
|
2010-05-09 12:52:27 -03:00
|
|
|
return -1;
|
|
|
|
|
2010-08-14 11:54:10 -03:00
|
|
|
/* copy path to buf */
|
|
|
|
len = PyUnicode_GET_SIZE(pathobj);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (len == 0) {
|
|
|
|
PyErr_SetString(ZipImportError, "archive path is empty");
|
2010-08-14 11:54:10 -03:00
|
|
|
goto error;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
if (len >= MAXPATHLEN) {
|
|
|
|
PyErr_SetString(ZipImportError,
|
|
|
|
"archive path too long");
|
2010-08-14 11:54:10 -03:00
|
|
|
goto error;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2010-08-14 11:54:10 -03:00
|
|
|
Py_UNICODE_strcpy(buf, PyUnicode_AS_UNICODE(pathobj));
|
2002-12-30 18:08:05 -04:00
|
|
|
|
|
|
|
#ifdef ALTSEP
|
2010-05-09 12:52:27 -03:00
|
|
|
for (p = buf; *p; p++) {
|
|
|
|
if (*p == ALTSEP)
|
|
|
|
*p = SEP;
|
|
|
|
}
|
2002-12-30 18:08:05 -04:00
|
|
|
#endif
|
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
path = NULL;
|
|
|
|
prefix = NULL;
|
|
|
|
for (;;) {
|
|
|
|
struct stat statbuf;
|
|
|
|
int rv;
|
|
|
|
|
2010-08-14 11:54:10 -03:00
|
|
|
if (pathobj == NULL) {
|
|
|
|
pathobj = PyUnicode_FromUnicode(buf, len);
|
|
|
|
if (pathobj == NULL)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
rv = _Py_stat(pathobj, &statbuf);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (rv == 0) {
|
|
|
|
/* it exists */
|
|
|
|
if (S_ISREG(statbuf.st_mode))
|
|
|
|
/* it's a file */
|
|
|
|
path = buf;
|
|
|
|
break;
|
|
|
|
}
|
2010-08-14 11:54:10 -03:00
|
|
|
else if (PyErr_Occurred())
|
|
|
|
goto error;
|
2010-05-09 12:52:27 -03:00
|
|
|
/* back up one path element */
|
2010-08-14 11:54:10 -03:00
|
|
|
p = Py_UNICODE_strrchr(buf, SEP);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (prefix != NULL)
|
|
|
|
*prefix = SEP;
|
|
|
|
if (p == NULL)
|
|
|
|
break;
|
|
|
|
*p = '\0';
|
2010-08-14 11:54:10 -03:00
|
|
|
len = p - buf;
|
2010-05-09 12:52:27 -03:00
|
|
|
prefix = p;
|
2010-08-14 11:54:10 -03:00
|
|
|
Py_CLEAR(pathobj);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2010-08-14 11:54:10 -03:00
|
|
|
if (path == NULL) {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_SetString(ZipImportError, "not a Zip file");
|
2010-08-14 11:54:10 -03:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
files = PyDict_GetItem(zip_directory_cache, pathobj);
|
|
|
|
if (files == NULL) {
|
2010-08-16 14:54:28 -03:00
|
|
|
files = read_directory(pathobj);
|
2010-08-14 11:54:10 -03:00
|
|
|
if (files == NULL)
|
|
|
|
goto error;
|
|
|
|
if (PyDict_SetItem(zip_directory_cache, pathobj, files) != 0)
|
|
|
|
goto error;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2010-08-14 11:54:10 -03:00
|
|
|
else
|
|
|
|
Py_INCREF(files);
|
|
|
|
self->files = files;
|
|
|
|
|
|
|
|
self->archive = pathobj;
|
|
|
|
pathobj = NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2010-08-14 11:54:10 -03:00
|
|
|
if (prefix != NULL) {
|
2010-05-09 12:52:27 -03:00
|
|
|
prefix++;
|
2010-08-14 11:54:10 -03:00
|
|
|
len = Py_UNICODE_strlen(prefix);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (prefix[len-1] != SEP) {
|
|
|
|
/* add trailing SEP */
|
|
|
|
prefix[len] = SEP;
|
|
|
|
prefix[len + 1] = '\0';
|
2010-08-14 11:54:10 -03:00
|
|
|
len++;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
}
|
2010-08-14 11:54:10 -03:00
|
|
|
else
|
|
|
|
len = 0;
|
|
|
|
self->prefix = PyUnicode_FromUnicode(prefix, len);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (self->prefix == NULL)
|
2010-08-14 11:54:10 -03:00
|
|
|
goto error;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
return 0;
|
2010-08-14 11:54:10 -03:00
|
|
|
|
|
|
|
error:
|
|
|
|
Py_XDECREF(pathobj);
|
|
|
|
return -1;
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* GC support. */
|
|
|
|
static int
|
|
|
|
zipimporter_traverse(PyObject *obj, visitproc visit, void *arg)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
ZipImporter *self = (ZipImporter *)obj;
|
|
|
|
Py_VISIT(self->files);
|
|
|
|
return 0;
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
zipimporter_dealloc(ZipImporter *self)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject_GC_UnTrack(self);
|
|
|
|
Py_XDECREF(self->archive);
|
|
|
|
Py_XDECREF(self->prefix);
|
|
|
|
Py_XDECREF(self->files);
|
|
|
|
Py_TYPE(self)->tp_free((PyObject *)self);
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
zipimporter_repr(ZipImporter *self)
|
|
|
|
{
|
2010-08-16 21:04:48 -03:00
|
|
|
if (self->archive == NULL)
|
|
|
|
return PyUnicode_FromString("<zipimporter object \"???\">");
|
|
|
|
else if (self->prefix != NULL && PyUnicode_GET_SIZE(self->prefix) != 0)
|
2010-10-18 19:45:54 -03:00
|
|
|
return PyUnicode_FromFormat("<zipimporter object \"%U%c%U\">",
|
2010-08-16 21:04:48 -03:00
|
|
|
self->archive, SEP, self->prefix);
|
2010-05-09 12:52:27 -03:00
|
|
|
else
|
2010-10-18 19:45:54 -03:00
|
|
|
return PyUnicode_FromFormat("<zipimporter object \"%U\">",
|
2010-08-16 21:04:48 -03:00
|
|
|
self->archive);
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* return fullname.split(".")[-1] */
|
|
|
|
static char *
|
|
|
|
get_subname(char *fullname)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
char *subname = strrchr(fullname, '.');
|
|
|
|
if (subname == NULL)
|
|
|
|
subname = fullname;
|
|
|
|
else
|
|
|
|
subname++;
|
|
|
|
return subname;
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Given a (sub)modulename, write the potential file path in the
|
|
|
|
archive (without extension) to the path buffer. Return the
|
|
|
|
length of the resulting string. */
|
|
|
|
static int
|
2010-10-18 17:40:59 -03:00
|
|
|
make_filename(PyObject *prefix_obj, char *name, char *path, size_t pathsize)
|
2002-12-30 18:08:05 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
size_t len;
|
|
|
|
char *p;
|
2010-10-18 08:44:21 -03:00
|
|
|
PyObject *prefix;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2010-10-18 08:44:21 -03:00
|
|
|
prefix = PyUnicode_EncodeFSDefault(prefix_obj);
|
|
|
|
if (prefix == NULL)
|
|
|
|
return -1;
|
|
|
|
len = PyBytes_GET_SIZE(prefix);
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
/* self.prefix + name [+ SEP + "__init__"] + ".py[co]" */
|
2010-10-18 17:40:59 -03:00
|
|
|
if (len + strlen(name) + 13 >= pathsize - 1) {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_SetString(ZipImportError, "path too long");
|
2010-10-18 08:44:21 -03:00
|
|
|
Py_DECREF(prefix);
|
2010-05-09 12:52:27 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-10-18 08:44:21 -03:00
|
|
|
strcpy(path, PyBytes_AS_STRING(prefix));
|
|
|
|
Py_DECREF(prefix);
|
2010-05-09 12:52:27 -03:00
|
|
|
strcpy(path + len, name);
|
|
|
|
for (p = path + len; *p; p++) {
|
|
|
|
if (*p == '.')
|
|
|
|
*p = SEP;
|
|
|
|
}
|
|
|
|
len += strlen(name);
|
|
|
|
assert(len < INT_MAX);
|
|
|
|
return (int)len;
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
2004-11-10 09:08:35 -04:00
|
|
|
enum zi_module_info {
|
2010-05-09 12:52:27 -03:00
|
|
|
MI_ERROR,
|
|
|
|
MI_NOT_FOUND,
|
|
|
|
MI_MODULE,
|
|
|
|
MI_PACKAGE
|
2002-12-30 18:08:05 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Return some information about a module. */
|
2004-11-10 09:08:35 -04:00
|
|
|
static enum zi_module_info
|
2010-10-18 18:44:33 -03:00
|
|
|
get_module_info(ZipImporter *self, char *fullname)
|
2002-12-30 18:08:05 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
char *subname, path[MAXPATHLEN + 1];
|
|
|
|
int len;
|
|
|
|
struct st_zip_searchorder *zso;
|
|
|
|
|
2010-10-18 18:44:33 -03:00
|
|
|
subname = get_subname(fullname);
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2010-10-18 17:40:59 -03:00
|
|
|
len = make_filename(self->prefix, subname, path, sizeof(path));
|
2010-05-09 12:52:27 -03:00
|
|
|
if (len < 0)
|
|
|
|
return MI_ERROR;
|
|
|
|
|
|
|
|
for (zso = zip_searchorder; *zso->suffix; zso++) {
|
|
|
|
strcpy(path + len, zso->suffix);
|
|
|
|
if (PyDict_GetItemString(self->files, path) != NULL) {
|
|
|
|
if (zso->type & IS_PACKAGE)
|
|
|
|
return MI_PACKAGE;
|
|
|
|
else
|
|
|
|
return MI_MODULE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return MI_NOT_FOUND;
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check whether we can satisfy the import of the module named by
|
|
|
|
'fullname'. Return self if we can, None if we can't. */
|
|
|
|
static PyObject *
|
|
|
|
zipimporter_find_module(PyObject *obj, PyObject *args)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
ZipImporter *self = (ZipImporter *)obj;
|
|
|
|
PyObject *path = NULL;
|
2010-10-18 18:44:33 -03:00
|
|
|
char *fullname;
|
2010-05-09 12:52:27 -03:00
|
|
|
enum zi_module_info mi;
|
|
|
|
|
2010-10-18 18:44:33 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "s|O:zipimporter.find_module",
|
|
|
|
&fullname, &path))
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
mi = get_module_info(self, fullname);
|
|
|
|
if (mi == MI_ERROR)
|
|
|
|
return NULL;
|
|
|
|
if (mi == MI_NOT_FOUND) {
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
Py_INCREF(self);
|
|
|
|
return (PyObject *)self;
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Load and return the module named by 'fullname'. */
|
|
|
|
static PyObject *
|
|
|
|
zipimporter_load_module(PyObject *obj, PyObject *args)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
ZipImporter *self = (ZipImporter *)obj;
|
2010-10-18 09:03:25 -03:00
|
|
|
PyObject *code = NULL, *mod, *dict;
|
2010-10-18 09:09:02 -03:00
|
|
|
char *fullname;
|
|
|
|
PyObject *modpath = NULL, *modpath_bytes;
|
2010-05-09 12:52:27 -03:00
|
|
|
int ispackage;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "s:zipimporter.load_module",
|
|
|
|
&fullname))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
code = get_module_code(self, fullname, &ispackage, &modpath);
|
|
|
|
if (code == NULL)
|
2010-10-18 09:03:25 -03:00
|
|
|
goto error;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
mod = PyImport_AddModule(fullname);
|
2010-10-18 09:03:25 -03:00
|
|
|
if (mod == NULL)
|
|
|
|
goto error;
|
2010-05-09 12:52:27 -03:00
|
|
|
dict = PyModule_GetDict(mod);
|
|
|
|
|
|
|
|
/* mod.__loader__ = self */
|
|
|
|
if (PyDict_SetItemString(dict, "__loader__", (PyObject *)self) != 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (ispackage) {
|
|
|
|
/* add __path__ to the module *before* the code gets
|
|
|
|
executed */
|
|
|
|
PyObject *pkgpath, *fullpath;
|
|
|
|
char *subname = get_subname(fullname);
|
|
|
|
int err;
|
|
|
|
|
|
|
|
fullpath = PyUnicode_FromFormat("%U%c%U%s",
|
|
|
|
self->archive, SEP,
|
|
|
|
self->prefix, subname);
|
|
|
|
if (fullpath == NULL)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
pkgpath = Py_BuildValue("[O]", fullpath);
|
|
|
|
Py_DECREF(fullpath);
|
|
|
|
if (pkgpath == NULL)
|
|
|
|
goto error;
|
|
|
|
err = PyDict_SetItemString(dict, "__path__", pkgpath);
|
|
|
|
Py_DECREF(pkgpath);
|
|
|
|
if (err != 0)
|
|
|
|
goto error;
|
|
|
|
}
|
2010-10-18 09:09:02 -03:00
|
|
|
modpath_bytes = PyUnicode_EncodeFSDefault(modpath);
|
|
|
|
if (modpath_bytes == NULL)
|
|
|
|
goto error;
|
|
|
|
mod = PyImport_ExecCodeModuleEx(fullname, code,
|
|
|
|
PyBytes_AS_STRING(modpath_bytes));
|
|
|
|
Py_DECREF(modpath_bytes);
|
2010-10-18 09:03:25 -03:00
|
|
|
Py_CLEAR(code);
|
|
|
|
if (mod == NULL)
|
|
|
|
goto error;
|
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (Py_VerboseFlag)
|
2010-10-18 09:09:02 -03:00
|
|
|
PySys_FormatStderr("import %s # loaded from Zip %U\n",
|
|
|
|
fullname, modpath);
|
|
|
|
Py_DECREF(modpath);
|
2010-05-09 12:52:27 -03:00
|
|
|
return mod;
|
2002-12-30 18:08:05 -04:00
|
|
|
error:
|
2010-10-18 09:03:25 -03:00
|
|
|
Py_XDECREF(code);
|
2010-10-18 09:09:02 -03:00
|
|
|
Py_XDECREF(modpath);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
2008-12-14 07:50:48 -04:00
|
|
|
/* Return a string matching __file__ for the named module */
|
|
|
|
static PyObject *
|
|
|
|
zipimporter_get_filename(PyObject *obj, PyObject *args)
|
|
|
|
{
|
|
|
|
ZipImporter *self = (ZipImporter *)obj;
|
|
|
|
PyObject *code;
|
2010-10-18 09:09:02 -03:00
|
|
|
char *fullname;
|
|
|
|
PyObject *modpath;
|
2008-12-14 07:50:48 -04:00
|
|
|
int ispackage;
|
|
|
|
|
2009-02-07 23:37:27 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "s:zipimporter.get_filename",
|
2010-10-18 19:34:46 -03:00
|
|
|
&fullname))
|
2010-10-18 08:39:05 -03:00
|
|
|
return NULL;
|
2008-12-14 07:50:48 -04:00
|
|
|
|
|
|
|
/* Deciding the filename requires working out where the code
|
|
|
|
would come from if the module was actually loaded */
|
|
|
|
code = get_module_code(self, fullname, &ispackage, &modpath);
|
|
|
|
if (code == NULL)
|
2010-10-18 08:39:05 -03:00
|
|
|
return NULL;
|
2008-12-14 07:50:48 -04:00
|
|
|
Py_DECREF(code); /* Only need the path info */
|
|
|
|
|
2010-10-18 09:09:02 -03:00
|
|
|
return modpath;
|
2008-12-14 07:50:48 -04:00
|
|
|
}
|
|
|
|
|
2002-12-30 18:08:05 -04:00
|
|
|
/* Return a bool signifying whether the module is a package or not. */
|
|
|
|
static PyObject *
|
|
|
|
zipimporter_is_package(PyObject *obj, PyObject *args)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
ZipImporter *self = (ZipImporter *)obj;
|
2010-10-18 18:44:33 -03:00
|
|
|
char *fullname;
|
2010-05-09 12:52:27 -03:00
|
|
|
enum zi_module_info mi;
|
|
|
|
|
2010-10-18 18:44:33 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "s:zipimporter.is_package",
|
|
|
|
&fullname))
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
mi = get_module_info(self, fullname);
|
|
|
|
if (mi == MI_ERROR)
|
2010-10-18 18:44:33 -03:00
|
|
|
return NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
if (mi == MI_NOT_FOUND) {
|
2010-10-18 19:45:54 -03:00
|
|
|
PyErr_Format(ZipImportError, "can't find module '%s'", fullname);
|
2010-10-18 18:44:33 -03:00
|
|
|
return NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
return PyBool_FromLong(mi == MI_PACKAGE);
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
zipimporter_get_data(PyObject *obj, PyObject *args)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
ZipImporter *self = (ZipImporter *)obj;
|
2010-08-16 20:48:11 -03:00
|
|
|
PyObject *pathobj, *key;
|
|
|
|
const Py_UNICODE *path;
|
2002-12-30 18:08:05 -04:00
|
|
|
#ifdef ALTSEP
|
2010-08-16 20:48:11 -03:00
|
|
|
Py_UNICODE *p, buf[MAXPATHLEN + 1];
|
2002-12-30 18:08:05 -04:00
|
|
|
#endif
|
2010-08-16 20:48:11 -03:00
|
|
|
Py_UNICODE *archive;
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *toc_entry;
|
2010-08-16 20:48:11 -03:00
|
|
|
Py_ssize_t path_len, len;
|
2002-12-30 18:08:05 -04:00
|
|
|
|
2010-08-16 20:48:11 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "U:zipimporter.get_data", &pathobj))
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
2002-12-30 18:08:05 -04:00
|
|
|
|
2010-08-16 20:48:11 -03:00
|
|
|
path_len = PyUnicode_GET_SIZE(pathobj);
|
|
|
|
path = PyUnicode_AS_UNICODE(pathobj);
|
2002-12-30 18:08:05 -04:00
|
|
|
#ifdef ALTSEP
|
2010-08-16 20:48:11 -03:00
|
|
|
if (path_len >= MAXPATHLEN) {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_SetString(ZipImportError, "path too long");
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-08-16 20:48:11 -03:00
|
|
|
Py_UNICODE_strcpy(buf, path);
|
2010-05-09 12:52:27 -03:00
|
|
|
for (p = buf; *p; p++) {
|
|
|
|
if (*p == ALTSEP)
|
|
|
|
*p = SEP;
|
|
|
|
}
|
|
|
|
path = buf;
|
2002-12-30 18:08:05 -04:00
|
|
|
#endif
|
2010-08-16 20:48:11 -03:00
|
|
|
archive = PyUnicode_AS_UNICODE(self->archive);
|
|
|
|
len = PyUnicode_GET_SIZE(self->archive);
|
|
|
|
if ((size_t)len < Py_UNICODE_strlen(path) &&
|
|
|
|
Py_UNICODE_strncmp(path, archive, len) == 0 &&
|
2010-05-09 12:52:27 -03:00
|
|
|
path[len] == SEP) {
|
2010-08-16 20:48:11 -03:00
|
|
|
path += len + 1;
|
|
|
|
path_len -= len + 1;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
|
2010-08-16 20:48:11 -03:00
|
|
|
key = PyUnicode_FromUnicode(path, path_len);
|
|
|
|
if (key == NULL)
|
|
|
|
return NULL;
|
|
|
|
toc_entry = PyDict_GetItem(self->files, key);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (toc_entry == NULL) {
|
2010-08-16 20:48:11 -03:00
|
|
|
PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, key);
|
|
|
|
Py_DECREF(key);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-08-16 20:48:11 -03:00
|
|
|
Py_DECREF(key);
|
|
|
|
return get_data(self->archive, toc_entry);
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
zipimporter_get_code(PyObject *obj, PyObject *args)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
ZipImporter *self = (ZipImporter *)obj;
|
|
|
|
char *fullname;
|
2002-12-30 18:08:05 -04:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "s:zipimporter.get_code", &fullname))
|
|
|
|
return NULL;
|
2002-12-30 18:08:05 -04:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
return get_module_code(self, fullname, NULL, NULL);
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
zipimporter_get_source(PyObject *obj, PyObject *args)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
ZipImporter *self = (ZipImporter *)obj;
|
|
|
|
PyObject *toc_entry;
|
2010-10-18 18:44:33 -03:00
|
|
|
char *fullname, *subname, path[MAXPATHLEN+1];
|
2010-05-09 12:52:27 -03:00
|
|
|
int len;
|
|
|
|
enum zi_module_info mi;
|
|
|
|
|
2010-10-18 18:44:33 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "s:zipimporter.get_source", &fullname))
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
mi = get_module_info(self, fullname);
|
2010-10-18 18:44:33 -03:00
|
|
|
if (mi == MI_ERROR)
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
if (mi == MI_NOT_FOUND) {
|
2010-10-18 19:45:54 -03:00
|
|
|
PyErr_Format(ZipImportError, "can't find module '%s'", fullname);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-10-18 18:44:33 -03:00
|
|
|
subname = get_subname(fullname);
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2010-10-18 17:40:59 -03:00
|
|
|
len = make_filename(self->prefix, subname, path, sizeof(path));
|
2010-05-09 12:52:27 -03:00
|
|
|
if (len < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (mi == MI_PACKAGE) {
|
|
|
|
path[len] = SEP;
|
|
|
|
strcpy(path + len + 1, "__init__.py");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
strcpy(path + len, ".py");
|
|
|
|
|
|
|
|
toc_entry = PyDict_GetItemString(self->files, path);
|
|
|
|
if (toc_entry != NULL) {
|
2010-08-16 20:48:11 -03:00
|
|
|
PyObject *res, *bytes;
|
|
|
|
bytes = get_data(self->archive, toc_entry);
|
|
|
|
if (bytes == NULL)
|
|
|
|
return NULL;
|
|
|
|
res = PyUnicode_FromStringAndSize(PyBytes_AS_STRING(bytes),
|
|
|
|
PyBytes_GET_SIZE(bytes));
|
|
|
|
Py_DECREF(bytes);
|
2010-05-09 12:52:27 -03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we have the module, but no source */
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(doc_find_module,
|
|
|
|
"find_module(fullname, path=None) -> self or None.\n\
|
|
|
|
\n\
|
|
|
|
Search for a module specified by 'fullname'. 'fullname' must be the\n\
|
|
|
|
fully qualified (dotted) module name. It returns the zipimporter\n\
|
|
|
|
instance itself if the module was found, or None if it wasn't.\n\
|
|
|
|
The optional 'path' argument is ignored -- it's there for compatibility\n\
|
|
|
|
with the importer protocol.");
|
|
|
|
|
|
|
|
PyDoc_STRVAR(doc_load_module,
|
|
|
|
"load_module(fullname) -> module.\n\
|
|
|
|
\n\
|
|
|
|
Load the module specified by 'fullname'. 'fullname' must be the\n\
|
|
|
|
fully qualified (dotted) module name. It returns the imported\n\
|
|
|
|
module, or raises ZipImportError if it wasn't found.");
|
|
|
|
|
|
|
|
PyDoc_STRVAR(doc_get_data,
|
|
|
|
"get_data(pathname) -> string with file data.\n\
|
|
|
|
\n\
|
|
|
|
Return the data associated with 'pathname'. Raise IOError if\n\
|
|
|
|
the file wasn't found.");
|
|
|
|
|
|
|
|
PyDoc_STRVAR(doc_is_package,
|
|
|
|
"is_package(fullname) -> bool.\n\
|
|
|
|
\n\
|
|
|
|
Return True if the module specified by fullname is a package.\n\
|
2010-07-20 22:44:19 -03:00
|
|
|
Raise ZipImportError if the module couldn't be found.");
|
2002-12-30 18:08:05 -04:00
|
|
|
|
|
|
|
PyDoc_STRVAR(doc_get_code,
|
|
|
|
"get_code(fullname) -> code object.\n\
|
|
|
|
\n\
|
|
|
|
Return the code object for the specified module. Raise ZipImportError\n\
|
2010-07-20 22:44:19 -03:00
|
|
|
if the module couldn't be found.");
|
2002-12-30 18:08:05 -04:00
|
|
|
|
|
|
|
PyDoc_STRVAR(doc_get_source,
|
|
|
|
"get_source(fullname) -> source string.\n\
|
|
|
|
\n\
|
|
|
|
Return the source code for the specified module. Raise ZipImportError\n\
|
2010-07-20 22:44:19 -03:00
|
|
|
if the module couldn't be found, return None if the archive does\n\
|
2002-12-30 18:08:05 -04:00
|
|
|
contain the module, but has no source for it.");
|
|
|
|
|
2008-12-14 07:50:48 -04:00
|
|
|
|
|
|
|
PyDoc_STRVAR(doc_get_filename,
|
2009-02-07 23:37:27 -04:00
|
|
|
"get_filename(fullname) -> filename string.\n\
|
2008-12-14 07:50:48 -04:00
|
|
|
\n\
|
|
|
|
Return the filename for the specified module.");
|
|
|
|
|
2002-12-30 18:08:05 -04:00
|
|
|
static PyMethodDef zipimporter_methods[] = {
|
2010-05-09 12:52:27 -03:00
|
|
|
{"find_module", zipimporter_find_module, METH_VARARGS,
|
|
|
|
doc_find_module},
|
|
|
|
{"load_module", zipimporter_load_module, METH_VARARGS,
|
|
|
|
doc_load_module},
|
|
|
|
{"get_data", zipimporter_get_data, METH_VARARGS,
|
|
|
|
doc_get_data},
|
|
|
|
{"get_code", zipimporter_get_code, METH_VARARGS,
|
|
|
|
doc_get_code},
|
|
|
|
{"get_source", zipimporter_get_source, METH_VARARGS,
|
|
|
|
doc_get_source},
|
|
|
|
{"get_filename", zipimporter_get_filename, METH_VARARGS,
|
|
|
|
doc_get_filename},
|
|
|
|
{"is_package", zipimporter_is_package, METH_VARARGS,
|
|
|
|
doc_is_package},
|
|
|
|
{NULL, NULL} /* sentinel */
|
2002-12-30 18:08:05 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static PyMemberDef zipimporter_members[] = {
|
2010-05-09 12:52:27 -03:00
|
|
|
{"archive", T_OBJECT, offsetof(ZipImporter, archive), READONLY},
|
|
|
|
{"prefix", T_OBJECT, offsetof(ZipImporter, prefix), READONLY},
|
|
|
|
{"_files", T_OBJECT, offsetof(ZipImporter, files), READONLY},
|
|
|
|
{NULL}
|
2002-12-30 18:08:05 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
PyDoc_STRVAR(zipimporter_doc,
|
|
|
|
"zipimporter(archivepath) -> zipimporter object\n\
|
|
|
|
\n\
|
|
|
|
Create a new zipimporter instance. 'archivepath' must be a path to\n\
|
Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63066 | georg.brandl | 2008-05-11 10:56:04 -0400 (Sun, 11 May 2008) | 2 lines
#2709 followup: better description of Tk's pros and cons.
........
r63067 | georg.brandl | 2008-05-11 11:05:13 -0400 (Sun, 11 May 2008) | 2 lines
#1326: document and test zipimporter.archive and zipimporter.prefix.
........
r63068 | georg.brandl | 2008-05-11 11:07:39 -0400 (Sun, 11 May 2008) | 2 lines
#2816: clarify error messages for EOF while scanning strings.
........
r63069 | georg.brandl | 2008-05-11 11:17:41 -0400 (Sun, 11 May 2008) | 3 lines
#2787: Flush stdout after writing test name, helpful when running
hanging or long-running tests. Patch by Adam Olsen.
........
r63070 | georg.brandl | 2008-05-11 11:20:16 -0400 (Sun, 11 May 2008) | 3 lines
#2803: fix wrong invocation of heappush in seldom-reached code.
Thanks to Matt Harden.
........
r63073 | benjamin.peterson | 2008-05-11 12:38:07 -0400 (Sun, 11 May 2008) | 2 lines
broaden .bzrignore
........
r63076 | andrew.kuchling | 2008-05-11 15:15:52 -0400 (Sun, 11 May 2008) | 1 line
Add message to test assertion
........
r63083 | andrew.kuchling | 2008-05-11 16:08:33 -0400 (Sun, 11 May 2008) | 1 line
Try setting HOME env.var to fix test on Win32
........
r63092 | georg.brandl | 2008-05-11 16:53:55 -0400 (Sun, 11 May 2008) | 2 lines
#2809 followup: even better split docstring.
........
r63094 | georg.brandl | 2008-05-11 17:03:42 -0400 (Sun, 11 May 2008) | 4 lines
- #2250: Exceptions raised during evaluation of names in rlcompleter's
``Completer.complete()`` method are now caught and ignored.
........
r63095 | georg.brandl | 2008-05-11 17:16:37 -0400 (Sun, 11 May 2008) | 2 lines
Clarify os.strerror()s exception behavior.
........
r63097 | georg.brandl | 2008-05-11 17:34:10 -0400 (Sun, 11 May 2008) | 2 lines
#2535: remove duplicated method.
........
r63104 | alexandre.vassalotti | 2008-05-11 19:04:27 -0400 (Sun, 11 May 2008) | 2 lines
Moved the Queue module stub in lib-old.
........
2008-05-15 21:41:41 -03:00
|
|
|
a zipfile, or to a specific path inside a zipfile. For example, it can be\n\
|
|
|
|
'/tmp/myimport.zip', or '/tmp/myimport.zip/mydirectory', if mydirectory is a\n\
|
|
|
|
valid directory inside the archive.\n\
|
|
|
|
\n\
|
|
|
|
'ZipImportError is raised if 'archivepath' doesn't point to a valid Zip\n\
|
|
|
|
archive.\n\
|
|
|
|
\n\
|
|
|
|
The 'archive' attribute of zipimporter objects contains the name of the\n\
|
|
|
|
zipfile targeted.");
|
2002-12-30 18:08:05 -04:00
|
|
|
|
|
|
|
#define DEFERRED_ADDRESS(ADDR) 0
|
|
|
|
|
|
|
|
static PyTypeObject ZipImporter_Type = {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
|
|
|
|
"zipimport.zipimporter",
|
|
|
|
sizeof(ZipImporter),
|
|
|
|
0, /* tp_itemsize */
|
|
|
|
(destructor)zipimporter_dealloc, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
0, /* tp_reserved */
|
|
|
|
(reprfunc)zipimporter_repr, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
|
|
|
|
Py_TPFLAGS_HAVE_GC, /* tp_flags */
|
|
|
|
zipimporter_doc, /* tp_doc */
|
|
|
|
zipimporter_traverse, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
zipimporter_methods, /* tp_methods */
|
|
|
|
zipimporter_members, /* tp_members */
|
|
|
|
0, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
0, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
0, /* tp_dictoffset */
|
|
|
|
(initproc)zipimporter_init, /* tp_init */
|
|
|
|
PyType_GenericAlloc, /* tp_alloc */
|
|
|
|
PyType_GenericNew, /* tp_new */
|
|
|
|
PyObject_GC_Del, /* tp_free */
|
2002-12-30 18:08:05 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* implementation */
|
|
|
|
|
|
|
|
/* Given a buffer, return the long that is represented by the first
|
|
|
|
4 bytes, encoded as little endian. This partially reimplements
|
|
|
|
marshal.c:r_long() */
|
|
|
|
static long
|
|
|
|
get_long(unsigned char *buf) {
|
2010-05-09 12:52:27 -03:00
|
|
|
long x;
|
|
|
|
x = buf[0];
|
|
|
|
x |= (long)buf[1] << 8;
|
|
|
|
x |= (long)buf[2] << 16;
|
|
|
|
x |= (long)buf[3] << 24;
|
2002-12-30 18:08:05 -04:00
|
|
|
#if SIZEOF_LONG > 4
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Sign extension for 64-bit machines */
|
|
|
|
x |= -(x & 0x80000000L);
|
2002-12-30 18:08:05 -04:00
|
|
|
#endif
|
2010-05-09 12:52:27 -03:00
|
|
|
return x;
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
read_directory(archive) -> files dict (new reference)
|
|
|
|
|
|
|
|
Given a path to a Zip archive, build a dict, mapping file names
|
|
|
|
(local to the archive, using SEP as a separator) to toc entries.
|
|
|
|
|
|
|
|
A toc_entry is a tuple:
|
|
|
|
|
2010-10-18 09:09:02 -03:00
|
|
|
(__file__, # value to use for __file__, available for all files,
|
|
|
|
# encoded to the filesystem encoding
|
2010-05-09 12:52:27 -03:00
|
|
|
compress, # compression kind; 0 for uncompressed
|
|
|
|
data_size, # size of compressed data on disk
|
|
|
|
file_size, # size of decompressed data
|
|
|
|
file_offset, # offset of file header from start of archive
|
|
|
|
time, # mod time of file (in dos format)
|
|
|
|
date, # mod data of file (in dos format)
|
|
|
|
crc, # crc checksum of the data
|
2010-10-18 08:39:05 -03:00
|
|
|
)
|
2002-12-30 18:08:05 -04:00
|
|
|
|
|
|
|
Directories can be recognized by the trailing SEP in the name,
|
|
|
|
data_size and file_offset are 0.
|
|
|
|
*/
|
|
|
|
static PyObject *
|
2010-08-16 14:54:28 -03:00
|
|
|
read_directory(PyObject *archive_obj)
|
2002-12-30 18:08:05 -04:00
|
|
|
{
|
2010-08-16 14:54:28 -03:00
|
|
|
/* FIXME: work on Py_UNICODE* instead of char* */
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *files = NULL;
|
|
|
|
FILE *fp;
|
2010-10-18 09:13:46 -03:00
|
|
|
unsigned short flags;
|
2010-05-09 12:52:27 -03:00
|
|
|
long compress, crc, data_size, file_size, file_offset, date, time;
|
|
|
|
long header_offset, name_size, header_size, header_position;
|
|
|
|
long i, l, count;
|
|
|
|
size_t length;
|
2010-08-16 14:54:28 -03:00
|
|
|
Py_UNICODE path[MAXPATHLEN + 5];
|
2010-05-09 12:52:27 -03:00
|
|
|
char name[MAXPATHLEN + 5];
|
2010-08-16 14:54:28 -03:00
|
|
|
PyObject *nameobj = NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
char *p, endof_central_dir[22];
|
|
|
|
long arc_offset; /* offset from beginning of file to start of zip-archive */
|
2010-08-16 14:54:28 -03:00
|
|
|
PyObject *pathobj;
|
2010-10-18 09:13:46 -03:00
|
|
|
const char *charset;
|
2011-01-22 06:30:29 -04:00
|
|
|
int bootstrap;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2010-08-16 14:54:28 -03:00
|
|
|
if (PyUnicode_GET_SIZE(archive_obj) > MAXPATHLEN) {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
|
|
"Zip path name is too long");
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-08-16 14:54:28 -03:00
|
|
|
Py_UNICODE_strcpy(path, PyUnicode_AS_UNICODE(archive_obj));
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2010-08-16 14:54:28 -03:00
|
|
|
fp = _Py_fopen(archive_obj, "rb");
|
2010-05-09 12:52:27 -03:00
|
|
|
if (fp == NULL) {
|
2011-12-18 16:04:17 -04:00
|
|
|
if (!PyErr_Occurred())
|
|
|
|
PyErr_Format(ZipImportError, "can't open Zip file: '%U'", archive_obj);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
fseek(fp, -22, SEEK_END);
|
|
|
|
header_position = ftell(fp);
|
|
|
|
if (fread(endof_central_dir, 1, 22, fp) != 22) {
|
|
|
|
fclose(fp);
|
2010-10-18 19:45:54 -03:00
|
|
|
PyErr_Format(ZipImportError, "can't read Zip file: '%U'", archive_obj);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (get_long((unsigned char *)endof_central_dir) != 0x06054B50) {
|
|
|
|
/* Bad: End of Central Dir signature */
|
|
|
|
fclose(fp);
|
2010-10-18 19:45:54 -03:00
|
|
|
PyErr_Format(ZipImportError, "not a Zip file: '%U'", archive_obj);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
header_size = get_long((unsigned char *)endof_central_dir + 12);
|
|
|
|
header_offset = get_long((unsigned char *)endof_central_dir + 16);
|
|
|
|
arc_offset = header_position - header_offset - header_size;
|
|
|
|
header_offset += arc_offset;
|
|
|
|
|
|
|
|
files = PyDict_New();
|
|
|
|
if (files == NULL)
|
|
|
|
goto error;
|
|
|
|
|
2010-08-16 14:54:28 -03:00
|
|
|
length = Py_UNICODE_strlen(path);
|
2010-05-09 12:52:27 -03:00
|
|
|
path[length] = SEP;
|
|
|
|
|
|
|
|
/* Start of Central Directory */
|
|
|
|
count = 0;
|
|
|
|
for (;;) {
|
|
|
|
PyObject *t;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
fseek(fp, header_offset, 0); /* Start of file header */
|
|
|
|
l = PyMarshal_ReadLongFromFile(fp);
|
|
|
|
if (l != 0x02014B50)
|
|
|
|
break; /* Bad: Central Dir File Header */
|
2010-10-18 09:13:46 -03:00
|
|
|
fseek(fp, header_offset + 8, 0);
|
|
|
|
flags = (unsigned short)PyMarshal_ReadShortFromFile(fp);
|
2010-05-09 12:52:27 -03:00
|
|
|
compress = PyMarshal_ReadShortFromFile(fp);
|
|
|
|
time = PyMarshal_ReadShortFromFile(fp);
|
|
|
|
date = PyMarshal_ReadShortFromFile(fp);
|
|
|
|
crc = PyMarshal_ReadLongFromFile(fp);
|
|
|
|
data_size = PyMarshal_ReadLongFromFile(fp);
|
|
|
|
file_size = PyMarshal_ReadLongFromFile(fp);
|
|
|
|
name_size = PyMarshal_ReadShortFromFile(fp);
|
|
|
|
header_size = 46 + name_size +
|
|
|
|
PyMarshal_ReadShortFromFile(fp) +
|
|
|
|
PyMarshal_ReadShortFromFile(fp);
|
|
|
|
fseek(fp, header_offset + 42, 0);
|
|
|
|
file_offset = PyMarshal_ReadLongFromFile(fp) + arc_offset;
|
|
|
|
if (name_size > MAXPATHLEN)
|
|
|
|
name_size = MAXPATHLEN;
|
|
|
|
|
|
|
|
p = name;
|
|
|
|
for (i = 0; i < name_size; i++) {
|
|
|
|
*p = (char)getc(fp);
|
|
|
|
if (*p == '/')
|
|
|
|
*p = SEP;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
*p = 0; /* Add terminating null byte */
|
|
|
|
header_offset += header_size;
|
|
|
|
|
2011-01-22 06:30:29 -04:00
|
|
|
bootstrap = 0;
|
2010-10-18 09:13:46 -03:00
|
|
|
if (flags & 0x0800)
|
|
|
|
charset = "utf-8";
|
2011-01-22 06:30:29 -04:00
|
|
|
else if (!PyThreadState_GET()->interp->codecs_initialized) {
|
|
|
|
/* During bootstrap, we may need to load the encodings
|
|
|
|
package from a ZIP file. But the cp437 encoding is implemented
|
|
|
|
in Python in the encodings package.
|
|
|
|
|
|
|
|
Break out of this dependency by assuming that the path to
|
|
|
|
the encodings module is ASCII-only. */
|
|
|
|
charset = "ascii";
|
|
|
|
bootstrap = 1;
|
|
|
|
}
|
2010-10-18 09:13:46 -03:00
|
|
|
else
|
|
|
|
charset = "cp437";
|
|
|
|
nameobj = PyUnicode_Decode(name, name_size, charset, NULL);
|
2011-01-22 06:30:29 -04:00
|
|
|
if (nameobj == NULL) {
|
|
|
|
if (bootstrap)
|
|
|
|
PyErr_Format(PyExc_NotImplementedError,
|
|
|
|
"bootstrap issue: python%i%i.zip contains non-ASCII "
|
|
|
|
"filenames without the unicode flag",
|
|
|
|
PY_MAJOR_VERSION, PY_MINOR_VERSION);
|
2010-08-16 14:54:28 -03:00
|
|
|
goto error;
|
2011-01-22 06:30:29 -04:00
|
|
|
}
|
2010-08-16 14:54:28 -03:00
|
|
|
Py_UNICODE_strncpy(path + length + 1, PyUnicode_AS_UNICODE(nameobj), MAXPATHLEN - length - 1);
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2010-08-16 14:54:28 -03:00
|
|
|
pathobj = PyUnicode_FromUnicode(path, Py_UNICODE_strlen(path));
|
|
|
|
if (pathobj == NULL)
|
|
|
|
goto error;
|
|
|
|
t = Py_BuildValue("Niiiiiii", pathobj, compress, data_size,
|
2010-05-09 12:52:27 -03:00
|
|
|
file_size, file_offset, time, date, crc);
|
|
|
|
if (t == NULL)
|
|
|
|
goto error;
|
2010-08-16 14:54:28 -03:00
|
|
|
err = PyDict_SetItem(files, nameobj, t);
|
|
|
|
Py_CLEAR(nameobj);
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_DECREF(t);
|
|
|
|
if (err != 0)
|
|
|
|
goto error;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
if (Py_VerboseFlag)
|
2010-08-16 14:54:28 -03:00
|
|
|
PySys_FormatStderr("# zipimport: found %ld names in %U\n",
|
|
|
|
count, archive_obj);
|
2010-05-09 12:52:27 -03:00
|
|
|
return files;
|
2002-12-30 18:08:05 -04:00
|
|
|
error:
|
2010-05-09 12:52:27 -03:00
|
|
|
fclose(fp);
|
|
|
|
Py_XDECREF(files);
|
2010-08-16 14:54:28 -03:00
|
|
|
Py_XDECREF(nameobj);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the zlib.decompress function object, or NULL if zlib couldn't
|
|
|
|
be imported. The function is cached when found, so subsequent calls
|
2011-05-19 19:16:09 -03:00
|
|
|
don't import zlib again. */
|
2002-12-30 18:08:05 -04:00
|
|
|
static PyObject *
|
|
|
|
get_decompress_func(void)
|
|
|
|
{
|
2011-05-19 19:16:09 -03:00
|
|
|
static int importing_zlib = 0;
|
|
|
|
PyObject *zlib;
|
|
|
|
PyObject *decompress;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2011-05-19 19:16:09 -03:00
|
|
|
if (importing_zlib != 0)
|
|
|
|
/* Someone has a zlib.py[co] in their Zip file;
|
|
|
|
let's avoid a stack overflow. */
|
|
|
|
return NULL;
|
|
|
|
importing_zlib = 1;
|
|
|
|
zlib = PyImport_ImportModuleNoBlock("zlib");
|
|
|
|
importing_zlib = 0;
|
|
|
|
if (zlib != NULL) {
|
|
|
|
decompress = PyObject_GetAttrString(zlib,
|
|
|
|
"decompress");
|
|
|
|
Py_DECREF(zlib);
|
2010-05-09 13:14:21 -03:00
|
|
|
}
|
2011-05-19 19:16:09 -03:00
|
|
|
else {
|
|
|
|
PyErr_Clear();
|
|
|
|
decompress = NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2011-05-19 19:16:09 -03:00
|
|
|
if (Py_VerboseFlag)
|
|
|
|
PySys_WriteStderr("# zipimport: zlib %s\n",
|
|
|
|
zlib != NULL ? "available": "UNAVAILABLE");
|
2010-05-09 12:52:27 -03:00
|
|
|
return decompress;
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Given a path to a Zip file and a toc_entry, return the (uncompressed)
|
|
|
|
data as a new reference. */
|
|
|
|
static PyObject *
|
2010-08-16 20:48:11 -03:00
|
|
|
get_data(PyObject *archive, PyObject *toc_entry)
|
2002-12-30 18:08:05 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *raw_data, *data = NULL, *decompress;
|
|
|
|
char *buf;
|
|
|
|
FILE *fp;
|
|
|
|
int err;
|
|
|
|
Py_ssize_t bytes_read = 0;
|
|
|
|
long l;
|
2010-08-16 20:48:11 -03:00
|
|
|
PyObject *datapath;
|
2010-05-09 12:52:27 -03:00
|
|
|
long compress, data_size, file_size, file_offset, bytes_size;
|
|
|
|
long time, date, crc;
|
|
|
|
|
2010-08-16 20:48:11 -03:00
|
|
|
if (!PyArg_ParseTuple(toc_entry, "Olllllll", &datapath, &compress,
|
2010-05-09 12:52:27 -03:00
|
|
|
&data_size, &file_size, &file_offset, &time,
|
|
|
|
&date, &crc)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-08-16 20:48:11 -03:00
|
|
|
fp = _Py_fopen(archive, "rb");
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!fp) {
|
2011-12-18 16:04:17 -04:00
|
|
|
if (!PyErr_Occurred())
|
|
|
|
PyErr_Format(PyExc_IOError,
|
|
|
|
"zipimport: can not open file %U", archive);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check to make sure the local file header is correct */
|
|
|
|
fseek(fp, file_offset, 0);
|
|
|
|
l = PyMarshal_ReadLongFromFile(fp);
|
|
|
|
if (l != 0x04034B50) {
|
|
|
|
/* Bad: Local File Header */
|
|
|
|
PyErr_Format(ZipImportError,
|
2010-08-16 20:48:11 -03:00
|
|
|
"bad local file header in %U",
|
2010-05-09 12:52:27 -03:00
|
|
|
archive);
|
|
|
|
fclose(fp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
fseek(fp, file_offset + 26, 0);
|
|
|
|
l = 30 + PyMarshal_ReadShortFromFile(fp) +
|
|
|
|
PyMarshal_ReadShortFromFile(fp); /* local header size */
|
|
|
|
file_offset += l; /* Start of file data */
|
|
|
|
|
|
|
|
bytes_size = compress == 0 ? data_size : data_size + 1;
|
|
|
|
if (bytes_size == 0)
|
|
|
|
bytes_size++;
|
|
|
|
raw_data = PyBytes_FromStringAndSize((char *)NULL, bytes_size);
|
|
|
|
|
|
|
|
if (raw_data == NULL) {
|
|
|
|
fclose(fp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
buf = PyBytes_AsString(raw_data);
|
|
|
|
|
|
|
|
err = fseek(fp, file_offset, 0);
|
|
|
|
if (err == 0)
|
|
|
|
bytes_read = fread(buf, 1, data_size, fp);
|
|
|
|
fclose(fp);
|
|
|
|
if (err || bytes_read != data_size) {
|
|
|
|
PyErr_SetString(PyExc_IOError,
|
|
|
|
"zipimport: can't read data");
|
|
|
|
Py_DECREF(raw_data);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (compress != 0) {
|
|
|
|
buf[data_size] = 'Z'; /* saw this in zipfile.py */
|
|
|
|
data_size++;
|
|
|
|
}
|
|
|
|
buf[data_size] = '\0';
|
|
|
|
|
|
|
|
if (compress == 0) { /* data is not compressed */
|
|
|
|
data = PyBytes_FromStringAndSize(buf, data_size);
|
|
|
|
Py_DECREF(raw_data);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decompress with zlib */
|
|
|
|
decompress = get_decompress_func();
|
|
|
|
if (decompress == NULL) {
|
|
|
|
PyErr_SetString(ZipImportError,
|
|
|
|
"can't decompress data; "
|
|
|
|
"zlib not available");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
data = PyObject_CallFunction(decompress, "Oi", raw_data, -15);
|
2011-05-19 19:16:09 -03:00
|
|
|
Py_DECREF(decompress);
|
2002-12-30 18:08:05 -04:00
|
|
|
error:
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_DECREF(raw_data);
|
|
|
|
return data;
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Lenient date/time comparison function. The precision of the mtime
|
|
|
|
in the archive is lower than the mtime stored in a .pyc: we
|
|
|
|
must allow a difference of at most one second. */
|
|
|
|
static int
|
|
|
|
eq_mtime(time_t t1, time_t t2)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
time_t d = t1 - t2;
|
|
|
|
if (d < 0)
|
|
|
|
d = -d;
|
|
|
|
/* dostime only stores even seconds, so be lenient */
|
|
|
|
return d <= 1;
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Given the contents of a .py[co] file in a buffer, unmarshal the data
|
|
|
|
and return the code object. Return None if it the magic word doesn't
|
|
|
|
match (we do this instead of raising an exception as we fall back
|
|
|
|
to .py if available and we don't want to mask other errors).
|
|
|
|
Returns a new reference. */
|
|
|
|
static PyObject *
|
|
|
|
unmarshal_code(char *pathname, PyObject *data, time_t mtime)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *code;
|
|
|
|
char *buf = PyBytes_AsString(data);
|
|
|
|
Py_ssize_t size = PyBytes_Size(data);
|
|
|
|
|
|
|
|
if (size <= 9) {
|
|
|
|
PyErr_SetString(ZipImportError,
|
|
|
|
"bad pyc data");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_long((unsigned char *)buf) != PyImport_GetMagicNumber()) {
|
|
|
|
if (Py_VerboseFlag)
|
|
|
|
PySys_WriteStderr("# %s has bad magic\n",
|
|
|
|
pathname);
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None; /* signal caller to try alternative */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mtime != 0 && !eq_mtime(get_long((unsigned char *)buf + 4),
|
|
|
|
mtime)) {
|
|
|
|
if (Py_VerboseFlag)
|
|
|
|
PySys_WriteStderr("# %s has bad mtime\n",
|
|
|
|
pathname);
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None; /* signal caller to try alternative */
|
|
|
|
}
|
|
|
|
|
|
|
|
code = PyMarshal_ReadObjectFromString(buf + 8, size - 8);
|
|
|
|
if (code == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (!PyCode_Check(code)) {
|
|
|
|
Py_DECREF(code);
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
2010-10-18 19:45:54 -03:00
|
|
|
"compiled module %s is not a code object",
|
2010-05-09 12:52:27 -03:00
|
|
|
pathname);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return code;
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Replace any occurances of "\r\n?" in the input string with "\n".
|
|
|
|
This converts DOS and Mac line endings to Unix line endings.
|
|
|
|
Also append a trailing "\n" to be compatible with
|
|
|
|
PyParser_SimpleParseFile(). Returns a new reference. */
|
|
|
|
static PyObject *
|
|
|
|
normalize_line_endings(PyObject *source)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
char *buf, *q, *p = PyBytes_AsString(source);
|
|
|
|
PyObject *fixed_source;
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
if (!p) {
|
|
|
|
return PyBytes_FromStringAndSize("\n\0", 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* one char extra for trailing \n and one for terminating \0 */
|
|
|
|
buf = (char *)PyMem_Malloc(PyBytes_Size(source) + 2);
|
|
|
|
if (buf == NULL) {
|
|
|
|
PyErr_SetString(PyExc_MemoryError,
|
|
|
|
"zipimport: no memory to allocate "
|
|
|
|
"source buffer");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* replace "\r\n?" by "\n" */
|
|
|
|
for (q = buf; *p != '\0'; p++) {
|
|
|
|
if (*p == '\r') {
|
|
|
|
*q++ = '\n';
|
|
|
|
if (*(p + 1) == '\n')
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*q++ = *p;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
*q++ = '\n'; /* add trailing \n */
|
|
|
|
*q = '\0';
|
|
|
|
fixed_source = PyBytes_FromStringAndSize(buf, len + 2);
|
|
|
|
PyMem_Free(buf);
|
|
|
|
return fixed_source;
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Given a string buffer containing Python source code, compile it
|
|
|
|
return and return a code object as a new reference. */
|
|
|
|
static PyObject *
|
|
|
|
compile_source(char *pathname, PyObject *source)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *code, *fixed_source;
|
2002-12-30 18:08:05 -04:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
fixed_source = normalize_line_endings(source);
|
|
|
|
if (fixed_source == NULL)
|
|
|
|
return NULL;
|
2002-12-30 18:08:05 -04:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
code = Py_CompileString(PyBytes_AsString(fixed_source), pathname,
|
|
|
|
Py_file_input);
|
|
|
|
Py_DECREF(fixed_source);
|
|
|
|
return code;
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert the date/time values found in the Zip archive to a value
|
|
|
|
that's compatible with the time stamp stored in .pyc files. */
|
2003-03-23 09:21:03 -04:00
|
|
|
static time_t
|
|
|
|
parse_dostime(int dostime, int dosdate)
|
2002-12-30 18:08:05 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
struct tm stm;
|
2002-12-30 18:08:05 -04:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
memset((void *) &stm, '\0', sizeof(stm));
|
Merged revisions 59985-60000,60002,60005-60007,60009-60042 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59987 | raymond.hettinger | 2008-01-15 21:52:42 +0100 (Tue, 15 Jan 2008) | 1 line
Refactor if/elif chain for clarity and speed. Remove dependency on subclasses having to implement _empty and _full.
........
r59988 | raymond.hettinger | 2008-01-15 22:22:47 +0100 (Tue, 15 Jan 2008) | 1 line
Fix-up half-written paragraph in the docs
........
r59989 | amaury.forgeotdarc | 2008-01-15 22:25:11 +0100 (Tue, 15 Jan 2008) | 3 lines
test_doctest fails since r59984.
Not sure if these are the correct values, but save_stdout has to be set before its usage...
........
r59992 | andrew.kuchling | 2008-01-16 01:32:03 +0100 (Wed, 16 Jan 2008) | 1 line
Docstring typos
........
r59993 | andrew.kuchling | 2008-01-16 04:17:25 +0100 (Wed, 16 Jan 2008) | 1 line
Add PEP 3141 section
........
r59998 | andrew.kuchling | 2008-01-16 14:01:51 +0100 (Wed, 16 Jan 2008) | 1 line
Markup fix
........
r59999 | georg.brandl | 2008-01-16 17:56:29 +0100 (Wed, 16 Jan 2008) | 2 lines
Fix MSDN library URL. (#1854)
........
r60006 | georg.brandl | 2008-01-16 21:27:56 +0100 (Wed, 16 Jan 2008) | 3 lines
Add Python-specific content to Doc dir. Update configuration file
to work with the newest Sphinx.
........
r60007 | georg.brandl | 2008-01-16 21:29:00 +0100 (Wed, 16 Jan 2008) | 2 lines
Doc build should work with 2.4 now.
........
r60009 | raymond.hettinger | 2008-01-17 00:38:16 +0100 (Thu, 17 Jan 2008) | 1 line
Minor wordsmithing.
........
r60010 | raymond.hettinger | 2008-01-17 00:40:45 +0100 (Thu, 17 Jan 2008) | 1 line
Add queues will alternative fetch orders (priority based and stack based).
........
r60011 | raymond.hettinger | 2008-01-17 00:49:35 +0100 (Thu, 17 Jan 2008) | 1 line
Add news entry.
........
r60013 | raymond.hettinger | 2008-01-17 04:02:14 +0100 (Thu, 17 Jan 2008) | 1 line
Make starmap() match its pure python definition and accept any itertable input (not just tuples).
........
r60015 | gregory.p.smith | 2008-01-17 08:43:20 +0100 (Thu, 17 Jan 2008) | 3 lines
Comply with RFC 3207.
Fixes issue 829951 - http://bugs.python.org/issue829951
........
r60018 | gregory.p.smith | 2008-01-17 09:03:17 +0100 (Thu, 17 Jan 2008) | 2 lines
entry for r60015
........
r60019 | raymond.hettinger | 2008-01-17 09:07:05 +0100 (Thu, 17 Jan 2008) | 1 line
Note versionadded.
........
r60020 | gregory.p.smith | 2008-01-17 09:35:49 +0100 (Thu, 17 Jan 2008) | 8 lines
Fixes (accepts patch) issue1339 - http://bugs.python.org/issue1339
- Factor out the duplication of EHLO/HELO in login() and sendmail() to
a new function, ehlo_or_helo_if_needed().
- Use ehlo_or_helo_if_needed() in starttls()
- Check for the starttls exception in starttls() in the same way as
login() checks for the auth extension.
Contributed by Bill Fenner.
........
r60021 | andrew.kuchling | 2008-01-17 13:00:15 +0100 (Thu, 17 Jan 2008) | 1 line
Revise 3141 section a bit; add some Windows items
........
r60022 | brett.cannon | 2008-01-17 19:45:10 +0100 (Thu, 17 Jan 2008) | 2 lines
Fix a function pointer declaration to silence the compiler.
........
r60024 | raymond.hettinger | 2008-01-17 20:31:38 +0100 (Thu, 17 Jan 2008) | 1 line
Issue #1861: Add read-only attribute listing upcoming events in the order they will be run.
........
r60025 | andrew.kuchling | 2008-01-17 20:49:24 +0100 (Thu, 17 Jan 2008) | 1 line
Correction from Jordan Lewis: halfdelay() uses tenths of a second, not milliseconds
........
r60026 | raymond.hettinger | 2008-01-17 23:27:49 +0100 (Thu, 17 Jan 2008) | 1 line
Add advice on choosing between scheduler and threading.Timer().
........
r60028 | christian.heimes | 2008-01-18 00:01:44 +0100 (Fri, 18 Jan 2008) | 2 lines
Updated new property syntax. An elaborate example for subclassing and the getter was missing.
Added comment about VS 2008 and PGO builds.
........
r60029 | raymond.hettinger | 2008-01-18 00:32:01 +0100 (Fri, 18 Jan 2008) | 1 line
Fix-up Timer() example.
........
r60030 | raymond.hettinger | 2008-01-18 00:56:56 +0100 (Fri, 18 Jan 2008) | 1 line
Fix markup
........
r60031 | raymond.hettinger | 2008-01-18 01:10:42 +0100 (Fri, 18 Jan 2008) | 1 line
clearcache() needs to remove the dict as well as clear it.
........
r60033 | andrew.kuchling | 2008-01-18 03:26:16 +0100 (Fri, 18 Jan 2008) | 1 line
Bump verson
........
r60034 | andrew.kuchling | 2008-01-18 03:42:52 +0100 (Fri, 18 Jan 2008) | 1 line
Typo fix
........
r60035 | christian.heimes | 2008-01-18 08:30:20 +0100 (Fri, 18 Jan 2008) | 3 lines
Coverity issue CID #197
var_decl: Declared variable "stm" without initializer
ninit_use_in_call: Using uninitialized value "stm" (field "stm".tm_zone uninitialized) in call to function "mktime"
........
r60036 | christian.heimes | 2008-01-18 08:45:30 +0100 (Fri, 18 Jan 2008) | 11 lines
Coverity issue CID #167
Event alloc_fn: Called allocation function "metacompile" [model]
Event var_assign: Assigned variable "gr" to storage returned from "metacompile"
gr = metacompile(n);
Event pass_arg: Variable "gr" not freed or pointed-to in function "maketables" [model]
g = maketables(gr);
translatelabels(g);
addfirstsets(g);
Event leaked_storage: Returned without freeing storage "gr"
return g;
........
r60038 | christian.heimes | 2008-01-18 09:04:57 +0100 (Fri, 18 Jan 2008) | 3 lines
Coverity issue CID #182
size_error: Allocating 1 bytes to pointer "children", which needs at least 4 bytes
........
r60041 | christian.heimes | 2008-01-18 09:47:59 +0100 (Fri, 18 Jan 2008) | 4 lines
Coverity issue CID #169
local_ptr_assign_local: Assigning address of stack variable "namebuf" to pointer "filename"
out_of_scope: Variable "namebuf" goes out of scope
use_invalid: Used "filename" pointing to out-of-scope variable "namebuf"
........
r60042 | christian.heimes | 2008-01-18 09:53:45 +0100 (Fri, 18 Jan 2008) | 2 lines
Coverity CID #168
leaked_storage: Returned without freeing storage "fp"
........
2008-01-18 05:56:22 -04:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
stm.tm_sec = (dostime & 0x1f) * 2;
|
|
|
|
stm.tm_min = (dostime >> 5) & 0x3f;
|
|
|
|
stm.tm_hour = (dostime >> 11) & 0x1f;
|
|
|
|
stm.tm_mday = dosdate & 0x1f;
|
|
|
|
stm.tm_mon = ((dosdate >> 5) & 0x0f) - 1;
|
|
|
|
stm.tm_year = ((dosdate >> 9) & 0x7f) + 80;
|
|
|
|
stm.tm_isdst = -1; /* wday/yday is ignored */
|
2002-12-30 18:08:05 -04:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
return mktime(&stm);
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Given a path to a .pyc or .pyo file in the archive, return the
|
2011-03-16 06:05:33 -03:00
|
|
|
modification time of the matching .py file, or 0 if no source
|
2002-12-30 18:08:05 -04:00
|
|
|
is available. */
|
|
|
|
static time_t
|
|
|
|
get_mtime_of_source(ZipImporter *self, char *path)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *toc_entry;
|
|
|
|
time_t mtime = 0;
|
|
|
|
Py_ssize_t lastchar = strlen(path) - 1;
|
|
|
|
char savechar = path[lastchar];
|
|
|
|
path[lastchar] = '\0'; /* strip 'c' or 'o' from *.py[co] */
|
|
|
|
toc_entry = PyDict_GetItemString(self->files, path);
|
|
|
|
if (toc_entry != NULL && PyTuple_Check(toc_entry) &&
|
|
|
|
PyTuple_Size(toc_entry) == 8) {
|
|
|
|
/* fetch the time stamp of the .py file for comparison
|
|
|
|
with an embedded pyc time stamp */
|
|
|
|
int time, date;
|
|
|
|
time = PyLong_AsLong(PyTuple_GetItem(toc_entry, 5));
|
|
|
|
date = PyLong_AsLong(PyTuple_GetItem(toc_entry, 6));
|
|
|
|
mtime = parse_dostime(time, date);
|
|
|
|
}
|
|
|
|
path[lastchar] = savechar;
|
|
|
|
return mtime;
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the code object for the module named by 'fullname' from the
|
|
|
|
Zip archive as a new reference. */
|
|
|
|
static PyObject *
|
|
|
|
get_code_from_data(ZipImporter *self, int ispackage, int isbytecode,
|
2010-05-09 12:52:27 -03:00
|
|
|
time_t mtime, PyObject *toc_entry)
|
2002-12-30 18:08:05 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *data, *code;
|
2010-10-18 09:15:34 -03:00
|
|
|
PyObject *modpath;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2010-08-16 20:48:11 -03:00
|
|
|
data = get_data(self->archive, toc_entry);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (data == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2010-10-18 09:15:34 -03:00
|
|
|
modpath = PyUnicode_EncodeFSDefault(PyTuple_GetItem(toc_entry, 0));
|
2010-10-16 08:29:07 -03:00
|
|
|
if (modpath == NULL) {
|
|
|
|
Py_DECREF(data);
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2010-10-18 09:15:34 -03:00
|
|
|
if (isbytecode)
|
|
|
|
code = unmarshal_code(PyBytes_AS_STRING(modpath), data, mtime);
|
|
|
|
else
|
|
|
|
code = compile_source(PyBytes_AS_STRING(modpath), data);
|
|
|
|
Py_DECREF(modpath);
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_DECREF(data);
|
|
|
|
return code;
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
2011-03-15 00:18:48 -03:00
|
|
|
/* Get the code object associated with the module specified by
|
2002-12-30 18:08:05 -04:00
|
|
|
'fullname'. */
|
|
|
|
static PyObject *
|
|
|
|
get_module_code(ZipImporter *self, char *fullname,
|
2010-10-18 09:09:02 -03:00
|
|
|
int *p_ispackage, PyObject **p_modpath)
|
2002-12-30 18:08:05 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *toc_entry;
|
|
|
|
char *subname, path[MAXPATHLEN + 1];
|
|
|
|
int len;
|
|
|
|
struct st_zip_searchorder *zso;
|
|
|
|
|
|
|
|
subname = get_subname(fullname);
|
|
|
|
|
2010-10-18 17:40:59 -03:00
|
|
|
len = make_filename(self->prefix, subname, path, sizeof(path));
|
2010-05-09 12:52:27 -03:00
|
|
|
if (len < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (zso = zip_searchorder; *zso->suffix; zso++) {
|
|
|
|
PyObject *code = NULL;
|
|
|
|
|
|
|
|
strcpy(path + len, zso->suffix);
|
|
|
|
if (Py_VerboseFlag > 1)
|
2010-10-18 08:40:40 -03:00
|
|
|
PySys_FormatStderr("# trying %U%c%s\n",
|
2010-10-18 08:44:21 -03:00
|
|
|
self->archive, (int)SEP, path);
|
2010-05-09 12:52:27 -03:00
|
|
|
toc_entry = PyDict_GetItemString(self->files, path);
|
|
|
|
if (toc_entry != NULL) {
|
|
|
|
time_t mtime = 0;
|
|
|
|
int ispackage = zso->type & IS_PACKAGE;
|
|
|
|
int isbytecode = zso->type & IS_BYTECODE;
|
|
|
|
|
|
|
|
if (isbytecode)
|
|
|
|
mtime = get_mtime_of_source(self, path);
|
|
|
|
if (p_ispackage != NULL)
|
|
|
|
*p_ispackage = ispackage;
|
|
|
|
code = get_code_from_data(self, ispackage,
|
|
|
|
isbytecode, mtime,
|
|
|
|
toc_entry);
|
|
|
|
if (code == Py_None) {
|
|
|
|
/* bad magic number or non-matching mtime
|
|
|
|
in byte code, try next */
|
|
|
|
Py_DECREF(code);
|
|
|
|
continue;
|
|
|
|
}
|
2010-10-18 09:09:02 -03:00
|
|
|
if (code != NULL && p_modpath != NULL) {
|
|
|
|
*p_modpath = PyTuple_GetItem(toc_entry, 0);
|
|
|
|
Py_INCREF(*p_modpath);
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
return code;
|
|
|
|
}
|
|
|
|
}
|
2010-10-18 19:45:54 -03:00
|
|
|
PyErr_Format(ZipImportError, "can't find module '%s'", fullname);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Module init */
|
|
|
|
|
|
|
|
PyDoc_STRVAR(zipimport_doc,
|
|
|
|
"zipimport provides support for importing Python modules from Zip archives.\n\
|
|
|
|
\n\
|
|
|
|
This module exports three objects:\n\
|
|
|
|
- zipimporter: a class; its constructor takes a path to a Zip archive.\n\
|
2006-01-15 11:00:40 -04:00
|
|
|
- ZipImportError: exception raised by zipimporter objects. It's a\n\
|
2002-12-30 18:08:05 -04:00
|
|
|
subclass of ImportError, so it can be caught as ImportError, too.\n\
|
|
|
|
- _zip_directory_cache: a dict, mapping archive paths to zip directory\n\
|
|
|
|
info dicts, as used in zipimporter._files.\n\
|
|
|
|
\n\
|
|
|
|
It is usually not needed to use the zipimport module explicitly; it is\n\
|
|
|
|
used by the builtin import mechanism for sys.path items that are paths\n\
|
|
|
|
to Zip archives.");
|
|
|
|
|
2008-06-11 02:26:20 -03:00
|
|
|
static struct PyModuleDef zipimportmodule = {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
"zipimport",
|
|
|
|
zipimport_doc,
|
|
|
|
-1,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL
|
2008-06-11 02:26:20 -03:00
|
|
|
};
|
|
|
|
|
2002-12-30 18:08:05 -04:00
|
|
|
PyMODINIT_FUNC
|
2008-06-11 02:26:20 -03:00
|
|
|
PyInit_zipimport(void)
|
2002-12-30 18:08:05 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *mod;
|
|
|
|
|
|
|
|
if (PyType_Ready(&ZipImporter_Type) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Correct directory separator */
|
|
|
|
zip_searchorder[0].suffix[0] = SEP;
|
|
|
|
zip_searchorder[1].suffix[0] = SEP;
|
|
|
|
zip_searchorder[2].suffix[0] = SEP;
|
|
|
|
if (Py_OptimizeFlag) {
|
|
|
|
/* Reverse *.pyc and *.pyo */
|
|
|
|
struct st_zip_searchorder tmp;
|
|
|
|
tmp = zip_searchorder[0];
|
|
|
|
zip_searchorder[0] = zip_searchorder[1];
|
|
|
|
zip_searchorder[1] = tmp;
|
|
|
|
tmp = zip_searchorder[3];
|
|
|
|
zip_searchorder[3] = zip_searchorder[4];
|
|
|
|
zip_searchorder[4] = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
mod = PyModule_Create(&zipimportmodule);
|
|
|
|
if (mod == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ZipImportError = PyErr_NewException("zipimport.ZipImportError",
|
|
|
|
PyExc_ImportError, NULL);
|
|
|
|
if (ZipImportError == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
Py_INCREF(ZipImportError);
|
|
|
|
if (PyModule_AddObject(mod, "ZipImportError",
|
|
|
|
ZipImportError) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
Py_INCREF(&ZipImporter_Type);
|
|
|
|
if (PyModule_AddObject(mod, "zipimporter",
|
|
|
|
(PyObject *)&ZipImporter_Type) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
zip_directory_cache = PyDict_New();
|
|
|
|
if (zip_directory_cache == NULL)
|
|
|
|
return NULL;
|
|
|
|
Py_INCREF(zip_directory_cache);
|
|
|
|
if (PyModule_AddObject(mod, "_zip_directory_cache",
|
|
|
|
zip_directory_cache) < 0)
|
|
|
|
return NULL;
|
|
|
|
return mod;
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|