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: Comment generates syntax error
Type: compile error Stage: resolved
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Jim Fasarakis-Hilliard, ammar2, sgrantham
Priority: normal Keywords:

Created on 2017-01-10 16:40 by sgrantham, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (6)
msg285129 - (view) Author: Simon Grantham (sgrantham) Date: 2017-01-10 16:40
Placing the word "coding:" in a hash tag comment in a file causes a syntax error.  Eg:

~ $ cat tst.py

# coding: Wow! How odd!

~ $ python tst.py
  File "tst.py", line 2
SyntaxError: encoding problem: Wow
~ $
msg285130 - (view) Author: Ammar Askar (ammar2) * (Python committer) Date: 2017-01-10 16:44
This is because the "coding:" in a comment is a special syntax used to define the encoding of the file. The only documentation I could find for it on the official docs is a brief mention here: https://docs.python.org/3/howto/unicode.html#the-string-type

More details can be found on the PEP for it:
https://www.python.org/dev/peps/pep-0263/

The SyntaxError itself is not a bug, though I think the documentation for this feature is certainly a little sparse.
msg285133 - (view) Author: Jim Fasarakis-Hilliard (Jim Fasarakis-Hilliard) * Date: 2017-01-10 17:10
If you use an encoding declaration the encoding must be recognized by Python. 

This is clearly stated in the documentation: https://docs.python.org/3/reference/lexical_analysis.html#encoding-declarations
msg285134 - (view) Author: Simon Grantham (sgrantham) Date: 2017-01-10 17:12
Thanks Ammar.  Curiously, the comment I had put in my code was a note 
regarding usage and actually didn't contain the word "coding:" but the 
word "encoding:".

# curl -v --header "Transfer-Encoding: chunked" -d @somefile.txt 
"http://localhost:80/"

Perhaps this is a slightly different kind of bug in that the interpreter 
doesn't regex /[^a-zA-Z]coding:/ .  Or a documentation bug. ;)

Simon

On 1/10/2017 11:44 AM, Ammar Askar wrote:
> Ammar Askar added the comment:
>
> This is because the "coding:" in a comment is a special syntax used to define the encoding of the file. The only documentation I could find for it on the official docs is a brief mention here: https://docs.python.org/3/howto/unicode.html#the-string-type
>
> More details can be found on the PEP for it:
> https://www.python.org/dev/peps/pep-0263/
>
> The SyntaxError itself is not a bug, though I think the documentation for this feature is certainly a little sparse.
>
> ----------
> nosy: +ammar2
> resolution:  -> not a bug
> stage:  -> resolved
> status: open -> closed
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue29226>
> _______________________________________
msg285138 - (view) Author: Ammar Askar (ammar2) * (Python committer) Date: 2017-01-10 17:58
Thanks for the link Jim, looks like my googling skills are a bit amiss.

I believe the reason the matching allows "encoding" is because a recommended format in the documentation is:

# vim:fileencoding=<encoding-name>

I think changing the behavior of the matching to be stricter and only match on "coding:" could result in breakages of such lines. Instead I think the error thrown could be more descriptive since I don't think it's too helpful.
msg285140 - (view) Author: Simon Grantham (sgrantham) Date: 2017-01-10 18:08
My googling skills are a bit amiss too.  I searched for some kind of 
encoding pragma before reporting.  Perhaps just a footnote in the basic 
documentation of a python comment is the easiest solution.

Simon

On 1/10/2017 12:58 PM, Ammar Askar wrote:
> Ammar Askar added the comment:
>
> Thanks for the link Jim, looks like my googling skills are a bit amiss.
>
> I believe the reason the matching allows "encoding" is because a recommended format in the documentation is:
>
> # vim:fileencoding=<encoding-name>
>
> I think changing the behavior of the matching to be stricter and only match on "coding:" could result in breakages of such lines. Instead I think the error thrown could be more descriptive since I don't think it's too helpful.
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue29226>
> _______________________________________
History
Date User Action Args
2022-04-11 14:58:41adminsetgithub: 73412
2017-01-10 18:08:34sgranthamsetmessages: + msg285140
2017-01-10 17:58:17ammar2setmessages: + msg285138
2017-01-10 17:12:08sgranthamsetmessages: + msg285134
title: encoding comment generates a SyntaxError -> Comment generates syntax error
2017-01-10 17:10:45Jim Fasarakis-Hilliardsetnosy: + Jim Fasarakis-Hilliard

messages: + msg285133
title: Comment generates syntax error -> encoding comment generates a SyntaxError
2017-01-10 16:44:30ammar2setstatus: open -> closed

nosy: + ammar2
messages: + msg285130

resolution: not a bug
stage: resolved
2017-01-10 16:40:42sgranthamsettype: compile error
2017-01-10 16:40:05sgranthamcreate