← Back to team overview

launchpad-reviewers team mailing list archive

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

 

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

Commit message:
Remove deprecated ensure_unicode function

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

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

This is equivalent to `six.ensure_text` (strictly with `encoding='ASCII'`, although that doesn't seem necessary in search methods) if the input string is not None.  It may be that we could get away without some of these `six.ensure_text` calls, but let's just keep it simple for now; we can drop them more easily once we're on Python 3 everywhere.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:py3-remove-ensure-unicode into launchpad:master.
diff --git a/lib/lp/registry/model/sourcepackagename.py b/lib/lp/registry/model/sourcepackagename.py
index 9d3615f..49e6335 100644
--- a/lib/lp/registry/model/sourcepackagename.py
+++ b/lib/lp/registry/model/sourcepackagename.py
@@ -31,7 +31,6 @@ from lp.services.database.sqlbase import (
     SQLBase,
     sqlvalues,
     )
-from lp.services.helpers import ensure_unicode
 
 
 @six.python_2_unicode_compatible
@@ -66,7 +65,7 @@ class SourcePackageNameSet:
 
     def __getitem__(self, name):
         """See `ISourcePackageNameSet`."""
-        name = ensure_unicode(name)
+        name = six.ensure_text(name, 'ASCII')
         try:
             return SourcePackageName.byName(name)
         except SQLObjectNotFound:
diff --git a/lib/lp/registry/vocabularies.py b/lib/lp/registry/vocabularies.py
index 59242f0..952899d 100644
--- a/lib/lp/registry/vocabularies.py
+++ b/lib/lp/registry/vocabularies.py
@@ -185,10 +185,7 @@ from lp.services.database.stormexpr import (
     fti_search,
     RegexpMatch,
     )
-from lp.services.helpers import (
-    ensure_unicode,
-    shortlist,
-    )
+from lp.services.helpers import shortlist
 from lp.services.identity.interfaces.account import AccountStatus
 from lp.services.identity.interfaces.emailaddress import (
     EmailAddressStatus,
@@ -1191,7 +1188,7 @@ class ProductReleaseVocabulary(SQLObjectVocabularyBase):
         if not query:
             return self.emptySelectResults()
 
-        query = ensure_unicode(query).lower()
+        query = six.ensure_text(query).lower()
         objs = self._table.select(
             AND(
                 Milestone.q.id == ProductRelease.q.milestoneID,
@@ -1248,7 +1245,7 @@ class ProductSeriesVocabulary(SQLObjectVocabularyBase):
             return self.emptySelectResults()
         user = getUtility(ILaunchBag).user
         privacy_filter = ProductSet.getProductPrivacyFilter(user)
-        query = ensure_unicode(query).lower().strip('/')
+        query = six.ensure_text(query).lower().strip('/')
         # If there is a slash splitting the product and productseries
         # names, they must both match. If there is no slash, we don't
         # know whether it is matching the product or the productseries
@@ -1582,7 +1579,7 @@ class DistroSeriesVocabulary(NamedSQLObjectVocabulary):
         if not query:
             return self.emptySelectResults()
 
-        query = ensure_unicode(query).lower()
+        query = six.ensure_text(query).lower()
         objs = self._table.select(
                 AND(
                     Distribution.q.id == DistroSeries.q.distributionID,
@@ -1991,7 +1988,7 @@ class SourcePackageNameVocabulary(NamedStormHugeVocabulary):
         if not query:
             return self.emptySelectResults()
 
-        query = ensure_unicode(query).lower()
+        query = six.ensure_text(query).lower()
         results = IStore(self._table).find(
             self._table,
             Or(
diff --git a/lib/lp/services/helpers.py b/lib/lp/services/helpers.py
index f676e61..66070c1 100644
--- a/lib/lp/services/helpers.py
+++ b/lib/lp/services/helpers.py
@@ -322,48 +322,3 @@ def english_list(items, conjunction='and'):
     else:
         items[-1] = '%s %s' % (conjunction, items[-1])
         return ', '.join(items)
-
-
-def ensure_unicode(string):
-    r"""Return input as unicode. None is passed through unharmed.
-
-    Do not use this method. This method exists only to help migration
-    of legacy code where str objects were being passed into contexts
-    where unicode objects are required. All invokations of
-    ensure_unicode() should eventually be removed.
-
-    This differs from the builtin unicode() function, as a TypeError
-    exception will be raised if the parameter is not a string type or if
-    a raw string is not ASCII.
-
-    >>> ensure_unicode(u'hello')
-    u'hello'
-
-    >>> ensure_unicode('hello')
-    u'hello'
-
-    >>> ensure_unicode(u'A'.encode('utf-16')) # Not ASCII
-    Traceback (most recent call last):
-    ...
-    TypeError: '\xff\xfeA\x00' is not US-ASCII
-
-    >>> ensure_unicode(42)
-    Traceback (most recent call last):
-    ...
-    TypeError: 42 is not a string type (<type 'int'>)
-
-    >>> ensure_unicode(None) is None
-    True
-    """
-    if string is None:
-        return None
-    elif isinstance(string, six.text_type):
-        return string
-    elif isinstance(string, bytes):
-        try:
-            return string.decode('US-ASCII')
-        except UnicodeDecodeError:
-            raise TypeError("%s is not US-ASCII" % repr(string))
-    else:
-        raise TypeError(
-            "%r is not a string type (%r)" % (string, type(string)))
diff --git a/lib/lp/services/webapp/vocabulary.py b/lib/lp/services/webapp/vocabulary.py
index b77961f..a75c7c5 100644
--- a/lib/lp/services/webapp/vocabulary.py
+++ b/lib/lp/services/webapp/vocabulary.py
@@ -50,7 +50,6 @@ from zope.security.proxy import isinstance as zisinstance
 
 from lp.services.database.interfaces import IStore
 from lp.services.database.sqlbase import SQLBase
-from lp.services.helpers import ensure_unicode
 
 
 class ForgivingSimpleVocabulary(SimpleVocabulary):
@@ -428,7 +427,7 @@ class NamedSQLObjectVocabulary(SQLObjectVocabularyBase):
     def search(self, query, vocab_filter=None):
         """Return terms where query is a subtring of the name."""
         if query:
-            clause = CONTAINSSTRING(self._table.q.name, ensure_unicode(query))
+            clause = CONTAINSSTRING(self._table.q.name, six.ensure_text(query))
             if self._filter:
                 clause = AND(clause, self._filter)
             return self._table.select(clause, orderBy=self._orderBy)
@@ -569,7 +568,7 @@ class NamedStormVocabulary(StormVocabularyBase):
         if not query:
             return self.emptySelectResults()
 
-        query = ensure_unicode(query).lower()
+        query = six.ensure_text(query).lower()
         results = IStore(self._table).find(
             self._table,
             self._table.name.contains_string(query),
diff --git a/lib/lp/services/worlddata/model/language.py b/lib/lp/services/worlddata/model/language.py
index 258fd1f..8edad4f 100644
--- a/lib/lp/services/worlddata/model/language.py
+++ b/lib/lp/services/worlddata/model/language.py
@@ -40,7 +40,6 @@ from lp.services.database.interfaces import (
     IStore,
     )
 from lp.services.database.sqlbase import SQLBase
-from lp.services.helpers import ensure_unicode
 from lp.services.propertycache import (
     cachedproperty,
     get_property_cache,
@@ -295,7 +294,7 @@ class LanguageSet:
     def search(self, text):
         """See `ILanguageSet`."""
         if text:
-            text = ensure_unicode(text).lower()
+            text = six.ensure_text(text).lower()
             results = ISlaveStore(Language).find(
                 Language, Or(
                     Language.code.lower().contains_string(text),
diff --git a/lib/lp/soyuz/model/binarypackagename.py b/lib/lp/soyuz/model/binarypackagename.py
index de0f71a..7367dee 100644
--- a/lib/lp/soyuz/model/binarypackagename.py
+++ b/lib/lp/soyuz/model/binarypackagename.py
@@ -19,7 +19,6 @@ from zope.interface import implementer
 from lp.app.errors import NotFoundError
 from lp.services.database.interfaces import IStore
 from lp.services.database.sqlbase import SQLBase
-from lp.services.helpers import ensure_unicode
 from lp.soyuz.interfaces.binarypackagename import (
     IBinaryPackageName,
     IBinaryPackageNameSet,
@@ -57,10 +56,10 @@ class BinaryPackageNameSet:
 
     def queryByName(self, name):
         return IStore(BinaryPackageName).find(
-            BinaryPackageName, name=ensure_unicode(name)).one()
+            BinaryPackageName, name=six.ensure_text(name, 'ASCII')).one()
 
     def new(self, name):
-        return BinaryPackageName(name=ensure_unicode(name))
+        return BinaryPackageName(name=six.ensure_text(name, 'ASCII'))
 
     def ensure(self, name):
         """Ensure that the given BinaryPackageName exists, creating it
@@ -68,7 +67,7 @@ class BinaryPackageNameSet:
 
         Returns the BinaryPackageName
         """
-        name = ensure_unicode(name)
+        name = six.ensure_text(name, 'ASCII')
         try:
             return self[name]
         except NotFoundError:
diff --git a/lib/lp/soyuz/model/packageset.py b/lib/lp/soyuz/model/packageset.py
index 7bf5204..bb17ad8 100644
--- a/lib/lp/soyuz/model/packageset.py
+++ b/lib/lp/soyuz/model/packageset.py
@@ -26,7 +26,6 @@ from lp.services.database.interfaces import (
     IMasterStore,
     IStore,
     )
-from lp.services.helpers import ensure_unicode
 from lp.soyuz.interfaces.packageset import (
     DuplicatePackagesetName,
     IPackageset,
@@ -302,7 +301,7 @@ class Packageset(Storm):
     def addSources(self, names):
         """See `IPackageset`."""
         if isinstance(names, six.string_types):
-            names = [ensure_unicode(names)]
+            names = [six.ensure_text(names, 'ASCII')]
         clauses = (SourcePackageName, SourcePackageName.name.is_in(names))
         self._api_add_or_remove(clauses, self._addSourcePackageNames)
 
diff --git a/lib/lp/soyuz/scripts/initialize_distroseries.py b/lib/lp/soyuz/scripts/initialize_distroseries.py
index 60c0bc8..2f64d2b 100644
--- a/lib/lp/soyuz/scripts/initialize_distroseries.py
+++ b/lib/lp/soyuz/scripts/initialize_distroseries.py
@@ -12,6 +12,7 @@ __all__ = [
 from collections import OrderedDict
 from operator import methodcaller
 
+import six
 import transaction
 from zope.component import getUtility
 
@@ -24,7 +25,6 @@ from lp.registry.model.distroseries import DistroSeries
 from lp.services.database import bulk
 from lp.services.database.interfaces import IMasterStore
 from lp.services.database.sqlbase import sqlvalues
-from lp.services.helpers import ensure_unicode
 from lp.services.scripts import log
 from lp.soyuz.adapters.packagelocation import PackageLocation
 from lp.soyuz.enums import (
@@ -125,7 +125,8 @@ class InitializeDistroSeries:
             self.packagesets = None
         else:
             self.packagesets_ids = [
-                ensure_unicode(packageset) for packageset in packagesets]
+                six.ensure_text(packageset, 'ASCII')
+                for packageset in packagesets]
             self.packagesets = bulk.load(
                 Packageset, [int(packageset) for packageset in packagesets])
         self.rebuild = rebuild