summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoshua Coats <joshu@fearchar.net>2019-02-11 21:13:58 -0800
committerJoshua Coats <joshu@fearchar.net>2019-02-11 21:13:58 -0800
commit4955a9c1d1481b71377447146ab8621affa8c02f (patch)
tree21664a4cd5ff53c2bcc73ae1d9e22f02991df53e
parent27515dbf9ba2137bd62f766d930273232338d953 (diff)
downloadisort-4955a9c1d1481b71377447146ab8621affa8c02f.tar.gz
Handle third-party packages ending with EXT_SUFFIX
-rw-r--r--isort/finders.py6
-rw-r--r--test_isort.py26
2 files changed, 31 insertions, 1 deletions
diff --git a/isort/finders.py b/isort/finders.py
index 54259afd..f4da2c15 100644
--- a/isort/finders.py
+++ b/isort/finders.py
@@ -148,11 +148,15 @@ class PathFinder(BaseFinder):
# handle case-insensitive paths on windows
self.stdlib_lib_prefix = os.path.normcase(sysconfig.get_paths()['stdlib'])
+ # handle compiled libraries
+ self.ext_suffix = sysconfig.get_config_var("EXT_SUFFIX") or ".so"
+
def find(self, module_name):
for prefix in self.paths:
package_path = "/".join((prefix, module_name.split(".")[0]))
is_module = (exists_case_sensitive(package_path + ".py") or
- exists_case_sensitive(package_path + ".so"))
+ exists_case_sensitive(package_path + ".so") or
+ exists_case_sensitive(package_path + self.ext_suffix))
is_package = exists_case_sensitive(package_path) and os.path.isdir(package_path)
if is_module or is_package:
if 'site-packages' in prefix:
diff --git a/test_isort.py b/test_isort.py
index 25ad87e7..a21c38d4 100644
--- a/test_isort.py
+++ b/test_isort.py
@@ -25,7 +25,9 @@ from __future__ import absolute_import, division, print_function, unicode_litera
from tempfile import NamedTemporaryFile
import io
+import os.path
import sys
+import sysconfig
import pytest
@@ -2592,3 +2594,27 @@ def test_monkey_patched_urllib():
# Previous versions of isort monkey patched urllib which caused unusual
# importing for other projects.
from urllib import quote # noqa: F401
+
+
+@pytest.mark.skipif(sys.version_info[0] == 2, reason="Requires Python 3")
+def test_path_finder(monkeypatch):
+ si = SortImports(file_contents="")
+ finder = finders.PathFinder(
+ config=si.config,
+ sections=si.sections,
+ )
+ third_party_prefix = next(path for path in finder.paths if "site-packages" in path)
+ ext_suffix = sysconfig.get_config_var("EXT_SUFFIX") or ".so"
+ imaginary_paths = set([
+ os.path.join(finder.stdlib_lib_prefix, "example_1.py"),
+ os.path.join(third_party_prefix, "example_2.py"),
+ os.path.join(third_party_prefix, "example_3.so"),
+ os.path.join(third_party_prefix, "example_4" + ext_suffix),
+ os.path.join(os.getcwd(), "example_5.py"),
+ ])
+ monkeypatch.setattr("isort.finders.exists_case_sensitive", lambda p: p in imaginary_paths)
+ assert finder.find("example_1") == finder.sections.STDLIB
+ assert finder.find("example_2") == finder.sections.THIRDPARTY
+ assert finder.find("example_3") == finder.sections.THIRDPARTY
+ assert finder.find("example_4") == finder.sections.THIRDPARTY
+ assert finder.find("example_5") == finder.sections.FIRSTPARTY \ No newline at end of file