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: please document that no code preceding encoding declaration is allowed
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.6, Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, jwilk, python-dev, r.david.murray, rbcollins, terry.reedy
Priority: normal Keywords: patch

Created on 2015-06-29 15:42 by jwilk, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
encode-declare.diff terry.reedy, 2015-07-03 23:40 review
Messages (6)
msg245946 - (view) Author: Jakub Wilk (jwilk) Date: 2015-06-29 15:42
If the encoding is declared on the second line, the first line must not contain anything except comments. However, this fact is not documented.

The Python Language reference says only that "the encoding declaration must appear on a line of its own";
and PEP 263 says only that "there must not be any Python statement on the line that contains the encoding declaration".

For example, Python ignores this encoding declaration:

spam = True
# encoding=ISO-8859-2

despite the fact that it appears on a line of its own.
msg245950 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-06-29 16:25
What docs are you looking at? https://docs.python.org/3/reference/lexical_analysis.html#encoding-declarations? I suspect the author thought that "If a comment in the first or second line..." implied both lines needed to be comments, but I agree that the wording is not clear.
msg245960 - (view) Author: Jakub Wilk (jwilk) Date: 2015-06-29 19:50
Right, I meant <https://docs.python.org/3/reference/lexical_analysis.html#encoding-declarations>.
msg246234 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015-07-03 23:40
I moved the existing comment-only sentence to the first paragraph, where I strongly believe it belongs, and added a second about the first.

(PS I opened #24563 to address the xxx visible in the patch.)
msg248117 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-08-06 09:15
New changeset 08e03dfb7dab by Robert Collins in branch '2.7':
Issue #24531: Document that encoding lines cannot follow non-comment lines.
https://hg.python.org/cpython/rev/08e03dfb7dab

New changeset ab75b7ff4dfe by Robert Collins in branch '3.4':
Issue #24531: Document that encoding lines cannot follow non-comment lines.
https://hg.python.org/cpython/rev/ab75b7ff4dfe

New changeset caecc1869921 by Robert Collins in branch '3.5':
Issue #24531: Document that encoding lines cannot follow non-comment lines.
https://hg.python.org/cpython/rev/caecc1869921

New changeset 82c9ce30c0a0 by Robert Collins in branch 'default':
Issue #24531: Document that encoding lines cannot follow non-comment lines.
https://hg.python.org/cpython/rev/82c9ce30c0a0
msg248118 - (view) Author: Robert Collins (rbcollins) * (Python committer) Date: 2015-08-06 09:15
Thanks, applied across the board; I had to tweak it a little on 2.7.
History
Date User Action Args
2022-04-11 14:58:18adminsetgithub: 68719
2015-08-06 09:15:54rbcollinssetstatus: open -> closed

nosy: + rbcollins
messages: + msg248118

resolution: fixed
stage: commit review -> resolved
2015-08-06 09:15:17python-devsetnosy: + python-dev
messages: + msg248117
2015-07-03 23:40:36terry.reedysetfiles: + encode-declare.diff

nosy: + terry.reedy
messages: + msg246234

keywords: + patch
stage: needs patch -> commit review
2015-06-29 19:50:17jwilksetmessages: + msg245960
2015-06-29 16:25:12r.david.murraysetversions: + Python 2.7, Python 3.4, Python 3.5, Python 3.6
nosy: + r.david.murray

messages: + msg245950

type: behavior
stage: needs patch
2015-06-29 15:42:25jwilkcreate