summaryrefslogtreecommitdiffstats
path: root/Demo/parser
diff options
context:
space:
mode:
authorMark Dickinson <dickinsm@gmail.com>2010-06-29 07:48:23 (GMT)
committerMark Dickinson <dickinsm@gmail.com>2010-06-29 07:48:23 (GMT)
commit82c8d93357016d81b051f50d394bce028576967f (patch)
treebb7f8caf622999f0534c1d72b2abc60afb3125ac /Demo/parser
parent3c0b317e36c799da5fdf6e7be69a8924dcccb956 (diff)
downloadcpython-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.py6
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)