Python3: __bool__, im_code, im_func

This commit is contained in:
Victor Sergeyev
2014-04-23 14:33:24 +03:00
committed by Sergey Shepelev
parent 49773bb12b
commit fb6b46dea0
7 changed files with 29 additions and 8 deletions

View File

@@ -150,6 +150,8 @@ class Queue(object):
def __nonzero__(self): def __nonzero__(self):
return len(self.items)>0 return len(self.items)>0
__bool__ = __nonzero__
def __len__(self): def __len__(self):
return len(self.items) return len(self.items)
@@ -224,6 +226,8 @@ class Channel(object):
def __nonzero__(self): def __nonzero__(self):
return len(self.items)>0 return len(self.items)>0
__bool__ = __nonzero__
def __len__(self): def __len__(self):
return len(self.items) return len(self.items)

View File

@@ -135,8 +135,9 @@ class BaseConnectionPool(Pool):
def _unwrap_connection(self, conn): def _unwrap_connection(self, conn):
""" If the connection was wrapped by a subclass of """ If the connection was wrapped by a subclass of
BaseConnectionWrapper and is still functional (as determined BaseConnectionWrapper and is still functional (as determined
by the __nonzero__ method), returns the unwrapped connection. by the __nonzero__, or __bool__ in python3, method), returns
If anything goes wrong with this process, returns None. the unwrapped connection. If anything goes wrong with this
process, returns None.
""" """
base = None base = None
try: try:
@@ -336,6 +337,8 @@ class PooledConnectionWrapper(GenericConnectionWrapper):
def __nonzero__(self): def __nonzero__(self):
return (hasattr(self, '_base') and bool(self._base)) return (hasattr(self, '_base') and bool(self._base))
__bool__ = __nonzero__
def _destroy(self): def _destroy(self):
self._pool = None self._pool = None
try: try:

View File

@@ -6,6 +6,7 @@ import eventlet
from eventlet import greenio from eventlet import greenio
from eventlet import patcher from eventlet import patcher
from eventlet.green import select from eventlet.green import select
from eventlet.support import six
patcher.inject('subprocess', globals(), ('select', select)) patcher.inject('subprocess', globals(), ('select', select))
@@ -77,19 +78,23 @@ class Popen(subprocess_orig.Popen):
# don't want to rewrite the original _communicate() method, we # don't want to rewrite the original _communicate() method, we
# just want a version that uses eventlet.green.select.select() # just want a version that uses eventlet.green.select.select()
# instead of select.select(). # instead of select.select().
_communicate = FunctionType(subprocess_orig.Popen._communicate.im_func.func_code, _communicate = FunctionType(
six.get_function_code(six.get_unbound_function(
subprocess_orig.Popen._communicate)),
globals()) globals())
try: try:
_communicate_with_select = FunctionType( _communicate_with_select = FunctionType(
subprocess_orig.Popen._communicate_with_select.im_func.func_code, six.get_function_code(six.get_unbound_function(
subprocess_orig.Popen._communicate_with_select)),
globals()) globals())
_communicate_with_poll = FunctionType( _communicate_with_poll = FunctionType(
subprocess_orig.Popen._communicate_with_poll.im_func.func_code, six.get_function_code(six.get_unbound_function(
subprocess_orig.Popen._communicate_with_poll)),
globals()) globals())
except AttributeError: except AttributeError:
pass pass
# Borrow subprocess.call() and check_call(), but patch them so they reference # Borrow subprocess.call() and check_call(), but patch them so they reference
# OUR Popen class rather than subprocess.Popen. # OUR Popen class rather than subprocess.Popen.
call = FunctionType(subprocess_orig.call.func_code, globals()) call = FunctionType(six.get_function_code(subprocess_orig.call), globals())
check_call = FunctionType(subprocess_orig.check_call.func_code, globals()) check_call = FunctionType(six.get_function_code(subprocess_orig.check_call), globals())

View File

@@ -42,6 +42,8 @@ class _QueueLock(object):
def __nonzero__(self): def __nonzero__(self):
return self._count return self._count
__bool__ = __nonzero__
def __enter__(self): def __enter__(self):
self.acquire() self.acquire()
@@ -88,6 +90,8 @@ class _BlockedThread(object):
def __nonzero__(self): def __nonzero__(self):
return self._blocked_thread is not None return self._blocked_thread is not None
__bool__ = __nonzero__
def block(self): def block(self):
if self._blocked_thread is not None: if self._blocked_thread is not None:
raise Exception("Cannot block more than one thread on one BlockedThread") raise Exception("Cannot block more than one thread on one BlockedThread")

View File

@@ -547,6 +547,8 @@ class Proc(Source):
if self.greenlet is not None: if self.greenlet is not None:
return bool(self.greenlet) return bool(self.greenlet)
__bool__ = __nonzero__
@property @property
def dead(self): def dead(self):
return self.ready() or self.greenlet.dead return self.ready() or self.greenlet.dead

View File

@@ -98,6 +98,8 @@ class Waiter(object):
def __nonzero__(self): def __nonzero__(self):
return self.greenlet is not None return self.greenlet is not None
__bool__ = __nonzero__
@property @property
def waiting(self): def waiting(self):
return self.greenlet is not None return self.greenlet is not None

View File

@@ -220,6 +220,7 @@ class Proxy(object):
return len(self._obj) return len(self._obj)
def __nonzero__(self): def __nonzero__(self):
return bool(self._obj) return bool(self._obj)
__bool__ = __nonzero__
def __iter__(self): def __iter__(self):
it = iter(self._obj) it = iter(self._obj)
if it == self._obj: if it == self._obj: