Skip to content

Commit 4a379a4

Browse files
committed
Do not raise an error for invalid octal escape sequences (#64)
1 parent e63e5fc commit 4a379a4

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 0.10.0.3
44
* Fixed a bug where notifications without a payload were not recognized as such
5+
* Invalid octal sequences produced by GDB are left unchanged instead of causing a `UnicodeDecodeError` (#64)
56

67
## 0.10.0.2
78
* Strings containing escapes are now unescaped, both for messages in error records, which were previously mangled (#57), and textual records, which were previously left escaped (#58)

pygdbmi/gdbescapes.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,13 @@ def _unescape_internal(
166166
raise ValueError(
167167
f"Invalid octal number {octal_number!r} in {escaped_str!r}"
168168
) from exc
169-
replaced = octal_sequence_bytes.decode("utf-8")
169+
try:
170+
replaced = octal_sequence_bytes.decode("utf-8")
171+
except UnicodeDecodeError:
172+
# GDB should never generate invalid sequences but, according to #64,
173+
# it can do that on Windows. In this case we just keep the sequence
174+
# unchanged.
175+
replaced = f"\\{escaped_octal}"
170176

171177
elif escaped_char is not None:
172178
# We found a single escaped character.

tests/test_pygdbmi.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,9 @@ def test_unescape(self) -> None:
502502
with self.assertRaisesRegex(ValueError, "Invalid escape character"):
503503
unescape(bad)
504504

505+
# An octal sequence that is not valid UTF-8 doesn't get changes, see #64.
506+
assert_match(unescape(r"254 '\376'"), r"254 '\376'")
507+
505508
def test_advance_past_string_with_gdb_escapes(self) -> None:
506509
"""Test the advance_past_string_with_gdb_escapes function"""
507510

0 commit comments

Comments
 (0)