[Buildroot] [PATCH v2 04/10] autobuild-run, python3: encode/decode mmap data

André Erdmann dywi at mailerd.de
Wed Mar 18 15:50:37 UTC 2015


mmap objects return bytes in Python 3,
which leads to errors when used in str context, for example:

  >>> fh      = open("file", "r")
  >>> fh_mmap = mmap.mmap(fh.fileno(), 0, access=ACCESS_READ)
  >>> fh_mmap.find("word")
  TypeError: 'str' does not support the buffer interface

Adds a new compat function, encode_str(), which is a no-op in Python 2.

Signed-off-by: André Erdmann <dywi at mailerd.de>
---
 scripts/autobuild-run | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/scripts/autobuild-run b/scripts/autobuild-run
index e536289..40bd657 100755
--- a/scripts/autobuild-run
+++ b/scripts/autobuild-run
@@ -139,12 +139,16 @@ if sys.hexversion >= 0x3000000:
 
     def decode_byte_list(bl):
         return [b.decode() for b in bl]
+
+    def encode_str(s):
+        return s.encode()
 else:
     def _identity(e):
         return e
 
     decode_bytes = _identity
     decode_byte_list = _identity
+    encode_str = _identity
 
 MAX_DURATION = 60 * 60 * 4
 VERSION = 1
@@ -607,10 +611,10 @@ def send_results(result, **kwargs):
             mf = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
             mf.seek(0)
             # Search for first action on the failed package
-            offset = mf.find('>>> %s' % ' '.join(reason))
+            offset = mf.find(encode_str('>>> %s' % ' '.join(reason)))
             if offset != -1:
                 with open(resultfile, "w") as endlog:
-                    endlog.write(mf[offset:])
+                    endlog.write(decode_bytes(mf[offset:]))
             else:
                 # not found, use last 500 lines as fallback
                 extract_last_500_lines()
-- 
2.3.2



More information about the buildroot mailing list