Skip to content

Commit c2ca2e2

Browse files
Merge pull request #1776 from PyCQA/issue/1767-encoding
Fix issue #1767: codec error in __init__ files without proper encodin…
2 parents 1c8df11 + 5fb7796 commit c2ca2e2

File tree

5 files changed

+10
-6
lines changed

5 files changed

+10
-6
lines changed

isort/place.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,14 @@ def _is_namespace_package(path: Path, src_extensions: FrozenSet[str]) -> bool:
125125
if filenames:
126126
return False
127127
else:
128-
with init_file.open() as open_init_file:
128+
with init_file.open("rb") as open_init_file:
129129
file_start = open_init_file.read(4096)
130130
if (
131-
"__import__('pkg_resources').declare_namespace(__name__)" not in file_start
132-
and '__import__("pkg_resources").declare_namespace(__name__)' not in file_start
133-
and "__path__ = __import__('pkgutil').extend_path(__path__, __name__)"
131+
b"__import__('pkg_resources').declare_namespace(__name__)" not in file_start
132+
and b'__import__("pkg_resources").declare_namespace(__name__)' not in file_start
133+
and b"__path__ = __import__('pkgutil').extend_path(__path__, __name__)"
134134
not in file_start
135-
and '__path__ = __import__("pkgutil").extend_path(__path__, __name__)'
135+
and b'__path__ = __import__("pkgutil").extend_path(__path__, __name__)'
136136
not in file_start
137137
):
138138
return False
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[settings]
2+
src_paths=root
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
description = "基于FastAPI + Mysql的 TodoList" # Exception: UnicodeDecodeError

tests/unit/example_projects/namespaces/weird_encoding/root/nested/__init__.py

Whitespace-only changes.

tests/unit/test_place.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ def test_namespace_package_placement(examples_path):
4747

4848
no_namespace = namespace_examples / "none"
4949
almost_implicit = namespace_examples / "almost-implicit"
50-
for lacks_namespace in (no_namespace, almost_implicit):
50+
weird_encoding = namespace_examples / "weird_encoding"
51+
for lacks_namespace in (no_namespace, almost_implicit, weird_encoding):
5152
config = Config(settings_path=lacks_namespace)
5253
manual_namespace = Config(settings_path=lacks_namespace, namespace_packages=["root"])
5354
assert place.module("root.name", config=config) == "FIRSTPARTY"

0 commit comments

Comments
 (0)