diff options
author | Chun-wei Fan <fanchunwei@src.gnome.org> | 2017-05-09 18:17:29 -0700 |
---|---|---|
committer | Chun-wei Fan <fanchunwei@src.gnome.org> | 2017-05-09 18:32:57 -0700 |
commit | e106b75b25c332cc55f7fad83a7e7ff124309b18 (patch) | |
tree | 948bd9bc73f45119c8184b5f4942c985bc8d256a /win32 | |
parent | c56660441e778a31e80271095deb4c89a74afd76 (diff) | |
download | atk-e106b75b25c332cc55f7fad83a7e7ff124309b18.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
-Opening the file with encoding='utf-8' on Python 3.x
Diffstat (limited to 'win32')
-rw-r--r-- | win32/replace.py | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/win32/replace.py b/win32/replace.py index a81bab9..f215cce 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()) |