summaryrefslogtreecommitdiffstats
path: root/editor.py
diff options
context:
space:
mode:
authorMike Frysinger <vapier@google.com>2019-11-15 01:19:03 -0500
committerMike Frysinger <vapier@google.com>2019-11-16 23:55:30 +0000
commit70c54dc2550084ed022a2f134065a011f37f30aa (patch)
tree6086cb67a1b1f928892b4d47d1b77a069b5ae31f /editor.py
parent6da17751ca4e3b90834ca763f448ddc39b32651b (diff)
downloadgit-repo-70c54dc2550084ed022a2f134065a011f37f30aa.tar.gz
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>
Diffstat (limited to 'editor.py')
-rw-r--r--editor.py17
1 files changed, 10 insertions, 7 deletions
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)
68 def EditString(cls, data): 68 def EditString(cls, data):
69 """Opens an editor to edit the given content. 69 """Opens an editor to edit the given content.
70 70
71 Args: 71 Args:
72 data : the text to edit 72 data: The text to edit.
73 73
74 Returns: 74 Returns:
75 new value of edited text; None if editing did not succeed 75 New value of edited text.
76
77 Raises:
78 EditorError: The editor failed to run.
76 """ 79 """
77 editor = cls._GetEditor() 80 editor = cls._GetEditor()
78 if editor == ':': 81 if editor == ':':
@@ -80,7 +83,7 @@ least one of these before using this command.""", file=sys.stderr)
80 83
81 fd, path = tempfile.mkstemp() 84 fd, path = tempfile.mkstemp()
82 try: 85 try:
83 os.write(fd, data) 86 os.write(fd, data.encode('utf-8'))
84 os.close(fd) 87 os.close(fd)
85 fd = None 88 fd = None
86 89
@@ -106,8 +109,8 @@ least one of these before using this command.""", file=sys.stderr)
106 raise EditorError('editor failed with exit status %d: %s %s' 109 raise EditorError('editor failed with exit status %d: %s %s'
107 % (rc, editor, path)) 110 % (rc, editor, path))
108 111
109 with open(path) as fd2: 112 with open(path, mode='rb') as fd2:
110 return fd2.read() 113 return fd2.read().decode('utf-8')
111 finally: 114 finally:
112 if fd: 115 if fd:
113 os.close(fd) 116 os.close(fd)