diff options
author | Mark Dickinson <dickinsm@gmail.com> | 2010-06-29 07:48:23 (GMT) |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2010-06-29 07:48:23 (GMT) |
commit | 82c8d93357016d81b051f50d394bce028576967f (patch) | |
tree | bb7f8caf622999f0534c1d72b2abc60afb3125ac /Demo/parser | |
parent | 3c0b317e36c799da5fdf6e7be69a8924dcccb956 (diff) | |
download | cpython-82c8d93357016d81b051f50d394bce028576967f.zip cpython-82c8d93357016d81b051f50d394bce028576967f.tar.gz cpython-82c8d93357016d81b051f50d394bce028576967f.tar.bz2 |
unparse.py: respect coding cookie in input files
Diffstat (limited to 'Demo/parser')
-rw-r--r-- | Demo/parser/unparse.py | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/Demo/parser/unparse.py b/Demo/parser/unparse.py index 1c034ba..5672873 100644 --- a/Demo/parser/unparse.py +++ b/Demo/parser/unparse.py @@ -1,6 +1,7 @@ "Usage: unparse.py <path to source file>" import sys import ast +import tokenize import io import os @@ -548,7 +549,10 @@ class Unparser: self.write(" as "+t.asname) def roundtrip(filename, output=sys.stdout): - source = open(filename).read() + with open(filename, "rb") as pyfile: + encoding = tokenize.detect_encoding(pyfile.readline)[0] + with open(filename, "r", encoding=encoding) as pyfile: + source = pyfile.read() tree = compile(source, filename, "exec", ast.PyCF_ONLY_AST) Unparser(tree, output) |