summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChun-wei Fan <fanchunwei@src.gnome.org>2017-05-09 18:17:29 -0700
committerChun-wei Fan <fanchunwei@src.gnome.org>2017-05-09 18:20:42 -0700
commit58ecc57ca79efa7486aab9a7d183b4a5f3dd5dbd (patch)
tree67b52b5d1a18046377ed52c1e6a344fe9c7a8c12
parent0d81bb4e318b97780c70a55605cacf7e5b3fcaf7 (diff)
downloadglib-58ecc57ca79efa7486aab9a7d183b4a5f3dd5dbd.tar.gz
win32/replace.py: Fix replacing items in files with UTF-8 content
Some files that this script will process might have UTF-8 items in there, which can cause problems on Python 3.x as it is more strict and careful on unicode issues. Fix this by: -Doing what we did before on Python 2.x -Open the file with encoding='utf-8' on Python 3.x
-rw-r--r--win32/replace.py10
1 files changed, 8 insertions, 2 deletions
diff --git a/win32/replace.py b/win32/replace.py
index 0cb269a87..3aeceb1f7 100644
--- a/win32/replace.py
+++ b/win32/replace.py
@@ -21,9 +21,15 @@ valid_actions = ['remove-prefix',
'replace-str',
'remove-str']
+def open_file(filename, mode):
+ if sys.version_info[0] < 3:
+ return open(filename, mode=mode)
+ else:
+ return open(filename, mode=mode, encoding='utf-8')
+
def replace_multi(src, dest, replace_items):
- with open(src, 'r') as s:
- with open(dest, 'w') as d:
+ with open_file(src, 'r') as s:
+ with open_file(dest, 'w') as d:
for line in s:
replace_dict = dict((re.escape(key), value) \
for key, value in replace_items.items())