diff --git a/isort/place.py b/isort/place.py index aaf954aa8..47a68c78a 100644 --- a/isort/place.py +++ b/isort/place.py @@ -125,14 +125,14 @@ def _is_namespace_package(path: Path, src_extensions: FrozenSet[str]) -> bool: if filenames: return False else: - with init_file.open() as open_init_file: + with init_file.open("rb") as open_init_file: file_start = open_init_file.read(4096) if ( - "__import__('pkg_resources').declare_namespace(__name__)" not in file_start - and '__import__("pkg_resources").declare_namespace(__name__)' not in file_start - and "__path__ = __import__('pkgutil').extend_path(__path__, __name__)" + b"__import__('pkg_resources').declare_namespace(__name__)" not in file_start + and b'__import__("pkg_resources").declare_namespace(__name__)' not in file_start + and b"__path__ = __import__('pkgutil').extend_path(__path__, __name__)" not in file_start - and '__path__ = __import__("pkgutil").extend_path(__path__, __name__)' + and b'__path__ = __import__("pkgutil").extend_path(__path__, __name__)' not in file_start ): return False diff --git a/tests/unit/example_projects/namespaces/weird_encoding/.isort.cfg b/tests/unit/example_projects/namespaces/weird_encoding/.isort.cfg new file mode 100644 index 000000000..d3ae4c35a --- /dev/null +++ b/tests/unit/example_projects/namespaces/weird_encoding/.isort.cfg @@ -0,0 +1,2 @@ +[settings] +src_paths=root diff --git a/tests/unit/example_projects/namespaces/weird_encoding/root/__init__.py b/tests/unit/example_projects/namespaces/weird_encoding/root/__init__.py new file mode 100644 index 000000000..1b8a961c7 --- /dev/null +++ b/tests/unit/example_projects/namespaces/weird_encoding/root/__init__.py @@ -0,0 +1 @@ +description = "基于FastAPI + Mysql的 TodoList" # Exception: UnicodeDecodeError diff --git a/tests/unit/example_projects/namespaces/weird_encoding/root/nested/__init__.py b/tests/unit/example_projects/namespaces/weird_encoding/root/nested/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/unit/test_place.py b/tests/unit/test_place.py index 18ac7fccd..11abdd66e 100644 --- a/tests/unit/test_place.py +++ b/tests/unit/test_place.py @@ -47,7 +47,8 @@ def test_namespace_package_placement(examples_path): no_namespace = namespace_examples / "none" almost_implicit = namespace_examples / "almost-implicit" - for lacks_namespace in (no_namespace, almost_implicit): + weird_encoding = namespace_examples / "weird_encoding" + for lacks_namespace in (no_namespace, almost_implicit, weird_encoding): config = Config(settings_path=lacks_namespace) manual_namespace = Config(settings_path=lacks_namespace, namespace_packages=["root"]) assert place.module("root.name", config=config) == "FIRSTPARTY"