Skip to content

Commit

Permalink
Replace invalid characters in string decoding (#4)
Browse files Browse the repository at this point in the history
* Replace invalid characters in string decoding

* Add test

* Add changelog entry
  • Loading branch information
cbrnr authored Nov 15, 2023
1 parent 6c020dc commit 80fedaa
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Changed
- When `EdfSignal.physical_min` or `EdfSignal.physical_max` do not fit into their header fields, they are now always rounded down or up, respectively, to ensure all physical values lie within the physical range ([#2](https://github.com/the-siesta-group/edfio/pull/2)).
- Support non-standard header fields (not encoded as UTF-8) by replacing incompatible characters with "�" ([#4](https://github.com/the-siesta-group/edfio/pull/4)).


## [0.1.0] - 2023-11-09
Expand Down
2 changes: 1 addition & 1 deletion edfio/_header_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def encode_str(value: str, length: int) -> bytes:


def decode_str(field: bytes) -> str:
return field.decode().rstrip()
return field.decode(errors="replace").rstrip()


def encode_int(value: int, length: int) -> bytes:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_header_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from edfio._header_field import (
RawHeaderFieldDate,
decode_float,
decode_str,
encode_float,
encode_int,
)
Expand Down Expand Up @@ -30,6 +31,11 @@ def test_encode_float(value: float, expected: bytes):
assert encode_float(value, 8) == expected


def test_decode_str():
assert decode_str("è".encode("latin-1")) == "�"
assert decode_str("è".encode()) == "è"


@pytest.mark.parametrize("field", [b"1E2345 ", b"-1E2345 "])
def test_decode_float_exceeding_float_range_fails(field: bytes):
with pytest.raises(ValueError, match="outside float range"):
Expand Down

0 comments on commit 80fedaa

Please sign in to comment.