[Python-checkins] cpython: Issue #3080: find_module() returns the path as Unicode (original) (raw)

victor.stinner python-checkins at python.org
Sun Mar 20 04:14:02 CET 2011


http://hg.python.org/cpython/rev/9aec6f0e4076 changeset: 68735:9aec6f0e4076 user: Victor Stinner <victor.stinner at haypocalc.com> date: Mon Mar 14 15:19:39 2011 -0400 summary: Issue #3080: find_module() returns the path as Unicode

files: Python/import.c

diff --git a/Python/import.c b/Python/import.c --- a/Python/import.c +++ b/Python/import.c @@ -1498,7 +1498,7 @@ /* Forward */ static PyObject *load_module(PyObject *, FILE *, PyObject *, int, PyObject *); static struct filedescr *find_module(PyObject *, PyObject *, PyObject *, - char *, size_t, FILE **, PyObject **); + PyObject **, FILE **, PyObject **); static struct _frozen * find_frozen(PyObject ); / Load a package and return its module object WITH INCREMENTED @@ -1510,7 +1510,6 @@ PyObject *m, *d, *bufobj; PyObject *file = NULL, *path_list = NULL; int err; - char buf[MAXPATHLEN+1]; FILE *fp = NULL; struct filedescr *fdp; @@ -1538,7 +1537,7 @@ return NULL; } fdp = find_module(name, initstr, path_list, - buf, sizeof(buf), &fp, NULL); + &bufobj, &fp, NULL); Py_DECREF(path_list); if (fdp == NULL) { if (PyErr_ExceptionMatches(PyExc_ImportError)) { @@ -1549,13 +1548,8 @@ else return NULL; } - bufobj = PyUnicode_DecodeFSDefault(buf); - if (bufobj != NULL) { - m = load_module(name, fp, bufobj, fdp->type, NULL); - Py_DECREF(bufobj); - } - else - m = NULL; + m = load_module(name, fp, bufobj, fdp->type, NULL); + Py_XDECREF(bufobj); if (fp != NULL) fclose(fp); return m; @@ -1686,43 +1680,40 @@ static int find_module_path(PyObject *fullname, PyObject *name, PyObject *path, PyObject *path_hooks, PyObject *path_importer_cache, - char *buf, size_t buflen, - PyObject **p_loader, struct filedescr **p_fd) + PyObject **p_path, PyObject **p_loader, struct filedescr **p_fd) { - PyObject *path_bytes; - const char *base; + Py_UNICODE buf[MAXPATHLEN+1]; + Py_ssize_t buflen = MAXPATHLEN+1; + PyObject *path_unicode, *filename; + const Py_UNICODE *base; Py_ssize_t len; - size_t namelen; struct stat statbuf; static struct filedescr fd_package = {"", "", PKG_DIRECTORY}; - char namestr; if (PyUnicode_Check(path)) { - path_bytes = PyUnicode_EncodeFSDefault(path); - if (path_bytes == NULL) - return -1; + Py_INCREF(path); + path_unicode = path; } else if (PyBytes_Check(path)) { - Py_INCREF(path); - path_bytes = path; + path_unicode = PyUnicode_DecodeFSDefaultAndSize( + PyBytes_AS_STRING(path), PyBytes_GET_SIZE(path)); + if (path_unicode == NULL) + return -1; } else return 0; - namestr = _PyUnicode_AsString(name); - namelen = strlen(namestr); - base = PyBytes_AS_STRING(path_bytes); - len = PyBytes_GET_SIZE(path_bytes); - if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) { - Py_DECREF(path_bytes); + base = PyUnicode_AS_UNICODE(path_unicode); + len = PyUnicode_GET_SIZE(path_unicode); + if (len + 2 + PyUnicode_GET_SIZE(name) + MAXSUFFIXSIZE >= buflen) { + Py_DECREF(path_unicode); return 0; / Too long */ } - strcpy(buf, base); - Py_DECREF(path_bytes);

@@ -1758,21 +1749,21 @@ #endif ) buf[len++] = SEP; - strcpy(buf+len, namestr); - len += namelen; + Py_UNICODE_strcpy(buf+len, PyUnicode_AS_UNICODE(name)); + len += PyUnicode_GET_SIZE(name); + + filename = PyUnicode_FromUnicode(buf, len); + if (filename == NULL) + return -1; /* 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 / + if (_Py_stat(filename, &statbuf) == 0 && / it exists / S_ISDIR(statbuf.st_mode)) / it's a directory */ { int match; - PyObject *filename;

@@ -1780,7 +1771,7 @@ } if (match) { /* case matches / if (find_init_module(filename)) { / and has init.py */ - Py_DECREF(filename); + *p_path = filename; *p_fd = &fd_package; return 2; } @@ -1795,9 +1786,9 @@ } } } - Py_DECREF(filename); } #endif + *p_path = filename; return 1; } @@ -1814,21 +1805,16 @@ find_module_path_list(PyObject *fullname, PyObject *name, PyObject *search_path_list, PyObject *path_hooks, PyObject *path_importer_cache, - char *buf, size_t buflen, - FILE **p_fp, PyObject **p_loader) + PyObject **p_path, FILE **p_fp, PyObject **p_loader) { Py_ssize_t i, npath; - size_t len, namelen; struct filedescr *fdp = NULL; char *filemode; FILE *fp = NULL; - char *namestr; - PyObject *filename; + PyObject *prefix, *filename; int match; npath = PyList_Size(search_path_list); - namestr = _PyUnicode_AsString(name); - namelen = strlen(namestr); for (i = 0; i < npath; i++) { PyObject *path; int ok; @@ -1837,27 +1823,29 @@ if (path == NULL) return NULL; + prefix = NULL; ok = find_module_path(fullname, name, path, path_hooks, path_importer_cache, - buf, buflen, - p_loader, &fdp); + &prefix, p_loader, &fdp); if (ok < 0) return NULL; if (ok == 0) continue; - if (ok == 2) + if (ok == 2) { + *p_path = prefix; return fdp;

