Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Determine the encoding of the source file before attempting to parse …
…the file.

Fixes ambv#2
  • Loading branch information
movermeyer committed Jun 9, 2017
1 parent 3fb4655 commit e55c73a
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions retype.py
Expand Up @@ -12,6 +12,7 @@
import re
import sys
import threading
import tokenize
import traceback

import click
Expand Down Expand Up @@ -138,7 +139,8 @@ def retype_file(src, pyi_dir, targets, *, quiet=False, hg=False):
Type comments in sources are normalized to type annotations.
"""
with open(src) as src_file:
src_encoding = tokenize.open(src).encoding
with open(src, encoding=src_encoding) as src_file:
src_txt = src_file.read()
src_node = lib2to3_parse(src_txt)
try:
Expand All @@ -156,7 +158,7 @@ def retype_file(src, pyi_dir, targets, *, quiet=False, hg=False):
reapply_all(pyi_ast.body, src_node)
fix_remaining_type_comments(src_node)
targets.mkdir(parents=True, exist_ok=True)
with open(targets / src.name, 'w') as target_file:
with open(targets / src.name, 'w', encoding=src_encoding) as target_file:
target_file.write(lib2to3_unparse(src_node, hg=hg))
return targets / src.name

Expand Down

0 comments on commit e55c73a

Please sign in to comment.