From 5fb77966c7c6670b0e5c29452b0a7aee87946ef4 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Thu, 8 Jul 2021 00:56:46 -0700 Subject: [PATCH] Fix issue #1767: codec error in __init__ files without proper encoding set --- isort/place.py | 10 +++++----- .../namespaces/weird_encoding/.isort.cfg | 2 ++ .../namespaces/weird_encoding/root/__init__.py | 1 + .../namespaces/weird_encoding/root/nested/__init__.py | 0 tests/unit/test_place.py | 3 ++- 5 files changed, 10 insertions(+), 6 deletions(-) create mode 100644 tests/unit/example_projects/namespaces/weird_encoding/.isort.cfg create mode 100644 tests/unit/example_projects/namespaces/weird_encoding/root/__init__.py create mode 100644 tests/unit/example_projects/namespaces/weird_encoding/root/nested/__init__.py 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"