← Back to team overview

launchpad-reviewers team mailing list archive

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

 

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

Commit message:
Handle Python 3's renaming of __nonzero__ to __bool__

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/396106
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:py3-bool into launchpad:master.
diff --git a/lib/lp/app/security.py b/lib/lp/app/security.py
index 7e85825..6fae684 100644
--- a/lib/lp/app/security.py
+++ b/lib/lp/app/security.py
@@ -13,6 +13,7 @@ __all__ = [
 
 from itertools import repeat
 
+import six
 from six.moves import zip as izip
 from zope.component import queryAdapter
 from zope.interface import implementer
@@ -119,7 +120,7 @@ class AnonymousAuthorization(AuthorizationBase):
 
 class non_boolean_izip(izip):
 
-    def __nonzero__(self):
+    def __bool__(self):
         # XXX wgrant 2016-11-15: Guard against security adapters
         # accidentally using a delegation as a boolean authentication
         # result.
@@ -127,6 +128,9 @@ class non_boolean_izip(izip):
             "DelegatedAuthorization results can't be used in boolean "
             "expressions.")
 
+    if six.PY2:
+        __nonzero__ = __bool__
+
 
 class DelegatedAuthorization(AuthorizationBase):
 
diff --git a/lib/lp/bugs/mail/handler.py b/lib/lp/bugs/mail/handler.py
index 8742e3e..1087fca 100644
--- a/lib/lp/bugs/mail/handler.py
+++ b/lib/lp/bugs/mail/handler.py
@@ -13,6 +13,7 @@ import os
 
 from lazr.lifecycle.event import ObjectCreatedEvent
 from lazr.lifecycle.interfaces import IObjectCreatedEvent
+import six
 import transaction
 from zope.component import getUtility
 from zope.event import notify
@@ -64,9 +65,12 @@ class BugTaskCommandGroup:
         if command is not None:
             self._commands.append(command)
 
-    def __nonzero__(self):
+    def __bool__(self):
         return len(self._commands) > 0
 
+    if six.PY2:
+        __nonzero__ = __bool__
+
     def __str__(self):
         text_commands = [str(cmd) for cmd in self.commands]
         return '\n'.join(text_commands).strip()
@@ -87,11 +91,14 @@ class BugCommandGroup(BugTaskCommandGroup):
         super(BugCommandGroup, self).__init__(command=command)
         self._groups = []
 
-    def __nonzero__(self):
+    def __bool__(self):
         if len(self._groups) > 0:
             return True
         else:
-            return super(BugCommandGroup, self).__nonzero__()
+            return super(BugCommandGroup, self).__bool__()
+
+    if six.PY2:
+        __nonzero__ = __bool__
 
     def __str__(self):
         text_commands = [super(BugCommandGroup, self).__str__()]
diff --git a/lib/lp/bugs/mail/tests/test_handler.py b/lib/lp/bugs/mail/tests/test_handler.py
index c5af232..90a9e4e 100644
--- a/lib/lp/bugs/mail/tests/test_handler.py
+++ b/lib/lp/bugs/mail/tests/test_handler.py
@@ -379,14 +379,14 @@ class BugTaskCommandGroupTestCase(TestCase):
         self.assertEqual(
             [command_1, command_2, command_3], group.commands)
 
-    def test_BugTaskCommandGroup__nonzero__false(self):
-        # A BugTaskCommandGroup is zero is it has no commands.
+    def test_BugTaskCommandGroup__bool__false(self):
+        # A BugTaskCommandGroup is false if it has no commands.
         group = BugTaskCommandGroup()
         self.assertEqual(0, len(group._commands))
         self.assertFalse(bool(group))
 
-    def test_BugTaskCommandGroup__nonzero__true(self):
-        # A BugTaskCommandGroup is non-zero is it has commands.
+    def test_BugTaskCommandGroup__bool__true(self):
+        # A BugTaskCommandGroup is true if it has commands.
         group = BugTaskCommandGroup(
             BugEmailCommands.get('affects', ['fnord']))
         self.assertEqual(1, len(group._commands))
@@ -470,23 +470,23 @@ class BugCommandGroupTestCase(TestCase):
         self.assertEqual(
             [affects_command, status_command], group.groups[0].commands)
 
-    def test_BugCommandGroup__nonzero__false(self):
-        # A BugCommandGroup is zero is it has no commands or groups.
+    def test_BugCommandGroup__bool__false(self):
+        # A BugCommandGroup is false if it has no commands or groups.
         group = BugCommandGroup()
         self.assertEqual(0, len(group._commands))
         self.assertEqual(0, len(group._groups))
         self.assertFalse(bool(group))
 
-    def test_BugCommandGroup__nonzero__true_commands(self):
-        # A BugCommandGroup is not zero is it has a command.
+    def test_BugCommandGroup__bool__true_commands(self):
+        # A BugCommandGroup is true if it has a command.
         group = BugCommandGroup(
             BugEmailCommands.get('private', ['true']))
         self.assertEqual(1, len(group._commands))
         self.assertEqual(0, len(group._groups))
         self.assertTrue(bool(group))
 
-    def test_BugCommandGroup__nonzero__true_groups(self):
-        # A BugCommandGroup is not zero is it has a group.
+    def test_BugCommandGroup__bool__true_groups(self):
+        # A BugCommandGroup is true if it has a group.
         group = BugCommandGroup()
         group.add(BugTaskCommandGroup(
             BugEmailCommands.get('affects', ['fnord'])))
