From 82c8d93357016d81b051f50d394bce028576967f Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Tue, 29 Jun 2010 07:48:23 +0000 Subject: unparse.py: respect coding cookie in input files --- Demo/parser/unparse.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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 " 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) -- cgit v0.12