Unrecognized backslash escapes in string literals

Dave Angel davea at davea.name
Sun Feb 22 21:47:27 EST 2015


On 02/22/2015 09:29 PM, Chris Angelico wrote:
> In Python, unrecognized escape sequences are treated literally,
> without (as far as I can tell) any sort of warning or anything. This
> can mask bugs, especially when Windows path names are used:
>
>>>> 'C:\sqlite\Beginner.db'
> 'C:\\sqlite\\Beginner.db'
>>>> 'c:\sqlite\beginner.db'
> 'c:\\sqlite\x08eginner.db'
>
> To a typical Windows user, the two strings should be equivalent - case
> insensitive file names, who cares whether you say "Beginner" or
> "beginner"? But to Python, one of them will happen to work, the other
> will fail badly.
>
> Why is it that Python interprets them this way, and doesn't even give
> a warning? What happened to errors not passing silently? Or, looking
> at this the other way: Is there a way to enable such warnings/errors?
> I can't see one in 'python[3] -h', but if there's some way elsewhere,
> that would be a useful thing to recommend to people (I already
> recommend running Python 2 with -tt).
>
> ChrisA
>

I've long thought they should be errors, but in Python they're not even 
warnings.  It's one thing to let a user be sloppy on a shell's 
commandline, but in a program, if you have an invalid escape sequence, 
it should be an invalid string literal, full stop.

And Python doesn't even treat these invalid sequences the same (broken) 
way C does.  The documentation explicitly says it's different than C. 
If you're going to be different, at least be strict.

-- 
DaveA



More information about the Python-list mailing list