@@ -255,6 +255,12 @@ def _format_time(value: time) -> str:
255255 return s
256256
257257
258+ # A carriage return that is not the CR of a CRLF line ending. Inside a multiline
259+ # string only a raw line feed or a CRLF pair is a valid line ending; a lone CR is
260+ # a control character that a basic string must escape.
261+ _BARE_CR = re .compile (r"\r(?!\n)" )
262+
263+
258264class StringType (Enum ):
259265 # Single Line Basic
260266 SLB = '"'
@@ -290,12 +296,13 @@ def escaped_sequences(self) -> Collection[str]:
290296 def invalid_sequences (self ) -> Collection [str ]:
291297 # https://toml.io/en/v1.0.0#string
292298 forbidden_in_literal = CONTROL_CHARS - {"\t " }
293- allowed_in_multiline = {"\n " , "\r " }
299+ # CR stays forbidden in multiline: the value is checked with CRLF
300+ # pairs collapsed, so any remaining CR is a lone control character
294301 return {
295302 StringType .SLB : (),
296303 StringType .MLB : (),
297304 StringType .SLL : forbidden_in_literal | {"'" },
298- StringType .MLL : (forbidden_in_literal | {"'''" }) - allowed_in_multiline ,
305+ StringType .MLL : (forbidden_in_literal | {"'''" }) - { " \n " } ,
299306 }[self ]
300307
301308 @property
@@ -2293,12 +2300,17 @@ def from_raw(
22932300 value = decode (value )
22942301
22952302 invalid = type_ .invalid_sequences
2296- if any (c in value for c in invalid ):
2303+ # collapse CRLF line endings so only a lone CR trips the CR check
2304+ checked = value .replace ("\r \n " , "\n " )
2305+ if any (c in checked for c in invalid ):
22972306 raise InvalidStringError (value , invalid , type_ .value )
22982307
22992308 escaped = type_ .escaped_sequences
23002309 string_value = escape_string (value , escaped ) if escape and escaped else value
23012310
2311+ if type_ is StringType .MLB and escape :
2312+ string_value = _BARE_CR .sub (r"\\r" , string_value )
2313+
23022314 if type_ .is_multiline () and string_value [:1 ] in ("\n " , "\r " ):
23032315 # A newline immediately following the opening delimiter of a
23042316 # multiline string is trimmed by the parser, so a value that
0 commit comments