[Python-Dev] Emit SyntaxWarning on unrecognized backslash escapes? (original) (raw)

Thomas Wouters thomas at python.org
Tue Feb 24 18🔞38 CET 2015


On Tue, Feb 24, 2015 at 5:37 PM, Ethan Furman <ethan at stoneleaf.us> wrote:

On 02/24/2015 02:58 AM, Chris Angelico wrote: > On Tue, Feb 24, 2015 at 7:40 PM, M.-A. Lemburg <mal at egenix.com> wrote: >> I think the easiest way would be to tweak the error message >> output to indicate the real problem. >> >> At the moment, you get: >> >>>>> open('c:\test.txt') >> Traceback (most recent call last): >> File "", line 1, in >> FileNotFoundError: [Errno 2] No such file or directory: 'c:\test.txt' >> >> which isn't helpful. > > The problem isn't the cases where the file can't be found. Those can > be dealt with fairly easily, one way or another. The problem is much, > much earlier, when the student was using "c:\sample.txt" and > everything was working fine. Then he changed the code over to use his > own file instead of the provided sample, and at the same time, > switched from using open() to using csv.reader(open()), and moved all > the code into a function, and fixed three other bugs, and now it isn't > working. And he can't figure out why. > > That's why I'd like "c:\sample.txt" to raise a warning. > > Should I start writing up a PEP? Is that the way forward with this?

Go for it. I think the solution will be two pieces: - deprecating unknown backslash-escapes, with the goal of eventually being a SyntaxError (this removes a wart from the language, as well as allowing a warning backport to 2.7) - M.A.Lemburg's idea of changing the exception message in key places to make a successful backslash replace obvious (FileNotFoundError: [Errno 2] No such file or directory: 'c:[\t]est.txt' - warning: embedded escapes)

Any ideas on how this check could be implemented? How would it distinguish \t from an actual tab in the string literal, or "C:\x64-python" from "C:\d-python"...?

-- Ethan


Python-Dev mailing list Python-Dev at python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/thomas%40python.org

-- Thomas Wouters <thomas at python.org>

Hi! I'm an email virus! Think twice before sending your email to help me spread! -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20150224/3d26a6cf/attachment.html>



More information about the Python-Dev mailing list