summaryrefslogtreecommitdiffstats
path: root/Python/import.c
diff options
context:
space:
mode:
Diffstat (limited to 'Python/import.c')
-rw-r--r--Python/import.c170
1 files changed, 67 insertions, 103 deletions
diff --git a/Python/import.c b/Python/import.c
index 07f5720..127b807 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -27,8 +27,6 @@ extern "C" {
typedef unsigned short mode_t;
#endif
-extern time_t PyOS_GetLastModificationTime(char *, FILE *);
- /* In getmtime.c */
/* Magic word to reject .pyc files generated by other Python versions.
It should change for each incompatible change to the bytecode.
@@ -73,9 +71,15 @@ extern time_t PyOS_GetLastModificationTime(char *, FILE *);
Python 2.5c2: 62131 (fix wrong code: for x, in ... in listcomp/genexp)
Python 2.6a0: 62151 (peephole optimizations and STORE_MAP opcode)
Python 2.6a1: 62161 (WITH_CLEANUP optimization)
+ Python 2.7a0: 62171 (optimize list comprehensions/change LIST_APPEND)
+ Python 2.7a0: 62181 (optimize conditional branches:
+ introduce POP_JUMP_IF_FALSE and POP_JUMP_IF_TRUE)
+ Python 2.7a0 62191 (introduce SETUP_WITH)
+ Python 2.7a0 62201 (introduce BUILD_SET)
+ Python 2.7a0 62211 (introduce MAP_ADD and SET_ADD)
.
*/
-#define MAGIC (62161 | ((long)'\r'<<16) | ((long)'\n'<<24))
+#define MAGIC (62211 | ((long)'\r'<<16) | ((long)'\n'<<24))
/* Magic word as global; note that _PyImport_Init() can change the
value of this global to accommodate for alterations of how the
@@ -110,6 +114,34 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
};
#endif
+#ifdef MS_WINDOWS
+int isdir(char *path) {
+ DWORD rv;
+ /* see issue1293 and issue3677:
+ * stat() on Windows doesn't recognise paths like
+ * "e:\\shared\\" and "\\\\whiterab-c2znlh\\shared" as dirs.
+ * Also reference issue6727:
+ * stat() on Windows is broken and doesn't resolve symlinks properly.
+ */
+ rv = GetFileAttributesA(path);
+ return rv != INVALID_FILE_ATTRIBUTES && rv & FILE_ATTRIBUTE_DIRECTORY;
+}
+#else
+#ifdef HAVE_STAT
+int isdir(char *path) {
+ struct stat statbuf;
+ return stat(path, &statbuf) == 0 && S_ISDIR(statbuf.st_mode);
+}
+#else
+#ifdef RISCOS
+/* with RISCOS, isdir is in unixstuff */
+#else
+int isdir(char *path) {
+ return 0;
+}
+#endif /* RISCOS */
+#endif /* HAVE_STAT */
+#endif /* MS_WINDOWS */
/* Initialize things */
@@ -619,7 +651,7 @@ PyImport_AddModule(const char *name)
/* Remove name from sys.modules, if it's there. */
static void
-_RemoveModule(const char *name)
+remove_module(const char *name)
{
PyObject *modules = PyImport_GetModuleDict();
if (PyDict_GetItemString(modules, name) == NULL)
@@ -691,7 +723,7 @@ PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
return m;
error:
- _RemoveModule(name);
+ remove_module(name);
return NULL;
}
@@ -901,9 +933,9 @@ write_compiled_module(PyCodeObject *co, char *cpathname, struct stat *srcstat)
(void) unlink(cpathname);
return;
}
- /* Now write the true mtime */
+ /* Now write the true mtime (as a 32-bit field) */
fseek(fp, 4L, 0);
- assert(mtime < LONG_MAX);
+ assert(mtime <= 0xFFFFFFFF);
PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
fflush(fp);
fclose(fp);
@@ -975,17 +1007,14 @@ load_source_module(char *name, char *pathname, FILE *fp)
pathname);
return NULL;
}
-#if SIZEOF_TIME_T > 4
- /* Python's .pyc timestamp handling presumes that the timestamp fits
- in 4 bytes. This will be fine until sometime in the year 2038,
- when a 4-byte signed time_t will overflow.
- */
- if (st.st_mtime >> 32) {
- PyErr_SetString(PyExc_OverflowError,
- "modification time overflows a 4 byte field");
- return NULL;
+ if (sizeof st.st_mtime > 4) {
+ /* Python's .pyc timestamp handling presumes that the timestamp fits
+ in 4 bytes. Since the code only does an equality comparison,
+ ordering is not important and we can safely ignore the higher bits
+ (collisions are extremely unlikely).
+ */
+ st.st_mtime &= 0xFFFFFFFF;
}
-#endif
cpathname = make_compiled_pathname(pathname, buf,
(size_t)MAXPATHLEN + 1);
if (cpathname != NULL &&
@@ -1203,9 +1232,6 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
char *filemode;
FILE *fp = NULL;
PyObject *path_hooks, *path_importer_cache;
-#ifndef RISCOS
- struct stat statbuf;
-#endif
static struct filedescr fd_frozen = {"", "", PY_FROZEN};
static struct filedescr fd_builtin = {"", "", C_BUILTIN};
static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
@@ -1231,7 +1257,7 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
meta_path = PySys_GetObject("meta_path");
if (meta_path == NULL || !PyList_Check(meta_path)) {
- PyErr_SetString(PyExc_ImportError,
+ PyErr_SetString(PyExc_RuntimeError,
"sys.meta_path must be a list of "
"import hooks");
return NULL;
@@ -1300,14 +1326,14 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
path = PySys_GetObject("path");
}
if (path == NULL || !PyList_Check(path)) {
- PyErr_SetString(PyExc_ImportError,
+ PyErr_SetString(PyExc_RuntimeError,
"sys.path must be a list of directory names");
return NULL;
}
path_hooks = PySys_GetObject("path_hooks");
if (path_hooks == NULL || !PyList_Check(path_hooks)) {
- PyErr_SetString(PyExc_ImportError,
+ PyErr_SetString(PyExc_RuntimeError,
"sys.path_hooks must be a list of "
"import hooks");
return NULL;
@@ -1315,7 +1341,7 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
path_importer_cache = PySys_GetObject("path_importer_cache");
if (path_importer_cache == NULL ||
!PyDict_Check(path_importer_cache)) {
- PyErr_SetString(PyExc_ImportError,
+ PyErr_SetString(PyExc_RuntimeError,
"sys.path_importer_cache must be a dict");
return NULL;
}
@@ -1391,9 +1417,7 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
/* Check for package import (buf holds a directory name,
and there's an __init__ module in that directory */
-#ifdef HAVE_STAT
- if (stat(buf, &statbuf) == 0 && /* it exists */
- S_ISDIR(statbuf.st_mode) && /* it's a directory */
+ if (isdir(buf) && /* it's an existing directory */
case_ok(buf, len, namelen, name)) { /* case matches */
if (find_init_module(buf)) { /* and has __init__.py */
Py_XDECREF(copy);
@@ -1411,28 +1435,6 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
}
}
}
-#else
- /* XXX How are you going to test for directories? */
-#ifdef RISCOS
- if (isdir(buf) &&
- case_ok(buf, len, namelen, name)) {
- if (find_init_module(buf)) {
- Py_XDECREF(copy);
- return &fd_package;
- }
- else {
- char warnstr[MAXPATHLEN+80];
- sprintf(warnstr, "Not importing directory "
- "'%.*s': missing __init__.py",
- MAXPATHLEN, buf);
- if (PyErr_Warn(PyExc_ImportWarning,
- warnstr)) {
- Py_XDECREF(copy);
- return NULL;
- }
- }
-#endif
-#endif
#if defined(PYOS_OS2)
/* take a snapshot of the module spec for restoration
* after the 8 character DLL hackery
@@ -1797,7 +1799,7 @@ static int init_builtin(char *); /* Forward */
its module object WITH INCREMENTED REFERENCE COUNT */
static PyObject *
-load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
+load_module(char *name, FILE *fp, char *pathname, int type, PyObject *loader)
{
PyObject *modules;
PyObject *m;
@@ -1818,27 +1820,27 @@ load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
switch (type) {
case PY_SOURCE:
- m = load_source_module(name, buf, fp);
+ m = load_source_module(name, pathname, fp);
break;
case PY_COMPILED:
- m = load_compiled_module(name, buf, fp);
+ m = load_compiled_module(name, pathname, fp);
break;
#ifdef HAVE_DYNAMIC_LOADING
case C_EXTENSION:
- m = _PyImport_LoadDynamicModule(name, buf, fp);
+ m = _PyImport_LoadDynamicModule(name, pathname, fp);
break;
#endif
case PKG_DIRECTORY:
- m = load_package(name, buf);
+ m = load_package(name, pathname);
break;
case C_BUILTIN:
case PY_FROZEN:
- if (buf != NULL && buf[0] != '\0')
- name = buf;
+ if (pathname != NULL && pathname[0] != '\0')
+ name = pathname;
if (type == C_BUILTIN)
err = init_builtin(name);
else
@@ -2060,7 +2062,9 @@ PyImport_ImportModuleNoBlock(const char *name)
{
PyObject *result;
PyObject *modules;
+#ifdef WITH_THREAD
long me;
+#endif
/* Try to get the module from sys.modules[name] */
modules = PyImport_GetModuleDict();
@@ -2839,10 +2843,8 @@ call_find_module(char *name, PyObject *path)
return NULL;
if (fp != NULL) {
fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
- if (fob == NULL) {
- fclose(fp);
+ if (fob == NULL)
return NULL;
- }
}
else {
fob = Py_None;
@@ -3198,49 +3200,11 @@ NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
PyErr_SetString(PyExc_ImportError, "empty pathname");
return -1;
} else {
-#ifndef RISCOS
-#ifndef MS_WINDOWS
- struct stat statbuf;
- int rv;
-
- rv = stat(path, &statbuf);
- if (rv == 0) {
- /* it exists */
- if (S_ISDIR(statbuf.st_mode)) {
- /* it's a directory */
- PyErr_SetString(PyExc_ImportError,
- "existing directory");
- return -1;
- }
- }
-#else /* MS_WINDOWS */
- DWORD rv;
- /* see issue1293 and issue3677:
- * stat() on Windows doesn't recognise paths like
- * "e:\\shared\\" and "\\\\whiterab-c2znlh\\shared" as dirs.
- */
- rv = GetFileAttributesA(path);
- if (rv != INVALID_FILE_ATTRIBUTES) {
- /* it exists */
- if (rv & FILE_ATTRIBUTE_DIRECTORY) {
- /* it's a directory */
- PyErr_SetString(PyExc_ImportError,
- "existing directory");
- return -1;
- }
- }
-#endif
-#else /* RISCOS */
- if (object_exists(path)) {
- /* it exists */
- if (isdir(path)) {
- /* it's a directory */
- PyErr_SetString(PyExc_ImportError,
- "existing directory");
- return -1;
- }
+ if(isdir(path)) {
+ PyErr_SetString(PyExc_ImportError,
+ "existing directory");
+ return -1;
}
-#endif
}
return 0;
}
@@ -3375,13 +3339,13 @@ PyImport_ExtendInittab(struct _inittab *newtab)
/* Shorthand to add a single entry given a name and a function */
int
-PyImport_AppendInittab(char *name, void (*initfunc)(void))
+PyImport_AppendInittab(const char *name, void (*initfunc)(void))
{
struct _inittab newtab[2];
memset(newtab, '\0', sizeof newtab);
- newtab[0].name = name;
+ newtab[0].name = (char *)name;
newtab[0].initfunc = initfunc;
return PyImport_ExtendInittab(newtab);