From fb6b46dea0b6a5c7ff83058c492095e3c351a54d Mon Sep 17 00:00:00 2001 From: Victor Sergeyev Date: Wed, 23 Apr 2014 14:33:24 +0300 Subject: [PATCH] Python3: __bool__, im_code, im_func --- eventlet/coros.py | 4 ++++ eventlet/db_pool.py | 7 +++++-- eventlet/green/subprocess.py | 17 +++++++++++------ eventlet/green/zmq.py | 4 ++++ eventlet/proc.py | 2 ++ eventlet/queue.py | 2 ++ eventlet/tpool.py | 1 + 7 files changed, 29 insertions(+), 8 deletions(-) diff --git a/eventlet/coros.py b/eventlet/coros.py index 7604810..a9d8aec 100644 --- a/eventlet/coros.py +++ b/eventlet/coros.py @@ -150,6 +150,8 @@ class Queue(object): def __nonzero__(self): return len(self.items)>0 + __bool__ = __nonzero__ + def __len__(self): return len(self.items) @@ -224,6 +226,8 @@ class Channel(object): def __nonzero__(self): return len(self.items)>0 + __bool__ = __nonzero__ + def __len__(self): return len(self.items) diff --git a/eventlet/db_pool.py b/eventlet/db_pool.py index a4ab3ef..67da250 100644 --- a/eventlet/db_pool.py +++ b/eventlet/db_pool.py @@ -135,8 +135,9 @@ class BaseConnectionPool(Pool): def _unwrap_connection(self, conn): """ If the connection was wrapped by a subclass of BaseConnectionWrapper and is still functional (as determined - by the __nonzero__ method), returns the unwrapped connection. - If anything goes wrong with this process, returns None. + by the __nonzero__, or __bool__ in python3, method), returns + the unwrapped connection. If anything goes wrong with this + process, returns None. """ base = None try: @@ -336,6 +337,8 @@ class PooledConnectionWrapper(GenericConnectionWrapper): def __nonzero__(self): return (hasattr(self, '_base') and bool(self._base)) + __bool__ = __nonzero__ + def _destroy(self): self._pool = None try: diff --git a/eventlet/green/subprocess.py b/eventlet/green/subprocess.py index a513b93..4a90f15 100644 --- a/eventlet/green/subprocess.py +++ b/eventlet/green/subprocess.py @@ -6,6 +6,7 @@ import eventlet from eventlet import greenio from eventlet import patcher from eventlet.green import select +from eventlet.support import six 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 # just want a version that uses eventlet.green.select.select() # instead of select.select(). - _communicate = FunctionType(subprocess_orig.Popen._communicate.im_func.func_code, - globals()) + _communicate = FunctionType( + six.get_function_code(six.get_unbound_function( + subprocess_orig.Popen._communicate)), + globals()) try: _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()) _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()) except AttributeError: pass # Borrow subprocess.call() and check_call(), but patch them so they reference # OUR Popen class rather than subprocess.Popen. -call = FunctionType(subprocess_orig.call.func_code, globals()) -check_call = FunctionType(subprocess_orig.check_call.func_code, globals()) +call = FunctionType(six.get_function_code(subprocess_orig.call), globals()) +check_call = FunctionType(six.get_function_code(subprocess_orig.check_call), globals()) diff --git a/eventlet/green/zmq.py b/eventlet/green/zmq.py index ea4d19c..5cbc549 100644 --- a/eventlet/green/zmq.py +++ b/eventlet/green/zmq.py @@ -42,6 +42,8 @@ class _QueueLock(object): def __nonzero__(self): return self._count + __bool__ = __nonzero__ + def __enter__(self): self.acquire() @@ -88,6 +90,8 @@ class _BlockedThread(object): def __nonzero__(self): return self._blocked_thread is not None + __bool__ = __nonzero__ + def block(self): if self._blocked_thread is not None: raise Exception("Cannot block more than one thread on one BlockedThread") diff --git a/eventlet/proc.py b/eventlet/proc.py index add2fa0..6548447 100644 --- a/eventlet/proc.py +++ b/eventlet/proc.py @@ -547,6 +547,8 @@ class Proc(Source): if self.greenlet is not None: return bool(self.greenlet) + __bool__ = __nonzero__ + @property def dead(self): return self.ready() or self.greenlet.dead diff --git a/eventlet/queue.py b/eventlet/queue.py index ad94e18..3378d09 100644 --- a/eventlet/queue.py +++ b/eventlet/queue.py @@ -98,6 +98,8 @@ class Waiter(object): def __nonzero__(self): return self.greenlet is not None + __bool__ = __nonzero__ + @property def waiting(self): return self.greenlet is not None diff --git a/eventlet/tpool.py b/eventlet/tpool.py index d0a68f0..f92e9ec 100644 --- a/eventlet/tpool.py +++ b/eventlet/tpool.py @@ -220,6 +220,7 @@ class Proxy(object): return len(self._obj) def __nonzero__(self): return bool(self._obj) + __bool__ = __nonzero__ def __iter__(self): it = iter(self._obj) if it == self._obj: