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
diff --git a/lib/lp/registry/interfaces/mailinglist.py b/lib/lp/registry/interfaces/mailinglist.py
index ac660cd..0924fba 100644
--- a/lib/lp/registry/interfaces/mailinglist.py
+++ b/lib/lp/registry/interfaces/mailinglist.py
@@ -27,6 +27,7 @@ from lazr.enum import (
27 DBEnumeratedType,27 DBEnumeratedType,
28 DBItem,28 DBItem,
29 )29 )
30import six
30from zope.interface import Interface31from zope.interface import Interface
31from zope.schema import (32from zope.schema import (
32 Bool,33 Bool,
@@ -872,6 +873,7 @@ class IHeldMessageDetails(Interface):
872 required=True, readonly=True)873 required=True, readonly=True)
873874
874875
876@six.python_2_unicode_compatible
875class BaseSubscriptionErrors(Exception):877class BaseSubscriptionErrors(Exception):
876 """Base class for subscription exceptions."""878 """Base class for subscription exceptions."""
877879
@@ -886,11 +888,8 @@ class BaseSubscriptionErrors(Exception):
886 Exception.__init__(self, error_string)888 Exception.__init__(self, error_string)
887 self._error_string = error_string889 self._error_string = error_string
888890
889 def __unicode__(self):
890 return self._error_string
891
892 def __str__(self):891 def __str__(self):
893 return self._error_string.encode('utf-8')892 return self._error_string
894893
895894
896class CannotSubscribe(BaseSubscriptionErrors):895class CannotSubscribe(BaseSubscriptionErrors):
diff --git a/lib/lp/registry/interfaces/sourcepackagename.py b/lib/lp/registry/interfaces/sourcepackagename.py
index d4ffa2a..89c0a8c 100644
--- a/lib/lp/registry/interfaces/sourcepackagename.py
+++ b/lib/lp/registry/interfaces/sourcepackagename.py
@@ -10,6 +10,7 @@ __all__ = [
10 'ISourcePackageNameSet',10 'ISourcePackageNameSet',
11 ]11 ]
1212
13import six
13from zope.interface import (14from zope.interface import (
14 Attribute,15 Attribute,
15 Interface,16 Interface,
@@ -36,9 +37,13 @@ class ISourcePackageName(Interface):
36 packagings = Attribute("Everything we know about the packaging of "37 packagings = Attribute("Everything we know about the packaging of "
37 "packages with this source package name.")38 "packages with this source package name.")
3839
39 def __unicode__():40 def __str__():
40 """Return the name"""41 """Return the name"""
4142
43 if six.PY2:
44 def __unicode__():
45 """Return the name"""
46
4247
43class ISourcePackageNameSet(Interface):48class ISourcePackageNameSet(Interface):
44 """A set of SourcePackageName."""49 """A set of SourcePackageName."""
diff --git a/lib/lp/registry/model/sourcepackagename.py b/lib/lp/registry/model/sourcepackagename.py
index 5ed2198..9d3615f 100644
--- a/lib/lp/registry/model/sourcepackagename.py
+++ b/lib/lp/registry/model/sourcepackagename.py
@@ -8,6 +8,7 @@ __all__ = [
8 'getSourcePackageDescriptions',8 'getSourcePackageDescriptions',
9 ]9 ]
1010
11import six
11from sqlobject import (12from sqlobject import (
12 SQLMultipleJoin,13 SQLMultipleJoin,
13 SQLObjectNotFound,14 SQLObjectNotFound,
@@ -33,6 +34,7 @@ from lp.services.database.sqlbase import (
33from lp.services.helpers import ensure_unicode34from lp.services.helpers import ensure_unicode
3435
3536
37@six.python_2_unicode_compatible
36@implementer(ISourcePackageName)38@implementer(ISourcePackageName)
37class SourcePackageName(SQLBase):39class SourcePackageName(SQLBase):
38 _table = 'SourcePackageName'40 _table = 'SourcePackageName'
@@ -45,7 +47,7 @@ class SourcePackageName(SQLBase):
45 packagings = SQLMultipleJoin(47 packagings = SQLMultipleJoin(
46 'Packaging', joinColumn='sourcepackagename', orderBy='Packaging.id')48 'Packaging', joinColumn='sourcepackagename', orderBy='Packaging.id')
4749
48 def __unicode__(self):50 def __str__(self):
49 return self.name51 return self.name
5052
51 def __repr__(self):53 def __repr__(self):
diff --git a/lib/lp/services/messages/model/message.py b/lib/lp/services/messages/model/message.py
index 296bb4c..10a62ee 100644
--- a/lib/lp/services/messages/model/message.py
+++ b/lib/lp/services/messages/model/message.py
@@ -29,6 +29,7 @@ import os.path
2929
30from lazr.config import as_timedelta30from lazr.config import as_timedelta
31import pytz31import pytz
32import six
32from sqlobject import (33from sqlobject import (
33 BoolCol,34 BoolCol,
34 ForeignKey,35 ForeignKey,
@@ -484,6 +485,7 @@ class MessageSet:
484 return message485 return message
485486
486487
488@six.python_2_unicode_compatible
487@implementer(IMessageChunk)489@implementer(IMessageChunk)
488class MessageChunk(SQLBase):490class MessageChunk(SQLBase):
489 """One part of a possibly multipart Message"""491 """One part of a possibly multipart Message"""
@@ -502,7 +504,7 @@ class MessageChunk(SQLBase):
502 foreignKey='LibraryFileAlias', dbName='blob', notNull=False,504 foreignKey='LibraryFileAlias', dbName='blob', notNull=False,
503 default=None)505 default=None)
504506
505 def __unicode__(self):507 def __str__(self):
506 """Return a text representation of this chunk.508 """Return a text representation of this chunk.
507509
508 This is either the content, or a link to the blob in a format510 This is either the content, or a link to the blob in a format
diff --git a/lib/lp/services/webapp/publisher.py b/lib/lp/services/webapp/publisher.py
index 055a765..f01466d 100644
--- a/lib/lp/services/webapp/publisher.py
+++ b/lib/lp/services/webapp/publisher.py
@@ -40,6 +40,7 @@ from lazr.restful.marshallers import URLDereferencingMixin
40from lazr.restful.tales import WebLayerAPI40from lazr.restful.tales import WebLayerAPI
41from lazr.restful.utils import get_current_browser_request41from lazr.restful.utils import get_current_browser_request
42import simplejson42import simplejson
43import six
43from six.moves import http_client44from six.moves import http_client
44from six.moves.urllib.parse import urlparse45from six.moves.urllib.parse import urlparse
45from zope.app.publisher.xmlrpc import IMethodPublisher46from zope.app.publisher.xmlrpc import IMethodPublisher
@@ -629,9 +630,10 @@ class CanonicalAbsoluteURL:
629 self.context = context630 self.context = context
630 self.request = request631 self.request = request
631632
632 def __unicode__(self):633 if six.PY2:
633 """Returns the URL as a unicode string."""634 def __unicode__(self):
634 raise NotImplementedError()635 """Returns the URL as a unicode string."""
636 raise NotImplementedError()
635637
636 def __str__(self):638 def __str__(self):
637 """Returns an ASCII string with all unicode characters url quoted."""639 """Returns an ASCII string with all unicode characters url quoted."""
diff --git a/lib/lp/services/webhooks/payload.py b/lib/lp/services/webhooks/payload.py
index 78b70b5..f7c7e3d 100644
--- a/lib/lp/services/webhooks/payload.py
+++ b/lib/lp/services/webhooks/payload.py
@@ -12,6 +12,7 @@ __all__ = [
12from io import BytesIO12from io import BytesIO
1313
14from lazr.restful.interfaces import IFieldMarshaller14from lazr.restful.interfaces import IFieldMarshaller
15import six
15from zope.component import getMultiAdapter16from zope.component import getMultiAdapter
16from zope.interface import implementer17from zope.interface import implementer
17from zope.traversing.browser.interfaces import IAbsoluteURL18from zope.traversing.browser.interfaces import IAbsoluteURL
@@ -42,9 +43,10 @@ class WebhookAbsoluteURL:
42 self.context = context43 self.context = context
43 self.request = request44 self.request = request
4445
45 def __unicode__(self):46 if six.PY2:
46 """Returns the URL as a unicode string."""47 def __unicode__(self):
47 raise NotImplementedError()48 """Returns the URL as a unicode string."""
49 raise NotImplementedError()
4850
49 def __str__(self):51 def __str__(self):
50 """Returns an ASCII string with all unicode characters url quoted."""52 """Returns an ASCII string with all unicode characters url quoted."""
diff --git a/lib/lp/soyuz/interfaces/binarypackagename.py b/lib/lp/soyuz/interfaces/binarypackagename.py
index 820553e..bfef027 100644
--- a/lib/lp/soyuz/interfaces/binarypackagename.py
+++ b/lib/lp/soyuz/interfaces/binarypackagename.py
@@ -11,6 +11,7 @@ __all__ = [
11 'IBinaryPackageNameSet',11 'IBinaryPackageNameSet',
12 ]12 ]
1313
14import six
14from zope.interface import Interface15from zope.interface import Interface
15from zope.schema import (16from zope.schema import (
16 Int,17 Int,
@@ -27,9 +28,13 @@ class IBinaryPackageName(Interface):
27 name = TextLine(title=_('Valid Binary package name'),28 name = TextLine(title=_('Valid Binary package name'),
28 required=True, constraint=name_validator)29 required=True, constraint=name_validator)
2930
30 def __unicode__():31 def __str__():
31 """Return the name"""32 """Return the name"""
3233
34 if six.PY2:
35 def __unicode__():
36 """Return the name"""
37
3338
34class IBinaryPackageNameSet(Interface):39class IBinaryPackageNameSet(Interface):
3540
diff --git a/lib/lp/soyuz/model/binarypackagename.py b/lib/lp/soyuz/model/binarypackagename.py
index aabcd75..de0f71a 100644
--- a/lib/lp/soyuz/model/binarypackagename.py
+++ b/lib/lp/soyuz/model/binarypackagename.py
@@ -7,6 +7,7 @@ __all__ = [
7 'BinaryPackageNameSet',7 'BinaryPackageNameSet',
8 ]8 ]
99
10import six
10from sqlobject import (11from sqlobject import (
11 SQLObjectNotFound,12 SQLObjectNotFound,
12 StringCol,13 StringCol,
@@ -26,13 +27,14 @@ from lp.soyuz.interfaces.binarypackagename import (
26from lp.soyuz.interfaces.publishing import active_publishing_status27from lp.soyuz.interfaces.publishing import active_publishing_status
2728
2829
30@six.python_2_unicode_compatible
29@implementer(IBinaryPackageName)31@implementer(IBinaryPackageName)
30class BinaryPackageName(SQLBase):32class BinaryPackageName(SQLBase):
31 _table = 'BinaryPackageName'33 _table = 'BinaryPackageName'
32 name = StringCol(dbName='name', notNull=True, unique=True,34 name = StringCol(dbName='name', notNull=True, unique=True,
33 alternateID=True)35 alternateID=True)
3436
35 def __unicode__(self):37 def __str__(self):
36 return self.name38 return self.name
3739
38 def __repr__(self):40 def __repr__(self):
diff --git a/lib/lp/translations/utilities/gettext_po_parser.py b/lib/lp/translations/utilities/gettext_po_parser.py
index 78de456..27b9385 100644
--- a/lib/lp/translations/utilities/gettext_po_parser.py
+++ b/lib/lp/translations/utilities/gettext_po_parser.py
@@ -44,6 +44,7 @@ from lp.translations.utilities.translation_common_format import (
44 )44 )
4545
4646
47@six.python_2_unicode_compatible
47class POSyntaxWarning(Warning):48class POSyntaxWarning(Warning):
48 """Syntax warning in a PO file."""49 """Syntax warning in a PO file."""
49 def __init__(self, message, line_number=None):50 def __init__(self, message, line_number=None):
@@ -62,8 +63,8 @@ class POSyntaxWarning(Warning):
62 self.message = message63 self.message = message
63 logging.info(self.message)64 logging.info(self.message)
6465
65 def __unicode__(self):66 def __str__(self):
66 return unicode(self.message)67 return six.ensure_text(self.message)
6768
6869
69def parse_charset(string_to_parse, is_escaped=True):70def parse_charset(string_to_parse, is_escaped=True):

Subscribers

People subscribed via source and target branches

to status/vote changes: