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: Trailing backslash in raw string format causes EOL
Type: behavior Stage: resolved
Components: Versions: Python 3.2
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ammar2, cfgbd
Priority: normal Keywords:

Created on 2016-09-03 02:47 by cfgbd, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg274282 - (view) Author: cfgbd (cfgbd) Date: 2016-09-03 02:47
In python shell, I typed words and got result as follows. It seems that the use of 'r' before a literal string may cause an error that the an odd '\' at the end of literal string blocked the quote.

>>> rb'abc\\\'
SyntaxError: EOL while scanning string literal
>>> rb'abc\\'
b'abc\\\\'
>>> br'abc\\\ '
b'abc\\\\\\ '
>>> br'abc\\\'
SyntaxError: EOL while scanning string literal
>>> r'\\'
'\\\\'
>>> r'\\\'
SyntaxError: EOL while scanning string literal
msg274283 - (view) Author: Ammar Askar (ammar2) * (Python committer) Date: 2016-09-03 03:03
This isn't a bug, in fact this very behavior is documented within the string docs, please read the last paragraph here:

https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
msg274284 - (view) Author: cfgbd (cfgbd) Date: 2016-09-03 03:12
Thanks for comment. Here I got my answer from string docs.

Even in a raw literal, quotes can be escaped with a backslash, but the backslash remains in the result; for example, r"\"" is a valid string literal consisting of two characters: a backslash and a double quote; r"\" is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw literal cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the literal, not as a line continuation.
History
Date User Action Args
2022-04-11 14:58:35adminsetgithub: 72134
2016-09-03 03:12:11cfgbdsetmessages: + msg274284
2016-09-03 03:05:31tim.peterssetstatus: open -> closed
stage: resolved
resolution: not a bug
versions: + Python 3.2, - Python 3.4
2016-09-03 03:03:12ammar2setnosy: + ammar2

messages: + msg274283
title: Bug of python interpreter -> Trailing backslash in raw string format causes EOL
2016-09-03 02:47:54cfgbdcreate