Skip to content

Commit 7c2a103

Browse files
committed
Fix string with non-scalar unicode code points not raising an error
1 parent c00ec35 commit 7c2a103

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

CHANGELOG.md

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

77
- Fixed table header missing when replacing a super table's sub table with a single item.
88
- Fixed comments being displayed in inline tables.
9+
- Fixed string with non-scalar unicode code points not raising an error.
910

1011

1112
## [0.5.1] - 2018-11-08

tomlkit/exceptions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ def __init__(self, line, col): # type: (int, int) -> None
101101
super(InvalidNumberOrDateError, self).__init__(line, col, message=message)
102102

103103

104+
class InvalidUnicodeValueError(ParseError):
105+
"""
106+
A unicode code was improperly specified.
107+
"""
108+
109+
def __init__(self, line, col): # type: (int, int) -> None
110+
message = "Invalid unicode value"
111+
112+
super(InvalidUnicodeValueError, self).__init__(line, col, message=message)
113+
114+
104115
class UnexpectedCharError(ParseError):
105116
"""
106117
An unexpected character was found during parsing.

tomlkit/parser.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from .exceptions import InvalidDateError
2626
from .exceptions import InvalidTimeError
2727
from .exceptions import InvalidNumberError
28+
from .exceptions import InvalidUnicodeValueError
2829
from .exceptions import MixedArrayTypesError
2930
from .exceptions import ParseError
3031
from .exceptions import UnexpectedCharError
@@ -766,6 +767,8 @@ def _parse_escaped_char(self, multiline):
766767

767768
return u
768769

770+
raise self.parse_error(InvalidUnicodeValueError)
771+
769772
raise self.parse_error(InvalidCharInStringError, self._current)
770773

771774
def _parse_string(self, delim): # type: (StringType) -> String
@@ -1075,7 +1078,9 @@ def _peek(self, n): # type: (int) -> str
10751078
break
10761079
return buf
10771080

1078-
def _peek_unicode(self, is_long): # type: (bool) -> Tuple[bool, str]
1081+
def _peek_unicode(
1082+
self, is_long
1083+
): # type: (bool) -> Tuple[Optional[str], Optional[str]]
10791084
"""
10801085
Peeks ahead non-intrusively by cloning then restoring the
10811086
initial state of the parser.
@@ -1089,7 +1094,6 @@ def _peek_unicode(self, is_long): # type: (bool) -> Tuple[bool, str]
10891094
InternalParserError, "_peek_unicode() entered on non-unicode value"
10901095
)
10911096

1092-
# AoT
10931097
self.inc() # Dropping prefix
10941098
self.mark()
10951099

@@ -1103,6 +1107,9 @@ def _peek_unicode(self, is_long): # type: (bool) -> Tuple[bool, str]
11031107
else:
11041108
extracted = self.extract()
11051109

1110+
if extracted[0].lower() == "d" and extracted[1].strip("01234567"):
1111+
return None, None
1112+
11061113
try:
11071114
value = chr(int(extracted, 16))
11081115
except ValueError:

0 commit comments

Comments
 (0)