[FIX] P3: __nonzero__ -> __bool__

Also ~removed bool-testing on cursors
This commit is contained in:
Xavier Morel
2017-06-26 16:45:03 +02:00
parent c5dcf75a81
commit 5ee9ec7d6c
2 changed files with 7 additions and 11 deletions
+4 -2
View File
@@ -169,8 +169,9 @@ class MetaModel(api.Meta):
class NewId(object): class NewId(object):
""" Pseudo-ids for new records. """ """ Pseudo-ids for new records. """
def __nonzero__(self): def __bool__(self):
return False return False
__nonzero__ = __bool__
IdType = pycompat.integer_types + (str, unicode, NewId) IdType = pycompat.integer_types + (str, unicode, NewId)
@@ -4501,9 +4502,10 @@ class BaseModel(MetaModel('DummyModel', (object,), {'_register': False})):
# "Dunder" methods # "Dunder" methods
# #
def __nonzero__(self): def __bool__(self):
""" Test whether ``self`` is nonempty. """ """ Test whether ``self`` is nonempty. """
return bool(getattr(self, '_ids', True)) return bool(getattr(self, '_ids', True))
__nonzero__ = __bool__
def __len__(self): def __len__(self):
""" Return the size of ``self``. """ """ Return the size of ``self``. """
+3 -9
View File
@@ -642,15 +642,9 @@ class Connection(object):
# serialized_cursor is deprecated - cursors are serialized by default # serialized_cursor is deprecated - cursors are serialized by default
serialized_cursor = cursor serialized_cursor = cursor
def __nonzero__(self): def __bool__(self):
"""Check if connection is possible""" raise NotImplementedError()
try: __nonzero__ = __bool__
_logger.info("__nonzero__() is deprecated. (It is too expensive to test a connection.)")
cr = self.cursor()
cr.close()
return True
except Exception:
return False
def connection_info_for(db_or_uri): def connection_info_for(db_or_uri):
""" parse the given `db_or_uri` and return a 2-tuple (dbname, connection_params) """ parse the given `db_or_uri` and return a 2-tuple (dbname, connection_params)