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
1=== modified file 'storm/databases/__init__.py'
2--- storm/databases/__init__.py 2019-06-05 11:41:07 +0000
3+++ storm/databases/__init__.py 2019-08-11 09:08:59 +0000
4@@ -21,6 +21,8 @@
5
6 from __future__ import print_function
7
8+import six
9+
10
11 class Dummy(object):
12 """Magic "infectious" class.
13@@ -38,7 +40,10 @@
14 def __add__(self, other):
15 return self
16
17- def __nonzero__(self):
18+ def __bool__(self):
19 return False
20
21+ if six.PY2:
22+ __nonzero__ = __bool__
23+
24 dummy = Dummy()
25
26=== modified file 'storm/sqlobject.py'
27--- storm/sqlobject.py 2019-06-05 11:41:07 +0000
28+++ storm/sqlobject.py 2019-08-11 09:08:59 +0000
29@@ -28,6 +28,8 @@
30 import re
31 import warnings
32
33+import six
34+
35 from storm.properties import (
36 RawStr, Int, Bool, Float, DateTime, Date, TimeDelta)
37 from storm.references import Reference, ReferenceSet
38@@ -550,15 +552,18 @@
39 result_set = self._without_prejoins()._result_set
40 return item in result_set
41
42- def __nonzero__(self):
43+ def __bool__(self):
44 """Return C{True} if this result set contains any results.
45
46 @note: This method is provided for compatibility with SQL Object. For
47 new code, prefer L{is_empty}. It's compatible with L{ResultSet}
48- which doesn't have a C{__nonzero__} implementation.
49+ which doesn't have a C{__bool__} implementation.
50 """
51 return not self.is_empty()
52
53+ if six.PY2:
54+ __nonzero__ = __bool__
55+
56 def is_empty(self):
57 """Return C{True} if this result set doesn't contain any results."""
58 result_set = self._without_prejoins()._result_set
59
60=== modified file 'storm/zope/interfaces.py'
61--- storm/zope/interfaces.py 2019-06-05 11:41:07 +0000
62+++ storm/zope/interfaces.py 2019-08-11 09:08:59 +0000
63@@ -20,6 +20,7 @@
64 #
65 from __future__ import print_function
66
67+import six
68 from zope.interface import Interface
69
70 from storm.expr import Undef
71@@ -165,14 +166,17 @@
72 def count():
73 """Return the number of items in the result set."""
74
75- def __nonzero__():
76+ def __bool__():
77 """Return C{True} if this result set contains any results.
78
79 @note: This method is provided for compatibility with SQL Object. For
80 new code, prefer L{is_empty}. It's compatible with L{ResultSet}
81- which doesn't have a C{__nonzero__} implementation.
82+ which doesn't have a C{__bool__} implementation.
83 """
84
85+ if six.PY2:
86+ __nonzero__ = __bool__
87+
88 def __contains__(item):
89 """Support C{if FooObject in Foo.select(query)}."""
90
91
92=== modified file 'tests/mocker.py'
93--- tests/mocker.py 2019-06-07 17:14:33 +0000
94+++ tests/mocker.py 2019-08-11 09:08:59 +0000
95@@ -13,6 +13,7 @@
96 import os
97 import gc
98
99+import six
100 from six.moves import builtins
101
102
103@@ -1083,12 +1084,15 @@
104 return 0
105 return result
106
107- def __nonzero__(self):
108+ def __bool__(self):
109 try:
110- return self.__mocker_act__("nonzero")
111+ return self.__mocker_act__("bool")
112 except MatchError as e:
113 return True
114
115+ if six.PY2:
116+ __nonzero__ = __bool__
117+
118 def __iter__(self):
119 # XXX On py3k, when next() becomes __next__(), we'll be able
120 # to return the mock itself because it will be considered
121@@ -1187,7 +1191,7 @@
122 result = None
123 elif kind == "len":
124 result = len(object)
125- elif kind == "nonzero":
126+ elif kind == "bool":
127 result = bool(object)
128 elif kind == "iter":
129 result = iter(object)
130@@ -1280,7 +1284,7 @@
131 result = "del %s[%r]" % (result, action.args[0])
132 elif action.kind == "len":
133 result = "len(%s)" % result
134- elif action.kind == "nonzero":
135+ elif action.kind == "bool":
136 result = "bool(%s)" % result
137 elif action.kind == "iter":
138 result = "iter(%s)" % result
139
140=== modified file 'tests/sqlobject.py'
141--- tests/sqlobject.py 2019-06-05 11:41:07 +0000
142+++ tests/sqlobject.py 2019-08-11 09:08:59 +0000
143@@ -23,6 +23,8 @@
144 import datetime
145 import operator
146
147+import six
148+
149 from storm.database import create_database
150 from storm.exceptions import NoneError
151 from storm.sqlobject import *
152@@ -813,16 +815,20 @@
153 result = self.Person.select()
154 self.assertEquals(list(result.__iter__())[0].name, "John Joe")
155
156- def test_result_set__nonzero__(self):
157+ def test_result_set__bool__(self):
158 """
159- L{SQLObjectResultSet.__nonzero__} returns C{True} if the result set
160+ L{SQLObjectResultSet.__bool__} returns C{True} if the result set
161 contains results. If it contains no results, C{False} is
162 returned.
163 """
164 result = self.Person.select()
165- self.assertEquals(result.__nonzero__(), True)
166+ self.assertEquals(result.__bool__(), True)
167+ if six.PY2:
168+ self.assertEquals(result.__nonzero__(), True)
169 result = self.Person.select(self.Person.q.name == "No Person")
170- self.assertEquals(result.__nonzero__(), False)
171+ self.assertEquals(result.__bool__(), False)
172+ if six.PY2:
173+ self.assertEquals(result.__nonzero__(), False)
174
175 def test_result_set_is_empty(self):
176 """

Subscribers

People subscribed via source and target branches

to status/vote changes: