1010//// STRING HANDLING FUNCTIONS ////
1111
1212static int
13- warn_invalid_escape_sequence (Parser * p , const char * buffer , const char * first_invalid_escape , Token * t )
13+ warn_invalid_escape_sequence (Parser * p , const char * buffer , const char * first_invalid_escape ,
14+ Py_UCS4 c , Token * t )
1415{
1516 if (p -> call_invalid_rules ) {
1617 // Do not report warnings if we are in the second pass of the parser
1718 // to avoid showing the warning twice.
1819 return 0 ;
1920 }
20- unsigned char c = (unsigned char )* first_invalid_escape ;
2121 if ((t -> type == FSTRING_MIDDLE || t -> type == FSTRING_END || t -> type == TSTRING_MIDDLE || t -> type == TSTRING_END )
2222 && (c == '{' || c == '}' )) {
2323 // in this case the tokenizer has already emitted a warning,
@@ -37,7 +37,7 @@ warn_invalid_escape_sequence(Parser *p, const char* buffer, const char *first_in
3737 "\"\\%c\" is an invalid escape sequence. "
3838 "Such sequences will not work in the future. "
3939 "Did you mean \"\\\\%c\"? A raw string is also an option." ,
40- c , c );
40+ ( int ) c , ( int ) c );
4141 if (msg == NULL ) {
4242 return -1 ;
4343 }
@@ -108,7 +108,7 @@ warn_invalid_escape_sequence(Parser *p, const char* buffer, const char *first_in
108108 RAISE_ERROR_KNOWN_LOCATION (p , PyExc_SyntaxError , lineno , col_offset - 1 , lineno , col_offset + 1 ,
109109 "\"\\%c\" is an invalid escape sequence. "
110110 "Did you mean \"\\\\%c\"? A raw string is also an option." ,
111- c , c );
111+ ( int ) c , ( int ) c );
112112 }
113113 }
114114 Py_DECREF (msg );
@@ -151,10 +151,19 @@ decode_unicode_with_escapes(Parser *parser, const char *s, size_t len, Token *t)
151151 }
152152 p = buf ;
153153 end = s + len ;
154+ // A backslash before a non-ASCII character becomes "\u005c" below, which
155+ // the decoder does not report as an invalid escape, so remember it here.
156+ const char * nonascii_escape_ptr = NULL ;
157+ Py_UCS4 nonascii_escape_char = 0 ;
154158 while (s < end ) {
159+ int escaped_nonascii = 0 ;
155160 if (* s == '\\' ) {
156161 * p ++ = * s ++ ;
157162 if (s >= end || * s & 0x80 ) {
163+ if (s < end && nonascii_escape_ptr == NULL ) {
164+ nonascii_escape_ptr = p ;
165+ escaped_nonascii = 1 ;
166+ }
158167 memcpy (p , "u005c" , 5 );
159168 p += 5 ;
160169 if (s >= end ) {
@@ -176,6 +185,9 @@ decode_unicode_with_escapes(Parser *parser, const char *s, size_t len, Token *t)
176185 kind = PyUnicode_KIND (w );
177186 data = PyUnicode_DATA (w );
178187 w_len = PyUnicode_GET_LENGTH (w );
188+ if (escaped_nonascii ) {
189+ nonascii_escape_char = PyUnicode_READ (kind , data , 0 );
190+ }
179191 for (i = 0 ; i < w_len ; i ++ ) {
180192 // sprintf() writes a null byte: the buffer is large enough
181193 // for that thanks to the overallocation.
@@ -202,10 +214,21 @@ decode_unicode_with_escapes(Parser *parser, const char *s, size_t len, Token *t)
202214 & first_invalid_escape_char ,
203215 & first_invalid_escape_ptr );
204216
217+ Py_UCS4 c = 0 ;
218+ if (first_invalid_escape_ptr != NULL ) {
219+ c = (unsigned char )* first_invalid_escape_ptr ;
220+ }
221+ if (nonascii_escape_ptr != NULL &&
222+ (first_invalid_escape_ptr == NULL ||
223+ nonascii_escape_ptr < first_invalid_escape_ptr )) {
224+ first_invalid_escape_ptr = nonascii_escape_ptr ;
225+ c = nonascii_escape_char ;
226+ }
227+
205228 // HACK: later we can simply pass the line no, since we don't preserve the tokens
206229 // when we are decoding the string but we preserve the line numbers.
207230 if (v != NULL && first_invalid_escape_ptr != NULL && t != NULL && !incomplete ) {
208- if (warn_invalid_escape_sequence (parser , buf , first_invalid_escape_ptr , t ) < 0 ) {
231+ if (warn_invalid_escape_sequence (parser , buf , first_invalid_escape_ptr , c , t ) < 0 ) {
209232 /* We have not deallocated the buffer before because
210233 * first_invalid_escape_ptr points inside buf. */
211234 PyMem_Free (buf );
@@ -230,7 +253,8 @@ decode_bytes_with_escapes(Parser *p, const char *s, Py_ssize_t len, Token *t)
230253 }
231254
232255 if (first_invalid_escape_ptr != NULL ) {
233- if (warn_invalid_escape_sequence (p , s , first_invalid_escape_ptr , t ) < 0 ) {
256+ if (warn_invalid_escape_sequence (p , s , first_invalid_escape_ptr ,
257+ (unsigned char )* first_invalid_escape_ptr , t ) < 0 ) {
234258 Py_DECREF (result );
235259 return NULL ;
236260 }
0 commit comments