[Python-Dev] Emit SyntaxWarning on unrecognized backslash escapes?

Chris Angelico rosuav at gmail.com
Tue Feb 24 08:02:03 CET 2015


On Tue, Feb 24, 2015 at 8:39 AM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Serhiy Storchaka wrote:
>>
>> The problem is that the user don't know that he should read the
>> documentation. It just find that his script works with "C:\sample.txt", but
>> doesn't work with "D:\test.txt". He has no ideas what happen.
>
>
> Even with the syntax error, there are filenames that
> will mysteriously fail, e.g. "C:\ninjamoves.txt".

Yes, there are; but they will fail, rather than randomly happening to
work. With the proposed syntax warning, there will be no cases which
silently succeed; everything will either be interpreted as an escape
sequence (as in your example), fail with a hard error ("C:\Users" in a
Unicode string, or "C:\x-do-not-track"), or produce a warning
(everything else). The most tricky escape sequence that I can envision
is \134, so you might find that an all-numeric path gets beheaded:

>>> print("C:\134689")
C:\689

The chances that someone will accidentally get something like this are
very low, and the main thing is, "\X" for any X will never be
interpreted the same way as "\\X", so the backslash will _always_ be
seen as special.

ChrisA


More information about the Python-Dev mailing list