← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:py3-base-image-upload into launchpad:master

 

Colin Watson has proposed merging ~cjwatson/launchpad:py3-base-image-upload into launchpad:master.

Commit message:
Port BaseImageUpload to Python 3

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/386526
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:py3-base-image-upload into launchpad:master.
diff --git a/lib/lp/services/fields/__init__.py b/lib/lp/services/fields/__init__.py
index 6bbc0b3..5675366 100644
--- a/lib/lp/services/fields/__init__.py
+++ b/lib/lp/services/fields/__init__.py
@@ -54,8 +54,8 @@ __all__ = [
     ]
 
 
+import io
 import re
-from StringIO import StringIO
 from textwrap import dedent
 
 from lazr.restful.fields import Reference
@@ -698,7 +698,7 @@ class BaseImageUpload(Bytes):
             raise LaunchpadValidationError(_(dedent("""
                 This image exceeds the maximum allowed size in bytes.""")))
         try:
-            pil_image = PIL.Image.open(StringIO(image))
+            pil_image = PIL.Image.open(io.BytesIO(image))
         except (IOError, ValueError):
             raise LaunchpadValidationError(_(dedent("""
                 The file uploaded was not recognized as an image; please
diff --git a/lib/lp/services/fields/tests/test_fields.py b/lib/lp/services/fields/tests/test_fields.py
index e235324..969e831 100644
--- a/lib/lp/services/fields/tests/test_fields.py
+++ b/lib/lp/services/fields/tests/test_fields.py
@@ -6,7 +6,7 @@
 __metaclass__ = type
 
 import datetime
-from StringIO import StringIO
+import io
 import time
 
 from zope.component import getUtility
@@ -489,13 +489,13 @@ class TestBaseImageUpload(TestCase):
     def test_validation_corrupt_image(self):
         # ValueErrors raised by PIL become LaunchpadValidationErrors.
         field = self.ExampleImageUpload(default_image_resource='dummy')
-        image = StringIO(
-            '/* XPM */\n'
-            'static char *pixmap[] = {\n'
-            '"32 32 253 2",\n'
-            '  "00 c #01CAA3",\n'
-            '  ".. s None c None",\n'
-            '};')
+        image = io.BytesIO(
+            b'/* XPM */\n'
+            b'static char *pixmap[] = {\n'
+            b'"32 32 253 2",\n'
+            b'  "00 c #01CAA3",\n'
+            b'  ".. s None c None",\n'
+            b'};')
         image.filename = 'foo.xpm'
         self.assertRaises(
             LaunchpadValidationError, field.validate, image)
@@ -503,7 +503,7 @@ class TestBaseImageUpload(TestCase):
     def test_validation_non_image(self):
         # IOError raised by PIL become LaunchpadValidationErrors.
         field = self.ExampleImageUpload(default_image_resource='dummy')
-        image = StringIO('foo bar bz')
+        image = io.BytesIO(b'foo bar bz')
         image.filename = 'foo.jpg'
         self.assertRaises(
             LaunchpadValidationError, field.validate, image)