← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:py3-iter-sentinels into launchpad:master

 

Colin Watson has proposed merging ~cjwatson/launchpad:py3-iter-sentinels into launchpad:master.

Commit message:
Fix iter() sentinels

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/386489

Loops that iterate over binary files using iter() with a sentinel need to set the sentinel to b'' rather than '', or else they loop forever on Python 3.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:py3-iter-sentinels into launchpad:master.
diff --git a/lib/lp/archivepublisher/publishing.py b/lib/lp/archivepublisher/publishing.py
index 18f273a..5908ccc 100644
--- a/lib/lp/archivepublisher/publishing.py
+++ b/lib/lp/archivepublisher/publishing.py
@@ -1427,7 +1427,7 @@ class Publisher(object):
             {"md5sum": {"md5sum": ..., "size": ..., "name": ...}}), or None
             if the file could not be found.
         """
-        open_func = open
+        open_func = partial(open, mode='rb')
         full_name = os.path.join(
             self._config.distsroot, suite, subpath or '.',
             real_file_name or file_name)
@@ -1453,7 +1453,7 @@ class Publisher(object):
             for archive_hash in archive_hashes}
         size = 0
         with open_func(full_name) as in_file:
-            for chunk in iter(lambda: in_file.read(256 * 1024), ""):
+            for chunk in iter(lambda: in_file.read(256 * 1024), b""):
                 for hashobj in hashes.values():
                     hashobj.update(chunk)
                 size += len(chunk)
@@ -1569,7 +1569,7 @@ class DirectoryHash:
             (checksum_file, archive_hash.hash_factory())
             for (_, checksum_file, archive_hash) in self.checksum_hash]
         with open(path, 'rb') as in_file:
-            for chunk in iter(lambda: in_file.read(256 * 1024), ""):
+            for chunk in iter(lambda: in_file.read(256 * 1024), b""):
                 for (checksum_file, hashobj) in hashes:
                     hashobj.update(chunk)
 
diff --git a/lib/lp/services/librarian/client.py b/lib/lp/services/librarian/client.py
index 9550d56..55b75ef 100644
--- a/lib/lp/services/librarian/client.py
+++ b/lib/lp/services/librarian/client.py
@@ -195,7 +195,7 @@ class FileUploadClient:
             # Read in and upload the file 64kb at a time, by using the two-arg
             # form of iter (see
             # /usr/share/doc/python/html/library/functions.html#iter).
-            for chunk in iter(lambda: file.read(1024 * 64), ''):
+            for chunk in iter(lambda: file.read(1024 * 64), b''):
                 self.state.f.write(chunk)
                 bytesWritten += len(chunk)
                 md5_digester.update(chunk)
@@ -261,7 +261,7 @@ class FileUploadClient:
             # Read in and upload the file 64kb at a time, by using the two-arg
             # form of iter (see
             # /usr/share/doc/python/html/library/functions.html#iter).
-            for chunk in iter(lambda: file.read(1024 * 64), ''):
+            for chunk in iter(lambda: file.read(1024 * 64), b''):
                 self.state.f.write(chunk)
                 bytesWritten += len(chunk)
 
diff --git a/lib/lp/services/librarian/utils.py b/lib/lp/services/librarian/utils.py
index 514aa08..7457fb3 100644
--- a/lib/lp/services/librarian/utils.py
+++ b/lib/lp/services/librarian/utils.py
@@ -18,7 +18,7 @@ MEGABYTE = 1024 * 1024
 
 def filechunks(file, chunk_size=4 * MEGABYTE):
     """Return an iterator which reads chunks of the given file."""
-    return iter(lambda: file.read(chunk_size), '')
+    return iter(lambda: file.read(chunk_size), b'')
 
 
 def copy_and_close(from_file, to_file):
diff --git a/lib/lp/services/librarianserver/librariangc.py b/lib/lp/services/librarianserver/librariangc.py
index ac2b630..7e52970 100644
--- a/lib/lp/services/librarianserver/librariangc.py
+++ b/lib/lp/services/librarianserver/librariangc.py
@@ -101,7 +101,7 @@ def open_stream(content_id):
 
 def sha1_file(content_id):
     file = open_stream(content_id)
-    chunks_iter = iter(lambda: file.read(STREAM_CHUNK_SIZE), '')
+    chunks_iter = iter(lambda: file.read(STREAM_CHUNK_SIZE), b'')
     length = 0
     hasher = hashlib.sha1()
     for chunk in chunks_iter:
diff --git a/lib/lp/soyuz/scripts/gina/library.py b/lib/lp/soyuz/scripts/gina/library.py
index adc2c41..1be3033 100644
--- a/lib/lp/soyuz/scripts/gina/library.py
+++ b/lib/lp/soyuz/scripts/gina/library.py
@@ -45,8 +45,8 @@ def checkLibraryForFile(path, filename):
     fullpath = os.path.join(path, filename)
     assert os.path.exists(fullpath)
     digester = hashlib.sha256()
-    openfile = open(fullpath, "r")
-    for chunk in iter(lambda: openfile.read(1024 * 4), ''):
+    openfile = open(fullpath, "rb")
+    for chunk in iter(lambda: openfile.read(1024 * 4), b''):
         digester.update(chunk)
     digest = digester.hexdigest()
     openfile.close()