Skip to contentSkip to conversation

patman: Update cros_subprocess to use bytes

PATCH New Simon Glass 1 participant 2 messages Jul 10, 2019 – Jul 11, 2019

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/21 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.

---

Changes in v2:
- Update to use bytes instead of bytearray

 tools/patman/cros_subprocess.py | 47 +++++++++++++++++++--------------
 tools/patman/gitutil.py         |  2 ++
 2 files changed, 29 insertions(+), 20 deletions(-)
tools/patman/cros_subprocess.py +27 -20
index ebd4300dfd5..0f0d60dfb7a 100644
@@ -100,6 +100,19 @@ class Popen(subprocess.Popen):
         if kwargs:
             raise ValueError("Unit tests do not test extra args - please add tests")
 
+    def ConvertData(self, data):
+        """Convert stdout/stderr data to the correct format for output
+
+        Args:
+            data: Data to convert, or None for ''
+
+        Returns:
+            Converted data, as bytes
+        """
+        if data is None:
+            return b''
+        return data
+
     def CommunicateFilter(self, output):
         """Interact with process: Read data from stdout and stderr.
 
@@ -156,11 +169,11 @@ class Popen(subprocess.Popen):
                 self.stdin.close()
         if self.stdout:
             read_set.append(self.stdout)
-            stdout = []
+            stdout = b''
         if self.stderr and self.stderr != self.stdout:
             read_set.append(self.stderr)
-            stderr = []
-        combined = []
+            stderr = b''
+        combined = b''
 
         input_offset = 0
         while read_set or write_set:
@@ -186,46 +199,40 @@ class Popen(subprocess.Popen):
                     write_set.remove(self.stdin)
 
             if self.stdout in rlist:
-                data = ""
+                data = b''
                 # We will get an error on read if the pty is closed
                 try:
                     data = os.read(self.stdout.fileno(), 1024)
                 except OSError:
                     pass
-                if data == "":
+                if not len(data):
                     self.stdout.close()
                     read_set.remove(self.stdout)
                 else:
-                    stdout.append(data)
-                    combined.append(data)
+                    stdout += data
+                    combined += data
                     if output:
                         output(sys.stdout, data)
             if self.stderr in rlist:
-                data = ""
+                data = b''
                 # We will get an error on read if the pty is closed
                 try:
                     data = os.read(self.stderr.fileno(), 1024)
                 except OSError:
                     pass
-                if data == "":
+                if not len(data):
                     self.stderr.close()
                     read_set.remove(self.stderr)
                 else:
-                    stderr.append(data)
-                    combined.append(data)
+                    stderr += data
+                    combined += data
                     if output:
                         output(sys.stderr, data)
 
         # All data exchanged.    Translate lists into strings.
-        if stdout is not None:
-            stdout = ''.join(stdout)
-        else:
-            stdout = ''
-        if stderr is not None:
-            stderr = ''.join(stderr)
-        else:
-            stderr = ''
-        combined = ''.join(combined)
+        stdout = self.ConvertData(stdout)
+        stderr = self.ConvertData(stderr)
+        combined = self.ConvertData(combined)
 
         # Translate newlines, if requested.    We cannot let the file
         # object do the translation: It is based on stdio, which is
tools/patman/gitutil.py +2 -0
index 9905bb0bbd8..7650b51bd5b 100644
@@ -326,6 +326,8 @@ def BuildEmailList(in_list, tag=None, alias=None, raise_on_error=True):
     result = []
     for item in raw:
         if not item in result:
+            if type(item) == unicode:
+                item = item.encode('utf-8')
             result.append(item)
     if tag:
         return ['%s %s%s%s' % (tag, quote, email, quote) for email in result]
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
#1 lore raw

PATCH v2 1/21 patman: Update cros_subprocess to use bytes

Quoted 6 lines · Simon Glass

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.

Quoted 8 lines

---

Changes in v2:
- Update to use bytes instead of bytearray

 tools/patman/cros_subprocess.py | 47 +++++++++++++++++++--------------
 tools/patman/gitutil.py         |  2 ++
 2 files changed, 29 insertions(+), 20 deletions(-)

Applied to u-boot-dm.