summaryrefslogtreecommitdiff
path: root/Python
diff options
context:
space:
mode:
Diffstat (limited to 'Python')
-rw-r--r--Python/future.c11
-rw-r--r--Python/getargs.c2
-rw-r--r--Python/import.c229
-rw-r--r--Python/pythonrun.c50
-rw-r--r--Python/thread.c2
5 files changed, 164 insertions, 130 deletions
diff --git a/Python/future.c b/Python/future.c
index 96be757b2e..0e68845981 100644
--- a/Python/future.c
+++ b/Python/future.c
@@ -59,13 +59,6 @@ future_parse(PyFutureFeatures *ff, mod_ty mod, const char *filename)
{
int i, found_docstring = 0, done = 0, prev_line = 0;
- static PyObject *future;
- if (!future) {
- future = PyString_InternFromString("__future__");
- if (!future)
- return 0;
- }
-
if (!(mod->kind == Module_kind || mod->kind == Interactive_kind))
return 1;
@@ -92,7 +85,9 @@ future_parse(PyFutureFeatures *ff, mod_ty mod, const char *filename)
*/
if (s->kind == ImportFrom_kind) {
- if (s->v.ImportFrom.module == future) {
+ identifier modname = s->v.ImportFrom.module;
+ if (modname && PyString_GET_SIZE(modname) == 10 &&
+ !strcmp(PyString_AS_STRING(modname), "__future__")) {
if (done) {
PyErr_SetString(PyExc_SyntaxError,
ERR_LATE_FUTURE);
diff --git a/Python/getargs.c b/Python/getargs.c
index eccdc9bfb3..a6cebbc75d 100644
--- a/Python/getargs.c
+++ b/Python/getargs.c
@@ -1410,7 +1410,7 @@ getbuffer(PyObject *arg, Py_buffer *view, char **errmsg)
*errmsg = "convertible to a buffer";
return count;
}
- PyBuffer_FillInfo(view, NULL, buf, count, 1, 0);
+ PyBuffer_FillInfo(view, arg, buf, count, 1, 0);
return 0;
}
diff --git a/Python/import.c b/Python/import.c
index 2cac9b530b..4d8a610c3d 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -114,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 */
@@ -968,7 +996,7 @@ load_source_module(char *name, char *pathname, FILE *fp)
{
struct stat st;
FILE *fpc;
- char buf[MAXPATHLEN+1];
+ char *buf;
char *cpathname;
PyCodeObject *co;
PyObject *m;
@@ -987,6 +1015,10 @@ load_source_module(char *name, char *pathname, FILE *fp)
*/
st.st_mtime &= 0xFFFFFFFF;
}
+ buf = PyMem_MALLOC(MAXPATHLEN+1);
+ if (buf == NULL) {
+ return PyErr_NoMemory();
+ }
cpathname = make_compiled_pathname(pathname, buf,
(size_t)MAXPATHLEN + 1);
if (cpathname != NULL &&
@@ -994,9 +1026,9 @@ load_source_module(char *name, char *pathname, FILE *fp)
co = read_compiled_module(cpathname, fpc);
fclose(fpc);
if (co == NULL)
- return NULL;
+ goto error_exit;
if (update_compiled_module(co, pathname) < 0)
- return NULL;
+ goto error_exit;
if (Py_VerboseFlag)
PySys_WriteStderr("import %s # precompiled from %s\n",
name, cpathname);
@@ -1005,7 +1037,7 @@ load_source_module(char *name, char *pathname, FILE *fp)
else {
co = parse_source_module(pathname, fp);
if (co == NULL)
- return NULL;
+ goto error_exit;
if (Py_VerboseFlag)
PySys_WriteStderr("import %s # from %s\n",
name, pathname);
@@ -1018,7 +1050,12 @@ load_source_module(char *name, char *pathname, FILE *fp)
m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Py_DECREF(co);
+ PyMem_FREE(buf);
return m;
+
+error_exit:
+ PyMem_FREE(buf);
+ return NULL;
}
@@ -1038,7 +1075,7 @@ load_package(char *name, char *pathname)
PyObject *file = NULL;
PyObject *path = NULL;
int err;
- char buf[MAXPATHLEN+1];
+ char *buf = NULL;
FILE *fp = NULL;
struct filedescr *fdp;
@@ -1060,8 +1097,13 @@ load_package(char *name, char *pathname)
err = PyDict_SetItemString(d, "__path__", path);
if (err != 0)
goto error;
+ buf = PyMem_MALLOC(MAXPATHLEN+1);
+ if (buf == NULL) {
+ PyErr_NoMemory();
+ goto error;
+ }
buf[0] = '\0';
- fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
+ fdp = find_module(name, "__init__", path, buf, MAXPATHLEN+1, &fp, NULL);
if (fdp == NULL) {
if (PyErr_ExceptionMatches(PyExc_ImportError)) {
PyErr_Clear();
@@ -1079,6 +1121,8 @@ load_package(char *name, char *pathname)
error:
m = NULL;
cleanup:
+ if (buf)
+ PyMem_FREE(buf);
Py_XDECREF(path);
Py_XDECREF(file);
return m;
@@ -1204,13 +1248,10 @@ 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};
- char name[MAXPATHLEN+1];
+ char *name;
#if defined(PYOS_OS2)
size_t saved_len;
size_t saved_namelen;
@@ -1224,6 +1265,10 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
"module name is too long");
return NULL;
}
+ name = PyMem_MALLOC(MAXPATHLEN+1);
+ if (name == NULL) {
+ return PyErr_NoMemory();
+ }
strcpy(name, subname);
/* sys.meta_path import hook */
@@ -1235,7 +1280,7 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
PyErr_SetString(PyExc_RuntimeError,
"sys.meta_path must be a list of "
"import hooks");
- return NULL;
+ goto error_exit;
}
Py_INCREF(meta_path); /* zap guard */
npath = PyList_Size(meta_path);
@@ -1248,12 +1293,13 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
path : Py_None);
if (loader == NULL) {
Py_DECREF(meta_path);
- return NULL; /* true error */
+ goto error_exit; /* true error */
}
if (loader != Py_None) {
/* a loader was found */
*p_loader = loader;
Py_DECREF(meta_path);
+ PyMem_FREE(name);
return &importhookdescr;
}
Py_DECREF(loader);
@@ -1267,7 +1313,7 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
PyErr_SetString(PyExc_ImportError,
"full frozen module name too long");
- return NULL;
+ goto error_exit;
}
strcpy(buf, PyString_AsString(path));
strcat(buf, ".");
@@ -1275,19 +1321,22 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
strcpy(name, buf);
if (find_frozen(name) != NULL) {
strcpy(buf, name);
+ PyMem_FREE(name);
return &fd_frozen;
}
PyErr_Format(PyExc_ImportError,
"No frozen submodule named %.200s", name);
- return NULL;
+ goto error_exit;
}
if (path == NULL) {
if (is_builtin(name)) {
strcpy(buf, name);
+ PyMem_FREE(name);
return &fd_builtin;
}
if ((find_frozen(name)) != NULL) {
strcpy(buf, name);
+ PyMem_FREE(name);
return &fd_frozen;
}
@@ -1295,6 +1344,7 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
if (fp != NULL) {
*p_fp = fp;
+ PyMem_FREE(name);
return fdp;
}
#endif
@@ -1303,7 +1353,7 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
if (path == NULL || !PyList_Check(path)) {
PyErr_SetString(PyExc_RuntimeError,
"sys.path must be a list of directory names");
- return NULL;
+ goto error_exit;
}
path_hooks = PySys_GetObject("path_hooks");
@@ -1311,14 +1361,14 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
PyErr_SetString(PyExc_RuntimeError,
"sys.path_hooks must be a list of "
"import hooks");
- return NULL;
+ goto error_exit;
}
path_importer_cache = PySys_GetObject("path_importer_cache");
if (path_importer_cache == NULL ||
!PyDict_Check(path_importer_cache)) {
PyErr_SetString(PyExc_RuntimeError,
"sys.path_importer_cache must be a dict");
- return NULL;
+ goto error_exit;
}
npath = PyList_Size(path);
@@ -1327,13 +1377,13 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
PyObject *copy = NULL;
PyObject *v = PyList_GetItem(path, i);
if (!v)
- return NULL;
+ goto error_exit;
#ifdef Py_USING_UNICODE
if (PyUnicode_Check(v)) {
copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v),
PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL);
if (copy == NULL)
- return NULL;
+ goto error_exit;
v = copy;
}
else
@@ -1359,7 +1409,7 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
path_hooks, v);
if (importer == NULL) {
Py_XDECREF(copy);
- return NULL;
+ goto error_exit;
}
/* Note: importer is a borrowed reference */
if (importer != Py_None) {
@@ -1369,10 +1419,11 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
"s", fullname);
Py_XDECREF(copy);
if (loader == NULL)
- return NULL; /* error */
+ goto error_exit; /* error */
if (loader != Py_None) {
/* a loader was found */
*p_loader = loader;
+ PyMem_FREE(name);
return &importhookdescr;
}
Py_DECREF(loader);
@@ -1392,12 +1443,11 @@ 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);
+ PyMem_FREE(name);
return &fd_package;
}
else {
@@ -1408,32 +1458,10 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
if (PyErr_Warn(PyExc_ImportWarning,
warnstr)) {
Py_XDECREF(copy);
- return NULL;
+ goto error_exit;
}
}
}
-#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
@@ -1505,10 +1533,15 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
if (fp == NULL) {
PyErr_Format(PyExc_ImportError,
"No module named %.200s", name);
- return NULL;
+ goto error_exit;
}
*p_fp = fp;
+ PyMem_FREE(name);
return fdp;
+
+error_exit:
+ PyMem_FREE(name);
+ return NULL;
}
/* Helpers for main.c
@@ -2115,7 +2148,7 @@ static PyObject *
import_module_level(char *name, PyObject *globals, PyObject *locals,
PyObject *fromlist, int level)
{
- char buf[MAXPATHLEN+1];
+ char *buf;
Py_ssize_t buflen = 0;
PyObject *parent, *head, *next, *tail;
@@ -2129,14 +2162,18 @@ import_module_level(char *name, PyObject *globals, PyObject *locals,
return NULL;
}
+ buf = PyMem_MALLOC(MAXPATHLEN+1);
+ if (buf == NULL) {
+ return PyErr_NoMemory();
+ }
parent = get_parent(globals, buf, &buflen, level);
if (parent == NULL)
- return NULL;
+ goto error_exit;
head = load_next(parent, level < 0 ? Py_None : parent, &name, buf,
&buflen);
if (head == NULL)
- return NULL;
+ goto error_exit;
tail = head;
Py_INCREF(tail);
@@ -2145,7 +2182,7 @@ import_module_level(char *name, PyObject *globals, PyObject *locals,
Py_DECREF(tail);
if (next == NULL) {
Py_DECREF(head);
- return NULL;
+ goto error_exit;
}
tail = next;
}
@@ -2157,7 +2194,7 @@ import_module_level(char *name, PyObject *globals, PyObject *locals,
Py_DECREF(head);
PyErr_SetString(PyExc_ValueError,
"Empty module name");
- return NULL;
+ goto error_exit;
}
if (fromlist != NULL) {
@@ -2167,16 +2204,22 @@ import_module_level(char *name, PyObject *globals, PyObject *locals,
if (fromlist == NULL) {
Py_DECREF(tail);
+ PyMem_FREE(buf);
return head;
}
Py_DECREF(head);
if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Py_DECREF(tail);
- return NULL;
+ goto error_exit;
}
+ PyMem_FREE(buf);
return tail;
+
+error_exit:
+ PyMem_FREE(buf);
+ return NULL;
}
PyObject *
@@ -2566,7 +2609,7 @@ import_submodule(PyObject *mod, char *subname, char *fullname)
}
else {
PyObject *path, *loader = NULL;
- char buf[MAXPATHLEN+1];
+ char *buf;
struct filedescr *fdp;
FILE *fp = NULL;
@@ -2581,11 +2624,16 @@ import_submodule(PyObject *mod, char *subname, char *fullname)
}
}
+ buf = PyMem_MALLOC(MAXPATHLEN+1);
+ if (buf == NULL) {
+ return PyErr_NoMemory();
+ }
buf[0] = '\0';
fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
&fp, &loader);
Py_XDECREF(path);
if (fdp == NULL) {
+ PyMem_FREE(buf);
if (!PyErr_ExceptionMatches(PyExc_ImportError))
return NULL;
PyErr_Clear();
@@ -2600,6 +2648,7 @@ import_submodule(PyObject *mod, char *subname, char *fullname)
Py_XDECREF(m);
m = NULL;
}
+ PyMem_FREE(buf);
}
return m;
@@ -2617,7 +2666,7 @@ PyImport_ReloadModule(PyObject *m)
PyObject *modules = PyImport_GetModuleDict();
PyObject *path = NULL, *loader = NULL, *existing_m = NULL;
char *name, *subname;
- char buf[MAXPATHLEN+1];
+ char *buf;
struct filedescr *fdp;
FILE *fp = NULL;
PyObject *newm;
@@ -2677,6 +2726,11 @@ PyImport_ReloadModule(PyObject *m)
if (path == NULL)
PyErr_Clear();
}
+ buf = PyMem_MALLOC(MAXPATHLEN+1);
+ if (buf == NULL) {
+ Py_XDECREF(path);
+ return PyErr_NoMemory();
+ }
buf[0] = '\0';
fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
Py_XDECREF(path);
@@ -2684,6 +2738,7 @@ PyImport_ReloadModule(PyObject *m)
if (fdp == NULL) {
Py_XDECREF(loader);
imp_modules_reloading_clear();
+ PyMem_FREE(buf);
return NULL;
}
@@ -2701,6 +2756,7 @@ PyImport_ReloadModule(PyObject *m)
PyDict_SetItemString(modules, name, m);
}
imp_modules_reloading_clear();
+ PyMem_FREE(buf);
return newm;
}
@@ -2831,19 +2887,27 @@ call_find_module(char *name, PyObject *path)
extern int fclose(FILE *);
PyObject *fob, *ret;
struct filedescr *fdp;
- char pathname[MAXPATHLEN+1];
+ char *pathname;
FILE *fp = NULL;
+ pathname = PyMem_MALLOC(MAXPATHLEN+1);
+ if (pathname == NULL) {
+ return PyErr_NoMemory();
+ }
pathname[0] = '\0';
if (path == Py_None)
path = NULL;
fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
- if (fdp == NULL)
+ if (fdp == NULL) {
+ PyMem_FREE(pathname);
return NULL;
+ }
if (fp != NULL) {
fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
- if (fob == NULL)
+ if (fob == NULL) {
+ PyMem_FREE(pathname);
return NULL;
+ }
}
else {
fob = Py_None;
@@ -2852,6 +2916,7 @@ call_find_module(char *name, PyObject *path)
ret = Py_BuildValue("Os(ssi)",
fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Py_DECREF(fob);
+ PyMem_FREE(pathname);
return ret;
}
@@ -3199,49 +3264,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;
}
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index dcb2c0ebcc..afb4c51017 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -989,55 +989,67 @@ parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
lineno, offset, text);
- /* new style errors. `err' is an instance */
+ *message = NULL;
- if (! (v = PyObject_GetAttrString(err, "msg")))
+ /* new style errors. `err' is an instance */
+ *message = PyObject_GetAttrString(err, "msg");
+ if (!*message)
goto finally;
- *message = v;
- if (!(v = PyObject_GetAttrString(err, "filename")))
+ v = PyObject_GetAttrString(err, "filename");
+ if (!v)
goto finally;
- if (v == Py_None)
+ if (v == Py_None) {
+ Py_DECREF(v);
*filename = NULL;
- else if (! (*filename = PyString_AsString(v)))
- goto finally;
+ }
+ else {
+ *filename = PyString_AsString(v);
+ Py_DECREF(v);
+ if (!*filename)
+ goto finally;
+ }
- Py_DECREF(v);
- if (!(v = PyObject_GetAttrString(err, "lineno")))
+ v = PyObject_GetAttrString(err, "lineno");
+ if (!v)
goto finally;
hold = PyInt_AsLong(v);
Py_DECREF(v);
- v = NULL;
if (hold < 0 && PyErr_Occurred())
goto finally;
*lineno = (int)hold;
- if (!(v = PyObject_GetAttrString(err, "offset")))
+ v = PyObject_GetAttrString(err, "offset");
+ if (!v)
goto finally;
if (v == Py_None) {
*offset = -1;
Py_DECREF(v);
- v = NULL;
} else {
hold = PyInt_AsLong(v);
Py_DECREF(v);
- v = NULL;
if (hold < 0 && PyErr_Occurred())
goto finally;
*offset = (int)hold;
}
- if (!(v = PyObject_GetAttrString(err, "text")))
+ v = PyObject_GetAttrString(err, "text");
+ if (!v)
goto finally;
- if (v == Py_None)
+ if (v == Py_None) {
+ Py_DECREF(v);
*text = NULL;
- else if (! (*text = PyString_AsString(v)))
- goto finally;
- Py_DECREF(v);
+ }
+ else {
+ *text = PyString_AsString(v);
+ Py_DECREF(v);
+ if (!*text)
+ goto finally;
+ }
return 1;
finally:
- Py_XDECREF(v);
+ Py_XDECREF(*message);
return 0;
}
diff --git a/Python/thread.c b/Python/thread.c
index dd359e9f7b..dd333e8f94 100644
--- a/Python/thread.c
+++ b/Python/thread.c
@@ -24,7 +24,7 @@
#include <stdlib.h>
#ifdef __sgi
-#ifndef HAVE_PTHREAD_H /* XXX Need to check in configure.in */
+#ifndef HAVE_PTHREAD_H /* XXX Need to check in configure.ac */
#undef _POSIX_THREADS
#endif
#endif