Skip to content

Commit

Permalink
Allow for byte decoding of windows codespace 1252 (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
rocco8773 authored Feb 21, 2023
1 parent 1d8916f commit 83f0f79
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
5 changes: 4 additions & 1 deletion bapsflib/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def _bytes_to_str(string: Union[bytes, str]) -> str:
return string

if isinstance(string, bytes):
return str(string, "utf-8")
try:
return str(string, "utf-8")
except UnicodeDecodeError:
return str(string, "cp1252")

raise TypeError(f"Argument 'string' is not of type str or bytes, got {type(string)}.")
2 changes: 2 additions & 0 deletions bapsflib/utils/tests/test_bytes_to_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def test_valid_vals(self):
conditions = [
("Hello", "Hello"),
(b"Goodbye", "Goodbye"),
(b"doesn't", "doesn't"),
(b"doesn\x92t", "doesn’t"),
]
for inputs, expected in conditions:
with self.subTest(inputs=inputs, expected=expected):
Expand Down
3 changes: 3 additions & 0 deletions changelog/100.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Updated :func:`bapsflib.utils._bytes_to_str` to handle byte strings
that my have been encoded using
`Windows codespace 1252 <https://en.wikipedia.org/wiki/Windows-1252>`_.
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None
language = "en"

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
Expand Down

0 comments on commit 83f0f79

Please sign in to comment.