This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Multiline string with quotes is not parsed correctly.
Type: behavior Stage:
Components: Interpreter Core Versions: Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Stavros, georg.brandl
Priority: normal Keywords:

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 "<ipython console>", 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:36adminsetgithub: 47548
2008-07-06 13:53:29Stavrossetmessages: + msg69330
2008-07-06 13:38:15georg.brandlsetmessages: + msg69329
2008-07-06 13:35:19Stavrossetmessages: + msg69328
2008-07-06 13:24:54georg.brandlsetstatus: open -> closed
resolution: not a bug
messages: + msg69327
nosy: + georg.brandl
2008-07-06 13:09:47Stavroscreate