diff --git a/lib/lp/registry/browser/person.py b/lib/lp/registry/browser/person.py
index 7e50377..27fdd52 100644
--- a/lib/lp/registry/browser/person.py
+++ b/lib/lp/registry/browser/person.py
@@ -4279,10 +4279,13 @@ class ContactViaWebNotificationRecipientSet:
                 self._count_recipients = 0
         return self._count_recipients
 
-    def __nonzero__(self):
+    def __bool__(self):
         """See `INotificationRecipientSet`."""
         return len(self) > 0
 
+    if six.PY2:
+        __nonzero__ = __bool__
+
     def getReason(self, person_or_email):
         """See `INotificationRecipientSet`."""
         if person_or_email not in self:
diff --git a/lib/lp/services/mail/interfaces.py b/lib/lp/services/mail/interfaces.py
index 9c7db64..cca09df 100644
--- a/lib/lp/services/mail/interfaces.py
+++ b/lib/lp/services/mail/interfaces.py
@@ -19,6 +19,7 @@ __all__ = [
     'UnknownRecipientError',
     ]
 
+import six
 from zope.interface import (
     Attribute,
     Interface,
@@ -152,9 +153,12 @@ class INotificationRecipientSet(Interface):
         Return true if person or email is in the notification recipients list.
         """
 
-    def __nonzero__():
+    def __bool__():
         """Return False when the set is empty, True when it's not."""
 
+    if six.PY2:
+        __nonzero__ = __bool__
+
     def getReason(person_or_email):
         """Return a reason tuple containing (text, header) for an address.
 
diff --git a/lib/lp/services/mail/notificationrecipientset.py b/lib/lp/services/mail/notificationrecipientset.py
index d08a804..7297da2 100644
--- a/lib/lp/services/mail/notificationrecipientset.py
+++ b/lib/lp/services/mail/notificationrecipientset.py
@@ -81,10 +81,13 @@ class NotificationRecipientSet:
         else:
             return False
 
-    def __nonzero__(self):
+    def __bool__(self):
         """See `INotificationRecipientSet`."""
         return bool(self._personToRationale)
 
+    if six.PY2:
+        __nonzero__ = __bool__
+
     def getReason(self, person_or_email):
         """See `INotificationRecipientSet`."""
         if zope_isinstance(person_or_email, six.string_types):
diff --git a/lib/lp/services/webapp/servers.py b/lib/lp/services/webapp/servers.py
index 9c47f21..d7bcad2 100644
--- a/lib/lp/services/webapp/servers.py
+++ b/lib/lp/services/webapp/servers.py
@@ -210,9 +210,12 @@ class StepsToGo(six.Iterator):
     def __len__(self):
         return len(self._stack)
 
-    def __nonzero__(self):
+    def __bool__(self):
         return bool(self._stack)
 
+    if six.PY2:
+        __nonzero__ = __bool__
+
 
 class ApplicationServerSettingRequestFactory:
     """Create a request and call its setApplicationServer method.
diff --git a/lib/lp/soyuz/adapters/archivesourcepublication.py b/lib/lp/soyuz/adapters/archivesourcepublication.py
index 2f25a2a..34e4919 100644
--- a/lib/lp/soyuz/adapters/archivesourcepublication.py
+++ b/lib/lp/soyuz/adapters/archivesourcepublication.py
@@ -17,6 +17,7 @@ __all__ = [
 from collections import defaultdict
 
 from lazr.delegates import delegate_to
+import six
 from zope.component import getUtility
 
 from lp.registry.model.distroseries import DistroSeries
@@ -99,10 +100,13 @@ class ArchiveSourcePublications:
             changesfile_mapping[source] = changesfile
         return changesfile_mapping
 
-    def __nonzero__(self):
+    def __bool__(self):
         """Are there any sources to iterate?"""
         return self.has_sources
 
+    if six.PY2:
+        __nonzero__ = __bool__
+
     def __iter__(self):
         """`ArchiveSourcePublication` iterator."""
         results = []
diff --git a/lib/lp/soyuz/doc/publishing.txt b/lib/lp/soyuz/doc/publishing.txt
index a2c3b3f..512b2a6 100644
--- a/lib/lp/soyuz/doc/publishing.txt
+++ b/lib/lp/soyuz/doc/publishing.txt
@@ -1335,7 +1335,7 @@ We use the source publications to initialize
     >>> decorated_set = ArchiveSourcePublications(cprov_published_sources)
     >>> empty_decorated_set = ArchiveSourcePublications([])
 
-`ArchiveSourcePublications` implements __nonzero__, so callsites can
+`ArchiveSourcePublications` implements __bool__, so callsites can
 verify in advance whether there are elements to be iterated or not.
 
     >>> bool(decorated_set)
diff --git a/lib/lp/translations/model/translatedlanguage.py b/lib/lp/translations/model/translatedlanguage.py
index 69298a5..e9d120a 100644
--- a/lib/lp/translations/model/translatedlanguage.py
+++ b/lib/lp/translations/model/translatedlanguage.py
@@ -4,6 +4,7 @@
 __all__ = ['TranslatedLanguageMixin']
 
 import pytz
+import six
 from storm.expr import (
     Coalesce,
     Desc,
@@ -71,9 +72,12 @@ class POFilesByPOTemplates(object):
     def __len__(self):
         return self.templates_collection.select(POTemplate).count()
 
-    def __nonzero__(self):
+    def __bool__(self):
         return bool(self.templates_collection.select(POTemplate).any())
 
+    if six.PY2:
+        __nonzero__ = __bool__
+
 
 @implementer(ITranslatedLanguage)
 class TranslatedLanguageMixin(object):