[Python-checkins] cpython: Issue #3080: find_init_module() expects Unicode (original) (raw)
victor.stinner python-checkins at python.org
Sun Mar 20 04:13:59 CET 2011
- Previous message: [Python-checkins] cpython: Issue #3080: Refactor find_module_path(), use return instead of break
- Next message: [Python-checkins] cpython: Issue #3080: case_ok() expects Unicode strings
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
http://hg.python.org/cpython/rev/298a70b27497 changeset: 68733:298a70b27497 user: Victor Stinner <victor.stinner at haypocalc.com> date: Sun Mar 20 03:07:28 2011 +0100 summary: Issue #3080: find_init_module() expects Unicode
files: Python/import.c
diff --git a/Python/import.c b/Python/import.c --- a/Python/import.c +++ b/Python/import.c @@ -1668,8 +1668,9 @@ PyObject *p_path); #endif +/ Forward */ static int case_ok(char *, Py_ssize_t, Py_ssize_t, const char *); -static int find_init_module(char ); / Forward / +static int find_init_module(PyObject ); static struct filedescr importhookdescr = {"", "", IMP_HOOK}; / Get the path of a module: get its importer and call importer.find_module() @@ -1766,24 +1767,27 @@ if (stat(buf, &statbuf) == 0 && / it exists / S_ISDIR(statbuf.st_mode)) / it's a directory */ { + PyObject bufobj = PyUnicode_DecodeFSDefault(buf); + if (bufobj == NULL) + return -1; if (case_ok(buf, len, namelen, namestr)) { / case matches / - if (find_init_module(buf)) { / and has init.py / + if (find_init_module(bufobj)) { / and has init.py */ + Py_DECREF(bufobj); *p_fd = &fd_package; return 2; } else { int err; - PyObject unicode = PyUnicode_DecodeFSDefault(buf); - if (unicode == NULL) + err = PyErr_WarnFormat(PyExc_ImportWarning, 1, + "Not importing directory %R: missing init.py", + bufobj); + if (err) { + Py_DECREF(bufobj); return -1; - err = PyErr_WarnFormat(PyExc_ImportWarning, 1, - "Not importing directory '%U': missing init.py", - unicode); - Py_DECREF(unicode); - if (err) - return -1; + } } } + Py_DECREF(bufobj); } #endif return 1; @@ -2154,49 +2158,47 @@ #ifdef HAVE_STAT -/ Helper to look for init.py or init.py[co] in potential package / +/ Helper to look for init.py or init.py[co] in potential package. + Return 1 if init was found, 0 if not, or -1 on error. */ static int -find_init_module(char *buf) +find_init_module(PyObject *directory) { - const size_t save_len = strlen(buf); - size_t i = save_len; - char pname; / pointer to start of init */ + size_t len; struct stat statbuf;
-/* For calling case_ok(buf, len, namelen, name):
/a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
^ ^ ^ ^
|--------------------- buf ---------------------|
|------------------- len ------------------|
|------ name -------|
|----- namelen -----|
- */
- if (save_len + 13 >= MAXPATHLEN)
return 0;
- buf[i++] = SEP;
- pname = buf + i;
- strcpy(pname, "init.py");
- if (stat(buf, &statbuf) == 0) {
if (case_ok(buf,
save_len + 9, /* len("/__init__") */
8, /* len("__init__") */
pname)) {
buf[save_len] = '\0';
- PyObject *filename;
- int match;
- char *filestr;
- size_t filelen;
- len = PyUnicode_GET_SIZE(directory);
- filename = PyUnicode_FromFormat("%U%c__init__.py", directory, SEP);
- if (filename == NULL)
return -1;
- if (_Py_stat(filename, &statbuf) == 0) {
/* 9=len("/__init__") */
filestr = _PyUnicode_AsString(filename);
filelen = strlen(filestr);
if (case_ok(filestr, filelen-9, 8, "__init__")) {
}Py_DECREF(filename); return 1; }
- i += strlen(pname);
- strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
- if (stat(buf, &statbuf) == 0) {
if (case_ok(buf,
save_len + 9, /* len("/__init__") */
8, /* len("__init__") */
pname)) {
buf[save_len] = '\0';
- Py_DECREF(filename);
- filename = PyUnicode_FromFormat("%U%c__init__.py%c",
directory, SEP, Py_OptimizeFlag ? 'o' : 'c');
- if (filename == NULL)
return -1;
- if (_Py_stat(filename, &statbuf) == 0) {
/* 9=len("/__init__") */
filestr = _PyUnicode_AsString(filename);
filelen = strlen(filestr);
if (case_ok(filestr, filelen-9, 8, "__init__")) {
}Py_DECREF(filename); return 1; }
- buf[save_len] = '\0';
- Py_DECREF(filename); return 0; }
-- Repository URL: http://hg.python.org/cpython
- Previous message: [Python-checkins] cpython: Issue #3080: Refactor find_module_path(), use return instead of break
- Next message: [Python-checkins] cpython: Issue #3080: case_ok() expects Unicode strings
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]