1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
From: Emmanuel Arias <eamanu@debian.org>
Date: Sat, 20 Dec 2025 13:02:00 -0300
Subject: Fix encoding default
Use sys module to get default file encoding
From: https://github.com/PyCQA/docformatter/commit/1b3f03d334d9c1322ccaa02b655aedadc150d9d3
---
src/docformatter/encode.py | 14 ++++++++++----
1 file changed, 10 insertions(+), 4 deletions(-)
diff --git a/src/docformatter/encode.py b/src/docformatter/encode.py
index 30e1042..e99ccab 100644
--- a/src/docformatter/encode.py
+++ b/src/docformatter/encode.py
@@ -43,7 +43,7 @@ class Encoder:
CRLF = "\r\n"
# Default encoding to use if the file encoding cannot be detected
- DEFAULT_ENCODING = "latin-1"
+ DEFAULT_ENCODING = sys.getdefaultencoding()
def __init__(self):
"""Initialize an Encoder instance."""
@@ -60,9 +60,15 @@ class Encoder:
"""
try:
detection_result = from_path(filename).best()
- self.encoding = (
- detection_result.encoding if detection_result else self.DEFAULT_ENCODING
- )
+ if detection_result and detection_result.encoding in ["utf_16", "utf_32"]:
+ # Treat undetectable/binary encodings as failure
+ self.encoding = self.DEFAULT_ENCODING
+ else:
+ self.encoding = (
+ detection_result.encoding
+ if detection_result
+ else self.DEFAULT_ENCODING
+ )
# Check for correctness of encoding.
with self.do_open_with_encoding(filename) as check_file:
|