Skip to content

Commit

Permalink
Merge pull request #1776 from PyCQA/issue/1767-encoding
Browse files Browse the repository at this point in the history
Fix issue #1767: codec error in __init__ files without proper encodin…
  • Loading branch information
timothycrosley committed Jul 8, 2021
2 parents 1c8df11 + 5fb7796 commit c2ca2e2
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 6 deletions.
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
Empty file.
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

0 comments on commit c2ca2e2

Please sign in to comment.