3

If I try to use a magic comment like #coding=utf-8 on top of a file, here's what happens:

Traceback (most recent call last):
  File <string>, line 0
SyntaxError: encoding declaration in Unicode string

I really haven't done anything wrong. Here is the code:

#coding=utf-8

string = raw_input()
chars = {}
for i in string:
    if i in chars:
        chars[i] += 1
    else:
        chars[i] = 0
print chars

I use repl.it.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
EKons
  • 887
  • 2
  • 20
  • 27
  • it's `encoding`, not `coding`. – DeepSpace Aug 25 '16 at 10:06
  • @DeepSpace See [here](https://www.python.org/dev/peps/pep-0263). Of course it can be `coding`! – EKons Aug 25 '16 at 10:07
  • 1
    @DeepSpace: no it isn't. It could be `decoding`, or `flubcoding`, or just `coding`. The regex used doesn't care. See [What's the difference between 'coding=utf8' and '-\*- coding: utf-8 -\*-'?](https://stackoverflow.com/a/20302074) – Martijn Pieters Aug 25 '16 at 10:08
  • @MartijnPieters Thanks, good to know! – DeepSpace Aug 25 '16 at 10:09
  • Are you using exec to run this by any chance? The error message would not make sense otherwise. – Martijn Pieters Aug 25 '16 at 10:10
  • @MartijnPieters Is `exec` the problem? I use repl.it Python 2, maybe it runs the code with `exec`. If you think you have got a solution, please answer. – EKons Aug 25 '16 at 10:11
  • @ΈρικΚωνσταντόπουλος: yes, I expect `repl.it` to be using exec. Environments like that should *always* be mentioned when you encounter weird issues, it may be that they are unique to that environment (as they are here). – Martijn Pieters Aug 25 '16 at 10:13
  • @MartijnPieters I only know from experience. I tried to run the statement `print open(__import__('sys').argv[0]).read()` to read the interpreter's code. – EKons Aug 25 '16 at 10:15

1 Answers1

13

You omitted something from your question: You are using exec to execute this code. And you passed a Unicode object to exec, which means you already have stated that the source is Unicode text:

>>> code = '''\
... # coding=utf8
... print 'hello world!'
... '''
>>> exec code
hello world!
>>> exec code.decode('utf8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 0
SyntaxError: encoding declaration in Unicode string

You can't use a PEP 263 declaration in Unicode text passed to exec.

If you are using a 'custom' environment like repl.it, then yes, such environments invariably use tricks like exec to execute code, and they load the source code as Unicode from your browser. See the actual code used, which passes JSON-sourced strings to exec (where such strings are always going to be unicode strings).

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343