From dae209fb46fb2910072f664a784434351581697e Mon Sep 17 00:00:00 2001 From: "maruel@chromium.org" Date: Tue, 3 Jul 2012 16:08:15 +0000 Subject: [PATCH] Enforce utf-8 codec on all files read and write. Otherwise, the files are opened in 'whatever happens to be the current encoding' which is highly system-dependent. Even if some files are not encoded with utf-8, the status quo is even worse. So it's worth trying out. TBR=cmp@chromium.org BUG= TEST= Review URL: https://chromiumcodereview.appspot.com/10697036 git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@145306 0039d316-1c4b-4281-b951-d872f2087c98 --- gclient_utils.py | 15 ++++----------- tests/gclient_utils_test.py | 6 +++--- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/gclient_utils.py b/gclient_utils.py index 35f8b3bf4..9ba6da50c 100644 --- a/gclient_utils.py +++ b/gclient_utils.py @@ -4,6 +4,7 @@ """Generic utils.""" +import codecs import errno import logging import os @@ -76,21 +77,13 @@ class PrintableObject(object): def FileRead(filename, mode='rU'): - content = None - f = open(filename, mode) - try: - content = f.read() - finally: - f.close() - return content + with codecs.open(filename, mode=mode, encoding='utf-8') as f: + return f.read() def FileWrite(filename, content, mode='w'): - f = open(filename, mode) - try: + with codecs.open(filename, mode=mode, encoding='utf-8') as f: f.write(content) - finally: - f.close() def rmtree(path): diff --git a/tests/gclient_utils_test.py b/tests/gclient_utils_test.py index 9f01c5d1e..d5c0c4ba3 100755 --- a/tests/gclient_utils_test.py +++ b/tests/gclient_utils_test.py @@ -37,9 +37,9 @@ class GclientUtilsUnittest(GclientUtilBase): 'PrintableObject', 'RemoveDirectory', 'RunEditor', 'SplitUrlRevision', 'SyntaxErrorToError', 'UpgradeToHttps', 'Wrapper', 'WorkItem', - 'errno', 'lockedmethod', 'logging', 'os', 'Queue', 're', 'rmtree', - 'safe_makedirs', 'stat', 'subprocess2', 'sys', 'tempfile', 'threading', - 'time', 'urlparse', + 'codecs', 'errno', 'lockedmethod', 'logging', 'os', 'Queue', 're', + 'rmtree', 'safe_makedirs', 'stat', 'subprocess2', 'sys', 'tempfile', + 'threading', 'time', 'urlparse', ] # If this test fails, you should add the relevant test. self.compareMembers(gclient_utils, members)