Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions hl7/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,11 @@ def unescape(container, field, app_map=None): # noqa: C901
else:
rv.append(str(c))

if in_seq:
# The field ended in the middle of an escape sequence (e.g. a trailing
# lone escape character). It cannot be decoded, so preserve the
# original characters literally rather than dropping them.
rv.append(container.esc)
rv.extend(collecting)

return "".join(rv)
4 changes: 4 additions & 0 deletions tests/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,10 @@ def test_unescape(self):
self.assertEqual(msg.unescape("\\X20202020\\"), " ")
self.assertEqual(msg.unescape("\\Xe1\\\\Xe9\\\\Xed\\\\Xf3\\\\Xfa\\"), "áéíóú")

# Trailing unterminated escape sequence is preserved literally (#84)
self.assertEqual(msg.unescape("\\E\\R\\"), "\\R\\")
self.assertEqual(msg.unescape("text\\"), "text\\")

def test_escape(self):
msg = hl7.parse(rep_sample_hl7)

Expand Down