mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Apply diff2.txt from SF patch http://www.python.org/sf/566999
This patch enhances Python/import.c/find_module() so that unicode objects found in sys.path will be treated as legal directory names (The current code ignores anything that is not a str). The unicode name is converted to str using Py_FileSystemDefaultEncoding.
This commit is contained in:
parent
3fca291a52
commit
3430d70e03
2 changed files with 32 additions and 7 deletions
|
@ -6,6 +6,9 @@ Type/class unification and new-style classes
|
||||||
|
|
||||||
Core and builtins
|
Core and builtins
|
||||||
|
|
||||||
|
- Unicode objects in sys.path are no longer ignored but treated
|
||||||
|
as directory names.
|
||||||
|
|
||||||
- The built-ins slice() and buffer() are now callable types. The
|
- The built-ins slice() and buffer() are now callable types. The
|
||||||
types classobj (formerly class), code, function, instance, and
|
types classobj (formerly class), code, function, instance, and
|
||||||
instancemethod (formerly instance-method), which have no built-in
|
instancemethod (formerly instance-method), which have no built-in
|
||||||
|
|
|
@ -969,15 +969,30 @@ find_module(char *realname, PyObject *path, char *buf, size_t buflen,
|
||||||
npath = PyList_Size(path);
|
npath = PyList_Size(path);
|
||||||
namelen = strlen(name);
|
namelen = strlen(name);
|
||||||
for (i = 0; i < npath; i++) {
|
for (i = 0; i < npath; i++) {
|
||||||
|
PyObject *copy = NULL;
|
||||||
PyObject *v = PyList_GetItem(path, i);
|
PyObject *v = PyList_GetItem(path, i);
|
||||||
|
#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;
|
||||||
|
v = copy;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
#endif
|
||||||
if (!PyString_Check(v))
|
if (!PyString_Check(v))
|
||||||
continue;
|
continue;
|
||||||
len = PyString_Size(v);
|
len = PyString_Size(v);
|
||||||
if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen)
|
if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
|
||||||
|
Py_XDECREF(copy);
|
||||||
continue; /* Too long */
|
continue; /* Too long */
|
||||||
|
}
|
||||||
strcpy(buf, PyString_AsString(v));
|
strcpy(buf, PyString_AsString(v));
|
||||||
if (strlen(buf) != len)
|
if (strlen(buf) != len) {
|
||||||
|
Py_XDECREF(copy);
|
||||||
continue; /* v contains '\0' */
|
continue; /* v contains '\0' */
|
||||||
|
}
|
||||||
#ifdef macintosh
|
#ifdef macintosh
|
||||||
/*
|
/*
|
||||||
** Speedup: each sys.path item is interned, and
|
** Speedup: each sys.path item is interned, and
|
||||||
|
@ -991,12 +1006,14 @@ find_module(char *realname, PyObject *path, char *buf, size_t buflen,
|
||||||
static struct filedescr resfiledescr =
|
static struct filedescr resfiledescr =
|
||||||
{"", "", PY_RESOURCE};
|
{"", "", PY_RESOURCE};
|
||||||
|
|
||||||
|
Py_XDECREF(copy);
|
||||||
return &resfiledescr;
|
return &resfiledescr;
|
||||||
}
|
}
|
||||||
if (PyMac_FindCodeResourceModule((PyStringObject *)v, name, buf)) {
|
if (PyMac_FindCodeResourceModule((PyStringObject *)v, name, buf)) {
|
||||||
static struct filedescr resfiledescr =
|
static struct filedescr resfiledescr =
|
||||||
{"", "", PY_CODERESOURCE};
|
{"", "", PY_CODERESOURCE};
|
||||||
|
|
||||||
|
Py_XDECREF(copy);
|
||||||
return &resfiledescr;
|
return &resfiledescr;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -1015,15 +1032,19 @@ find_module(char *realname, PyObject *path, char *buf, size_t buflen,
|
||||||
if (stat(buf, &statbuf) == 0 && /* it exists */
|
if (stat(buf, &statbuf) == 0 && /* it exists */
|
||||||
S_ISDIR(statbuf.st_mode) && /* it's a directory */
|
S_ISDIR(statbuf.st_mode) && /* it's a directory */
|
||||||
find_init_module(buf) && /* it has __init__.py */
|
find_init_module(buf) && /* it has __init__.py */
|
||||||
case_ok(buf, len, namelen, name)) /* and case matches */
|
case_ok(buf, len, namelen, name)) { /* and case matches */
|
||||||
|
Py_XDECREF(copy);
|
||||||
return &fd_package;
|
return &fd_package;
|
||||||
|
}
|
||||||
#else
|
#else
|
||||||
/* XXX How are you going to test for directories? */
|
/* XXX How are you going to test for directories? */
|
||||||
#ifdef RISCOS
|
#ifdef RISCOS
|
||||||
if (isdir(buf) &&
|
if (isdir(buf) &&
|
||||||
find_init_module(buf) &&
|
find_init_module(buf) &&
|
||||||
case_ok(buf, len, namelen, name))
|
case_ok(buf, len, namelen, name)) {
|
||||||
|
Py_XDECREF(copy);
|
||||||
return &fd_package;
|
return &fd_package;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
#ifdef macintosh
|
#ifdef macintosh
|
||||||
|
@ -1095,6 +1116,7 @@ find_module(char *realname, PyObject *path, char *buf, size_t buflen,
|
||||||
saved_buf = NULL;
|
saved_buf = NULL;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
Py_XDECREF(copy);
|
||||||
if (fp != NULL)
|
if (fp != NULL)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue