← Back to team overview

launchpad-reviewers team mailing list archive

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

 

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

Commit message:
Port unicode() calls in lp.translations to Python 3

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

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

This unfortunately requires some contextual clues, because `six.text_type(b'foo')` returns `u'foo'` on Python 2 but `"b'foo'"` on Python 3, while `six.ensure_text` works on bytes or text but not on other types.  Use single-argument `six.text_type` in cases where we know that the argument is not bytes, `six.ensure_text` where we know the argument is either bytes or text, and something more elaborate otherwise.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:py3-translations-unicode into launchpad:master.
diff --git a/lib/lp/translations/browser/tests/distroseries-views.txt b/lib/lp/translations/browser/tests/distroseries-views.txt
index 9b028f5..086e9a2 100644
--- a/lib/lp/translations/browser/tests/distroseries-views.txt
+++ b/lib/lp/translations/browser/tests/distroseries-views.txt
@@ -3,6 +3,7 @@ DistroSeries translations view classes
 
 Let's use ubuntu/hoary for these tests.
 
+    >>> import six
     >>> from lp.services.webapp.servers import LaunchpadTestRequest
     >>> from lp.registry.interfaces.distribution import IDistributionSet
     >>> from lp.registry.interfaces.series import SeriesStatus
@@ -41,7 +42,7 @@ reveal its translations to the public or hide them from the public.
     ...     try:
     ...         view.checkTranslationsViewable()
     ...     except TranslationUnavailable as message:
-    ...         return unicode(message)
+    ...         return six.text_type(message)
     ...     return None
 
     >>> def check_effect_of_hiding(distroseries):
diff --git a/lib/lp/translations/browser/translationmessage.py b/lib/lp/translations/browser/translationmessage.py
index dbdf1d1..60bcff2 100644
--- a/lib/lp/translations/browser/translationmessage.py
+++ b/lib/lp/translations/browser/translationmessage.py
@@ -441,7 +441,7 @@ class BaseTranslationView(LaunchpadView):
         try:
             self._storeTranslations(potmsgset)
         except GettextValidationError as e:
-            return unicode(e)
+            return six.text_type(e)
         except TranslationConflict:
             # The translations are demoted to suggestions, but they may
             # still affect the "messages with new suggestions" filter.
diff --git a/lib/lp/translations/model/potemplate.py b/lib/lp/translations/model/potemplate.py
index 06bf24f..6098946 100644
--- a/lib/lp/translations/model/potemplate.py
+++ b/lib/lp/translations/model/potemplate.py
@@ -1003,7 +1003,8 @@ class POTemplate(SQLBase, RosettaStats):
                         txn.begin()
                     if logger:
                         logger.warn(
-                            "Statistics update failed: %s" % unicode(error))
+                            "Statistics update failed: %s" %
+                            six.text_type(error))
 
         if template_mail is not None:
             template = get_email_template(
diff --git a/lib/lp/translations/scripts/gettext_check_messages.py b/lib/lp/translations/scripts/gettext_check_messages.py
index bed3b6b..fa6a388 100644
--- a/lib/lp/translations/scripts/gettext_check_messages.py
+++ b/lib/lp/translations/scripts/gettext_check_messages.py
@@ -10,6 +10,7 @@ from datetime import (
     timedelta,
     )
 
+import six
 from zope.component import getUtility
 from zope.security.proxy import removeSecurityProxy
 
@@ -102,7 +103,7 @@ class GettextCheckMessages(LaunchpadScript):
                 msgstrs, potmsgset.flags)
         except GettextValidationError as error:
             self._error_count += 1
-            return unicode(error)
+            return six.text_type(error)
 
         return None
 
diff --git a/lib/lp/translations/scripts/po_import.py b/lib/lp/translations/scripts/po_import.py
index 9c9779e..48f64bb 100644
--- a/lib/lp/translations/scripts/po_import.py
+++ b/lib/lp/translations/scripts/po_import.py
@@ -78,7 +78,9 @@ class TranslationsImport(LaunchpadCronScript):
 
     def _registerFailure(self, entry, reason, traceback=False, abort=False):
         """Note that a queue entry is unusable in some way."""
-        reason_text = unicode(reason)
+        reason_text = (
+            six.ensure_text(reason) if reason is bytes
+            else six.text_type(reason))
         entry.setStatus(RosettaImportStatus.FAILED,
                         getUtility(ILaunchpadCelebrities).rosetta_experts)
         entry.setErrorOutput(reason_text)
