Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
python: Read python source as bytes
Syntastic unsets LC_ALL when running commands, this leads to
`compile.py` dying horribly when encountering non-ascii characters in
source code running under Python 3.

Consuming the input source as bytes and passing this directly to
`compile` solves this:
  • Loading branch information
Martin Øinæs Myrseth committed Mar 23, 2015
1 parent ac96ae4 commit f0f4c38
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion syntax_checkers/python/compile.py
Expand Up @@ -8,6 +8,6 @@
exit(1)

try:
compile(open(argv[1]).read(), argv[1], 'exec', 0, 1)
compile(open(argv[1], 'rb').read(), argv[1], 'exec', 0, 1)
except SyntaxError as err:
print('%s:%s:%s: %s' % (err.filename, err.lineno, err.offset, err.msg))

1 comment on commit f0f4c38

@myme
Copy link

@myme myme commented on f0f4c38 Mar 23, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to me having some LC_* settings set to non-default locales, unsetting LC_ALL (which Syntastic does) causes Python 3 to barf up a decoding error when trying to compile a source file containing a unicode literal.

Contents of reprod.py:

    a = 'ราชอาณาจักรไทย'
$ LC_ALL="" TERM=dumb python2 compile.py reprod.py
reprod.py:1:4: unexpected indent
$ LC_ALL="" TERM=dumb python3 compile.py reprod.py
Traceback (most recent call last):
  File "compile.py", line 11, in <module>
    compile(open(argv[1]).read(), argv[1], 'exec', 0, 1)
  File "/usr/lib/python3.4/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 9:
)

After reading source as bytes:

$ LC_ALL="" TERM=dumb python2 compile.py reprod.py
reprod.py:1:4: unexpected indent
$ LC_ALL="" TERM=dumb python3 compile.py reprod.py
reprod.py:1:4: unexpected indent

Please sign in to comment.