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 <dpursehouse@collab.net>
Tested-by: Mike Frysinger <vapier@google.com>
diff --git a/editor.py b/editor.py
index 19b96c3..fcf1638 100644
--- a/editor.py
+++ b/editor.py
@@ -68,11 +68,14 @@
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 @@
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 @@
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)