diff --git a/lib/lp/translations/utilities/gettext_po_parser.py b/lib/lp/translations/utilities/gettext_po_parser.py
index 865d0c5..d4bdafc 100644
--- a/lib/lp/translations/utilities/gettext_po_parser.py
+++ b/lib/lp/translations/utilities/gettext_po_parser.py
@@ -152,7 +152,7 @@ class POHeader:
 
     def _emitSyntaxWarning(self, message):
         """Issue syntax warning, add to warnings list."""
-        self.syntax_warnings.append(unicode(POSyntaxWarning(message)))
+        self.syntax_warnings.append(six.text_type(POSyntaxWarning(message)))
 
     def _getHeaderDict(self, raw_header):
         """Return dictionary with all keys in raw_header.
@@ -460,7 +460,8 @@ class POParser(object):
     def _emitSyntaxWarning(self, message):
         warning = POSyntaxWarning(message, line_number=self._lineno)
         if self._translation_file:
-            self._translation_file.syntax_warnings.append(unicode(warning))
+            self._translation_file.syntax_warnings.append(
+                six.text_type(warning))
 
     def _decode(self):
         # is there anything to convert?
diff --git a/lib/lp/translations/utilities/tests/test_file_importer.py b/lib/lp/translations/utilities/tests/test_file_importer.py
index 013d13f..18aa370 100644
--- a/lib/lp/translations/utilities/tests/test_file_importer.py
+++ b/lib/lp/translations/utilities/tests/test_file_importer.py
@@ -7,6 +7,7 @@ __metaclass__ = type
 
 from textwrap import dedent
 
+import six
 import transaction
 from zope.component import getUtility
 from zope.security.proxy import removeSecurityProxy
@@ -387,7 +388,7 @@ class FileImporterTestCase(TestCaseWithFactory):
                 "POFileImporter.importFile returned errors where there "
                 "should be none.")
             potmsgset = po_importer.pofile.potemplate.getPOTMsgSetByMsgIDText(
-                                                        unicode(TEST_MSGID))
+                six.ensure_text(TEST_MSGID))
             message = potmsgset.getCurrentTranslation(
                 po_importer.potemplate, po_importer.pofile.language,
                 po_importer.potemplate.translation_side)
@@ -452,7 +453,7 @@ class FileImporterTestCase(TestCaseWithFactory):
         # Although the message has an error, it should still be stored
         # in the database, though only as a suggestion.
         potmsgset = po_importer.pofile.potemplate.getPOTMsgSetByMsgIDText(
-            unicode(TEST_MSGID_ERROR))
+            six.ensure_text(TEST_MSGID_ERROR))
         message = potmsgset.getLocalTranslationMessages(
             po_importer.potemplate, po_importer.pofile.language)[0]
         self.assertIsNotNone(message,
@@ -477,7 +478,7 @@ class FileImporterTestCase(TestCaseWithFactory):
         po_importer2.importFile()
 
         potmsgset = po_importer.pofile.potemplate.getPOTMsgSetByMsgIDText(
-            unicode(TEST_MSGID_ERROR))
+            six.ensure_text(TEST_MSGID_ERROR))
         messages = potmsgset.getLocalTranslationMessages(
             po_importer.pofile.potemplate, po_importer.pofile.language)
 
diff --git a/lib/lp/translations/utilities/translation_import.py b/lib/lp/translations/utilities/translation_import.py
index f9af932..600962a 100644
--- a/lib/lp/translations/utilities/translation_import.py
+++ b/lib/lp/translations/utilities/translation_import.py
@@ -466,7 +466,7 @@ class FileImporter(object):
                 potmsgset.singular_text, potmsgset.plural_text,
                 translations, potmsgset.flags)
         except GettextValidationError as e:
-            self._addUpdateError(message_data, potmsgset, unicode(e))
+            self._addUpdateError(message_data, potmsgset, six.text_type(e))
             message.validation_status = (
                 TranslationValidationStatus.UNKNOWNERROR)
             return False
@@ -603,7 +603,7 @@ class FileImporter(object):
             'pofile': self.pofile,
             'pomessage': self.format_exporter.exportTranslationMessageData(
                 message),
-            'error-message': unicode(errormsg),
+            'error-message': six.ensure_text(errormsg),
         })
 
     def _addConflictError(self, message, potmsgset):
diff --git a/lib/lp/translations/utilities/validate.py b/lib/lp/translations/utilities/validate.py
index 371a060..956d9b6 100644
--- a/lib/lp/translations/utilities/validate.py
+++ b/lib/lp/translations/utilities/validate.py
@@ -49,4 +49,4 @@ def validate_translation(original_singular, original_plural,
         msg.check_format()
     except gettextpo.error as e:
         # Wrap gettextpo.error in GettextValidationError.
-        raise GettextValidationError(unicode(e))
+        raise GettextValidationError(six.text_type(e))