From: Mark Dickinson Date: Tue, 29 Jun 2010 07:48:23 +0000 (+0000) Subject: unparse.py: respect coding cookie in input files X-Git-Tag: v3.2a1~393 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=82c8d93357016d81b051f50d394bce028576967f;p=python unparse.py: respect coding cookie in input files --- diff --git a/Demo/parser/unparse.py b/Demo/parser/unparse.py index 1c034bab96..56728734c7 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)