Merge lp:~brian.curtin/ubuntu-sso-client/py3-StringIO into lp:ubuntu-sso-client

Proposed by Brian Curtin on 2012-06-27
Status: Merged
Approved by: dobey on 2012-07-06
Approved revision: 985
Merged at revision: 983
Proposed branch: lp:~brian.curtin/ubuntu-sso-client/py3-StringIO
Merge into: lp:ubuntu-sso-client
Diff against target: 54 lines (+6/-5)
3 files modified
ubuntu_sso/qt/setup_account_page.py (+2/-2)
ubuntu_sso/utils/tests/test_parse_args.py (+3/-2)
ubuntu_sso/utils/webclient/qtnetwork.py (+1/-1)
To merge this branch: bzr merge lp:~brian.curtin/ubuntu-sso-client/py3-StringIO
Reviewer Review Type Date Requested Status
dobey (community) Approve on 2012-07-06
Alejandro J. Cura (community) 2012-06-27 Approve on 2012-07-03
Review via email: mp+112443@code.launchpad.net

Commit Message

Import the StringIO class in a Python 2 and 3 way

Description of the Change

Python 3 moved the StringIO class from the StringIO module to the io module. First try to import it from io, then fall back to StringIO.

To post a comment you must log in.
Alejandro J. Cura (alecu) wrote :

It seems that io.StringIO exists in 2.7, and it's incompatible from StringIO:

----

[ERROR]
Traceback (most recent call last):
  File "/home/alecu/canonical/ubuntu-sso-client/review_py3-StringIO/ubuntu_sso/utils/tests/test_parse_args.py", line 97, in test_parse_args_app_name_is_required
    main.parse_args()
  File "/home/alecu/canonical/ubuntu-sso-client/review_py3-StringIO/ubuntu_sso/utils/ui.py", line 261, in parse_args
    args = parser.parse_args()
  File "/usr/lib/python2.7/argparse.py", line 1688, in parse_args
    args, argv = self.parse_known_args(args, namespace)
  File "/usr/lib/python2.7/argparse.py", line 1720, in parse_known_args
    namespace, args = self._parse_known_args(args, namespace)
  File "/usr/lib/python2.7/argparse.py", line 1944, in _parse_known_args
    self.error(_('argument %s is required') % name)
  File "/usr/lib/python2.7/argparse.py", line 2346, in error
    self.print_usage(_sys.stderr)
  File "/usr/lib/python2.7/argparse.py", line 2308, in print_usage
    self._print_message(self.format_usage(), file)
  File "/usr/lib/python2.7/argparse.py", line 2327, in _print_message
    file.write(message)
exceptions.TypeError: unicode argument expected, got 'str'

ubuntu_sso.utils.tests.test_parse_args.ParseArgsTestCase.test_parse_args_app_name_is_required

----

Please make sure to run the tests on 2 after each of these branches.

review: Needs Fixing
Brian Curtin (brian.curtin) wrote :

All of the above changes have this fixed.

dobey (dobey) wrote :

I think using io.StringIO here is correct even in 2.7, with the try/except. The test error from alecu seems to be that we also need to do from __future__ import unicode_literals as part of the work to port to python3, and it would likely solve the problem in the test; though opens another can of worms which we also need to deal with. Perhaps we can do some of that in this branch?

984. By Brian Curtin on 2012-07-02

Use StringIO from the io module regardless of version (since it was introduced in 2.6 and that's our minimum version). Additionally use unicode_literals in test_parse_args

Brian Curtin (brian.curtin) wrote :

Good point. I have other branches that are covering unicode by itself, but moving to always using io.StringIO regardless of version and adding the unicode_literals import to the test seem to solve this.

We can remove the if/else and try/except because if we're only supporting 2.6+ and 3, io.StringIO is always there.

985. By Brian Curtin on 2012-07-03

Remove sys import

Alejandro J. Cura (alecu) wrote :

+1

review: Approve
dobey (dobey) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ubuntu_sso/qt/setup_account_page.py'
2--- ubuntu_sso/qt/setup_account_page.py 2012-06-22 16:55:35 +0000
3+++ ubuntu_sso/qt/setup_account_page.py 2012-07-03 21:32:20 +0000
4@@ -28,9 +28,9 @@
5 # files in the program, then also delete it here.
6 """Customized Setup Account page for SSO."""
7
8-import StringIO
9 import tempfile
10 import os
11+from io import StringIO
12 from functools import partial
13
14 # pylint: disable=F0401
15@@ -326,7 +326,7 @@
16 # done either by a setParent or something within the qtreactor, PIL
17 # in this case does solve the issue. Sorry :(
18 pil_image = Image.open(self.captcha_file)
19- string_io = StringIO.StringIO()
20+ string_io = StringIO()
21 pil_image.save(string_io, format='png')
22 pixmap_image = QtGui.QPixmap()
23 pixmap_image.loadFromData(string_io.getvalue())
24
25=== modified file 'ubuntu_sso/utils/tests/test_parse_args.py'
26--- ubuntu_sso/utils/tests/test_parse_args.py 2012-04-09 17:38:24 +0000
27+++ ubuntu_sso/utils/tests/test_parse_args.py 2012-07-03 21:32:20 +0000
28@@ -28,9 +28,10 @@
29 # files in the program, then also delete it here.
30 """Tests for the parse_args function."""
31
32+from __future__ import unicode_literals
33+
34 import sys
35-
36-from StringIO import StringIO
37+from io import StringIO
38
39 from twisted.trial.unittest import TestCase
40
41
42=== modified file 'ubuntu_sso/utils/webclient/qtnetwork.py'
43--- ubuntu_sso/utils/webclient/qtnetwork.py 2012-06-22 21:04:49 +0000
44+++ ubuntu_sso/utils/webclient/qtnetwork.py 2012-07-03 21:32:20 +0000
45@@ -29,8 +29,8 @@
46 """A webclient backend that uses QtNetwork."""
47
48 import sys
49+from io import StringIO
50
51-from StringIO import StringIO
52 # pylint: disable=E0611
53 from PyQt4.QtCore import (
54 QBuffer,

Subscribers

People subscribed via source and target branches