← Back to team overview

launchpad-reviewers team mailing list archive

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

 

Colin Watson has proposed merging ~cjwatson/launchpad:py3-py2-unicode-compatible into launchpad:master.

Commit message:
Use six.python_2_unicode_compatible

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/387710

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.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:py3-py2-unicode-compatible into launchpad:master.
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 (
     DBEnumeratedType,
     DBItem,
     )
+import six
 from zope.interface import Interface
 from zope.schema import (
     Bool,
@@ -872,6 +873,7 @@ class IHeldMessageDetails(Interface):
         required=True, readonly=True)
 
 
+@six.python_2_unicode_compatible
 class BaseSubscriptionErrors(Exception):
     """Base class for subscription exceptions."""
 
@@ -886,11 +888,8 @@ class BaseSubscriptionErrors(Exception):
         Exception.__init__(self, error_string)
         self._error_string = error_string
 
-    def __unicode__(self):
-        return self._error_string
-
     def __str__(self):
-        return self._error_string.encode('utf-8')
+        return self._error_string
 
 
 class 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__ = [
     'ISourcePackageNameSet',
     ]
 
+import six
 from zope.interface import (
     Attribute,
     Interface,
@@ -36,9 +37,13 @@ class ISourcePackageName(Interface):
     packagings = Attribute("Everything we know about the packaging of "
         "packages with this source package name.")
 
-    def __unicode__():
+    def __str__():
         """Return the name"""
 
+    if six.PY2:
+        def __unicode__():
+            """Return the name"""
+
 
 class ISourcePackageNameSet(Interface):
     """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__ = [
     'getSourcePackageDescriptions',
     ]
 
+import six
 from sqlobject import (
     SQLMultipleJoin,
     SQLObjectNotFound,
@@ -33,6 +34,7 @@ from lp.services.database.sqlbase import (
 from lp.services.helpers import ensure_unicode
 
 
+@six.python_2_unicode_compatible
 @implementer(ISourcePackageName)
 class SourcePackageName(SQLBase):
     _table = 'SourcePackageName'
@@ -45,7 +47,7 @@ class SourcePackageName(SQLBase):
     packagings = SQLMultipleJoin(
         'Packaging', joinColumn='sourcepackagename', orderBy='Packaging.id')
 
-    def __unicode__(self):
+    def __str__(self):
         return self.name
 
     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
 
 from lazr.config import as_timedelta
 import pytz
+import six
 from sqlobject import (
     BoolCol,
     ForeignKey,
@@ -484,6 +485,7 @@ class MessageSet:
         return message
 
 
+@six.python_2_unicode_compatible
 @implementer(IMessageChunk)
 class MessageChunk(SQLBase):
     """One part of a possibly multipart Message"""
@@ -502,7 +504,7 @@ class MessageChunk(SQLBase):
         foreignKey='LibraryFileAlias', dbName='blob', notNull=False,
         default=None)
 
-    def __unicode__(self):
+    def __str__(self):
         """Return a text representation of this chunk.
 
         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
 from lazr.restful.tales import WebLayerAPI
 from lazr.restful.utils import get_current_browser_request
 import simplejson
+import six
 from six.moves import http_client
 from six.moves.urllib.parse import urlparse
 from zope.app.publisher.xmlrpc import IMethodPublisher
@@ -629,9 +630,10 @@ class CanonicalAbsoluteURL:
         self.context = context
         self.request = request
 
-    def __unicode__(self):
-        """Returns the URL as a unicode string."""
-        raise NotImplementedError()
+    if six.PY2:
+        def __unicode__(self):
+            """Returns the URL as a unicode string."""
+            raise NotImplementedError()
 
     def __str__(self):
         """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__ = [
 from io import BytesIO
 
 from lazr.restful.interfaces import IFieldMarshaller
+import six
 from zope.component import getMultiAdapter
 from zope.interface import implementer
 from zope.traversing.browser.interfaces import IAbsoluteURL
@@ -42,9 +43,10 @@ class WebhookAbsoluteURL:
         self.context = context
         self.request = request
 
-    def __unicode__(self):
-        """Returns the URL as a unicode string."""
-        raise NotImplementedError()
+    if six.PY2:
+        def __unicode__(self):
+            """Returns the URL as a unicode string."""
+            raise NotImplementedError()
 
     def __str__(self):
         """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__ = [
     'IBinaryPackageNameSet',
     ]
 
+import six
 from zope.interface import Interface
 from zope.schema import (
     Int,
@@ -27,9 +28,13 @@ class IBinaryPackageName(Interface):
     name = TextLine(title=_('Valid Binary package name'),
                     required=True, constraint=name_validator)
 
-    def __unicode__():
+    def __str__():
         """Return the name"""
 
+    if six.PY2:
+        def __unicode__():
+            """Return the name"""
+
 
 class IBinaryPackageNameSet(Interface):
 
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__ = [
     'BinaryPackageNameSet',
     ]
 
+import six
 from sqlobject import (
     SQLObjectNotFound,
     StringCol,
@@ -26,13 +27,14 @@ from lp.soyuz.interfaces.binarypackagename import (
 from lp.soyuz.interfaces.publishing import active_publishing_status
 
 
+@six.python_2_unicode_compatible
 @implementer(IBinaryPackageName)
 class BinaryPackageName(SQLBase):
     _table = 'BinaryPackageName'
     name = StringCol(dbName='name', notNull=True, unique=True,
                      alternateID=True)
 
-    def __unicode__(self):
+    def __str__(self):
         return self.name
 
     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 (
     )
 
 
+@six.python_2_unicode_compatible
 class POSyntaxWarning(Warning):
     """Syntax warning in a PO file."""
     def __init__(self, message, line_number=None):
@@ -62,8 +63,8 @@ class POSyntaxWarning(Warning):
             self.message = message
         logging.info(self.message)
 
-    def __unicode__(self):
-        return unicode(self.message)
+    def __str__(self):
+        return six.ensure_text(self.message)
 
 
 def parse_charset(string_to_parse, is_escaped=True):