From 4861444124e0dba81e9bf74798cebbd01f19f744 Mon Sep 17 00:00:00 2001 From: Yura Yaroshevich Date: Fri, 25 Oct 2019 19:26:15 +0000 Subject: [PATCH] Specify open file encoding explicitly. This fixes an error on some Windows installations: ``` fetch webrtc Traceback (most recent call last): File "googlesource.com\depot_tools\bootstrap\bootstrap.py", line 365, in sys.exit(main(sys.argv[1:])) File "googlesource.com\depot_tools\bootstrap\bootstrap.py", line 324, in main git_postprocess(template, os.path.join(bootstrap_dir, 'git')) File "googlesource.com\depot_tools\bootstrap\bootstrap.py", line 264, in git_postprocess maybe_copy( File "googlesource.com\depot_tools\bootstrap\bootstrap.py", line 108, in maybe_copy content = fd.read() File "googlesource.com\depot_tools\bootstrap-3_8_0b1_chromium_1_bin\python3\bin\lib\encodings\cp1251.py", line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x98 in position 18734: character maps to ``` Bug: None Change-Id: I43cf7b51879ac9a66c33566536dcdcb4c93e0fc0 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1881227 Reviewed-by: Edward Lesmes Commit-Queue: Edward Lesmes --- bootstrap/bootstrap.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bootstrap/bootstrap.py b/bootstrap/bootstrap.py index 533bc8f1b..3245df9da 100644 --- a/bootstrap/bootstrap.py +++ b/bootstrap/bootstrap.py @@ -60,7 +60,7 @@ class Template(collections.namedtuple('Template', ( Returns (bool): True if |dst_path| was updated, False otherwise. """ template_path = os.path.join(THIS_DIR, name) - with open(template_path, 'r') as fd: + with open(template_path, 'r', encoding='utf8') as fd: t = string.Template(fd.read()) return maybe_update(t.safe_substitute(self._asdict()), dst_path) @@ -82,12 +82,12 @@ def maybe_update(content, dst_path): # If the path already exists and matches the new content, refrain from writing # a new one. if os.path.exists(dst_path): - with open(dst_path, 'r') as fd: + with open(dst_path, 'r', encoding='utf-8') as fd: if fd.read() == content: return False logging.debug('Updating %r', dst_path) - with open(dst_path, 'w') as fd: + with open(dst_path, 'w', encoding='utf-8') as fd: fd.write(content) os.chmod(dst_path, 0o755) return True @@ -104,7 +104,7 @@ def maybe_copy(src_path, dst_path): Returns (bool): True if |dst_path| was updated, False otherwise. """ - with open(src_path, 'r') as fd: + with open(src_path, 'r', encoding='utf-8') as fd: content = fd.read() return maybe_update(content, dst_path) @@ -130,14 +130,14 @@ def call_if_outdated(stamp_path, stamp_version, fn): stamp_version = stamp_version.strip() if os.path.isfile(stamp_path): - with open(stamp_path, 'r') as fd: + with open(stamp_path, 'r', encoding='utf-8') as fd: current_version = fd.read().strip() if current_version == stamp_version: return False fn() - with open(stamp_path, 'w') as fd: + with open(stamp_path, 'w', encoding='utf-8') as fd: fd.write(stamp_version) return True