Merge lp:~tseaver/subunit/py3-hacks into lp:~subunit/subunit/trunk

Proposed by Tres Seaver
Status: Merged
Merge reported by: Robert Collins
Merged at revision: not available
Proposed branch: lp:~tseaver/subunit/py3-hacks
Merge into: lp:~subunit/subunit/trunk
Diff against target: 121 lines (+30/-14)
1 file modified
python/subunit/__init__.py (+30/-14)
To merge this branch: bzr merge lp:~tseaver/subunit/py3-hacks
Reviewer Review Type Date Requested Status
Subunit Developers Pending
Review via email: mp+52941@code.launchpad.net

Description of the change

Preliminary work toward Python3 compatibility.

To post a comment you must log in.
Revision history for this message
Robert Collins (lifeless) wrote :

Thanks for this. testtools, which subunit depends on, has u and b
macros already defined - we might want to import them from there
rather than implementing them fresh. What do you think?

-Rob

Revision history for this message
Tres Seaver (tseaver) wrote :

Sounds fine to me -- I was a bit rushed, and didn't have all the dependencies installed.

Revision history for this message
Robert Collins (lifeless) wrote :

What was the iso8601 import change needed for?

Revision history for this message
Robert Collins (lifeless) wrote :

Ah, default import style, got it.

Revision history for this message
Robert Collins (lifeless) wrote :

Ok, so this isn't complete enough to import for me; I'm doing a bit of a spike on this; will commit whatever I come up with.

Revision history for this message
Robert Collins (lifeless) wrote :

Some feedback for other migrations:
this is a bug:

u"foo %s" % bar
->
u("foo %s" % bar)

instead it needs to be
u("foo %s") % bar

Its a bug because in py2 the u is dropped but the unicodeness of bar isn't altered so implicit conversions will be triggered.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'python/subunit/__init__.py'
2--- python/subunit/__init__.py 2011-02-11 17:53:17 +0000
3+++ python/subunit/__init__.py 2011-03-10 22:53:58 +0000
4@@ -118,12 +118,11 @@
5
6 import os
7 import re
8-from StringIO import StringIO
9 import subprocess
10 import sys
11 import unittest
12
13-import iso8601
14+import subunit.iso8601
15 from testtools import content, content_type, ExtendedToOriginalDecorator
16 try:
17 from testtools.testresult.real import _StringException
18@@ -142,6 +141,22 @@
19 PROGRESS_PUSH = 2
20 PROGRESS_POP = 3
21
22+if sys.version_info >= (3, 0):
23+ def b(s):
24+ return s.encode("latin-1")
25+ def u(s):
26+ return s
27+ import io
28+ StringIO = io.StringIO
29+ BytesIO = io.BytesIO
30+else:
31+ def b(s):
32+ return s
33+ def u(s):
34+ return unicode(s)
35+ import StringIO
36+ StringIO = BytesIO = StringIO.StringIO
37+
38
39 def test_suite():
40 import subunit.tests
41@@ -241,7 +256,7 @@
42
43 def lostConnection(self):
44 """Connection lost."""
45- self.parser._lostConnectionInTest(u'unknown state of ')
46+ self.parser._lostConnectionInTest(u('unknown state of '))
47
48 def startTest(self, offset, line):
49 """A test start command received."""
50@@ -321,7 +336,7 @@
51
52 def lostConnection(self):
53 """Connection lost."""
54- self.parser._lostConnectionInTest(u'')
55+ self.parser._lostConnectionInTest(u(''))
56
57
58 class _OutSideTest(_ParserState):
59@@ -356,8 +371,8 @@
60
61 def lostConnection(self):
62 """Connection lost."""
63- self.parser._lostConnectionInTest(u'%s report of ' %
64- self._outcome_label())
65+ self.parser._lostConnectionInTest(u('%s report of ' %
66+ self._outcome_label()))
67
68 def _outcome_label(self):
69 """The label to describe this outcome."""
70@@ -488,9 +503,10 @@
71 def _handleTime(self, offset, line):
72 # Accept it, but do not do anything with it yet.
73 try:
74- event_time = iso8601.parse_date(line[offset:-1])
75- except TypeError, e:
76- raise TypeError("Failed to parse %r, got %r" % (line, e))
77+ event_time = subunit.iso8601.parse_date(line[offset:-1])
78+ except TypeError:
79+ raise TypeError("Failed to parse %r, got %r"
80+ % (line, sys.exec_info[1]))
81 self.client.time(event_time)
82
83 def lineReceived(self, line):
84@@ -498,8 +514,8 @@
85 self._state.lineReceived(line)
86
87 def _lostConnectionInTest(self, state_string):
88- error_string = u"lost connection during %stest '%s'" % (
89- state_string, self.current_test_description)
90+ error_string = u("lost connection during %stest '%s'" % (
91+ state_string, self.current_test_description))
92 self.client.addError(self._current_test, RemoteError(error_string))
93 self.client.stopTest(self._current_test)
94
95@@ -680,7 +696,7 @@
96
97 ":param datetime: A datetime.datetime object.
98 """
99- time = a_datetime.astimezone(iso8601.Utc())
100+ time = a_datetime.astimezone(subunit.iso8601.Utc())
101 self._stream.write("time: %04d-%02d-%02d %02d:%02d:%02d.%06dZ\n" % (
102 time.year, time.month, time.day, time.hour, time.minute,
103 time.second, time.microsecond))
104@@ -710,7 +726,7 @@
105 """Obey the testtools result.done() interface."""
106
107
108-def RemoteError(description=u""):
109+def RemoteError(description=u("")):
110 return (_StringException, _StringException(description), None)
111
112
113@@ -760,7 +776,7 @@
114 def run(self, result=None):
115 if result is None: result = self.defaultTestResult()
116 result.startTest(self)
117- result.addError(self, RemoteError(u"Cannot run RemotedTestCases.\n"))
118+ result.addError(self, RemoteError(u("Cannot run RemotedTestCases.\n")))
119 result.stopTest(self)
120
121 def _strclass(self):

Subscribers

People subscribed via source and target branches