Skip to content

Preserve a trailing unterminated escape sequence in unescape() (#84)#85

Open
CedricConday wants to merge 1 commit into
johnpaulett:mainfrom
CedricConday:fix/84-unescape-trailing-escape
Open

Preserve a trailing unterminated escape sequence in unescape() (#84)#85
CedricConday wants to merge 1 commit into
johnpaulett:mainfrom
CedricConday:fix/84-unescape-trailing-escape

Conversation

@CedricConday

@CedricConday CedricConday commented Jun 30, 2026

Copy link
Copy Markdown

Closes #84

msg = hl7.Message(separator="\r", sequence=[["MSH", "^~\\&"]])
msg.unescape("\\E\\R\\")   # '\E\R\'

returns \R but should return \R\:

  • the leading \E\ decodes to a literal \,
  • leaving R\ — an R followed by a trailing lone escape character.

Cause

unescape() walks the field character by character. A trailing escape character sets in_seq = True but the sequence never closes, so at the end of the loop the opening escape character (and any characters collected after it) are left unflushed and silently dropped.

Fix

After the loop, if we're still mid-sequence, flush the unterminated sequence literally — it can't be decoded, so the original characters are preserved instead of lost.

Tests

Added to test_unescape:

self.assertEqual(msg.unescape("\\E\\R\\"), "\\R\\")
self.assertEqual(msg.unescape("text\\"), "text\\")

These fail on main and pass with the fix. Full suite stays green (100 tests); existing escape-sequence cases (separators, highlight, hex, app-overrides) are unaffected.


AI disclosure: prepared with AI assistance (Claude Code), human-reviewed and verified before opening.

…aulett#84)

When a field ended in the middle of an escape sequence (e.g. a trailing
lone escape character as in '\E\R\'), the loop left the opening escape
character and any collected characters unflushed, so they were silently
dropped: unescape('\E\R\') returned '\R' instead of '\R\'.

Flush the unterminated sequence literally after the loop, since it cannot
be decoded. Closes johnpaulett#84.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Backslash is swallowed when unescaping sequence like \E\R\

1 participant