2002-12-30 18:08:05 -04:00
|
|
|
#include "Python.h"
|
2017-09-08 02:51:28 -03:00
|
|
|
#include "internal/pystate.h"
|
2002-12-30 18:08:05 -04:00
|
|
|
#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
|
|
|
};
|
|
|
|
|
2013-11-12 16:44:18 -04:00
|
|
|
#ifdef ALTSEP
|
|
|
|
_Py_IDENTIFIER(replace);
|
|
|
|
#endif
|
|
|
|
|
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
|
2015-04-13 15:21:02 -03:00
|
|
|
non-package .pyc, and .py entries. The .pyc entries
|
2002-12-30 18:08:05 -04:00
|
|
|
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__.py", IS_PACKAGE | IS_SOURCE},
|
|
|
|
{".pyc", 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 */
|
2014-02-16 15:17:28 -04:00
|
|
|
static PyObject *read_directory(PyObject *archive);
|
|
|
|
static PyObject *get_data(PyObject *archive, PyObject *toc_entry);
|
2011-03-14 21:46:50 -03:00
|
|
|
static PyObject *get_module_code(ZipImporter *self, PyObject *fullname,
|
2010-10-18 09:09:02 -03:00
|
|
|
int *p_ispackage, PyObject **p_modpath);
|
2002-12-30 18:08:05 -04:00
|
|
|
|
2017-08-18 15:41:13 -03:00
|
|
|
static PyTypeObject ZipImporter_Type;
|
2002-12-30 18:08:05 -04:00
|
|
|
|
|
|
|
#define ZipImporter_Check(op) PyObject_TypeCheck(op, &ZipImporter_Type)
|
|
|
|
|
2017-08-18 15:41:13 -03:00
|
|
|
/*[clinic input]
|
|
|
|
module zipimport
|
|
|
|
class zipimport.zipimporter "ZipImporter *" "&ZipImporter_Type"
|
|
|
|
[clinic start generated code]*/
|
|
|
|
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=9db8b61557d911e7]*/
|
|
|
|
#include "clinic/zipimport.c.h"
|
|
|
|
|
2002-12-30 18:08:05 -04:00
|
|
|
|
|
|
|
/* 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. */
|
2017-08-18 15:41:13 -03:00
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
zipimport.zipimporter.__init__
|
|
|
|
|
|
|
|
archivepath as path: object(converter="PyUnicode_FSDecoder")
|
|
|
|
A path-like object to a zipfile, or to a specific path inside
|
|
|
|
a zipfile.
|
|
|
|
/
|
|
|
|
|
|
|
|
Create a new zipimporter instance.
|
|
|
|
|
|
|
|
'archivepath' must be a path-like object to a zipfile, or to a specific path
|
|
|
|
inside a zipfile. For example, it can be '/tmp/myimport.zip', or
|
|
|
|
'/tmp/myimport.zip/mydirectory', if mydirectory is a valid directory inside
|
|
|
|
the archive.
|
|
|
|
|
|
|
|
'ZipImportError' is raised if 'archivepath' doesn't point to a valid Zip
|
|
|
|
archive.
|
|
|
|
|
|
|
|
The 'archive' attribute of the zipimporter object contains the name of the
|
|
|
|
zipfile targeted.
|
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2002-12-30 18:08:05 -04:00
|
|
|
static int
|
2017-08-18 15:41:13 -03:00
|
|
|
zipimport_zipimporter___init___impl(ZipImporter *self, PyObject *path)
|
|
|
|
/*[clinic end generated code: output=141558fefdb46dc8 input=92b9ebeed1f6a704]*/
|
2002-12-30 18:08:05 -04:00
|
|
|
{
|
2017-08-18 15:41:13 -03:00
|
|
|
PyObject *files, *tmp;
|
2011-10-31 04:33:37 -03:00
|
|
|
PyObject *filename = NULL;
|
|
|
|
Py_ssize_t len, flen;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2011-10-31 04:33:37 -03:00
|
|
|
if (PyUnicode_READY(path) == -1)
|
2011-09-28 02:41:54 -03:00
|
|
|
return -1;
|
|
|
|
|
2011-10-31 04:33:37 -03:00
|
|
|
len = PyUnicode_GET_LENGTH(path);
|
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
|
|
|
}
|
2002-12-30 18:08:05 -04:00
|
|
|
|
|
|
|
#ifdef ALTSEP
|
2011-10-31 05:01:22 -03:00
|
|
|
tmp = _PyObject_CallMethodId(path, &PyId_replace, "CC", ALTSEP, SEP);
|
2011-10-31 04:33:37 -03:00
|
|
|
if (!tmp)
|
|
|
|
goto error;
|
|
|
|
Py_DECREF(path);
|
|
|
|
path = tmp;
|
2002-12-30 18:08:05 -04:00
|
|
|
#endif
|
|
|
|
|
2011-10-31 04:33:37 -03:00
|
|
|
filename = path;
|
|
|
|
Py_INCREF(filename);
|
|
|
|
flen = len;
|
2010-05-09 12:52:27 -03:00
|
|
|
for (;;) {
|
|
|
|
struct stat statbuf;
|
|
|
|
int rv;
|
|
|
|
|
2011-10-31 04:33:37 -03:00
|
|
|
rv = _Py_stat(filename, &statbuf);
|
2011-12-18 15:47:30 -04:00
|
|
|
if (rv == -2)
|
|
|
|
goto error;
|
2010-05-09 12:52:27 -03:00
|
|
|
if (rv == 0) {
|
|
|
|
/* it exists */
|
2011-10-31 04:33:37 -03:00
|
|
|
if (!S_ISREG(statbuf.st_mode))
|
|
|
|
/* it's a not file */
|
|
|
|
Py_CLEAR(filename);
|
2010-05-09 12:52:27 -03:00
|
|
|
break;
|
|
|
|
}
|
2011-10-31 04:33:37 -03:00
|
|
|
Py_CLEAR(filename);
|
2010-05-09 12:52:27 -03:00
|
|
|
/* back up one path element */
|
2011-10-31 04:33:37 -03:00
|
|
|
flen = PyUnicode_FindChar(path, SEP, 0, flen, -1);
|
|
|
|
if (flen == -1)
|
2010-05-09 12:52:27 -03:00
|
|
|
break;
|
2011-10-31 04:33:37 -03:00
|
|
|
filename = PyUnicode_Substring(path, 0, flen);
|
2013-10-28 21:46:24 -03:00
|
|
|
if (filename == NULL)
|
|
|
|
goto error;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2011-10-31 04:33:37 -03:00
|
|
|
if (filename == 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;
|
|
|
|
}
|
|
|
|
|
2011-10-31 04:33:37 -03:00
|
|
|
if (PyUnicode_READY(filename) < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
files = PyDict_GetItem(zip_directory_cache, filename);
|
2010-08-14 11:54:10 -03:00
|
|
|
if (files == NULL) {
|
2014-02-16 15:17:28 -04:00
|
|
|
files = read_directory(filename);
|
|
|
|
if (files == NULL)
|
2010-08-14 11:54:10 -03:00
|
|
|
goto error;
|
2014-02-16 15:17:28 -04:00
|
|
|
if (PyDict_SetItem(zip_directory_cache, filename, files) != 0)
|
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
|
|
|
else
|
|
|
|
Py_INCREF(files);
|
2017-10-09 12:06:19 -03:00
|
|
|
Py_XSETREF(self->files, files);
|
2010-08-14 11:54:10 -03:00
|
|
|
|
2011-10-31 04:33:37 -03:00
|
|
|
/* Transfer reference */
|
2017-10-09 12:06:19 -03:00
|
|
|
Py_XSETREF(self->archive, filename);
|
2011-10-31 04:33:37 -03:00
|
|
|
filename = NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2011-10-31 04:33:37 -03:00
|
|
|
/* Check if there is a prefix directory following the filename. */
|
|
|
|
if (flen != len) {
|
|
|
|
tmp = PyUnicode_Substring(path, flen+1,
|
|
|
|
PyUnicode_GET_LENGTH(path));
|
|
|
|
if (tmp == NULL)
|
|
|
|
goto error;
|
2017-10-09 12:06:19 -03:00
|
|
|
Py_XSETREF(self->prefix, tmp);
|
2011-10-31 04:33:37 -03:00
|
|
|
if (PyUnicode_READ_CHAR(path, len-1) != SEP) {
|
2010-05-09 12:52:27 -03:00
|
|
|
/* add trailing SEP */
|
2011-10-31 04:33:37 -03:00
|
|
|
tmp = PyUnicode_FromFormat("%U%c", self->prefix, SEP);
|
|
|
|
if (tmp == NULL)
|
|
|
|
goto error;
|
2016-04-10 12:05:40 -03:00
|
|
|
Py_SETREF(self->prefix, tmp);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
}
|
2017-10-09 12:06:19 -03:00
|
|
|
else {
|
|
|
|
Py_XSETREF(self->prefix, PyUnicode_New(0, 0));
|
|
|
|
}
|
2011-10-31 04:33:37 -03:00
|
|
|
Py_DECREF(path);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2010-08-14 11:54:10 -03:00
|
|
|
|
|
|
|
error:
|
2011-10-31 04:33:37 -03:00
|
|
|
Py_DECREF(path);
|
|
|
|
Py_XDECREF(filename);
|
2010-08-14 11:54:10 -03:00
|
|
|
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 \"???\">");
|
2011-09-28 02:41:54 -03:00
|
|
|
else if (self->prefix != NULL && PyUnicode_GET_LENGTH(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] */
|
2011-03-14 21:46:50 -03:00
|
|
|
static PyObject *
|
|
|
|
get_subname(PyObject *fullname)
|
2002-12-30 18:08:05 -04:00
|
|
|
{
|
2011-10-31 04:33:37 -03:00
|
|
|
Py_ssize_t len, dot;
|
|
|
|
if (PyUnicode_READY(fullname) < 0)
|
2011-09-28 02:41:54 -03:00
|
|
|
return NULL;
|
2011-10-31 04:33:37 -03:00
|
|
|
len = PyUnicode_GET_LENGTH(fullname);
|
|
|
|
dot = PyUnicode_FindChar(fullname, '.', 0, len, -1);
|
|
|
|
if (dot == -1) {
|
2011-03-14 21:46:50 -03:00
|
|
|
Py_INCREF(fullname);
|
|
|
|
return fullname;
|
2011-10-31 04:33:37 -03:00
|
|
|
} else
|
|
|
|
return PyUnicode_Substring(fullname, dot+1, len);
|
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
|
2011-03-14 21:46:50 -03:00
|
|
|
length of the resulting string.
|
|
|
|
|
|
|
|
return self.prefix + name.replace('.', os.sep) */
|
|
|
|
static PyObject*
|
|
|
|
make_filename(PyObject *prefix, PyObject *name)
|
2002-12-30 18:08:05 -04:00
|
|
|
{
|
2011-03-14 21:46:50 -03:00
|
|
|
PyObject *pathobj;
|
2011-09-28 02:41:54 -03:00
|
|
|
Py_UCS4 *p, *buf;
|
|
|
|
Py_ssize_t len;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
len = PyUnicode_GET_LENGTH(prefix) + PyUnicode_GET_LENGTH(name) + 1;
|
2015-02-16 07:28:22 -04:00
|
|
|
p = buf = PyMem_New(Py_UCS4, len);
|
2011-09-28 02:41:54 -03:00
|
|
|
if (buf == NULL) {
|
|
|
|
PyErr_NoMemory();
|
2011-03-14 21:46:50 -03:00
|
|
|
return NULL;
|
2011-09-28 02:41:54 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2012-09-09 21:00:34 -03:00
|
|
|
if (!PyUnicode_AsUCS4(prefix, p, len, 0)) {
|
|
|
|
PyMem_Free(buf);
|
2011-09-28 02:41:54 -03:00
|
|
|
return NULL;
|
2012-09-09 21:00:34 -03:00
|
|
|
}
|
2011-09-28 02:41:54 -03:00
|
|
|
p += PyUnicode_GET_LENGTH(prefix);
|
|
|
|
len -= PyUnicode_GET_LENGTH(prefix);
|
2012-09-09 21:00:34 -03:00
|
|
|
if (!PyUnicode_AsUCS4(name, p, len, 1)) {
|
|
|
|
PyMem_Free(buf);
|
2011-09-28 02:41:54 -03:00
|
|
|
return NULL;
|
2012-09-09 21:00:34 -03:00
|
|
|
}
|
2011-03-14 21:46:50 -03:00
|
|
|
for (; *p; p++) {
|
2010-05-09 12:52:27 -03:00
|
|
|
if (*p == '.')
|
|
|
|
*p = SEP;
|
|
|
|
}
|
2011-09-28 02:41:54 -03:00
|
|
|
pathobj = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
|
|
|
|
buf, p-buf);
|
|
|
|
PyMem_Free(buf);
|
2011-03-14 21:46:50 -03:00
|
|
|
return pathobj;
|
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
|
|
|
};
|
|
|
|
|
2012-05-24 21:21:04 -03:00
|
|
|
/* Does this path represent a directory?
|
|
|
|
on error, return < 0
|
|
|
|
if not a dir, return 0
|
|
|
|
if a dir, return 1
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
check_is_directory(ZipImporter *self, PyObject* prefix, PyObject *path)
|
|
|
|
{
|
|
|
|
PyObject *dirpath;
|
2012-05-25 04:24:42 -03:00
|
|
|
int res;
|
2012-05-24 21:21:04 -03:00
|
|
|
|
|
|
|
/* See if this is a "directory". If so, it's eligible to be part
|
|
|
|
of a namespace package. We test by seeing if the name, with an
|
|
|
|
appended path separator, exists. */
|
|
|
|
dirpath = PyUnicode_FromFormat("%U%U%c", prefix, path, SEP);
|
|
|
|
if (dirpath == NULL)
|
|
|
|
return -1;
|
|
|
|
/* If dirpath is present in self->files, we have a directory. */
|
2012-05-25 04:24:42 -03:00
|
|
|
res = PyDict_Contains(self->files, dirpath);
|
2012-05-24 21:21:04 -03:00
|
|
|
Py_DECREF(dirpath);
|
2012-05-25 04:24:42 -03:00
|
|
|
return res;
|
2012-05-24 21:21:04 -03:00
|
|
|
}
|
|
|
|
|
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
|
2011-03-14 21:46:50 -03:00
|
|
|
get_module_info(ZipImporter *self, PyObject *fullname)
|
2002-12-30 18:08:05 -04:00
|
|
|
{
|
2011-03-14 21:46:50 -03:00
|
|
|
PyObject *subname;
|
|
|
|
PyObject *path, *fullpath, *item;
|
2010-05-09 12:52:27 -03:00
|
|
|
struct st_zip_searchorder *zso;
|
|
|
|
|
2017-10-20 17:42:35 -03:00
|
|
|
if (self->prefix == NULL) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"zipimporter.__init__() wasn't called");
|
|
|
|
return MI_ERROR;
|
|
|
|
}
|
|
|
|
|
2010-10-18 18:44:33 -03:00
|
|
|
subname = get_subname(fullname);
|
2011-03-14 21:46:50 -03:00
|
|
|
if (subname == NULL)
|
|
|
|
return MI_ERROR;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2011-03-14 21:46:50 -03:00
|
|
|
path = make_filename(self->prefix, subname);
|
|
|
|
Py_DECREF(subname);
|
|
|
|
if (path == NULL)
|
2010-05-09 12:52:27 -03:00
|
|
|
return MI_ERROR;
|
|
|
|
|
|
|
|
for (zso = zip_searchorder; *zso->suffix; zso++) {
|
2011-03-14 21:46:50 -03:00
|
|
|
fullpath = PyUnicode_FromFormat("%U%s", path, zso->suffix);
|
|
|
|
if (fullpath == NULL) {
|
|
|
|
Py_DECREF(path);
|
|
|
|
return MI_ERROR;
|
|
|
|
}
|
|
|
|
item = PyDict_GetItem(self->files, fullpath);
|
|
|
|
Py_DECREF(fullpath);
|
|
|
|
if (item != NULL) {
|
|
|
|
Py_DECREF(path);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (zso->type & IS_PACKAGE)
|
|
|
|
return MI_PACKAGE;
|
|
|
|
else
|
|
|
|
return MI_MODULE;
|
|
|
|
}
|
|
|
|
}
|
2011-03-14 21:46:50 -03:00
|
|
|
Py_DECREF(path);
|
2010-05-09 12:52:27 -03:00
|
|
|
return MI_NOT_FOUND;
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
2012-05-25 02:54:15 -03:00
|
|
|
typedef enum {
|
2016-01-15 15:22:19 -04:00
|
|
|
FL_ERROR = -1, /* error */
|
|
|
|
FL_NOT_FOUND, /* no loader or namespace portions found */
|
|
|
|
FL_MODULE_FOUND, /* module/package found */
|
|
|
|
FL_NS_FOUND /* namespace portion found: */
|
|
|
|
/* *namespace_portion will point to the name */
|
2012-05-25 02:54:15 -03:00
|
|
|
} find_loader_result;
|
|
|
|
|
2016-01-15 15:22:19 -04:00
|
|
|
/* The guts of "find_loader" and "find_module".
|
2012-05-24 21:21:04 -03:00
|
|
|
*/
|
2012-05-25 02:54:15 -03:00
|
|
|
static find_loader_result
|
2012-05-24 21:21:04 -03:00
|
|
|
find_loader(ZipImporter *self, PyObject *fullname, PyObject **namespace_portion)
|
|
|
|
{
|
|
|
|
enum zi_module_info mi;
|
|
|
|
|
|
|
|
*namespace_portion = NULL;
|
|
|
|
|
|
|
|
mi = get_module_info(self, fullname);
|
|
|
|
if (mi == MI_ERROR)
|
2012-05-25 14:22:29 -03:00
|
|
|
return FL_ERROR;
|
2012-05-24 21:21:04 -03:00
|
|
|
if (mi == MI_NOT_FOUND) {
|
|
|
|
/* Not a module or regular package. See if this is a directory, and
|
|
|
|
therefore possibly a portion of a namespace package. */
|
2016-01-15 15:22:19 -04:00
|
|
|
find_loader_result result = FL_NOT_FOUND;
|
|
|
|
PyObject *subname;
|
|
|
|
int is_dir;
|
|
|
|
|
|
|
|
/* We're only interested in the last path component of fullname;
|
|
|
|
earlier components are recorded in self->prefix. */
|
|
|
|
subname = get_subname(fullname);
|
|
|
|
if (subname == NULL) {
|
|
|
|
return FL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
is_dir = check_is_directory(self, self->prefix, subname);
|
2012-05-24 21:21:04 -03:00
|
|
|
if (is_dir < 0)
|
2016-01-15 15:22:19 -04:00
|
|
|
result = FL_ERROR;
|
|
|
|
else if (is_dir) {
|
2012-05-24 21:21:04 -03:00
|
|
|
/* This is possibly a portion of a namespace
|
|
|
|
package. Return the string representing its path,
|
|
|
|
without a trailing separator. */
|
|
|
|
*namespace_portion = PyUnicode_FromFormat("%U%c%U%U",
|
|
|
|
self->archive, SEP,
|
2016-01-15 15:22:19 -04:00
|
|
|
self->prefix, subname);
|
2012-05-24 21:21:04 -03:00
|
|
|
if (*namespace_portion == NULL)
|
2016-01-15 15:22:19 -04:00
|
|
|
result = FL_ERROR;
|
|
|
|
else
|
|
|
|
result = FL_NS_FOUND;
|
2012-05-24 21:21:04 -03:00
|
|
|
}
|
2016-01-15 15:22:19 -04:00
|
|
|
Py_DECREF(subname);
|
|
|
|
return result;
|
2012-05-24 21:21:04 -03:00
|
|
|
}
|
|
|
|
/* This is a module or package. */
|
2012-05-25 14:22:29 -03:00
|
|
|
return FL_MODULE_FOUND;
|
2012-05-24 21:21:04 -03:00
|
|
|
}
|
|
|
|
|
2017-08-18 15:41:13 -03:00
|
|
|
/*[clinic input]
|
|
|
|
zipimport.zipimporter.find_module
|
|
|
|
|
|
|
|
fullname: unicode
|
|
|
|
path: object = None
|
|
|
|
/
|
|
|
|
|
|
|
|
Search for a module specified by 'fullname'.
|
|
|
|
|
|
|
|
'fullname' must be the fully qualified (dotted) module name. It returns the
|
|
|
|
zipimporter instance itself if the module was found, or None if it wasn't.
|
|
|
|
The optional 'path' argument is ignored -- it's there for compatibility
|
|
|
|
with the importer protocol.
|
|
|
|
|
|
|
|
[clinic start generated code]*/
|
2012-05-24 21:21:04 -03:00
|
|
|
|
2002-12-30 18:08:05 -04:00
|
|
|
static PyObject *
|
2017-08-18 15:41:13 -03:00
|
|
|
zipimport_zipimporter_find_module_impl(ZipImporter *self, PyObject *fullname,
|
|
|
|
PyObject *path)
|
|
|
|
/*[clinic end generated code: output=506087f609466dc7 input=e3528520e075063f]*/
|
2002-12-30 18:08:05 -04:00
|
|
|
{
|
2012-05-25 02:54:15 -03:00
|
|
|
PyObject *namespace_portion = NULL;
|
|
|
|
PyObject *result = NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2012-05-24 21:21:04 -03:00
|
|
|
switch (find_loader(self, fullname, &namespace_portion)) {
|
2012-05-25 14:22:29 -03:00
|
|
|
case FL_ERROR:
|
2012-05-25 04:22:04 -03:00
|
|
|
return NULL;
|
2012-05-25 14:22:29 -03:00
|
|
|
case FL_NS_FOUND:
|
2012-05-25 02:54:15 -03:00
|
|
|
/* A namespace portion is not allowed via find_module, so return None. */
|
2012-05-24 21:21:04 -03:00
|
|
|
Py_DECREF(namespace_portion);
|
2012-05-25 02:54:15 -03:00
|
|
|
/* FALL THROUGH */
|
2012-05-25 14:22:29 -03:00
|
|
|
case FL_NOT_FOUND:
|
2012-05-25 02:54:15 -03:00
|
|
|
result = Py_None;
|
|
|
|
break;
|
2012-05-25 14:22:29 -03:00
|
|
|
case FL_MODULE_FOUND:
|
2012-05-25 02:54:15 -03:00
|
|
|
result = (PyObject *)self;
|
|
|
|
break;
|
2016-01-15 15:22:19 -04:00
|
|
|
default:
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2012-05-25 04:22:04 -03:00
|
|
|
Py_INCREF(result);
|
2012-05-25 04:19:40 -03:00
|
|
|
return result;
|
2012-05-24 21:21:04 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-18 15:41:13 -03:00
|
|
|
/*[clinic input]
|
|
|
|
zipimport.zipimporter.find_loader
|
|
|
|
|
|
|
|
fullname: unicode
|
|
|
|
path: object = None
|
|
|
|
/
|
|
|
|
|
|
|
|
Search for a module specified by 'fullname'.
|
|
|
|
|
|
|
|
'fullname' must be the fully qualified (dotted) module name. It returns the
|
|
|
|
zipimporter instance itself if the module was found, a string containing the
|
|
|
|
full path name if it's possibly a portion of a namespace package,
|
|
|
|
or None otherwise. The optional 'path' argument is ignored -- it's
|
|
|
|
there for compatibility with the importer protocol.
|
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2012-05-24 21:21:04 -03:00
|
|
|
static PyObject *
|
2017-08-18 15:41:13 -03:00
|
|
|
zipimport_zipimporter_find_loader_impl(ZipImporter *self, PyObject *fullname,
|
|
|
|
PyObject *path)
|
|
|
|
/*[clinic end generated code: output=601599a43bc0f49a input=dc73f275b0d5be23]*/
|
2012-05-24 21:21:04 -03:00
|
|
|
{
|
|
|
|
PyObject *result = NULL;
|
|
|
|
PyObject *namespace_portion = NULL;
|
|
|
|
|
|
|
|
switch (find_loader(self, fullname, &namespace_portion)) {
|
2012-05-25 14:22:29 -03:00
|
|
|
case FL_ERROR:
|
2012-05-25 02:54:15 -03:00
|
|
|
return NULL;
|
2012-05-25 14:22:29 -03:00
|
|
|
case FL_NOT_FOUND: /* Not found, return (None, []) */
|
2012-05-25 02:54:15 -03:00
|
|
|
result = Py_BuildValue("O[]", Py_None);
|
|
|
|
break;
|
2012-05-25 14:22:29 -03:00
|
|
|
case FL_MODULE_FOUND: /* Return (self, []) */
|
2012-05-25 02:54:15 -03:00
|
|
|
result = Py_BuildValue("O[]", self);
|
|
|
|
break;
|
2012-05-25 14:22:29 -03:00
|
|
|
case FL_NS_FOUND: /* Return (None, [namespace_portion]) */
|
2012-05-25 02:54:15 -03:00
|
|
|
result = Py_BuildValue("O[O]", Py_None, namespace_portion);
|
2012-05-25 02:35:39 -03:00
|
|
|
Py_DECREF(namespace_portion);
|
2012-05-24 21:21:04 -03:00
|
|
|
return result;
|
2016-01-15 15:22:19 -04:00
|
|
|
default:
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
2012-05-24 21:21:04 -03:00
|
|
|
}
|
2012-05-25 02:54:15 -03:00
|
|
|
return result;
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
2017-08-18 15:41:13 -03:00
|
|
|
/*[clinic input]
|
|
|
|
zipimport.zipimporter.load_module
|
|
|
|
|
|
|
|
fullname: unicode
|
|
|
|
/
|
|
|
|
|
|
|
|
Load the module specified by 'fullname'.
|
|
|
|
|
|
|
|
'fullname' must be the fully qualified (dotted) module name. It returns the
|
|
|
|
imported module, or raises ZipImportError if it wasn't found.
|
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2002-12-30 18:08:05 -04:00
|
|
|
static PyObject *
|
2017-08-18 15:41:13 -03:00
|
|
|
zipimport_zipimporter_load_module_impl(ZipImporter *self, PyObject *fullname)
|
|
|
|
/*[clinic end generated code: output=7303cebf88d47953 input=c236e2e8621f04ef]*/
|
2002-12-30 18:08:05 -04:00
|
|
|
{
|
2010-10-18 09:03:25 -03:00
|
|
|
PyObject *code = NULL, *mod, *dict;
|
2011-03-14 21:46:50 -03:00
|
|
|
PyObject *modpath = NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
int ispackage;
|
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
if (PyUnicode_READY(fullname) == -1)
|
|
|
|
return NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
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
|
|
|
|
2011-03-14 21:46:50 -03:00
|
|
|
mod = PyImport_AddModuleObject(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 */
|
2013-10-28 21:46:24 -03:00
|
|
|
PyObject *pkgpath, *fullpath, *subname;
|
2010-05-09 12:52:27 -03:00
|
|
|
int err;
|
|
|
|
|
2013-10-28 21:46:24 -03:00
|
|
|
subname = get_subname(fullname);
|
|
|
|
if (subname == NULL)
|
|
|
|
goto error;
|
|
|
|
|
2011-03-14 21:46:50 -03:00
|
|
|
fullpath = PyUnicode_FromFormat("%U%c%U%U",
|
2010-05-09 12:52:27 -03:00
|
|
|
self->archive, SEP,
|
|
|
|
self->prefix, subname);
|
2011-03-14 21:46:50 -03:00
|
|
|
Py_DECREF(subname);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (fullpath == NULL)
|
|
|
|
goto error;
|
|
|
|
|
2011-03-14 21:46:50 -03:00
|
|
|
pkgpath = Py_BuildValue("[N]", fullpath);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (pkgpath == NULL)
|
|
|
|
goto error;
|
|
|
|
err = PyDict_SetItemString(dict, "__path__", pkgpath);
|
|
|
|
Py_DECREF(pkgpath);
|
|
|
|
if (err != 0)
|
|
|
|
goto error;
|
|
|
|
}
|
2011-03-14 21:46:50 -03:00
|
|
|
mod = PyImport_ExecCodeModuleObject(fullname, code, modpath, NULL);
|
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)
|
2011-03-14 21:46:50 -03:00
|
|
|
PySys_FormatStderr("import %U # loaded from Zip %U\n",
|
2010-10-18 09:09:02 -03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-08-18 15:41:13 -03:00
|
|
|
/*[clinic input]
|
|
|
|
zipimport.zipimporter.get_filename
|
|
|
|
|
|
|
|
fullname: unicode
|
|
|
|
/
|
|
|
|
|
|
|
|
Return the filename for the specified module.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2008-12-14 07:50:48 -04:00
|
|
|
static PyObject *
|
2017-08-18 15:41:13 -03:00
|
|
|
zipimport_zipimporter_get_filename_impl(ZipImporter *self,
|
|
|
|
PyObject *fullname)
|
|
|
|
/*[clinic end generated code: output=c5b92b58bea86506 input=28d2eb57e4f25c8a]*/
|
2008-12-14 07:50:48 -04:00
|
|
|
{
|
2017-08-18 15:41:13 -03:00
|
|
|
PyObject *code, *modpath;
|
2008-12-14 07:50:48 -04:00
|
|
|
int ispackage;
|
|
|
|
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
2017-08-18 15:41:13 -03:00
|
|
|
/*[clinic input]
|
|
|
|
zipimport.zipimporter.is_package
|
|
|
|
|
|
|
|
fullname: unicode
|
|
|
|
/
|
|
|
|
|
|
|
|
Return True if the module specified by fullname is a package.
|
|
|
|
|
|
|
|
Raise ZipImportError if the module couldn't be found.
|
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2002-12-30 18:08:05 -04:00
|
|
|
static PyObject *
|
2017-08-18 15:41:13 -03:00
|
|
|
zipimport_zipimporter_is_package_impl(ZipImporter *self, PyObject *fullname)
|
|
|
|
/*[clinic end generated code: output=c32958c2a5216ae6 input=a7ba752f64345062]*/
|
2002-12-30 18:08:05 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
enum zi_module_info mi;
|
|
|
|
|
|
|
|
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) {
|
2011-03-14 21:46:50 -03:00
|
|
|
PyErr_Format(ZipImportError, "can't find module %R", 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
|
|
|
}
|
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
|
2017-08-18 15:41:13 -03:00
|
|
|
/*[clinic input]
|
|
|
|
zipimport.zipimporter.get_data
|
|
|
|
|
|
|
|
pathname as path: unicode
|
|
|
|
/
|
|
|
|
|
|
|
|
Return the data associated with 'pathname'.
|
|
|
|
|
|
|
|
Raise OSError if the file was not found.
|
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2002-12-30 18:08:05 -04:00
|
|
|
static PyObject *
|
2017-08-18 15:41:13 -03:00
|
|
|
zipimport_zipimporter_get_data_impl(ZipImporter *self, PyObject *path)
|
|
|
|
/*[clinic end generated code: output=65dc506aaa268436 input=fa6428b74843c4ae]*/
|
2002-12-30 18:08:05 -04:00
|
|
|
{
|
2017-08-18 15:41:13 -03:00
|
|
|
PyObject *key;
|
2014-02-16 15:17:28 -04:00
|
|
|
PyObject *toc_entry;
|
2011-10-31 04:33:37 -03:00
|
|
|
Py_ssize_t path_start, path_len, len;
|
2002-12-30 18:08:05 -04:00
|
|
|
|
2017-10-20 17:42:35 -03:00
|
|
|
if (self->archive == NULL) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"zipimporter.__init__() wasn't called");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
#ifdef ALTSEP
|
2017-08-29 14:40:15 -03:00
|
|
|
path = _PyObject_CallMethodId((PyObject *)&PyUnicode_Type, &PyId_replace,
|
|
|
|
"OCC", path, ALTSEP, SEP);
|
2011-10-31 04:33:37 -03:00
|
|
|
if (!path)
|
|
|
|
return NULL;
|
|
|
|
#else
|
|
|
|
Py_INCREF(path);
|
2002-12-30 18:08:05 -04:00
|
|
|
#endif
|
2011-10-31 04:33:37 -03:00
|
|
|
if (PyUnicode_READY(path) == -1)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
path_len = PyUnicode_GET_LENGTH(path);
|
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
len = PyUnicode_GET_LENGTH(self->archive);
|
2011-10-31 04:33:37 -03:00
|
|
|
path_start = 0;
|
|
|
|
if (PyUnicode_Tailmatch(path, self->archive, 0, len, -1)
|
|
|
|
&& PyUnicode_READ_CHAR(path, len) == SEP) {
|
|
|
|
path_start = len + 1;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
|
2011-10-31 04:33:37 -03:00
|
|
|
key = PyUnicode_Substring(path, path_start, path_len);
|
2010-08-16 20:48:11 -03:00
|
|
|
if (key == NULL)
|
2011-10-31 04:33:37 -03:00
|
|
|
goto error;
|
2010-08-16 20:48:11 -03:00
|
|
|
toc_entry = PyDict_GetItem(self->files, key);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (toc_entry == NULL) {
|
2017-04-16 04:46:38 -03:00
|
|
|
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, key);
|
2010-08-16 20:48:11 -03:00
|
|
|
Py_DECREF(key);
|
2011-10-31 04:33:37 -03:00
|
|
|
goto error;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2010-08-16 20:48:11 -03:00
|
|
|
Py_DECREF(key);
|
2011-10-31 04:33:37 -03:00
|
|
|
Py_DECREF(path);
|
2014-02-16 15:17:28 -04:00
|
|
|
return get_data(self->archive, toc_entry);
|
2011-10-31 04:33:37 -03:00
|
|
|
error:
|
|
|
|
Py_DECREF(path);
|
|
|
|
return NULL;
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
2017-08-18 15:41:13 -03:00
|
|
|
/*[clinic input]
|
|
|
|
zipimport.zipimporter.get_code
|
2002-12-30 18:08:05 -04:00
|
|
|
|
2017-08-18 15:41:13 -03:00
|
|
|
fullname: unicode
|
|
|
|
/
|
|
|
|
|
|
|
|
Return the code object for the specified module.
|
2002-12-30 18:08:05 -04:00
|
|
|
|
2017-08-18 15:41:13 -03:00
|
|
|
Raise ZipImportError if the module couldn't be found.
|
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
zipimport_zipimporter_get_code_impl(ZipImporter *self, PyObject *fullname)
|
|
|
|
/*[clinic end generated code: output=b923c37fa99cbac4 input=2761412bc37f3549]*/
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
return get_module_code(self, fullname, NULL, NULL);
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
2017-08-18 15:41:13 -03:00
|
|
|
/*[clinic input]
|
|
|
|
zipimport.zipimporter.get_source
|
|
|
|
|
|
|
|
fullname: unicode
|
|
|
|
/
|
|
|
|
|
|
|
|
Return the source code for the specified module.
|
|
|
|
|
|
|
|
Raise ZipImportError if the module couldn't be found, return None if the
|
|
|
|
archive does contain the module, but has no source for it.
|
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2002-12-30 18:08:05 -04:00
|
|
|
static PyObject *
|
2017-08-18 15:41:13 -03:00
|
|
|
zipimport_zipimporter_get_source_impl(ZipImporter *self, PyObject *fullname)
|
|
|
|
/*[clinic end generated code: output=bc059301b0c33729 input=4e4b186f2e690716]*/
|
2002-12-30 18:08:05 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *toc_entry;
|
2017-08-18 15:41:13 -03:00
|
|
|
PyObject *subname, *path, *fullpath;
|
2010-05-09 12:52:27 -03:00
|
|
|
enum zi_module_info mi;
|
|
|
|
|
|
|
|
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) {
|
2011-03-14 21:46:50 -03:00
|
|
|
PyErr_Format(ZipImportError, "can't find module %R", fullname);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-03-14 21:46:50 -03:00
|
|
|
|
2010-10-18 18:44:33 -03:00
|
|
|
subname = get_subname(fullname);
|
2011-03-14 21:46:50 -03:00
|
|
|
if (subname == NULL)
|
|
|
|
return NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2011-03-14 21:46:50 -03:00
|
|
|
path = make_filename(self->prefix, subname);
|
|
|
|
Py_DECREF(subname);
|
|
|
|
if (path == NULL)
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
|
2011-03-14 21:46:50 -03:00
|
|
|
if (mi == MI_PACKAGE)
|
|
|
|
fullpath = PyUnicode_FromFormat("%U%c__init__.py", path, SEP);
|
2010-05-09 12:52:27 -03:00
|
|
|
else
|
2011-03-14 21:46:50 -03:00
|
|
|
fullpath = PyUnicode_FromFormat("%U.py", path);
|
|
|
|
Py_DECREF(path);
|
|
|
|
if (fullpath == NULL)
|
|
|
|
return NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2011-03-14 21:46:50 -03:00
|
|
|
toc_entry = PyDict_GetItem(self->files, fullpath);
|
|
|
|
Py_DECREF(fullpath);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (toc_entry != NULL) {
|
2010-08-16 20:48:11 -03:00
|
|
|
PyObject *res, *bytes;
|
2014-02-16 15:17:28 -04:00
|
|
|
bytes = get_data(self->archive, toc_entry);
|
2010-08-16 20:48:11 -03:00
|
|
|
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 */
|
2017-01-23 03:47:21 -04:00
|
|
|
Py_RETURN_NONE;
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
2018-01-24 16:36:21 -04:00
|
|
|
/*[clinic input]
|
|
|
|
zipimport.zipimporter.get_resource_reader
|
|
|
|
|
|
|
|
fullname: unicode
|
|
|
|
/
|
|
|
|
|
|
|
|
Return the ResourceReader for a package in a zip file.
|
|
|
|
|
|
|
|
If 'fullname' is a package within the zip file, return the 'ResourceReader'
|
|
|
|
object for the package. Otherwise return None.
|
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
zipimport_zipimporter_get_resource_reader_impl(ZipImporter *self,
|
|
|
|
PyObject *fullname)
|
|
|
|
/*[clinic end generated code: output=5e367d431f830726 input=bfab94d736e99151]*/
|
|
|
|
{
|
|
|
|
PyObject *module = PyImport_ImportModule("importlib.resources");
|
|
|
|
if (module == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyObject *retval = PyObject_CallMethod(
|
|
|
|
module, "_zipimport_get_resource_reader",
|
|
|
|
"OO", (PyObject *)self, fullname);
|
|
|
|
Py_DECREF(module);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2008-12-14 07:50:48 -04:00
|
|
|
|
2002-12-30 18:08:05 -04:00
|
|
|
static PyMethodDef zipimporter_methods[] = {
|
2017-08-18 15:41:13 -03:00
|
|
|
ZIPIMPORT_ZIPIMPORTER_FIND_MODULE_METHODDEF
|
|
|
|
ZIPIMPORT_ZIPIMPORTER_FIND_LOADER_METHODDEF
|
|
|
|
ZIPIMPORT_ZIPIMPORTER_LOAD_MODULE_METHODDEF
|
|
|
|
ZIPIMPORT_ZIPIMPORTER_GET_FILENAME_METHODDEF
|
|
|
|
ZIPIMPORT_ZIPIMPORTER_IS_PACKAGE_METHODDEF
|
|
|
|
ZIPIMPORT_ZIPIMPORTER_GET_DATA_METHODDEF
|
|
|
|
ZIPIMPORT_ZIPIMPORTER_GET_CODE_METHODDEF
|
|
|
|
ZIPIMPORT_ZIPIMPORTER_GET_SOURCE_METHODDEF
|
2018-01-24 16:36:21 -04:00
|
|
|
ZIPIMPORT_ZIPIMPORTER_GET_RESOURCE_READER_METHODDEF
|
2010-05-09 12:52:27 -03:00
|
|
|
{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
|
|
|
};
|
|
|
|
|
|
|
|
#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 */
|
2017-08-18 15:41:13 -03:00
|
|
|
zipimport_zipimporter___init____doc__, /* tp_doc */
|
2010-05-09 12:52:27 -03:00
|
|
|
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 */
|
2017-08-18 15:41:13 -03:00
|
|
|
(initproc)zipimport_zipimporter___init__, /* tp_init */
|
2010-05-09 12:52:27 -03:00
|
|
|
PyType_GenericAlloc, /* tp_alloc */
|
|
|
|
PyType_GenericNew, /* tp_new */
|
|
|
|
PyObject_GC_Del, /* tp_free */
|
2002-12-30 18:08:05 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* implementation */
|
|
|
|
|
2016-01-28 15:30:16 -04:00
|
|
|
/* Given a buffer, return the unsigned int that is represented by the first
|
2002-12-30 18:08:05 -04:00
|
|
|
4 bytes, encoded as little endian. This partially reimplements
|
|
|
|
marshal.c:r_long() */
|
2016-01-28 15:30:16 -04:00
|
|
|
static unsigned int
|
|
|
|
get_uint32(const unsigned char *buf)
|
|
|
|
{
|
|
|
|
unsigned int x;
|
2010-05-09 12:52:27 -03:00
|
|
|
x = buf[0];
|
2016-01-28 15:30:16 -04:00
|
|
|
x |= (unsigned int)buf[1] << 8;
|
|
|
|
x |= (unsigned int)buf[2] << 16;
|
|
|
|
x |= (unsigned int)buf[3] << 24;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Given a buffer, return the unsigned int that is represented by the first
|
|
|
|
2 bytes, encoded as little endian. This partially reimplements
|
|
|
|
marshal.c:r_short() */
|
|
|
|
static unsigned short
|
|
|
|
get_uint16(const unsigned char *buf)
|
|
|
|
{
|
|
|
|
unsigned short x;
|
|
|
|
x = buf[0];
|
|
|
|
x |= (unsigned short)buf[1] << 8;
|
2010-05-09 12:52:27 -03:00
|
|
|
return x;
|
2002-12-30 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
2016-01-28 15:30:16 -04:00
|
|
|
static void
|
|
|
|
set_file_error(PyObject *archive, int eof)
|
|
|
|
{
|
|
|
|
if (eof) {
|
|
|
|
PyErr_SetString(PyExc_EOFError, "EOF read where not expected");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, archive);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-30 18:08:05 -04:00
|
|
|
/*
|
2014-02-16 15:17:28 -04:00
|
|
|
read_directory(archive) -> files dict (new reference)
|
2002-12-30 18:08:05 -04:00
|
|
|
|
2014-02-16 15:17:28 -04:00
|
|
|
Given a path to a Zip archive, build a dict, mapping file names
|
2002-12-30 18:08:05 -04:00
|
|
|
(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 *
|
2014-02-16 15:17:28 -04:00
|
|
|
read_directory(PyObject *archive)
|
2002-12-30 18:08:05 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *files = NULL;
|
2014-02-16 15:17:28 -04:00
|
|
|
FILE *fp;
|
2016-01-28 15:30:16 -04:00
|
|
|
unsigned short flags, compress, time, date, name_size;
|
|
|
|
unsigned int crc, data_size, file_size, header_size, header_offset;
|
|
|
|
unsigned long file_offset, header_position;
|
|
|
|
unsigned long arc_offset; /* Absolute offset to start of the zip-archive. */
|
|
|
|
unsigned int count, i;
|
|
|
|
unsigned char buffer[46];
|
2010-05-09 12:52:27 -03:00
|
|
|
char name[MAXPATHLEN + 5];
|
2010-08-16 14:54:28 -03:00
|
|
|
PyObject *nameobj = NULL;
|
2011-10-31 04:33:37 -03:00
|
|
|
PyObject *path;
|
2010-10-18 09:13:46 -03:00
|
|
|
const char *charset;
|
2011-01-22 06:30:29 -04:00
|
|
|
int bootstrap;
|
2016-01-28 15:30:16 -04:00
|
|
|
const char *errmsg = NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2014-02-16 15:17:28 -04:00
|
|
|
fp = _Py_fopen_obj(archive, "rb");
|
|
|
|
if (fp == NULL) {
|
2015-03-20 06:52:25 -03:00
|
|
|
if (PyErr_ExceptionMatches(PyExc_OSError)) {
|
2016-10-21 11:09:17 -03:00
|
|
|
_PyErr_FormatFromCause(ZipImportError,
|
|
|
|
"can't open Zip file: %R", archive);
|
2015-03-20 06:52:25 -03:00
|
|
|
}
|
2014-02-16 15:17:28 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-10-02 21:13:05 -03:00
|
|
|
if (fseek(fp, -22, SEEK_END) == -1) {
|
2016-01-28 15:30:16 -04:00
|
|
|
goto file_error;
|
2012-10-02 21:13:05 -03:00
|
|
|
}
|
2016-01-28 15:30:16 -04:00
|
|
|
header_position = (unsigned long)ftell(fp);
|
|
|
|
if (header_position == (unsigned long)-1) {
|
|
|
|
goto file_error;
|
|
|
|
}
|
|
|
|
assert(header_position <= (unsigned long)LONG_MAX);
|
|
|
|
if (fread(buffer, 1, 22, fp) != 22) {
|
|
|
|
goto file_error;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2016-01-28 15:30:16 -04:00
|
|
|
if (get_uint32(buffer) != 0x06054B50u) {
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Bad: End of Central Dir signature */
|
2016-01-28 15:30:16 -04:00
|
|
|
errmsg = "not a Zip file";
|
|
|
|
goto invalid_header;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
|
2016-01-28 15:30:16 -04:00
|
|
|
header_size = get_uint32(buffer + 12);
|
|
|
|
header_offset = get_uint32(buffer + 16);
|
|
|
|
if (header_position < header_size) {
|
|
|
|
errmsg = "bad central directory size";
|
|
|
|
goto invalid_header;
|
|
|
|
}
|
|
|
|
if (header_position < header_offset) {
|
|
|
|
errmsg = "bad central directory offset";
|
|
|
|
goto invalid_header;
|
|
|
|
}
|
|
|
|
if (header_position - header_size < header_offset) {
|
|
|
|
errmsg = "bad central directory size or offset";
|
|
|
|
goto invalid_header;
|
|
|
|
}
|
|
|
|
header_position -= header_size;
|
|
|
|
arc_offset = header_position - header_offset;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
files = PyDict_New();
|
2016-01-28 15:30:16 -04:00
|
|
|
if (files == NULL) {
|
2010-05-09 12:52:27 -03:00
|
|
|
goto error;
|
2016-01-28 15:30:16 -04:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Start of Central Directory */
|
|
|
|
count = 0;
|
2016-01-28 15:30:16 -04:00
|
|
|
if (fseek(fp, (long)header_position, 0) == -1) {
|
2013-02-16 11:43:45 -04:00
|
|
|
goto file_error;
|
2016-01-28 15:30:16 -04:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
for (;;) {
|
|
|
|
PyObject *t;
|
2016-01-28 15:30:16 -04:00
|
|
|
size_t n;
|
2010-05-09 12:52:27 -03:00
|
|
|
int err;
|
|
|
|
|
2016-01-28 15:30:16 -04:00
|
|
|
n = fread(buffer, 1, 46, fp);
|
|
|
|
if (n < 4) {
|
|
|
|
goto eof_error;
|
|
|
|
}
|
2013-02-16 11:43:45 -04:00
|
|
|
/* Start of file header */
|
2016-01-28 15:30:16 -04:00
|
|
|
if (get_uint32(buffer) != 0x02014B50u) {
|
2010-05-09 12:52:27 -03:00
|
|
|
break; /* Bad: Central Dir File Header */
|
2016-01-28 15:30:16 -04:00
|
|
|
}
|
|
|
|
if (n != 46) {
|
|
|
|
goto eof_error;
|
|
|
|
}
|
|
|
|
flags = get_uint16(buffer + 8);
|
|
|
|
compress = get_uint16(buffer + 10);
|
|
|
|
time = get_uint16(buffer + 12);
|
|
|
|
date = get_uint16(buffer + 14);
|
|
|
|
crc = get_uint32(buffer + 16);
|
|
|
|
data_size = get_uint32(buffer + 20);
|
|
|
|
file_size = get_uint32(buffer + 24);
|
|
|
|
name_size = get_uint16(buffer + 28);
|
|
|
|
header_size = (unsigned int)name_size +
|
|
|
|
get_uint16(buffer + 30) /* extra field */ +
|
|
|
|
get_uint16(buffer + 32) /* comment */;
|
|
|
|
|
|
|
|
file_offset = get_uint32(buffer + 42);
|
|
|
|
if (file_offset > header_offset) {
|
|
|
|
errmsg = "bad local header offset";
|
|
|
|
goto invalid_header;
|
|
|
|
}
|
|
|
|
file_offset += arc_offset;
|
2013-02-16 11:43:45 -04:00
|
|
|
|
2016-01-28 15:30:16 -04:00
|
|
|
if (name_size > MAXPATHLEN) {
|
2010-05-09 12:52:27 -03:00
|
|
|
name_size = MAXPATHLEN;
|
|
|
|
}
|
2016-01-28 15:30:16 -04:00
|
|
|
if (fread(name, 1, name_size, fp) != name_size) {
|
|
|
|
goto file_error;
|
|
|
|
}
|
|
|
|
name[name_size] = '\0'; /* Add terminating null byte */
|
2016-12-05 12:55:36 -04:00
|
|
|
#if SEP != '/'
|
|
|
|
for (i = 0; i < name_size; i++) {
|
|
|
|
if (name[i] == '/') {
|
|
|
|
name[i] = SEP;
|
2016-01-28 15:30:16 -04:00
|
|
|
}
|
|
|
|
}
|
2016-12-05 12:55:36 -04:00
|
|
|
#endif
|
2016-01-28 15:30:16 -04:00
|
|
|
/* Skip the rest of the header.
|
|
|
|
* On Windows, calling fseek to skip over the fields we don't use is
|
|
|
|
* slower than reading the data because fseek flushes stdio's
|
|
|
|
* internal buffers. See issue #8745. */
|
|
|
|
assert(header_size <= 3*0xFFFFu);
|
|
|
|
for (i = name_size; i < header_size; i++) {
|
|
|
|
if (getc(fp) == EOF) {
|
2013-02-16 11:43:45 -04:00
|
|
|
goto file_error;
|
2016-01-28 15:30:16 -04:00
|
|
|
}
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2011-01-22 06:30:29 -04:00
|
|
|
bootstrap = 0;
|
2016-01-28 15:30:16 -04:00
|
|
|
if (flags & 0x0800) {
|
2010-10-18 09:13:46 -03:00
|
|
|
charset = "utf-8";
|
2016-01-28 15:30:16 -04:00
|
|
|
}
|
2018-08-03 10:33:52 -03:00
|
|
|
else if (!_PyInterpreterState_Get()->codecs_initialized) {
|
2011-01-22 06:30:29 -04:00
|
|
|
/* 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;
|
|
|
|
}
|
2016-01-28 15:30:16 -04:00
|
|
|
else {
|
2010-10-18 09:13:46 -03:00
|
|
|
charset = "cp437";
|
2016-01-28 15:30:16 -04:00
|
|
|
}
|
2010-10-18 09:13:46 -03:00
|
|
|
nameobj = PyUnicode_Decode(name, name_size, charset, NULL);
|
2011-01-22 06:30:29 -04:00
|
|
|
if (nameobj == NULL) {
|
2016-01-28 15:30:16 -04:00
|
|
|
if (bootstrap) {
|
2011-01-22 06:30:29 -04:00
|
|
|
PyErr_Format(PyExc_NotImplementedError,
|
|
|
|
"bootstrap issue: python%i%i.zip contains non-ASCII "
|
|
|
|
"filenames without the unicode flag",
|
|
|
|
PY_MAJOR_VERSION, PY_MINOR_VERSION);
|
2016-01-28 15:30:16 -04:00
|
|
|
}
|
2010-08-16 14:54:28 -03:00
|
|
|
goto error;
|
2011-01-22 06:30:29 -04:00
|
|
|
}
|
2016-01-28 15:30:16 -04:00
|
|
|
if (PyUnicode_READY(nameobj) == -1) {
|
2012-08-20 09:14:49 -03:00
|
|
|
goto error;
|
2016-01-28 15:30:16 -04:00
|
|
|
}
|
2011-10-31 04:33:37 -03:00
|
|
|
path = PyUnicode_FromFormat("%U%c%U", archive, SEP, nameobj);
|
2016-01-28 15:30:16 -04:00
|
|
|
if (path == NULL) {
|
2010-08-16 14:54:28 -03:00
|
|
|
goto error;
|
2016-01-28 15:30:16 -04:00
|
|
|
}
|
|
|
|
t = Py_BuildValue("NHIIkHHI", path, compress, data_size,
|
2010-05-09 12:52:27 -03:00
|
|
|
file_size, file_offset, time, date, crc);
|
2016-01-28 15:30:16 -04:00
|
|
|
if (t == NULL) {
|
2010-05-09 12:52:27 -03:00
|
|
|
goto error;
|
2016-01-28 15:30:16 -04:00
|
|
|
}
|
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);
|
2016-01-28 15:30:16 -04:00
|
|
|
if (err != 0) {
|
2010-05-09 12:52:27 -03:00
|
|
|
goto error;
|
2016-01-28 15:30:16 -04:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
count++;
|
|
|
|
}
|
2014-02-16 15:17:28 -04:00
|
|
|
fclose(fp);
|
2016-01-28 15:30:16 -04:00
|
|
|
if (Py_VerboseFlag) {
|
|
|
|
PySys_FormatStderr("# zipimport: found %u names in %R\n",
|
2011-03-14 21:46:50 -03:00
|
|
|
count, archive);
|
2016-01-28 15:30:16 -04:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
return files;
|
2016-01-28 15:30:16 -04:00
|
|
|
|
|
|
|
eof_error:
|
|
|
|
set_file_error(archive, !ferror(fp));
|
|
|
|
goto error;
|
|
|
|
|
2013-02-16 11:43:45 -04:00
|
|
|
file_error:
|
2012-10-02 21:13:05 -03:00
|
|
|
PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
|
2016-01-28 15:30:16 -04:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
invalid_header:
|
|
|
|
assert(errmsg != NULL);
|
|
|
|
PyErr_Format(ZipImportError, "%s: %R", errmsg, archive);
|
|
|
|
goto error;
|
|
|
|
|
2002-12-30 18:08:05 -04:00
|
|
|
error:
|
2014-02-16 15:17:28 -04:00
|
|
|
fclose(fp);
|
2010-05-09 12:52:27 -03:00
|
|
|
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;
|
2011-10-14 05:20:37 -03:00
|
|
|
_Py_IDENTIFIER(decompress);
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2011-05-19 19:16:09 -03:00
|
|
|
if (importing_zlib != 0)
|
2017-03-11 01:02:52 -04:00
|
|
|
/* Someone has a zlib.pyc in their Zip file;
|
2011-05-19 19:16:09 -03:00
|
|
|
let's avoid a stack overflow. */
|
|
|
|
return NULL;
|
|
|
|
importing_zlib = 1;
|
|
|
|
zlib = PyImport_ImportModuleNoBlock("zlib");
|
|
|
|
importing_zlib = 0;
|
|
|
|
if (zlib != NULL) {
|
2011-10-10 13:11:30 -03:00
|
|
|
decompress = _PyObject_GetAttrId(zlib,
|
|
|
|
&PyId_decompress);
|
2011-05-19 19:16:09 -03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-02-16 15:17:28 -04:00
|
|
|
/* Given a path to a Zip file and a toc_entry, return the (uncompressed)
|
2002-12-30 18:08:05 -04:00
|
|
|
data as a new reference. */
|
|
|
|
static PyObject *
|
2014-02-16 15:17:28 -04:00
|
|
|
get_data(PyObject *archive, PyObject *toc_entry)
|
2002-12-30 18:08:05 -04:00
|
|
|
{
|
2016-01-28 15:30:16 -04:00
|
|
|
PyObject *raw_data = NULL, *data, *decompress;
|
2010-05-09 12:52:27 -03:00
|
|
|
char *buf;
|
2014-02-16 15:17:28 -04:00
|
|
|
FILE *fp;
|
2010-08-16 20:48:11 -03:00
|
|
|
PyObject *datapath;
|
2016-01-28 15:30:16 -04:00
|
|
|
unsigned short compress, time, date;
|
|
|
|
unsigned int crc;
|
|
|
|
Py_ssize_t data_size, file_size, bytes_size;
|
|
|
|
long file_offset, header_size;
|
|
|
|
unsigned char buffer[30];
|
|
|
|
const char *errmsg = NULL;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(toc_entry, "OHnnlHHI", &datapath, &compress,
|
2010-05-09 12:52:27 -03:00
|
|
|
&data_size, &file_size, &file_offset, &time,
|
|
|
|
&date, &crc)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-01-22 02:02:46 -04:00
|
|
|
if (data_size < 0) {
|
|
|
|
PyErr_Format(ZipImportError, "negative data size");
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2014-02-16 15:17:28 -04:00
|
|
|
fp = _Py_fopen_obj(archive, "rb");
|
2016-01-28 15:30:16 -04:00
|
|
|
if (!fp) {
|
2014-02-16 15:17:28 -04:00
|
|
|
return NULL;
|
2016-01-28 15:30:16 -04:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Check to make sure the local file header is correct */
|
2012-10-02 21:13:05 -03:00
|
|
|
if (fseek(fp, file_offset, 0) == -1) {
|
2016-01-28 15:30:16 -04:00
|
|
|
goto file_error;
|
2012-10-02 21:13:05 -03:00
|
|
|
}
|
2016-01-28 15:30:16 -04:00
|
|
|
if (fread(buffer, 1, 30, fp) != 30) {
|
|
|
|
goto eof_error;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2016-01-28 15:30:16 -04:00
|
|
|
if (get_uint32(buffer) != 0x04034B50u) {
|
|
|
|
/* Bad: Local File Header */
|
|
|
|
errmsg = "bad local file header";
|
|
|
|
goto invalid_header;
|
2012-10-02 21:13:05 -03:00
|
|
|
}
|
|
|
|
|
2016-01-28 15:30:16 -04:00
|
|
|
header_size = (unsigned int)30 +
|
|
|
|
get_uint16(buffer + 26) /* file name */ +
|
|
|
|
get_uint16(buffer + 28) /* extra field */;
|
|
|
|
if (file_offset > LONG_MAX - header_size) {
|
|
|
|
errmsg = "bad local file header size";
|
|
|
|
goto invalid_header;
|
2013-10-28 21:43:44 -03:00
|
|
|
}
|
2016-01-28 15:30:16 -04:00
|
|
|
file_offset += header_size; /* Start of file data */
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2016-01-21 02:23:44 -04:00
|
|
|
if (data_size > LONG_MAX - 1) {
|
|
|
|
fclose(fp);
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
bytes_size = compress == 0 ? data_size : data_size + 1;
|
2016-01-28 15:30:16 -04:00
|
|
|
if (bytes_size == 0) {
|
2010-05-09 12:52:27 -03:00
|
|
|
bytes_size++;
|
2016-01-28 15:30:16 -04:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
raw_data = PyBytes_FromStringAndSize((char *)NULL, bytes_size);
|
|
|
|
if (raw_data == NULL) {
|
2016-01-28 15:30:16 -04:00
|
|
|
goto error;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
buf = PyBytes_AsString(raw_data);
|
|
|
|
|
2016-01-28 15:30:16 -04:00
|
|
|
if (fseek(fp, file_offset, 0) == -1) {
|
|
|
|
goto file_error;
|
2012-10-02 21:13:05 -03:00
|
|
|
}
|
2016-01-28 15:30:16 -04:00
|
|
|
if (fread(buf, 1, data_size, fp) != (size_t)data_size) {
|
2017-04-16 04:46:38 -03:00
|
|
|
PyErr_SetString(PyExc_OSError,
|
2010-05-09 12:52:27 -03:00
|
|
|
"zipimport: can't read data");
|
2016-01-28 15:30:16 -04:00
|
|
|
goto error;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
|
2016-01-28 15:30:16 -04:00
|
|
|
fclose(fp);
|
|
|
|
fp = NULL;
|
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
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);
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_DECREF(raw_data);
|
2017-09-29 15:34:31 -03:00
|
|
|
if (data != NULL && !PyBytes_Check(data)) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"zlib.decompress() must return a bytes object, not "
|
|
|
|
"%.200s",
|
|
|
|
Py_TYPE(data)->tp_name);
|
|
|
|
Py_DECREF(data);
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
return data;
|
2016-01-28 15:30:16 -04:00
|
|
|
|
|
|
|
eof_error:
|
|
|
|
set_file_error(archive, !ferror(fp));
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
file_error:
|
|
|
|
PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
invalid_header:
|
|
|
|
assert(errmsg != NULL);
|
|
|
|
PyErr_Format(ZipImportError, "%s: %R", errmsg, archive);
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (fp != NULL) {
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
Py_XDECREF(raw_data);
|
|
|
|
return NULL;
|
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
|
|
|
}
|
|
|
|
|
2017-03-11 01:02:52 -04:00
|
|
|
/* Given the contents of a .pyc file in a buffer, unmarshal the data
|
2002-12-30 18:08:05 -04:00
|
|
|
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 *
|
2011-03-14 21:46:50 -03:00
|
|
|
unmarshal_code(PyObject *pathname, PyObject *data, time_t mtime)
|
2002-12-30 18:08:05 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *code;
|
2016-01-28 15:30:16 -04:00
|
|
|
unsigned char *buf = (unsigned char *)PyBytes_AsString(data);
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_ssize_t size = PyBytes_Size(data);
|
|
|
|
|
2017-12-09 14:26:52 -04:00
|
|
|
if (size < 16) {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_SetString(ZipImportError,
|
|
|
|
"bad pyc data");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-01-28 15:30:16 -04:00
|
|
|
if (get_uint32(buf) != (unsigned int)PyImport_GetMagicNumber()) {
|
|
|
|
if (Py_VerboseFlag) {
|
2011-03-14 21:46:50 -03:00
|
|
|
PySys_FormatStderr("# %R has bad magic\n",
|
|
|
|
pathname);
|
2016-01-28 15:30:16 -04:00
|
|
|
}
|
2017-01-23 03:47:21 -04:00
|
|
|
Py_RETURN_NONE; /* signal caller to try alternative */
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
|
2017-12-09 14:26:52 -04:00
|
|
|
uint32_t flags = get_uint32(buf + 4);
|
|
|
|
if (flags != 0) {
|
2018-08-03 10:33:52 -03:00
|
|
|
_PyCoreConfig *config = &_PyInterpreterState_Get()->core_config;
|
2017-12-09 14:26:52 -04:00
|
|
|
// Hash-based pyc. We currently refuse to handle checked hash-based
|
|
|
|
// pycs. We could validate hash-based pycs against the source, but it
|
|
|
|
// seems likely that most people putting hash-based pycs in a zipfile
|
|
|
|
// will use unchecked ones.
|
2018-08-01 13:18:07 -03:00
|
|
|
if (strcmp(config->_check_hash_pycs_mode, "never") &&
|
|
|
|
(flags != 0x1 || !strcmp(config->_check_hash_pycs_mode, "always")))
|
2017-12-09 14:26:52 -04:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
} else if ((mtime != 0 && !eq_mtime(get_uint32(buf + 8), mtime))) {
|
2016-01-28 15:30:16 -04:00
|
|
|
if (Py_VerboseFlag) {
|
2011-03-14 21:46:50 -03:00
|
|
|
PySys_FormatStderr("# %R has bad mtime\n",
|
|
|
|
pathname);
|
2016-01-28 15:30:16 -04:00
|
|
|
}
|
2017-01-23 03:47:21 -04:00
|
|
|
Py_RETURN_NONE; /* signal caller to try alternative */
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
|
2012-01-13 13:52:16 -04:00
|
|
|
/* XXX the pyc's size field is ignored; timestamp collisions are probably
|
|
|
|
unimportant with zip files. */
|
2017-12-09 14:26:52 -04:00
|
|
|
code = PyMarshal_ReadObjectFromString((char *)buf + 16, size - 16);
|
2016-01-28 15:30:16 -04:00
|
|
|
if (code == NULL) {
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
2016-01-28 15:30:16 -04:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyCode_Check(code)) {
|
|
|
|
Py_DECREF(code);
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
2011-03-14 21:46:50 -03:00
|
|
|
"compiled module %R 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
|
|
|
}
|
|
|
|
|
2016-09-07 09:03:06 -03:00
|
|
|
/* Replace any occurrences of "\r\n?" in the input string with "\n".
|
2002-12-30 18:08:05 -04:00
|
|
|
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)
|
|
|
|
{
|
2011-03-14 21:46:50 -03:00
|
|
|
char *buf, *q, *p;
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *fixed_source;
|
|
|
|
int len = 0;
|
|
|
|
|
2011-03-14 21:46:50 -03:00
|
|
|
p = PyBytes_AsString(source);
|
|
|
|
if (p == NULL) {
|
2010-05-09 12:52:27 -03:00
|
|
|
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
|
2013-06-20 22:30:32 -03:00
|
|
|
and return a code object as a new reference. */
|
2002-12-30 18:08:05 -04:00
|
|
|
static PyObject *
|
2011-03-14 21:46:50 -03:00
|
|
|
compile_source(PyObject *pathname, PyObject *source)
|
2002-12-30 18:08:05 -04:00
|
|
|
{
|
2016-09-09 21:27:33 -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);
|
2011-03-14 21:46:50 -03:00
|
|
|
if (fixed_source == NULL) {
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
2011-03-14 21:46:50 -03:00
|
|
|
}
|
2002-12-30 18:08:05 -04:00
|
|
|
|
2016-09-09 21:27:33 -03:00
|
|
|
code = Py_CompileStringObject(PyBytes_AsString(fixed_source),
|
2016-09-14 02:09:48 -03:00
|
|
|
pathname, Py_file_input, NULL, -1);
|
2016-09-09 21:27:33 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-04-13 15:21:02 -03:00
|
|
|
/* Given a path to a .pyc 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
|
2011-03-14 21:46:50 -03:00
|
|
|
get_mtime_of_source(ZipImporter *self, PyObject *path)
|
2002-12-30 18:08:05 -04:00
|
|
|
{
|
2011-03-14 21:46:50 -03:00
|
|
|
PyObject *toc_entry, *stripped;
|
|
|
|
time_t mtime;
|
|
|
|
|
2017-03-11 01:02:52 -04:00
|
|
|
/* strip 'c' from *.pyc */
|
2011-09-28 02:41:54 -03:00
|
|
|
if (PyUnicode_READY(path) == -1)
|
|
|
|
return (time_t)-1;
|
|
|
|
stripped = PyUnicode_FromKindAndData(PyUnicode_KIND(path),
|
|
|
|
PyUnicode_DATA(path),
|
|
|
|
PyUnicode_GET_LENGTH(path) - 1);
|
2011-03-14 21:46:50 -03:00
|
|
|
if (stripped == NULL)
|
|
|
|
return (time_t)-1;
|
|
|
|
|
|
|
|
toc_entry = PyDict_GetItem(self->files, stripped);
|
|
|
|
Py_DECREF(stripped);
|
2010-05-09 12:52:27 -03:00
|
|
|
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);
|
2011-03-14 21:46:50 -03:00
|
|
|
} else
|
|
|
|
mtime = 0;
|
2010-05-09 12:52:27 -03:00
|
|
|
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 *
|
2014-02-16 15:17:28 -04:00
|
|
|
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
|
|
|
{
|
2011-03-14 21:46:50 -03:00
|
|
|
PyObject *data, *modpath, *code;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2014-02-16 15:17:28 -04:00
|
|
|
data = get_data(self->archive, toc_entry);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (data == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2011-03-14 21:46:50 -03:00
|
|
|
modpath = PyTuple_GetItem(toc_entry, 0);
|
2010-10-18 09:15:34 -03:00
|
|
|
if (isbytecode)
|
2011-03-14 21:46:50 -03:00
|
|
|
code = unmarshal_code(modpath, data, mtime);
|
2010-10-18 09:15:34 -03:00
|
|
|
else
|
2011-03-14 21:46:50 -03:00
|
|
|
code = compile_source(modpath, data);
|
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 *
|
2011-03-14 21:46:50 -03:00
|
|
|
get_module_code(ZipImporter *self, PyObject *fullname,
|
2010-10-18 09:09:02 -03:00
|
|
|
int *p_ispackage, PyObject **p_modpath)
|
2002-12-30 18:08:05 -04:00
|
|
|
{
|
2011-05-21 09:19:42 -03:00
|
|
|
PyObject *code = NULL, *toc_entry, *subname;
|
2011-05-26 08:59:41 -03:00
|
|
|
PyObject *path, *fullpath = NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
struct st_zip_searchorder *zso;
|
|
|
|
|
2017-10-20 17:42:35 -03:00
|
|
|
if (self->prefix == NULL) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"zipimporter.__init__() wasn't called");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
subname = get_subname(fullname);
|
2011-03-14 21:46:50 -03:00
|
|
|
if (subname == NULL)
|
|
|
|
return NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2011-03-14 21:46:50 -03:00
|
|
|
path = make_filename(self->prefix, subname);
|
|
|
|
Py_DECREF(subname);
|
|
|
|
if (path == NULL)
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (zso = zip_searchorder; *zso->suffix; zso++) {
|
2011-03-14 21:46:50 -03:00
|
|
|
code = NULL;
|
|
|
|
|
|
|
|
fullpath = PyUnicode_FromFormat("%U%s", path, zso->suffix);
|
|
|
|
if (fullpath == NULL)
|
|
|
|
goto exit;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
if (Py_VerboseFlag > 1)
|
2011-03-14 21:46:50 -03:00
|
|
|
PySys_FormatStderr("# trying %U%c%U\n",
|
|
|
|
self->archive, (int)SEP, fullpath);
|
|
|
|
toc_entry = PyDict_GetItem(self->files, fullpath);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (toc_entry != NULL) {
|
|
|
|
time_t mtime = 0;
|
|
|
|
int ispackage = zso->type & IS_PACKAGE;
|
|
|
|
int isbytecode = zso->type & IS_BYTECODE;
|
|
|
|
|
2011-03-14 21:46:50 -03:00
|
|
|
if (isbytecode) {
|
|
|
|
mtime = get_mtime_of_source(self, fullpath);
|
|
|
|
if (mtime == (time_t)-1 && PyErr_Occurred()) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Py_CLEAR(fullpath);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (p_ispackage != NULL)
|
|
|
|
*p_ispackage = ispackage;
|
2014-02-16 15:17:28 -04:00
|
|
|
code = get_code_from_data(self, ispackage,
|
2010-05-09 12:52:27 -03:00
|
|
|
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);
|
|
|
|
}
|
2011-03-14 21:46:50 -03:00
|
|
|
goto exit;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2011-03-14 21:46:50 -03:00
|
|
|
else
|
|
|
|
Py_CLEAR(fullpath);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2011-03-14 21:46:50 -03:00
|
|
|
PyErr_Format(ZipImportError, "can't find module %R", fullname);
|
|
|
|
exit:
|
|
|
|
Py_DECREF(path);
|
|
|
|
Py_XDECREF(fullpath);
|
|
|
|
return code;
|
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;
|
|
|
|
|
|
|
|
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
|
|
|
}
|