From: Alexander Belopolsky <alexander.belopolsky@gmail.com>
Date: Tue, 30 Nov 2010 17:30:43 +0000 (+0000)
Subject: Issue #9598: untabify.py will now respect encoding cookie in the files it processes
X-Git-Tag: v3.2b1~158
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=73f382de1cf6e7a0c31ac8fb7b50126b780d4e21;p=python

Issue #9598: untabify.py will now respect encoding cookie in the files it processes
---

diff --git a/Tools/scripts/untabify.py b/Tools/scripts/untabify.py
index ef63c41b57..4b67c15154 100755
--- a/Tools/scripts/untabify.py
+++ b/Tools/scripts/untabify.py
@@ -5,7 +5,7 @@
 import os
 import sys
 import getopt
-
+import tokenize
 
 def main():
     tabsize = 8
@@ -27,8 +27,9 @@ def main():
 
 def process(filename, tabsize, verbose=True):
     try:
-        with open(filename) as f:
+        with tokenize.open(filename) as f:
             text = f.read()
+            encoding = f.encoding
     except IOError as msg:
         print("%r: I/O error: %s" % (filename, msg))
         return
@@ -44,7 +45,7 @@ def process(filename, tabsize, verbose=True):
         os.rename(filename, backup)
     except os.error:
         pass
-    with open(filename, "w") as f:
+    with open(filename, "w", encoding=encoding) as f:
         f.write(newtext)
     if verbose:
         print(filename)