Issue 3298: Multiline string with quotes is not parsed correctly. (original) (raw)

Created on 2008-07-06 13:09 by Stavros, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg69326 - (view) Author: Stavros Korokithakis (Stavros) Date: 2008-07-06 13:09
A multiline string with quotes next to the ending quote is not parsed correctly: In [1]: """A "test"""" ------------------------------------------------------------ File "", line 1 """A "test"""" ^ <type 'exceptions.SyntaxError'>: EOL while scanning single-quoted string Is this normal behaviour? It seems to me like the parser should parse everything inside the triple double quotes (there is no ambiguity).
msg69327 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-07-06 13:24
There is an ambiguity: did you mean """ " or " """ (spaces added for clarity). Python does not count quotes, parentheses or similar in strings, so the first occurrence of """ will close the literal. You can either use triple single quotes, or escape the first quote, like that: """A "test\"""".
msg69328 - (view) Author: Stavros Korokithakis (Stavros) Date: 2008-07-06 13:35
Hmm, what would be the meaning of """ " in this particular context? I can't think of a situation where """A test""" " would be valid code.
msg69329 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-07-06 13:38
The last quote starts another string literal, so this is valid Python: """A "test"""" B" and results in a string 'A "test B'. The lexer cannot know whether the second string literal is terminated or not, yielding valid or invalid code.
msg69330 - (view) Author: Stavros Korokithakis (Stavros) Date: 2008-07-06 13:53
Ah, interesting, I had forgotten that adjacent strings are merged. Thanks.
History
Date User Action Args
2022-04-11 14:56:36 admin set github: 47548
2008-07-06 13:53:29 Stavros set messages: +
2008-07-06 13:38:15 georg.brandl set messages: +
2008-07-06 13:35:19 Stavros set messages: +
2008-07-06 13:24:54 georg.brandl set status: open -> closedresolution: not a bugmessages: + nosy: + georg.brandl
2008-07-06 13:09:47 Stavros create