[issue13525] Tutorial: Example of Source Code Encoding triggers error
New submission from Nicolas Goutte <nicolas.goutte@extragroup.de>: Current Behaviour The tutorial of Python 3.2.x has an example to set an encoding in a source file: http://docs.python.org/py3k/tutorial/interpreter.html#source-code-encoding It explains to set the following line at the start of the source code: # -*- coding: cp-1252 -*- However when done exactly so, Python raises the following exception: SyntaxError: encoding problem: with BOM The problem seems to be that Python knows Windows codepage 1252 as windows-1252 (its IANA charset name, see http://www.iana.org/assignments/charset-reg/windows-1252 ) or alternatively as cp1252 (without dash) but not as cp-1252 (with dash). As this is an example in the tutorial is particularly problematic, as users might not understand how to do it correctly. This is still the case in the tutorial of Python 3.3 alpha: http://docs.python.org/dev/tutorial/interpreter.html#source-code-encoding Expected Behaviour The tutorial should give a correct example, for example with: # -*- coding: windows-1252 -*- Alternatively a totally other example as for Python 2.7 would be nice too: http://docs.python.org/tutorial/interpreter.html#source-code-encoding Notes: I have tested this with following Python implementations: - Python 3.2.1 (openSUSE 12.1) on Linux - Python 3.2.2 on Windows 7 SP1 64 Bits - Python 3.2.2 on MacOS 10.5.8 (Always on the command line; I have not tested in IDLE.) ---------- assignee: docs@python components: Documentation files: cp_1252broken.py messages: 148798 nosy: docs@python, nicolasg priority: normal severity: normal status: open title: Tutorial: Example of Source Code Encoding triggers error versions: Python 3.2 Added file: http://bugs.python.org/file23840/cp_1252broken.py _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13525> _______________________________________
Changes by Nicolas Goutte <nicolas.goutte@extragroup.de>: Added file: http://bugs.python.org/file23841/windows_1252ok.py _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13525> _______________________________________
Changes by Ezio Melotti <ezio.melotti@gmail.com>: ---------- nosy: +ezio.melotti stage: -> needs patch versions: +Python 2.7, Python 3.3 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13525> _______________________________________
Éric Araujo <merwok@netwok.org> added the comment: Thanks for the detailed bug report. I thought the normalization performed by the codec lookup system would convert 'cp-1252' to 'cp1252' (its “real” name, i.e. the name of the module implementing the codec), but it does not. I’m +1 to removing the hyphen in the example, then.
Python raises the following exception: SyntaxError: encoding problem: with BOM I reproduced this and it’s surprising. Maybe there is a bug with error reporting here.
Alternatively a totally other example as for Python 2.7 would be nice too This file has seen different changes in 2.7 and 3.2, given that the default encoding is different in 3.x. I’ll check the history and upload a patch here to get your feedback.
---------- assignee: docs@python -> eric.araujo nosy: +eric.araujo _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13525> _______________________________________
Changes by Ezio Melotti <ezio.melotti@gmail.com>: ---------- type: -> enhancement _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13525> _______________________________________
Serhiy Storchaka <storchaka+cpython@gmail.com> added the comment: Error message was fixed. In all supported versions it is: $ ./python cp_1252broken.py File "cp_1252broken.py", line 1 SyntaxError: encoding problem: cp-1252 But the tutorial still contains non-working example. This is an easy issue, but it was open for long time. ---------- nosy: +serhiy.storchaka _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue13525> _______________________________________
Change by Serhiy Storchaka <storchaka+cpython@gmail.com>: ---------- keywords: +patch pull_requests: +6427 stage: needs patch -> patch review _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue13525> _______________________________________
Serhiy Storchaka <storchaka+cpython@gmail.com> added the comment: New changeset ddb6215a55b0218b621d5cb755e9dfac8dab231a by Serhiy Storchaka in branch 'master': bpo-13525: Fix incorrect encoding name in the tutorial example. (GH-6738) https://github.com/python/cpython/commit/ddb6215a55b0218b621d5cb755e9dfac8da... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue13525> _______________________________________
Change by miss-islington <mariatta.wijaya+miss-islington@gmail.com>: ---------- pull_requests: +6429 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue13525> _______________________________________
Change by miss-islington <mariatta.wijaya+miss-islington@gmail.com>: ---------- pull_requests: +6430 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue13525> _______________________________________
Change by Serhiy Storchaka <storchaka+cpython@gmail.com>: ---------- pull_requests: +6431 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue13525> _______________________________________
miss-islington <mariatta.wijaya+miss-islington@gmail.com> added the comment: New changeset 8ffff34ea12ca6478d73a337ce52f33660f6f174 by Miss Islington (bot) in branch '3.7': bpo-13525: Fix incorrect encoding name in the tutorial example. (GH-6738) https://github.com/python/cpython/commit/8ffff34ea12ca6478d73a337ce52f33660f... ---------- nosy: +miss-islington _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue13525> _______________________________________
miss-islington <mariatta.wijaya+miss-islington@gmail.com> added the comment: New changeset fa40fc0593012893e447875632e9ed3df277561f by Miss Islington (bot) in branch '3.6': bpo-13525: Fix incorrect encoding name in the tutorial example. (GH-6738) https://github.com/python/cpython/commit/fa40fc0593012893e447875632e9ed3df27... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue13525> _______________________________________
Serhiy Storchaka <storchaka+cpython@gmail.com> added the comment: New changeset d7e783b17feaedbe0f5b30467cb7f43cefadf904 by Serhiy Storchaka in branch '2.7': [2.7] bpo-13525: Fix incorrect encoding name in the tutorial example. (GH-6738). (GH-6744) https://github.com/python/cpython/commit/d7e783b17feaedbe0f5b30467cb7f43cefa... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue13525> _______________________________________
Change by Serhiy Storchaka <storchaka+cpython@gmail.com>: ---------- resolution: -> fixed stage: patch review -> resolved status: open -> closed versions: +Python 3.6, Python 3.7, Python 3.8 -Python 3.2, Python 3.3 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue13525> _______________________________________
participants (5)
-
Ezio Melotti
-
miss-islington
-
Nicolas Goutte
-
Serhiy Storchaka
-
Éric Araujo