mirror of
https://github.com/python/cpython.git
synced 2025-08-01 07:33:08 +00:00
unparse.py: respect coding cookie in input files
This commit is contained in:
parent
3c0b317e36
commit
82c8d93357
1 changed files with 5 additions and 1 deletions
|
@ -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)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue