Skip to content

Commit d526a94

Browse files
holtvogtweibullguy
andauthored
Fix AttributeError when encoding detection returns None (#304)
* Add venv to ignore list * Add default encoding constant * Fallback to default encoding when detection fails Now, uses the default encoding (latin-1) when from_path(filename).best() returns None. * Update encode.py --------- Co-authored-by: Doyle Rowland <[email protected]>
1 parent 8f3f550 commit d526a94

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ htmlcov/
1616
.idea/
1717
.vscode/
1818
.tox/
19+
.venv/

src/docformatter/encode.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ class Encoder:
4242
LF = "\n"
4343
CRLF = "\r\n"
4444

45+
# Default encoding to use if the file encoding cannot be detected
46+
DEFAULT_ENCODING = "latin-1"
47+
4548
def __init__(self):
4649
"""Initialize an Encoder instance."""
47-
self.encoding = "latin-1"
50+
self.encoding = self.DEFAULT_ENCODING
4851
self.system_encoding = locale.getpreferredencoding() or sys.getdefaultencoding()
4952

5053
def do_detect_encoding(self, filename) -> None:
@@ -56,13 +59,16 @@ def do_detect_encoding(self, filename) -> None:
5659
The full path name of the file whose encoding is to be detected.
5760
"""
5861
try:
59-
self.encoding = from_path(filename).best().encoding
62+
detection_result = from_path(filename).best()
63+
self.encoding = (
64+
detection_result.encoding if detection_result else self.DEFAULT_ENCODING
65+
)
6066

6167
# Check for correctness of encoding.
6268
with self.do_open_with_encoding(filename) as check_file:
6369
check_file.read()
6470
except (SyntaxError, LookupError, UnicodeDecodeError):
65-
self.encoding = "latin-1"
71+
self.encoding = self.DEFAULT_ENCODING
6672

6773
def do_find_newline(self, source: List[str]) -> str:
6874
"""Return type of newline used in source.

tests/test_encoding_functions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,21 @@ def test_detect_encoding_with_bad_encoding(self, temporary_file, contents):
8383

8484
assert "ascii" == uut.encoding
8585

86+
@pytest.mark.unit
87+
@pytest.mark.parametrize("contents", [""])
88+
def test_detect_encoding_with_undetectable_encoding(self, temporary_file):
89+
"""Default to latin-1 when encoding detection fails."""
90+
uut = Encoder()
91+
92+
# Simulate a file with undetectable encoding
93+
with open(temporary_file, "wb") as file:
94+
# Binary content unlikely to have a detectable encoding
95+
file.write(b"\xFF\xFE\xFD\xFC\x00\x00\x00\x00")
96+
97+
uut.do_detect_encoding(temporary_file)
98+
99+
assert uut.encoding == uut.DEFAULT_ENCODING
100+
86101

87102
class TestFindNewline:
88103
"""Class for testing the find_newline() function."""

0 commit comments

Comments
 (0)