[Python-ideas] Make undefined escape sequences have SyntaxWarnings

Steven D'Aprano steve at pearwood.info
Thu Oct 11 02:08:13 CEST 2012


On 11/10/12 07:08, Mike Graham wrote:
> On Wed, Oct 10, 2012 at 3:46 PM, Antoine Pitrou<solipsis at pitrou.net>  wrote:
>> On Wed, 10 Oct 2012 15:36:08 -0400
>> Mike Graham<mikegraham at gmail.com>  wrote:

>>> The literal"\c" should be an error

Who says so? My bash shell disagrees with you:

[steve at ando ~]$ touch spam
[steve at ando ~]$ ls s\pa\m
spam

and so do I.

There are three obvious behaviours for extraneous escapes:

1) backslash-c resolves to just c (what bash and VisualStudio do)
2) backslash-c resolves to backslash-c (what Python does)
3) raise an exception or compile-time error (what Java does)

It is undefined behaviour in C.

It is a matter of opinion that Java got it right and the others got it
wrong, one which I do not share.



>>> but in practice means "\\c". It's
>>> probably too late to make this invalid syntax as it out to be, but I
>>> wonder if a warning isn't in order, especially with the theoretical
>>> potential of adding new string escapes in the future.
>>
>> -1. This will make life more difficult with regular expressions (and
>> produce lots of spurious warnings in existing code).

I agree with Antoine here.

If and when there is a serious, concrete proposal to add a new string
escape, and not just a "theoretical potential", then we should consider
adding warnings.



> Regular expressions are difficult if you're remembering which escape
> sequences exist and are easy if you're using raw string literals.

Just because some people find it hard to remember doesn't mean that it
should be an error *not* to use raw strings.


-- 
Steven



More information about the Python-ideas mailing list