From 70c54dc2550084ed022a2f134065a011f37f30aa Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Fri, 15 Nov 2019 01:19:03 -0500 Subject: upload/editor: fix bytes/string confusion The upload module tries to turn the strings into bytes before passing to EditString, but it combines bytes & strings causing an error. The return value might be bytes or string, but the caller only expects a string. Lets simplify this by sticking to strings everywhere and have EditString take care of converting to/from bytes when reading/writing the underlying files. This also avoids possible locale confusion when reading the file by forcing UTF-8 everywhere. Bug: https://crbug.com/gerrit/11929 Change-Id: I07b146170c5e8b5b0500a2c79e4213cd12140a96 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/245621 Reviewed-by: David Pursehouse Tested-by: Mike Frysinger --- editor.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'editor.py') diff --git a/editor.py b/editor.py index 19b96c37..fcf16386 100644 --- a/editor.py +++ b/editor.py @@ -68,11 +68,14 @@ least one of these before using this command.""", file=sys.stderr) def EditString(cls, data): """Opens an editor to edit the given content. - Args: - data : the text to edit + Args: + data: The text to edit. - Returns: - new value of edited text; None if editing did not succeed + Returns: + New value of edited text. + + Raises: + EditorError: The editor failed to run. """ editor = cls._GetEditor() if editor == ':': @@ -80,7 +83,7 @@ least one of these before using this command.""", file=sys.stderr) fd, path = tempfile.mkstemp() try: - os.write(fd, data) + os.write(fd, data.encode('utf-8')) os.close(fd) fd = None @@ -106,8 +109,8 @@ least one of these before using this command.""", file=sys.stderr) raise EditorError('editor failed with exit status %d: %s %s' % (rc, editor, path)) - with open(path) as fd2: - return fd2.read() + with open(path, mode='rb') as fd2: + return fd2.read().decode('utf-8') finally: if fd: os.close(fd) -- cgit v1.2.3-54-g00ecf