Skip to content

Commit 7e58677

Browse files
committed
Merge pull request #1037 from LoyukiL/sys_path_unicode
Fix #599 by allowing subclasses of string and unicode in sys.path
2 parents 7fc3720 + 65388a2 commit 7e58677

File tree

5 files changed

+27
-2
lines changed

5 files changed

+27
-2
lines changed

src/runtime/import.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,15 @@ SearchResult findModule(const std::string& name, BoxedString* full_name, BoxedLi
222222
llvm::SmallString<128> joined_path;
223223
for (int i = 0; i < path_list->size; i++) {
224224
Box* _p = path_list->elts->elts[i];
225-
if (_p->cls != str_cls)
225+
if (isSubclass(_p->cls, unicode_cls)) {
226+
_p = PyUnicode_AsEncodedString(_p, Py_FileSystemDefaultEncoding, NULL);
227+
if (_p == NULL) {
228+
continue;
229+
}
230+
}
231+
if (!isSubclass(_p->cls, str_cls)) {
226232
continue;
233+
}
227234
BoxedString* p = static_cast<BoxedString*>(_p);
228235

229236
joined_path.clear();
@@ -686,7 +693,7 @@ static int isdir(const char* path) {
686693
Box* nullImporterInit(Box* self, Box* _path) {
687694
RELEASE_ASSERT(self->cls == null_importer_cls, "");
688695

689-
if (_path->cls != str_cls)
696+
if (!isSubclass(_path->cls, str_cls))
690697
raiseExcHelper(TypeError, "must be string, not %s", getTypeName(_path));
691698

692699
BoxedString* path = (BoxedString*)_path;

test/tests/sys_path_str.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import sys
2+
sys.path.append("test_package")
3+
import import_target

test/tests/sys_path_str_subclass.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import sys
2+
class TestClass(str):
3+
pass
4+
t = TestClass("test_package")
5+
sys.path.append(t)
6+
import import_target

test/tests/sys_path_unicode.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import sys
2+
sys.path.append(u"test_package")
3+
import import_target
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import sys
2+
class TestClass(unicode):
3+
pass
4+
t = TestClass("test_package")
5+
sys.path.append(t)
6+
import import_target

0 commit comments

Comments
 (0)