Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #1767: codec error in __init__ files without proper encodin… #1776

Merged
merged 1 commit into from Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions isort/place.py
Expand Up @@ -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
Expand Down
@@ -0,0 +1,2 @@
[settings]
src_paths=root
@@ -0,0 +1 @@
description = "基于FastAPI + Mysql的 TodoList" # Exception: UnicodeDecodeError
3 changes: 2 additions & 1 deletion tests/unit/test_place.py
Expand Up @@ -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"
Expand Down