← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:py3-image-widget-bytesio into launchpad:master

 

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

Commit message:
Port image-widget.txt to io.BytesIO

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/395699
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:py3-image-widget-bytesio into launchpad:master.
diff --git a/lib/lp/app/widgets/doc/image-widget.txt b/lib/lp/app/widgets/doc/image-widget.txt
index 204cad7..164576c 100644
--- a/lib/lp/app/widgets/doc/image-widget.txt
+++ b/lib/lp/app/widgets/doc/image-widget.txt
@@ -124,12 +124,13 @@ Then we tell it to delete the existing one.
 And now we change it to a random image.
 
     >>> import canonical.launchpad
+    >>> import io
     >>> import os
-    >>> from StringIO import StringIO
     >>> logo_file_name = os.path.join(
     ...     os.path.dirname(canonical.launchpad.__file__),
     ...     'images/team-logo.png')
-    >>> logo = StringIO(open(logo_file_name, 'r').read())
+    >>> with open(logo_file_name, 'rb') as logo_file:
+    ...     logo = io.BytesIO(logo_file.read())
     >>> logo.filename = 'logo.png'
     >>> form = {'field.logo.action': 'change',
     ...         'field.logo.image': logo}
@@ -139,7 +140,7 @@ And now we change it to a random image.
     >>> fileupload.filename
     u'logo.png'
 
-    >>> fileupload.content.filesize == logo.len
+    >>> fileupload.content.filesize == len(logo.getvalue())
     True
 
 In order for this widget to work on add forms, we need to make sure it
@@ -170,7 +171,7 @@ which creates a new database object for us.
     keep: NOT SELECTED
     change: SELECTED
 
-    >>> widget.getInputValue().content.filesize == logo.len
+    >>> widget.getInputValue().content.filesize == len(logo.getvalue())
     True
 
 
@@ -252,9 +253,10 @@ dimensions smaller than person_mugshot.dimensions, it must be rejected.
     >>> logo_file_name = os.path.join(
     ...     os.path.dirname(canonical.launchpad.__file__),
     ...     'images/team-logo.png')
-    >>> logo = StringIO(open(logo_file_name, 'r').read())
+    >>> with open(logo_file_name, 'rb') as logo_file:
+    ...     logo = io.BytesIO(logo_file.read())
     >>> logo.filename = 'logo.png'
-    >>> logo.len <= person_mugshot.max_size
+    >>> len(logo.getvalue()) <= person_mugshot.max_size
     True
 
     >>> image = PIL.Image.open(logo)
@@ -277,12 +279,13 @@ the max_size:
     >>> mugshot_file_name = os.path.join(
     ...     os.path.dirname(canonical.launchpad.__file__),
     ...     'images/team-mugshot.png')
-    >>> mugshot = StringIO(open(mugshot_file_name, 'r').read())
+    >>> with open(mugshot_file_name, 'rb') as mugshot_file:
+    ...     mugshot = io.BytesIO(mugshot_file.read())
     >>> mugshot.filename = 'mugshot.png'
 
 Image is a small enough file:
 
-    >>> mugshot.len <= person_mugshot.max_size
+    >>> len(mugshot.getvalue()) <= person_mugshot.max_size
     True
 
 Image is the correct dimensions:
@@ -299,14 +302,14 @@ Image is the correct dimensions:
     >>> fileupload.filename
     u'mugshot.png'
 
-    >>> fileupload.content.filesize == mugshot.len
+    >>> fileupload.content.filesize == len(mugshot.getvalue())
     True
 
 If we change person_mugshot's max_size to be smaller than our test
 image, we'll get a validation error.
 
-    >>> person_mugshot.max_size = mugshot.len - 1
-    >>> mugshot.seek(0)
+    >>> person_mugshot.max_size = len(mugshot.getvalue()) - 1
+    >>> _ = mugshot.seek(0)
     >>> widget = ImageChangeWidget(
     ...     person_mugshot, LaunchpadTestRequest(form=form), edit_style)
     >>> widget.getInputValue()
@@ -320,9 +323,9 @@ image, we'll get a validation error.
 A similar error will be raised if the image's dimensions are bigger than
 the maximum we allow.
 
-    >>> person_mugshot.max_size = mugshot.len
+    >>> person_mugshot.max_size = len(mugshot.getvalue())
     >>> person_mugshot.dimensions = (image.size[0] - 1, image.size[1] + 1)
-    >>> mugshot.seek(0)
+    >>> _ = mugshot.seek(0)
     >>> widget = ImageChangeWidget(
     ...     person_mugshot, LaunchpadTestRequest(form=form), edit_style)
     >>> widget.getInputValue()
@@ -333,7 +336,7 @@ the maximum we allow.
                                                   191x193\npixels in size.'))
 
     >>> person_mugshot.dimensions = (image.size[0] + 1, image.size[1] - 1)
-    >>> mugshot.seek(0)
+    >>> _ = mugshot.seek(0)
     >>> widget = ImageChangeWidget(
     ...     person_mugshot, LaunchpadTestRequest(form=form), edit_style)
     >>> widget.getInputValue()
@@ -366,7 +369,8 @@ by setting the exact_dimensions attribute of the field to False:
 
     >>> person_mugshot.exact_dimensions = False
     >>> person_mugshot.dimensions = (64, 64)
-    >>> mugshot = StringIO(open(mugshot_file_name, 'r').read())
+    >>> with open(mugshot_file_name, 'rb') as mugshot_file:
+    ...     mugshot = io.BytesIO(mugshot_file.read())
     >>> mugshot.filename = 'mugshot.png'
     >>> form = {'field.mugshot.action': 'change',
     ...         'field.mugshot.image': mugshot}
@@ -382,7 +386,7 @@ by setting the exact_dimensions attribute of the field to False:
 If the image is smaller than the dimensions, the input validates:
 
     >>> person_mugshot.dimensions = (256, 256)
-    >>> mugshot.seek(0)
+    >>> _ = mugshot.seek(0)
     >>> widget = ImageChangeWidget(
     ...     person_mugshot, LaunchpadTestRequest(form=form), edit_style)
     >>> fileupload = widget.getInputValue()
@@ -392,7 +396,7 @@ If the image is smaller than the dimensions, the input validates:
 The same occurs if the image matches the specified dimensions:
 
     >>> person_mugshot.dimensions = (192, 192)
-    >>> mugshot.seek(0)
+    >>> _ = mugshot.seek(0)
     >>> widget = ImageChangeWidget(
     ...     person_mugshot, LaunchpadTestRequest(form=form), edit_style)
     >>> fileupload = widget.getInputValue()