Просмотр исходного кода

Set the file encoding in checkIncludes.py with Python3

Nick Mathewson 7 лет назад
Родитель
Сommit
b4b8fa4899
1 измененных файлов с 9 добавлено и 2 удалено
  1. 9 2
      scripts/maint/checkIncludes.py

+ 9 - 2
scripts/maint/checkIncludes.py

@@ -26,6 +26,13 @@ import sys
 # Global: Have there been any errors?
 # Global: Have there been any errors?
 trouble = False
 trouble = False
 
 
+if sys.version_info[0] <= 2:
+    def open_file(fname):
+        return open(fname, 'r')
+else:
+    def open_file(fname):
+        return open(fname, 'r', encoding='utf-8')
+
 def err(msg):
 def err(msg):
     """ Declare that an error has happened, and remember that there has
     """ Declare that an error has happened, and remember that there has
         been an error. """
         been an error. """
@@ -70,7 +77,7 @@ class Rules(object):
                         include, lineno, context))
                         include, lineno, context))
 
 
     def applyToFile(self, fname):
     def applyToFile(self, fname):
-        with open(fname, 'r') as f:
+        with open_file(fname) as f:
             #print(fname)
             #print(fname)
             self.applyToLines(iter(f), " of {}".format(fname))
             self.applyToLines(iter(f), " of {}".format(fname))
 
 
@@ -82,7 +89,7 @@ class Rules(object):
 def load_include_rules(fname):
 def load_include_rules(fname):
     """ Read a rules file from 'fname', and return it as a Rules object. """
     """ Read a rules file from 'fname', and return it as a Rules object. """
     result = Rules(os.path.split(fname)[0])
     result = Rules(os.path.split(fname)[0])
-    with open(fname, 'r') as f:
+    with open_file(fname) as f:
         for line in f:
         for line in f:
             line = line.strip()
             line = line.strip()
             if line.startswith("#") or not line:
             if line.startswith("#") or not line: