mirror of
https://chromium.googlesource.com/chromium/tools/depot_tools.git
synced 2026-01-11 10:41:31 +00:00
gclient_utils: Make FileRead always return a Unicode string.
Previously, it returned bytes if the file did not contain valid Unicode data, and a Unicode string otherwise. It is confusing to reason about such a function, and no current caller needs bytes data AFAICT, so make FileRead always return Unicode strings. Bug: 1009814 Change-Id: I89dd1935e5d4fcaf9af71585b85bda6c47695950 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1880013 Reviewed-by: Anthony Polito <apolito@google.com> Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
This commit is contained in:
@@ -164,19 +164,16 @@ class PrintableObject(object):
|
||||
return output
|
||||
|
||||
|
||||
def FileRead(filename, mode='rU'):
|
||||
def FileRead(filename, mode='rbU'):
|
||||
# Always decodes output to a Unicode string.
|
||||
# On Python 3 newlines are converted to '\n' by default and 'U' is deprecated.
|
||||
if mode == 'rU' and sys.version_info.major == 3:
|
||||
mode = 'r'
|
||||
if mode == 'rbU' and sys.version_info.major == 3:
|
||||
mode = 'rb'
|
||||
with open(filename, mode=mode) as f:
|
||||
# codecs.open() has different behavior than open() on python 2.6 so use
|
||||
# open() and decode manually.
|
||||
s = f.read()
|
||||
try:
|
||||
return s.decode('utf-8')
|
||||
# AttributeError is for Py3 compatibility
|
||||
except (UnicodeDecodeError, AttributeError):
|
||||
return s
|
||||
if isinstance(s, bytes):
|
||||
return s.decode('utf-8', 'replace')
|
||||
return s
|
||||
|
||||
|
||||
def FileWrite(filename, content, mode='w'):
|
||||
|
||||
Reference in New Issue
Block a user