@@ -1869,11 +1857,13 @@ } match = case_ok(filename, -(Py_ssize_t)strlen(fdp->suffix), name); if (match < 0) {

@@ -1882,6 +1872,7 @@ fclose(fp); fp = NULL; }

@@ -1897,32 +1888,35 @@ - otherwise, call find_module_path_list() with search_path_list (if not NULL) or sys.path

static struct filedescr * find_module(PyObject *fullname, PyObject *name, PyObject *search_path_list,

{ Py_ssize_t i, npath; static struct filedescr fd_frozen = {"", "", PY_FROZEN}; static struct filedescr fd_builtin = {"", "", C_BUILTIN}; PyObject *path_hooks, *path_importer_cache;

@@ -1975,20 +1969,12 @@ #ifdef MS_COREDLL FILE *fp; struct filedescr *fdp;

#endif if (is_builtin(name)) return &fd_builtin; #ifdef MS_COREDLL

@@ -2022,8 +2008,7 @@ return find_module_path_list(fullname, name, search_path_list, path_hooks, path_importer_cache,

}

/* case_bytes(char* buf, Py_ssize_t len, Py_ssize_t namelen, char* name) @@ -3185,7 +3170,6 @@ { PyObject *modules = PyImport_GetModuleDict(); PyObject *m = NULL, *bufobj, *path_list, *loader;

@@ -3211,7 +3195,7 @@ }

 fdp = find_module(fullname, subname, path_list,

@@ -3220,13 +3204,8 @@ Py_INCREF(Py_None); return Py_None; }

@@ -3249,7 +3228,6 @@ PyInterpreterState *interp = PyThreadState_Get()->interp; PyObject *modules_reloading = interp->modules_reloading; PyObject *modules = PyImport_GetModuleDict();

@@ -3333,13 +3311,8 @@ goto error; }

@@ -3509,17 +3482,15 @@ PyObject *fob, *ret; PyObject *pathobj; struct filedescr *fdp;

@@ -3552,7 +3526,10 @@ fob = Py_None; Py_INCREF(fob); }

-- Repository URL: http://hg.python.org/cpython



More information about the Python-checkins mailing list