Merge lp:~cjwatson/storm/py3-bool into lp:storm

Proposed by Colin Watson
Status: Merged
Merged at revision: 518
Proposed branch: lp:~cjwatson/storm/py3-bool
Merge into: lp:storm
Diff against target: 176 lines (+37/-13)
5 files modified
storm/databases/__init__.py (+6/-1)
storm/sqlobject.py (+7/-2)
storm/zope/interfaces.py (+6/-2)
tests/mocker.py (+8/-4)
tests/sqlobject.py (+10/-4)
To merge this branch: bzr merge lp:~cjwatson/storm/py3-bool
Reviewer Review Type Date Requested Status
Kristian Glass (community) Approve
Storm Developers Pending
Review via email: mp+371159@code.launchpad.net

Commit message

Handle Python 3's renaming of __nonzero__ to __bool__.

To post a comment you must log in.
Revision history for this message
Kristian Glass (doismellburning) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'storm/databases/__init__.py'
--- storm/databases/__init__.py 2019-06-05 11:41:07 +0000
+++ storm/databases/__init__.py 2019-08-11 09:08:59 +0000
@@ -21,6 +21,8 @@
2121
22from __future__ import print_function22from __future__ import print_function
2323
24import six
25
2426
25class Dummy(object):27class Dummy(object):
26 """Magic "infectious" class.28 """Magic "infectious" class.
@@ -38,7 +40,10 @@
38 def __add__(self, other):40 def __add__(self, other):
39 return self41 return self
4042
41 def __nonzero__(self):43 def __bool__(self):
42 return False44 return False
4345
46 if six.PY2:
47 __nonzero__ = __bool__
48
44dummy = Dummy()49dummy = Dummy()
4550
=== modified file 'storm/sqlobject.py'
--- storm/sqlobject.py 2019-06-05 11:41:07 +0000
+++ storm/sqlobject.py 2019-08-11 09:08:59 +0000
@@ -28,6 +28,8 @@
28import re28import re
29import warnings29import warnings
3030
31import six
32
31from storm.properties import (33from storm.properties import (
32 RawStr, Int, Bool, Float, DateTime, Date, TimeDelta)34 RawStr, Int, Bool, Float, DateTime, Date, TimeDelta)
33from storm.references import Reference, ReferenceSet35from storm.references import Reference, ReferenceSet
@@ -550,15 +552,18 @@
550 result_set = self._without_prejoins()._result_set552 result_set = self._without_prejoins()._result_set
551 return item in result_set553 return item in result_set
552554
553 def __nonzero__(self):555 def __bool__(self):
554 """Return C{True} if this result set contains any results.556 """Return C{True} if this result set contains any results.
555557
556 @note: This method is provided for compatibility with SQL Object. For558 @note: This method is provided for compatibility with SQL Object. For
557 new code, prefer L{is_empty}. It's compatible with L{ResultSet}559 new code, prefer L{is_empty}. It's compatible with L{ResultSet}
558 which doesn't have a C{__nonzero__} implementation.560 which doesn't have a C{__bool__} implementation.
559 """561 """
560 return not self.is_empty()562 return not self.is_empty()
561563
564 if six.PY2:
565 __nonzero__ = __bool__
566
562 def is_empty(self):567 def is_empty(self):
563 """Return C{True} if this result set doesn't contain any results."""568 """Return C{True} if this result set doesn't contain any results."""
564 result_set = self._without_prejoins()._result_set569 result_set = self._without_prejoins()._result_set
565570
=== modified file 'storm/zope/interfaces.py'
--- storm/zope/interfaces.py 2019-06-05 11:41:07 +0000
+++ storm/zope/interfaces.py 2019-08-11 09:08:59 +0000
@@ -20,6 +20,7 @@
20#20#
21from __future__ import print_function21from __future__ import print_function
2222
23import six
23from zope.interface import Interface24from zope.interface import Interface
2425
25from storm.expr import Undef26from storm.expr import Undef
@@ -165,14 +166,17 @@
165 def count():166 def count():
166 """Return the number of items in the result set."""167 """Return the number of items in the result set."""
167168
168 def __nonzero__():169 def __bool__():
169 """Return C{True} if this result set contains any results.170 """Return C{True} if this result set contains any results.
170171
171 @note: This method is provided for compatibility with SQL Object. For172 @note: This method is provided for compatibility with SQL Object. For
172 new code, prefer L{is_empty}. It's compatible with L{ResultSet}173 new code, prefer L{is_empty}. It's compatible with L{ResultSet}
173 which doesn't have a C{__nonzero__} implementation.174 which doesn't have a C{__bool__} implementation.
174 """175 """
175176
177 if six.PY2:
178 __nonzero__ = __bool__
179
176 def __contains__(item):180 def __contains__(item):
177 """Support C{if FooObject in Foo.select(query)}."""181 """Support C{if FooObject in Foo.select(query)}."""
178182
179183
=== modified file 'tests/mocker.py'
--- tests/mocker.py 2019-06-07 17:14:33 +0000
+++ tests/mocker.py 2019-08-11 09:08:59 +0000
@@ -13,6 +13,7 @@
13import os13import os
14import gc14import gc
1515
16import six
16from six.moves import builtins17from six.moves import builtins
1718
1819
@@ -1083,12 +1084,15 @@
1083 return 01084 return 0
1084 return result1085 return result
10851086
1086 def __nonzero__(self):1087 def __bool__(self):
1087 try:1088 try:
1088 return self.__mocker_act__("nonzero")1089 return self.__mocker_act__("bool")
1089 except MatchError as e:1090 except MatchError as e:
1090 return True1091 return True
10911092
1093 if six.PY2:
1094 __nonzero__ = __bool__
1095
1092 def __iter__(self):1096 def __iter__(self):
1093 # XXX On py3k, when next() becomes __next__(), we'll be able1097 # XXX On py3k, when next() becomes __next__(), we'll be able
1094 # to return the mock itself because it will be considered1098 # to return the mock itself because it will be considered
@@ -1187,7 +1191,7 @@
1187 result = None1191 result = None
1188 elif kind == "len":1192 elif kind == "len":
1189 result = len(object)1193 result = len(object)
1190 elif kind == "nonzero":1194 elif kind == "bool":
1191 result = bool(object)1195 result = bool(object)
1192 elif kind == "iter":1196 elif kind == "iter":
1193 result = iter(object)1197 result = iter(object)
@@ -1280,7 +1284,7 @@
1280 result = "del %s[%r]" % (result, action.args[0])1284 result = "del %s[%r]" % (result, action.args[0])
1281 elif action.kind == "len":1285 elif action.kind == "len":
1282 result = "len(%s)" % result1286 result = "len(%s)" % result
1283 elif action.kind == "nonzero":1287 elif action.kind == "bool":
1284 result = "bool(%s)" % result1288 result = "bool(%s)" % result
1285 elif action.kind == "iter":1289 elif action.kind == "iter":
1286 result = "iter(%s)" % result1290 result = "iter(%s)" % result
12871291
=== modified file 'tests/sqlobject.py'
--- tests/sqlobject.py 2019-06-05 11:41:07 +0000
+++ tests/sqlobject.py 2019-08-11 09:08:59 +0000
@@ -23,6 +23,8 @@
23import datetime23import datetime
24import operator24import operator
2525
26import six
27
26from storm.database import create_database28from storm.database import create_database
27from storm.exceptions import NoneError29from storm.exceptions import NoneError
28from storm.sqlobject import *30from storm.sqlobject import *
@@ -813,16 +815,20 @@
813 result = self.Person.select()815 result = self.Person.select()
814 self.assertEquals(list(result.__iter__())[0].name, "John Joe")816 self.assertEquals(list(result.__iter__())[0].name, "John Joe")
815817
816 def test_result_set__nonzero__(self):818 def test_result_set__bool__(self):
817 """819 """
818 L{SQLObjectResultSet.__nonzero__} returns C{True} if the result set820 L{SQLObjectResultSet.__bool__} returns C{True} if the result set
819 contains results. If it contains no results, C{False} is821 contains results. If it contains no results, C{False} is
820 returned.822 returned.
821 """823 """
822 result = self.Person.select()824 result = self.Person.select()
823 self.assertEquals(result.__nonzero__(), True)825 self.assertEquals(result.__bool__(), True)
826 if six.PY2:
827 self.assertEquals(result.__nonzero__(), True)
824 result = self.Person.select(self.Person.q.name == "No Person")828 result = self.Person.select(self.Person.q.name == "No Person")
825 self.assertEquals(result.__nonzero__(), False)829 self.assertEquals(result.__bool__(), False)
830 if six.PY2:
831 self.assertEquals(result.__nonzero__(), False)
826832
827 def test_result_set_is_empty(self):833 def test_result_set_is_empty(self):
828 """834 """

Subscribers

People subscribed via source and target branches

to status/vote changes: