Merge ~cjwatson/launchpad:py3-py2-unicode-compatible into launchpad:master

Proposed by Colin Watson
Status: Merged
Approved by: Colin Watson
Approved revision: 3639ded8f4f521e419c4dcd1e045a7fb92460aa7
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~cjwatson/launchpad:py3-py2-unicode-compatible
Merge into: launchpad:master
Diff against target: 248 lines (+37/-17)
9 files modified
lib/lp/registry/interfaces/mailinglist.py (+3/-4)
lib/lp/registry/interfaces/sourcepackagename.py (+6/-1)
lib/lp/registry/model/sourcepackagename.py (+3/-1)
lib/lp/services/messages/model/message.py (+3/-1)
lib/lp/services/webapp/publisher.py (+5/-3)
lib/lp/services/webhooks/payload.py (+5/-3)
lib/lp/soyuz/interfaces/binarypackagename.py (+6/-1)
lib/lp/soyuz/model/binarypackagename.py (+3/-1)
lib/lp/translations/utilities/gettext_po_parser.py (+3/-2)
Reviewer Review Type Date Requested Status
Thiago F. Pappacena (community) Approve
Review via email: mp+387710@code.launchpad.net

Commit message

Use six.python_2_unicode_compatible

Description of the change

Where appropriate, this makes it easier to define __str__ and __unicode__ for Python 2 and only __str__ for Python 3.

In some places we define __unicode__ only to deliberately mark it as not implemented. However, those are related to URLs, where it seems to make sense to define whatever the native __str__ is on each version, so mark the __unicode__ definition as Python-2-only.

To post a comment you must log in.
Revision history for this message
Thiago F. Pappacena (pappacena) wrote :

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lib/lp/registry/interfaces/mailinglist.py b/lib/lp/registry/interfaces/mailinglist.py
2index ac660cd..0924fba 100644
3--- a/lib/lp/registry/interfaces/mailinglist.py
4+++ b/lib/lp/registry/interfaces/mailinglist.py
5@@ -27,6 +27,7 @@ from lazr.enum import (
6 DBEnumeratedType,
7 DBItem,
8 )
9+import six
10 from zope.interface import Interface
11 from zope.schema import (
12 Bool,
13@@ -872,6 +873,7 @@ class IHeldMessageDetails(Interface):
14 required=True, readonly=True)
15
16
17+@six.python_2_unicode_compatible
18 class BaseSubscriptionErrors(Exception):
19 """Base class for subscription exceptions."""
20
21@@ -886,11 +888,8 @@ class BaseSubscriptionErrors(Exception):
22 Exception.__init__(self, error_string)
23 self._error_string = error_string
24
25- def __unicode__(self):
26- return self._error_string
27-
28 def __str__(self):
29- return self._error_string.encode('utf-8')
30+ return self._error_string
31
32
33 class CannotSubscribe(BaseSubscriptionErrors):
34diff --git a/lib/lp/registry/interfaces/sourcepackagename.py b/lib/lp/registry/interfaces/sourcepackagename.py
35index d4ffa2a..89c0a8c 100644
36--- a/lib/lp/registry/interfaces/sourcepackagename.py
37+++ b/lib/lp/registry/interfaces/sourcepackagename.py
38@@ -10,6 +10,7 @@ __all__ = [
39 'ISourcePackageNameSet',
40 ]
41
42+import six
43 from zope.interface import (
44 Attribute,
45 Interface,
46@@ -36,9 +37,13 @@ class ISourcePackageName(Interface):
47 packagings = Attribute("Everything we know about the packaging of "
48 "packages with this source package name.")
49
50- def __unicode__():
51+ def __str__():
52 """Return the name"""
53
54+ if six.PY2:
55+ def __unicode__():
56+ """Return the name"""
57+
58
59 class ISourcePackageNameSet(Interface):
60 """A set of SourcePackageName."""
61diff --git a/lib/lp/registry/model/sourcepackagename.py b/lib/lp/registry/model/sourcepackagename.py
62index 5ed2198..9d3615f 100644
63--- a/lib/lp/registry/model/sourcepackagename.py
64+++ b/lib/lp/registry/model/sourcepackagename.py
65@@ -8,6 +8,7 @@ __all__ = [
66 'getSourcePackageDescriptions',
67 ]
68
69+import six
70 from sqlobject import (
71 SQLMultipleJoin,
72 SQLObjectNotFound,
73@@ -33,6 +34,7 @@ from lp.services.database.sqlbase import (
74 from lp.services.helpers import ensure_unicode
75
76
77+@six.python_2_unicode_compatible
78 @implementer(ISourcePackageName)
79 class SourcePackageName(SQLBase):
80 _table = 'SourcePackageName'
81@@ -45,7 +47,7 @@ class SourcePackageName(SQLBase):
82 packagings = SQLMultipleJoin(
83 'Packaging', joinColumn='sourcepackagename', orderBy='Packaging.id')
84
85- def __unicode__(self):
86+ def __str__(self):
87 return self.name
88
89 def __repr__(self):
90diff --git a/lib/lp/services/messages/model/message.py b/lib/lp/services/messages/model/message.py
91index 296bb4c..10a62ee 100644
92--- a/lib/lp/services/messages/model/message.py
93+++ b/lib/lp/services/messages/model/message.py
94@@ -29,6 +29,7 @@ import os.path
95
96 from lazr.config import as_timedelta
97 import pytz
98+import six
99 from sqlobject import (
100 BoolCol,
101 ForeignKey,
102@@ -484,6 +485,7 @@ class MessageSet:
103 return message
104
105
106+@six.python_2_unicode_compatible
107 @implementer(IMessageChunk)
108 class MessageChunk(SQLBase):
109 """One part of a possibly multipart Message"""
110@@ -502,7 +504,7 @@ class MessageChunk(SQLBase):
111 foreignKey='LibraryFileAlias', dbName='blob', notNull=False,
112 default=None)
113
114- def __unicode__(self):
115+ def __str__(self):
116 """Return a text representation of this chunk.
117
118 This is either the content, or a link to the blob in a format
119diff --git a/lib/lp/services/webapp/publisher.py b/lib/lp/services/webapp/publisher.py
120index 055a765..f01466d 100644
121--- a/lib/lp/services/webapp/publisher.py
122+++ b/lib/lp/services/webapp/publisher.py
123@@ -40,6 +40,7 @@ from lazr.restful.marshallers import URLDereferencingMixin
124 from lazr.restful.tales import WebLayerAPI
125 from lazr.restful.utils import get_current_browser_request
126 import simplejson
127+import six
128 from six.moves import http_client
129 from six.moves.urllib.parse import urlparse
130 from zope.app.publisher.xmlrpc import IMethodPublisher
131@@ -629,9 +630,10 @@ class CanonicalAbsoluteURL:
132 self.context = context
133 self.request = request
134
135- def __unicode__(self):
136- """Returns the URL as a unicode string."""
137- raise NotImplementedError()
138+ if six.PY2:
139+ def __unicode__(self):
140+ """Returns the URL as a unicode string."""
141+ raise NotImplementedError()
142
143 def __str__(self):
144 """Returns an ASCII string with all unicode characters url quoted."""
145diff --git a/lib/lp/services/webhooks/payload.py b/lib/lp/services/webhooks/payload.py
146index 78b70b5..f7c7e3d 100644
147--- a/lib/lp/services/webhooks/payload.py
148+++ b/lib/lp/services/webhooks/payload.py
149@@ -12,6 +12,7 @@ __all__ = [
150 from io import BytesIO
151
152 from lazr.restful.interfaces import IFieldMarshaller
153+import six
154 from zope.component import getMultiAdapter
155 from zope.interface import implementer
156 from zope.traversing.browser.interfaces import IAbsoluteURL
157@@ -42,9 +43,10 @@ class WebhookAbsoluteURL:
158 self.context = context
159 self.request = request
160
161- def __unicode__(self):
162- """Returns the URL as a unicode string."""
163- raise NotImplementedError()
164+ if six.PY2:
165+ def __unicode__(self):
166+ """Returns the URL as a unicode string."""
167+ raise NotImplementedError()
168
169 def __str__(self):
170 """Returns an ASCII string with all unicode characters url quoted."""
171diff --git a/lib/lp/soyuz/interfaces/binarypackagename.py b/lib/lp/soyuz/interfaces/binarypackagename.py
172index 820553e..bfef027 100644
173--- a/lib/lp/soyuz/interfaces/binarypackagename.py
174+++ b/lib/lp/soyuz/interfaces/binarypackagename.py
175@@ -11,6 +11,7 @@ __all__ = [
176 'IBinaryPackageNameSet',
177 ]
178
179+import six
180 from zope.interface import Interface
181 from zope.schema import (
182 Int,
183@@ -27,9 +28,13 @@ class IBinaryPackageName(Interface):
184 name = TextLine(title=_('Valid Binary package name'),
185 required=True, constraint=name_validator)
186
187- def __unicode__():
188+ def __str__():
189 """Return the name"""
190
191+ if six.PY2:
192+ def __unicode__():
193+ """Return the name"""
194+
195
196 class IBinaryPackageNameSet(Interface):
197
198diff --git a/lib/lp/soyuz/model/binarypackagename.py b/lib/lp/soyuz/model/binarypackagename.py
199index aabcd75..de0f71a 100644
200--- a/lib/lp/soyuz/model/binarypackagename.py
201+++ b/lib/lp/soyuz/model/binarypackagename.py
202@@ -7,6 +7,7 @@ __all__ = [
203 'BinaryPackageNameSet',
204 ]
205
206+import six
207 from sqlobject import (
208 SQLObjectNotFound,
209 StringCol,
210@@ -26,13 +27,14 @@ from lp.soyuz.interfaces.binarypackagename import (
211 from lp.soyuz.interfaces.publishing import active_publishing_status
212
213
214+@six.python_2_unicode_compatible
215 @implementer(IBinaryPackageName)
216 class BinaryPackageName(SQLBase):
217 _table = 'BinaryPackageName'
218 name = StringCol(dbName='name', notNull=True, unique=True,
219 alternateID=True)
220
221- def __unicode__(self):
222+ def __str__(self):
223 return self.name
224
225 def __repr__(self):
226diff --git a/lib/lp/translations/utilities/gettext_po_parser.py b/lib/lp/translations/utilities/gettext_po_parser.py
227index 78de456..27b9385 100644
228--- a/lib/lp/translations/utilities/gettext_po_parser.py
229+++ b/lib/lp/translations/utilities/gettext_po_parser.py
230@@ -44,6 +44,7 @@ from lp.translations.utilities.translation_common_format import (
231 )
232
233
234+@six.python_2_unicode_compatible
235 class POSyntaxWarning(Warning):
236 """Syntax warning in a PO file."""
237 def __init__(self, message, line_number=None):
238@@ -62,8 +63,8 @@ class POSyntaxWarning(Warning):
239 self.message = message
240 logging.info(self.message)
241
242- def __unicode__(self):
243- return unicode(self.message)
244+ def __str__(self):
245+ return six.ensure_text(self.message)
246
247
248 def parse_charset(string_to_parse, is_escaped=True):

Subscribers

People subscribed via source and target branches

to status/vote changes: