Kaydet (Commit) 82c8d933 authored tarafından Mark Dickinson's avatar Mark Dickinson

unparse.py: respect coding cookie in input files

üst 3c0b317e
"Usage: unparse.py <path to source file>" "Usage: unparse.py <path to source file>"
import sys import sys
import ast import ast
import tokenize
import io import io
import os import os
...@@ -548,7 +549,10 @@ class Unparser: ...@@ -548,7 +549,10 @@ class Unparser:
self.write(" as "+t.asname) self.write(" as "+t.asname)
def roundtrip(filename, output=sys.stdout): 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) tree = compile(source, filename, "exec", ast.PyCF_ONLY_AST)
Unparser(tree, output) Unparser(tree, output)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment