[py3] Replaced __nonzero__ by __bool__

Of course, __nonzero__ alias has been kept for Python 2 compatibility.
This commit is contained in:
Claude Paroz 2012-08-08 14:52:21 +02:00
parent 12cda89ffe
commit 576ec12f8e
7 changed files with 15 additions and 8 deletions

View File

@ -11,8 +11,9 @@ class PermLookupDict(object):
def __getitem__(self, perm_name):
return self.user.has_perm("%s.%s" % (self.module_name, perm_name))
def __nonzero__(self):
def __bool__(self):
return self.user.has_module_perms(self.module_name)
__nonzero__ = __bool__ # Python 2
class PermWrapper(object):

View File

@ -26,8 +26,9 @@ class File(FileProxyMixin):
def __repr__(self):
return "<%s: %s>" % (self.__class__.__name__, self or "None")
def __nonzero__(self):
def __bool__(self):
return bool(self.name)
__nonzero__ = __bool__ # Python 2
def __len__(self):
return self.size
@ -135,8 +136,9 @@ class ContentFile(File):
def __str__(self):
return 'Raw content'
def __nonzero__(self):
def __bool__(self):
return True
__nonzero__ = __bool__ # Python 2
def open(self, mode=None):
self.seek(0)

View File

@ -120,7 +120,7 @@ class QuerySet(object):
if len(self._result_cache) <= pos:
self._fill_cache()
def __nonzero__(self):
def __bool__(self):
if self._prefetch_related_lookups and not self._prefetch_done:
# We need all the results in order to be able to do the prefetch
# in one go. To minimize code duplication, we use the __len__
@ -134,6 +134,7 @@ class QuerySet(object):
except StopIteration:
return False
return True
__nonzero__ = __bool__ # Python 2
def __contains__(self, val):
# The 'in' operator works without this method, due to __iter__. This

View File

@ -152,9 +152,10 @@ class BoundMethodWeakref(object):
__repr__ = __str__
def __nonzero__( self ):
def __bool__( self ):
"""Whether we are still a valid reference"""
return self() is not None
__nonzero__ = __bool__ # Python 2
def __eq__(self, other):
"""Compare with another reference"""

View File

@ -65,9 +65,10 @@ class BaseFormSet(StrAndUnicode):
def __len__(self):
return len(self.forms)
def __nonzero__(self):
def __bool__(self):
"""All formsets have a management form which is not included in the length"""
return True
__nonzero__ = __bool__ # Python 2
def _management_form(self):
"""Returns the ManagementForm instance for this FormSet."""

View File

@ -68,11 +68,12 @@ class Node(object):
"""
return len(self.children)
def __nonzero__(self):
def __bool__(self):
"""
For truth value testing.
"""
return bool(self.children)
__nonzero__ = __bool__ # Python 2
def __contains__(self, other):
"""

View File

@ -230,7 +230,7 @@ It is optimal because:
#. Use of :ttag:`with` means that we store ``user.emails.all`` in a variable
for later use, allowing its cache to be re-used.
#. The line ``{% if emails %}`` causes ``QuerySet.__nonzero__()`` to be called,
#. The line ``{% if emails %}`` causes ``QuerySet.__bool__()`` to be called,
which causes the ``user.emails.all()`` query to be run on the database, and
at the least the first line to be turned into an ORM object. If there aren't
any results, it will return False, otherwise True.