Skip to content

Commit 57f5409

Browse files
authored
feat: Use errors=replace instead of returning an empty string (#556)
1 parent 2dcdd2a commit 57f5409

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

aws_lambda_builders/utils.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,11 @@ def extract_tarfile(tarfile_path: Union[str, os.PathLike], unpack_dir: Union[str
235235

236236
def decode(to_decode: bytes, encoding: Optional[str] = None) -> str:
237237
"""
238-
Perform a "safe" decoding of a series of bytes. If the decoding works, returns the decoded bytes.
239-
If the decoding fails, returns an empty string instead of throwing an exception.
238+
Perform a "safe" decoding of a series of bytes. Attempts to find the localized encoding
239+
if not provided, and avoids raising an exception, instead, if an unrecognized character
240+
is found, replaces it with a replacement character.
241+
242+
https://docs.python.org/3/library/codecs.html#codec-base-classes
240243
241244
Parameters
242245
----------
@@ -248,11 +251,7 @@ def decode(to_decode: bytes, encoding: Optional[str] = None) -> str:
248251
Returns
249252
-------
250253
str
251-
Decoded string if decoding succeeds, empty string if decoding fails
254+
Decoded string with unrecognized characters replaced with a replacement character
252255
"""
253-
encoding = encoding if encoding else locale.getpreferredencoding()
254-
try:
255-
return to_decode.decode(encoding).strip()
256-
except UnicodeDecodeError:
257-
LOG.debug(f"Unable to decode bytes: {to_decode} with encoding: {encoding}")
258-
return ""
256+
encoding = encoding or locale.getpreferredencoding()
257+
return to_decode.decode(encoding, errors="replace").strip()

tests/unit/test_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class TestDecode(TestCase):
3939
def test_does_not_crash_non_utf8_encoding(self):
4040
message = "hello\n\n ß".encode("iso-8859-1")
4141
# Windows will decode this string as expected, *nix systems won't
42-
expected_message = "hello\n\n ß" if platform.system().lower() == "windows" else ""
42+
expected_message = "hello\n\n ß" if platform.system().lower() == "windows" else "hello\n\n"
4343
response = decode(message)
4444
self.assertEqual(response, expected_message)
4545

@@ -49,7 +49,7 @@ def test_is_able_to_decode_non_utf8_encoding(self):
4949
self.assertEqual(response, "hello\n\n ß")
5050

5151
@patch("aws_lambda_builders.utils.locale")
52-
def test_isa_able_to_decode_non_utf8_locale(self, mock_locale):
52+
def test_is_able_to_decode_non_utf8_locale(self, mock_locale):
5353
mock_locale.getpreferredencoding.return_value = "iso-8859-1"
5454
message = "hello\n\n ß".encode("iso-8859-1")
5555
response = decode(message)

0 commit comments

Comments
 (0)