Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3430d70

Browse files
committed
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.
1 parent 3fca291 commit 3430d70

2 files changed

Lines changed: 32 additions & 7 deletions

File tree

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ Type/class unification and new-style classes
66

77
Core and builtins
88

9+
- Unicode objects in sys.path are no longer ignored but treated
10+
as directory names.
11+
912
- The built-ins slice() and buffer() are now callable types. The
1013
types classobj (formerly class), code, function, instance, and
1114
instancemethod (formerly instance-method), which have no built-in

Python/import.c

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -969,15 +969,30 @@ find_module(char *realname, PyObject *path, char *buf, size_t buflen,
969969
npath = PyList_Size(path);
970970
namelen = strlen(name);
971971
for (i = 0; i < npath; i++) {
972+
PyObject *copy = NULL;
972973
PyObject *v = PyList_GetItem(path, i);
974+
#ifdef Py_USING_UNICODE
975+
if (PyUnicode_Check(v)) {
976+
copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v),
977+
PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL);
978+
if (copy == NULL)
979+
return NULL;
980+
v = copy;
981+
}
982+
else
983+
#endif
973984
if (!PyString_Check(v))
974985
continue;
975986
len = PyString_Size(v);
976-
if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen)
987+
if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
988+
Py_XDECREF(copy);
977989
continue; /* Too long */
990+
}
978991
strcpy(buf, PyString_AsString(v));
979-
if (strlen(buf) != len)
992+
if (strlen(buf) != len) {
993+
Py_XDECREF(copy);
980994
continue; /* v contains '\0' */
995+
}
981996
#ifdef macintosh
982997
/*
983998
** Speedup: each sys.path item is interned, and
@@ -991,12 +1006,14 @@ find_module(char *realname, PyObject *path, char *buf, size_t buflen,
9911006
static struct filedescr resfiledescr =
9921007
{"", "", PY_RESOURCE};
9931008

1009+
Py_XDECREF(copy);
9941010
return &resfiledescr;
9951011
}
9961012
if (PyMac_FindCodeResourceModule((PyStringObject *)v, name, buf)) {
9971013
static struct filedescr resfiledescr =
9981014
{"", "", PY_CODERESOURCE};
9991015

1016+
Py_XDECREF(copy);
10001017
return &resfiledescr;
10011018
}
10021019
#endif
@@ -1012,18 +1029,22 @@ find_module(char *realname, PyObject *path, char *buf, size_t buflen,
10121029
/* Check for package import (buf holds a directory name,
10131030
and there's an __init__ module in that directory */
10141031
#ifdef HAVE_STAT
1015-
if (stat(buf, &statbuf) == 0 && /* it exists */
1016-
S_ISDIR(statbuf.st_mode) && /* it's a directory */
1017-
find_init_module(buf) && /* it has __init__.py */
1018-
case_ok(buf, len, namelen, name)) /* and case matches */
1032+
if (stat(buf, &statbuf) == 0 && /* it exists */
1033+
S_ISDIR(statbuf.st_mode) && /* it's a directory */
1034+
find_init_module(buf) && /* it has __init__.py */
1035+
case_ok(buf, len, namelen, name)) { /* and case matches */
1036+
Py_XDECREF(copy);
10191037
return &fd_package;
1038+
}
10201039
#else
10211040
/* XXX How are you going to test for directories? */
10221041
#ifdef RISCOS
10231042
if (isdir(buf) &&
10241043
find_init_module(buf) &&
1025-
case_ok(buf, len, namelen, name))
1044+
case_ok(buf, len, namelen, name)) {
1045+
Py_XDECREF(copy);
10261046
return &fd_package;
1047+
}
10271048
#endif
10281049
#endif
10291050
#ifdef macintosh
@@ -1095,6 +1116,7 @@ find_module(char *realname, PyObject *path, char *buf, size_t buflen,
10951116
saved_buf = NULL;
10961117
}
10971118
#endif
1119+
Py_XDECREF(copy);
10981120
if (fp != NULL)
10991121
break;
11001122
}

0 commit comments

Comments
 (0)