Commit b1793a531e5934ea5453b7e24495e2fcddd9c493

Authored by Simon Glass
1 parent ef8b7e045e

patman: Update cros_subprocess to use bytes

At present this function uses lists and strings. This does not work so
well with Python 3, and testing against '' does not work for a bytearray.
Update the code to fix these issues.

Signed-off-by: Simon Glass <sjg@chromium.org>

Showing 2 changed files with 29 additions and 20 deletions Side-by-side Diff

tools/patman/cros_subprocess.py
... ... @@ -100,6 +100,19 @@
100 100 if kwargs:
101 101 raise ValueError("Unit tests do not test extra args - please add tests")
102 102  
  103 + def ConvertData(self, data):
  104 + """Convert stdout/stderr data to the correct format for output
  105 +
  106 + Args:
  107 + data: Data to convert, or None for ''
  108 +
  109 + Returns:
  110 + Converted data, as bytes
  111 + """
  112 + if data is None:
  113 + return b''
  114 + return data
  115 +
103 116 def CommunicateFilter(self, output):
104 117 """Interact with process: Read data from stdout and stderr.
105 118  
106 119  
... ... @@ -156,11 +169,11 @@
156 169 self.stdin.close()
157 170 if self.stdout:
158 171 read_set.append(self.stdout)
159   - stdout = []
  172 + stdout = b''
160 173 if self.stderr and self.stderr != self.stdout:
161 174 read_set.append(self.stderr)
162   - stderr = []
163   - combined = []
  175 + stderr = b''
  176 + combined = b''
164 177  
165 178 input_offset = 0
166 179 while read_set or write_set:
167 180  
168 181  
169 182  
170 183  
171 184  
172 185  
... ... @@ -186,46 +199,40 @@
186 199 write_set.remove(self.stdin)
187 200  
188 201 if self.stdout in rlist:
189   - data = ""
  202 + data = b''
190 203 # We will get an error on read if the pty is closed
191 204 try:
192 205 data = os.read(self.stdout.fileno(), 1024)
193 206 except OSError:
194 207 pass
195   - if data == "":
  208 + if not len(data):
196 209 self.stdout.close()
197 210 read_set.remove(self.stdout)
198 211 else:
199   - stdout.append(data)
200   - combined.append(data)
  212 + stdout += data
  213 + combined += data
201 214 if output:
202 215 output(sys.stdout, data)
203 216 if self.stderr in rlist:
204   - data = ""
  217 + data = b''
205 218 # We will get an error on read if the pty is closed
206 219 try:
207 220 data = os.read(self.stderr.fileno(), 1024)
208 221 except OSError:
209 222 pass
210   - if data == "":
  223 + if not len(data):
211 224 self.stderr.close()
212 225 read_set.remove(self.stderr)
213 226 else:
214   - stderr.append(data)
215   - combined.append(data)
  227 + stderr += data
  228 + combined += data
216 229 if output:
217 230 output(sys.stderr, data)
218 231  
219 232 # All data exchanged. Translate lists into strings.
220   - if stdout is not None:
221   - stdout = ''.join(stdout)
222   - else:
223   - stdout = ''
224   - if stderr is not None:
225   - stderr = ''.join(stderr)
226   - else:
227   - stderr = ''
228   - combined = ''.join(combined)
  233 + stdout = self.ConvertData(stdout)
  234 + stderr = self.ConvertData(stderr)
  235 + combined = self.ConvertData(combined)
229 236  
230 237 # Translate newlines, if requested. We cannot let the file
231 238 # object do the translation: It is based on stdio, which is
tools/patman/gitutil.py
... ... @@ -326,6 +326,8 @@
326 326 result = []
327 327 for item in raw:
328 328 if not item in result:
  329 + if type(item) == unicode:
  330 + item = item.encode('utf-8')
329 331 result.append(item)
330 332 if tag:
331 333 return ['%s %s%s%s' % (tag, quote, email, quote) for email in result]