diff --git a/hl7/util.py b/hl7/util.py index 86aac2c..ef5ca31 100644 --- a/hl7/util.py +++ b/hl7/util.py @@ -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) diff --git a/tests/test_parse.py b/tests/test_parse.py index 39cc786..0e1c244 100644 --- a/tests/test_parse.py +++ b/tests/test_parse.py @@ -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)