]> granicus.if.org Git - python/commitdiff
Issue #9598: untabify.py will now respect encoding cookie in the files it processes
authorAlexander Belopolsky <alexander.belopolsky@gmail.com>
Tue, 30 Nov 2010 17:30:43 +0000 (17:30 +0000)
committerAlexander Belopolsky <alexander.belopolsky@gmail.com>
Tue, 30 Nov 2010 17:30:43 +0000 (17:30 +0000)
Tools/scripts/untabify.py

index ef63c41b57bc28d559da9839f42271dc7e7784bb..4b67c15154391bcca4e3bb2ec36e7028c1c8cf66 100755 (executable)
@@ -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)