python3 compat: remove lots of Python 2.5 and earlier dependent code; use print() function syntax

This commit is contained in:
Sergey Shepelev
2013-10-07 16:48:34 +04:00
parent 35f600600c
commit d2bbbd79d3
23 changed files with 439 additions and 568 deletions

View File

@@ -1,18 +1,12 @@
language: python language: python
python: 2.7 python: 2.7
env: env:
- TOX_ENV=py25selects
- TOX_ENV=py25poll
- TOX_ENV=py26selects - TOX_ENV=py26selects
- TOX_ENV=py26poll - TOX_ENV=py26poll
- TOX_ENV=py26epolls - TOX_ENV=py26epolls
- TOX_ENV=py27selects - TOX_ENV=py27selects
- TOX_ENV=py27poll - TOX_ENV=py27poll
- TOX_ENV=py27epolls - TOX_ENV=py27epolls
matrix:
allow_failures:
- env: TOX_ENV=py25selects
- env: TOX_ENV=py25poll
install: install:
- sudo apt-get update -qq - sudo apt-get update -qq
- sudo apt-get install -qq libssl-dev libmysqlclient-dev libpq-dev - sudo apt-get install -qq libssl-dev libmysqlclient-dev libpq-dev

View File

@@ -20,8 +20,7 @@
... ...
Timeout: 0.1 seconds Timeout: 0.1 seconds
In Python 2.5 and newer, you can use the ``with`` statement for additional You can use the ``with`` statement for additional convenience::
convenience::
with Timeout(seconds, exception) as timeout: with Timeout(seconds, exception) as timeout:
pass # ... code block ... pass # ... code block ...

View File

@@ -23,8 +23,6 @@ That's it! The output from running nose is the same as unittest's output, if th
Many tests are skipped based on environmental factors; for example, it makes no sense to test Twisted-specific functionality when Twisted is not installed. These are printed as S's during execution, and in the summary printed after the tests run it will tell you how many were skipped. Many tests are skipped based on environmental factors; for example, it makes no sense to test Twisted-specific functionality when Twisted is not installed. These are printed as S's during execution, and in the summary printed after the tests run it will tell you how many were skipped.
.. note:: If running Python version 2.4, use this command instead: ``python tests/nosewrapper.py``. There are several tests which make use of the `with` statement and therefore will cause nose grief when it tries to import them; nosewrapper.py excludes these tests so they are skipped.
Doctests Doctests
-------- --------

View File

@@ -6,7 +6,7 @@ import linecache
import inspect import inspect
import warnings import warnings
from eventlet.support import greenlets as greenlet, BaseException from eventlet.support import greenlets as greenlet
from eventlet import hubs from eventlet import hubs
from eventlet import greenthread from eventlet import greenthread
from eventlet import debug from eventlet import debug
@@ -111,10 +111,12 @@ call_after_global = greenthread.call_after_global
class _SilentException(BaseException): class _SilentException(BaseException):
pass pass
class FakeTimer(object): class FakeTimer(object):
def cancel(self): def cancel(self):
pass pass
class timeout(object): class timeout(object):
"""Raise an exception in the block after timeout. """Raise an exception in the block after timeout.
@@ -130,10 +132,9 @@ class timeout(object):
try: try:
with timeout(10, MySpecialError, error_arg_1): with timeout(10, MySpecialError, error_arg_1):
urllib2.open('http://example.com') urllib2.open('http://example.com')
except MySpecialError, e: except MySpecialError as e:
print "special error received" print "special error received"
When *exc* is ``None``, code block is interrupted silently. When *exc* is ``None``, code block is interrupted silently.
""" """
@@ -160,6 +161,7 @@ class timeout(object):
if typ is _SilentException and value in self.throw_args: if typ is _SilentException and value in self.throw_args:
return True return True
with_timeout = greenthread.with_timeout with_timeout = greenthread.with_timeout
exc_after = greenthread.exc_after exc_after = greenthread.exc_after

View File

@@ -1,11 +1,12 @@
import sys import sys
from eventlet import greenio from eventlet import greenio
from eventlet import greenthread
from eventlet import greenpool from eventlet import greenpool
from eventlet import greenthread
from eventlet.green import socket from eventlet.green import socket
from eventlet.support import greenlets as greenlet from eventlet.support import greenlets as greenlet
def connect(addr, family=socket.AF_INET, bind=None): def connect(addr, family=socket.AF_INET, bind=None):
"""Convenience function for opening client sockets. """Convenience function for opening client sockets.
@@ -39,10 +40,12 @@ def listen(addr, family=socket.AF_INET, backlog=50):
sock.listen(backlog) sock.listen(backlog)
return sock return sock
class StopServe(Exception): class StopServe(Exception):
"""Exception class used for quitting :func:`~eventlet.serve` gracefully.""" """Exception class used for quitting :func:`~eventlet.serve` gracefully."""
pass pass
def _stop_checker(t, server_gt, conn): def _stop_checker(t, server_gt, conn):
try: try:
try: try:
@@ -54,6 +57,7 @@ def _stop_checker(t, server_gt, conn):
except Exception: except Exception:
greenthread.kill(server_gt, *sys.exc_info()) greenthread.kill(server_gt, *sys.exc_info())
def serve(sock, handle, concurrency=1000): def serve(sock, handle, concurrency=1000):
"""Runs a server on the supplied socket. Calls the function *handle* in a """Runs a server on the supplied socket. Calls the function *handle* in a
separate greenthread for every incoming client connection. *handle* takes separate greenthread for every incoming client connection. *handle* takes
@@ -100,10 +104,9 @@ def serve(sock, handle, concurrency=1000):
def wrap_ssl(sock, *a, **kw): def wrap_ssl(sock, *a, **kw):
"""Convenience function for converting a regular socket into an """Convenience function for converting a regular socket into an
SSL socket. Has the same interface as :func:`ssl.wrap_socket`, SSL socket. Has the same interface as :func:`ssl.wrap_socket`,
but works on 2.5 or earlier, using PyOpenSSL (though note that it but can also use PyOpenSSL. Though, note that it ignores the
ignores the *cert_reqs*, *ssl_version*, *ca_certs*, `cert_reqs`, `ssl_version`, `ca_certs`, `do_handshake_on_connect`,
*do_handshake_on_connect*, and *suppress_ragged_eofs* arguments and `suppress_ragged_eofs` arguments when using PyOpenSSL.
when using PyOpenSSL).
The preferred idiom is to call wrap_ssl directly on the creation The preferred idiom is to call wrap_ssl directly on the creation
method, e.g., ``wrap_ssl(connect(addr))`` or method, e.g., ``wrap_ssl(connect(addr))`` or
@@ -119,15 +122,18 @@ try:
from eventlet.green import ssl from eventlet.green import ssl
wrap_ssl_impl = ssl.wrap_socket wrap_ssl_impl = ssl.wrap_socket
except ImportError: except ImportError:
# < 2.6, trying PyOpenSSL # trying PyOpenSSL
try: try:
from eventlet.green.OpenSSL import SSL from eventlet.green.OpenSSL import SSL
except ImportError:
def wrap_ssl_impl(*a, **kw):
raise ImportError("To use SSL with Eventlet, you must install PyOpenSSL or use Python 2.6 or later.")
else:
def wrap_ssl_impl(sock, keyfile=None, certfile=None, server_side=False, def wrap_ssl_impl(sock, keyfile=None, certfile=None, server_side=False,
cert_reqs=None, ssl_version=None, ca_certs=None, cert_reqs=None, ssl_version=None, ca_certs=None,
do_handshake_on_connect=True, do_handshake_on_connect=True,
suppress_ragged_eofs=True, ciphers=None): suppress_ragged_eofs=True, ciphers=None):
# theoretically the ssl_version could be respected in this # theoretically the ssl_version could be respected in this line
# next line
context = SSL.Context(SSL.SSLv23_METHOD) context = SSL.Context(SSL.SSLv23_METHOD)
if certfile is not None: if certfile is not None:
context.use_certificate_file(certfile) context.use_certificate_file(certfile)
@@ -141,8 +147,3 @@ except ImportError:
else: else:
connection.set_connect_state() connection.set_connect_state()
return connection return connection
except ImportError:
def wrap_ssl_impl(*a, **kw):
raise ImportError("To use SSL with Eventlet, "
"you must install PyOpenSSL or use Python 2.6 or later.")

View File

@@ -161,9 +161,8 @@ def hub_blocking_detection(state=False, resolution=1):
blocking detector (don't use it in production!). blocking detector (don't use it in production!).
The *resolution* argument governs how long the SIGALARM timeout The *resolution* argument governs how long the SIGALARM timeout
waits in seconds. If on Python 2.6 or later, the implementation waits in seconds. The implementation uses :func:`signal.setitimer`
uses :func:`signal.setitimer` and can be specified as a and can be specified as a floating-point value.
floating-point value. On 2.5 or earlier, 1 second is the minimum.
The shorter the resolution, the greater the chance of false The shorter the resolution, the greater the chance of false
positives. positives.
""" """
@@ -171,5 +170,5 @@ def hub_blocking_detection(state=False, resolution=1):
assert resolution > 0 assert resolution > 0
hubs.get_hub().debug_blocking = state hubs.get_hub().debug_blocking = state
hubs.get_hub().debug_blocking_resolution = resolution hubs.get_hub().debug_blocking_resolution = resolution
if(not state): if not state:
hubs.get_hub().block_detect_post() hubs.get_hub().block_detect_post()

View File

@@ -101,7 +101,7 @@ class Event(object):
>>> evt = event.Event() >>> evt = event.Event()
>>> def wait_on(): >>> def wait_on():
... retval = evt.wait() ... retval = evt.wait()
... print("waited for", retval) ... print("waited for {0}".format(retval))
>>> _ = eventlet.spawn(wait_on) >>> _ = eventlet.spawn(wait_on)
>>> evt.send('result') >>> evt.send('result')
>>> eventlet.sleep(0) >>> eventlet.sleep(0)
@@ -134,7 +134,7 @@ class Event(object):
>>> def waiter(): >>> def waiter():
... print('about to wait') ... print('about to wait')
... result = evt.wait() ... result = evt.wait()
... print('waited for', result) ... print('waited for {0}'.format(result))
>>> _ = eventlet.spawn(waiter) >>> _ = eventlet.spawn(waiter)
>>> eventlet.sleep(0) >>> eventlet.sleep(0)
about to wait about to wait

View File

@@ -87,24 +87,15 @@ class GreenSSLObject(object):
try: try:
try:
# >= Python 2.6
from eventlet.green import ssl as ssl_module from eventlet.green import ssl as ssl_module
sslerror = __socket.sslerror sslerror = __socket.sslerror
__socket.ssl __socket.ssl
def ssl(sock, certificate=None, private_key=None):
warnings.warn("socket.ssl() is deprecated. Use ssl.wrap_socket() instead.",
DeprecationWarning, stacklevel=2)
return ssl_module.sslwrap_simple(sock, private_key, certificate)
except ImportError:
# <= Python 2.5 compatibility
sslerror = __socket.sslerror
__socket.ssl
def ssl(sock, certificate=None, private_key=None):
from eventlet import util
wrapped = util.wrap_ssl(sock, certificate, private_key)
return GreenSSLObject(wrapped)
except AttributeError: except AttributeError:
# if the real socket module doesn't have the ssl method or sslerror # if the real socket module doesn't have the ssl method or sslerror
# exception, we can't emulate them # exception, we can't emulate them
pass pass
else:
def ssl(sock, certificate=None, private_key=None):
warnings.warn("socket.ssl() is deprecated. Use ssl.wrap_socket() instead.",
DeprecationWarning, stacklevel=2)
return ssl_module.sslwrap_simple(sock, private_key, certificate)

View File

@@ -77,7 +77,6 @@ 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().
try:
_communicate = new.function(subprocess_orig.Popen._communicate.im_func.func_code, _communicate = new.function(subprocess_orig.Popen._communicate.im_func.func_code,
globals()) globals())
try: try:
@@ -89,17 +88,8 @@ class Popen(subprocess_orig.Popen):
globals()) globals())
except AttributeError: except AttributeError:
pass pass
except AttributeError:
# 2.4 only has communicate
_communicate = new.function(subprocess_orig.Popen.communicate.im_func.func_code,
globals())
def communicate(self, input=None):
return self._communicate(input)
# 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 = new.function(subprocess_orig.call.func_code, globals()) call = new.function(subprocess_orig.call.func_code, globals())
try:
check_call = new.function(subprocess_orig.check_call.func_code, globals()) check_call = new.function(subprocess_orig.check_call.func_code, globals())
except AttributeError:
pass # check_call added in 2.5

View File

@@ -486,10 +486,7 @@ class GreenPipe(_fileobject):
return n return n
def _get_readahead_len(self): def _get_readahead_len(self):
try: return len(self._rbuf.getvalue())
return len(self._rbuf.getvalue()) # StringIO in 2.5
except AttributeError:
return len(self._rbuf) # str in 2.4
def _clear_readahead_buf(self): def _clear_readahead_buf(self):
len = self._get_readahead_len() len = self._get_readahead_len()

View File

@@ -1,38 +1,13 @@
from __future__ import print_function from __future__ import print_function
import collections import collections
from contextlib import contextmanager
from eventlet import queue from eventlet import queue
__all__ = ['Pool', 'TokenPool'] __all__ = ['Pool', 'TokenPool']
# have to stick this in an exec so it works in 2.4
try:
from contextlib import contextmanager
exec('''
@contextmanager
def item_impl(self):
""" Get an object out of the pool, for use with with statement.
>>> from eventlet import pools
>>> pool = pools.TokenPool(max_size=4)
>>> with pool.item() as obj:
... print("got token")
...
got token
>>> pool.free()
4
"""
obj = self.get()
try:
yield obj
finally:
self.put(obj)
''')
except ImportError:
item_impl = None
class Pool(object): class Pool(object):
""" """
@@ -69,15 +44,6 @@ class Pool(object):
with mypool.item() as thing: with mypool.item() as thing:
thing.dostuff() thing.dostuff()
If stuck on 2.4, the :meth:`get` and :meth:`put` methods are the preferred
nomenclature. Use a ``finally`` to ensure that nothing is leaked::
thing = self.pool.get()
try:
thing.dostuff()
finally:
self.pool.put(thing)
The maximum size of the pool can be modified at runtime via The maximum size of the pool can be modified at runtime via
the :meth:`resize` method. the :meth:`resize` method.
@@ -126,8 +92,24 @@ class Pool(object):
self.current_size -= 1 # did not create self.current_size -= 1 # did not create
return self.channel.get() return self.channel.get()
if item_impl is not None: @contextmanager
item = item_impl def item(self):
""" Get an object out of the pool, for use with with statement.
>>> from eventlet import pools
>>> pool = pools.TokenPool(max_size=4)
>>> with pool.item() as obj:
... print("got token")
...
got token
>>> pool.free()
4
"""
obj = self.get()
try:
yield obj
finally:
self.put(obj)
def put(self, item): def put(self, item):
"""Put an item back into the pool, when done. This may """Put an item back into the pool, when done. This may

View File

@@ -22,6 +22,7 @@ def get_errno(exc):
except IndexError: except IndexError:
return None return None
if sys.version_info[0] < 3 and not greenlets.preserves_excinfo: if sys.version_info[0] < 3 and not greenlets.preserves_excinfo:
from sys import exc_clear as clear_sys_exc_info from sys import exc_clear as clear_sys_exc_info
else: else:
@@ -30,11 +31,3 @@ else:
Exception information is not visible outside of except statements. Exception information is not visible outside of except statements.
sys.exc_clear became obsolete and removed.""" sys.exc_clear became obsolete and removed."""
pass pass
if sys.version_info[0]==2 and sys.version_info[1]<5:
class BaseException: # pylint: disable-msg=W0622
# not subclassing from object() intentionally, because in
# that case "raise Timeout" fails with TypeError.
pass
else:
from __builtin__ import BaseException

View File

@@ -20,7 +20,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.from eventlet.support import greenlets as greenlet # THE SOFTWARE.from eventlet.support import greenlets as greenlet
from eventlet.support import greenlets as greenlet, BaseException from eventlet.support import greenlets as greenlet
from eventlet.hubs import get_hub from eventlet.hubs import get_hub
__all__ = ['Timeout', __all__ = ['Timeout',
@@ -84,10 +84,7 @@ class Timeout(BaseException):
self.timer = None self.timer = None
def __repr__(self): def __repr__(self):
try:
classname = self.__class__.__name__ classname = self.__class__.__name__
except AttributeError: # Python < 2.5
classname = 'Timeout'
if self.pending: if self.pending:
pending = ' pending' pending = ' pending'
else: else:

View File

@@ -1,26 +1,6 @@
import socket import socket
import warnings
def g_log(*args):
warnings.warn("eventlet.util.g_log is deprecated because "
"we're pretty sure no one uses it. "
"Send mail to eventletdev@lists.secondlife.com "
"if you are actually using it.",
DeprecationWarning, stacklevel=2)
import sys import sys
from eventlet.support import greenlets as greenlet import warnings
g_id = id(greenlet.getcurrent())
if g_id is None:
if greenlet.getcurrent().parent is None:
ident = 'greenlet-main'
else:
g_id = id(greenlet.getcurrent())
if g_id < 0:
g_id += 1 + ((sys.maxint + 1) << 1)
ident = '%08X' % (g_id,)
else:
ident = 'greenlet-%d' % (g_id,)
print('[%s] %s' % (ident, ' '.join(map(str, args))), file=sys.stderr)
__original_socket__ = socket.socket __original_socket__ = socket.socket
@@ -32,37 +12,20 @@ def tcp_socket():
s = __original_socket__(socket.AF_INET, socket.SOCK_STREAM) s = __original_socket__(socket.AF_INET, socket.SOCK_STREAM)
return s return s
try:
# if ssl is available, use eventlet.green.ssl for our ssl implementation # if ssl is available, use eventlet.green.ssl for our ssl implementation
from eventlet.green import ssl from eventlet.green import ssl
def wrap_ssl(sock, certificate=None, private_key=None, server_side=False): def wrap_ssl(sock, certificate=None, private_key=None, server_side=False):
return ssl.wrap_socket(sock, warnings.warn("eventlet.util.wrap_ssl is deprecated. "
keyfile=private_key, certfile=certificate, "Please use the eventlet.green.ssl.wrap_socket()",
server_side=server_side, cert_reqs=ssl.CERT_NONE, DeprecationWarning, stacklevel=2)
ssl_version=ssl.PROTOCOL_SSLv23, ca_certs=None, return ssl.wrap_socket(
do_handshake_on_connect=True, sock,
suppress_ragged_eofs=True) keyfile=private_key,
except ImportError: certfile=certificate,
# if ssl is not available, use PyOpenSSL server_side=server_side,
def wrap_ssl(sock, certificate=None, private_key=None, server_side=False): )
try:
from eventlet.green.OpenSSL import SSL
except ImportError:
raise ImportError("To use SSL with Eventlet, "
"you must install PyOpenSSL or use Python 2.6 or later.")
context = SSL.Context(SSL.SSLv23_METHOD)
if certificate is not None:
context.use_certificate_file(certificate)
if private_key is not None:
context.use_privatekey_file(private_key)
context.set_verify(SSL.VERIFY_NONE, lambda *x: True)
connection = SSL.Connection(context, sock)
if server_side:
connection.set_accept_state()
else:
connection.set_connect_state()
return connection
def wrap_socket_with_coroutine_socket(use_thread_pool=None): def wrap_socket_with_coroutine_socket(use_thread_pool=None):
warnings.warn("eventlet.util.wrap_socket_with_coroutine_socket() is now " warnings.warn("eventlet.util.wrap_socket_with_coroutine_socket() is now "
@@ -79,6 +42,7 @@ def wrap_pipes_with_coroutine_pipes():
from eventlet import patcher from eventlet import patcher
patcher.monkey_patch(all=False, os=True) patcher.monkey_patch(all=False, os=True)
def wrap_select_with_coroutine_select(): def wrap_select_with_coroutine_select():
warnings.warn("eventlet.util.wrap_select_with_coroutine_select() is now " warnings.warn("eventlet.util.wrap_select_with_coroutine_select() is now "
"eventlet.patcher.monkey_patch(all=False, select=True)", "eventlet.patcher.monkey_patch(all=False, select=True)",
@@ -86,6 +50,7 @@ def wrap_select_with_coroutine_select():
from eventlet import patcher from eventlet import patcher
patcher.monkey_patch(all=False, select=True) patcher.monkey_patch(all=False, select=True)
def wrap_threading_local_with_coro_local(): def wrap_threading_local_with_coro_local():
""" """
monkey patch ``threading.local`` with something that is greenlet aware. monkey patch ``threading.local`` with something that is greenlet aware.

View File

@@ -29,8 +29,6 @@ setup(
"Operating System :: MacOS :: MacOS X", "Operating System :: MacOS :: MacOS X",
"Operating System :: POSIX", "Operating System :: POSIX",
"Operating System :: Microsoft :: Windows", "Operating System :: Microsoft :: Windows",
"Programming Language :: Python :: 2.4",
"Programming Language :: Python :: 2.5",
"Programming Language :: Python :: 2.6", "Programming Language :: Python :: 2.6",
"Programming Language :: Python :: 2.7", "Programming Language :: Python :: 2.7",
"Topic :: Internet", "Topic :: Internet",

View File

@@ -43,13 +43,16 @@ sentinel = Sentinel()
DEFAULT = sentinel.DEFAULT DEFAULT = sentinel.DEFAULT
class OldStyleClass: class OldStyleClass:
pass pass
ClassType = type(OldStyleClass) ClassType = type(OldStyleClass)
def _is_magic(name): def _is_magic(name):
return '__%s__' % name[2:-2] == name return '__%s__' % name[2:-2] == name
def _copy(value): def _copy(value):
if type(value) in (dict, list, tuple, set): if type(value) in (dict, list, tuple, set):
return type(value)(value) return type(value)(value)
@@ -73,7 +76,6 @@ class Mock(object):
self.reset_mock() self.reset_mock()
def reset_mock(self): def reset_mock(self):
self.called = False self.called = False
self.call_args = None self.call_args = None
@@ -85,7 +87,6 @@ class Mock(object):
if isinstance(self._return_value, Mock): if isinstance(self._return_value, Mock):
self._return_value.reset_mock() self._return_value.reset_mock()
def __get_return_value(self): def __get_return_value(self):
if self._return_value is DEFAULT: if self._return_value is DEFAULT:
self._return_value = Mock() self._return_value = Mock()
@@ -96,7 +97,6 @@ class Mock(object):
return_value = property(__get_return_value, __set_return_value) return_value = property(__get_return_value, __set_return_value)
def __call__(self, *args, **kwargs): def __call__(self, *args, **kwargs):
self.called = True self.called = True
self.call_count += 1 self.call_count += 1
@@ -129,7 +129,6 @@ class Mock(object):
ret_val = self.return_value ret_val = self.return_value
return ret_val return ret_val
def __getattr__(self, name): def __getattr__(self, name):
if self._methods is not None: if self._methods is not None:
if name not in self._methods: if name not in self._methods:
@@ -145,7 +144,6 @@ class Mock(object):
return self._children[name] return self._children[name]
def assert_called_with(self, *args, **kwargs): def assert_called_with(self, *args, **kwargs):
assert self.call_args == (args, kwargs), 'Expected: %s\nCalled with: %s' % ((args, kwargs), self.call_args) assert self.call_args == (args, kwargs), 'Expected: %s\nCalled with: %s' % ((args, kwargs), self.call_args)
@@ -178,7 +176,6 @@ class _patch(object):
self.create = create self.create = create
self.has_local = False self.has_local = False
def __call__(self, func): def __call__(self, func):
if hasattr(func, 'patchings'): if hasattr(func, 'patchings'):
func.patchings.append(self) func.patchings.append(self)
@@ -204,7 +201,6 @@ class _patch(object):
func.func_code.co_firstlineno) func.func_code.co_firstlineno)
return patched return patched
def get_original(self): def get_original(self):
target = self.target target = self.target
name = self.attribute name = self.attribute
@@ -221,7 +217,6 @@ class _patch(object):
raise AttributeError("%s does not have the attribute %r" % (target, name)) raise AttributeError("%s does not have the attribute %r" % (target, name))
return original return original
def __enter__(self): def __enter__(self):
new, spec, = self.new, self.spec new, spec, = self.new, self.spec
original = self.get_original() original = self.get_original()
@@ -240,7 +235,6 @@ class _patch(object):
setattr(self.target, self.attribute, new) setattr(self.target, self.attribute, new)
return new return new
def __exit__(self, *_): def __exit__(self, *_):
if self.temp_original is not DEFAULT: if self.temp_original is not DEFAULT:
setattr(self.target, self.attribute, self.temp_original) setattr(self.target, self.attribute, self.temp_original)
@@ -262,7 +256,6 @@ def patch(target, new=DEFAULT, spec=None, create=False):
return _patch(target, attribute, new, spec, create) return _patch(target, attribute, new, spec, create)
def _has_local_attr(obj, name): def _has_local_attr(obj, name):
try: try:
return name in vars(obj) return name in vars(obj)

View File

@@ -4,22 +4,17 @@ import nose
from os.path import dirname, realpath, abspath from os.path import dirname, realpath, abspath
import sys import sys
parent_dir = dirname(dirname(realpath(abspath(__file__)))) parent_dir = dirname(dirname(realpath(abspath(__file__))))
if parent_dir not in sys.path: if parent_dir not in sys.path:
sys.path.insert(0, parent_dir) sys.path.insert(0, parent_dir)
# hacky hacks: skip test__api_timeout when under 2.4 because otherwise it SyntaxErrors
if sys.version_info < (2,5):
argv = sys.argv + ["--exclude=.*_with_statement.*"]
else:
argv = sys.argv
# hudson does a better job printing the test results if the exit value is 0 # hudson does a better job printing the test results if the exit value is 0
zero_status = '--force-zero-status' zero_status = '--force-zero-status'
if zero_status in argv: if zero_status in sys.argv:
argv.remove(zero_status) sys.argv.remove(zero_status)
launch = nose.run launch = nose.run
else: else:
launch = nose.main launch = nose.main
launch(argv=argv) launch(argv=sys.argv)

View File

@@ -1,12 +1,10 @@
from eventlet import patcher from eventlet import patcher
from eventlet.green import select from eventlet.green import select
patcher.inject('test.test_select', patcher.inject('test.test_select',
globals(), globals(),
('select', select)) ('select', select))
if __name__ == "__main__": if __name__ == "__main__":
try:
test_main() test_main()
except NameError:
pass # 2.5

View File

@@ -2,9 +2,6 @@ from eventlet import patcher
from eventlet.green import thread from eventlet.green import thread
from eventlet.green import time from eventlet.green import time
# necessary to initialize the hub before running on 2.5
from eventlet import hubs
hubs.get_hub()
patcher.inject('test.test_thread', globals()) patcher.inject('test.test_thread', globals())
@@ -15,7 +12,4 @@ except NameError:
pass pass
if __name__ == "__main__": if __name__ == "__main__":
try:
test_main() test_main()
except NameError:
pass # 2.5

View File

@@ -9,10 +9,7 @@ patcher.inject('test.test_urllib2',
HandlerTests.test_file = patcher.patch_function(HandlerTests.test_file, ('socket', socket)) HandlerTests.test_file = patcher.patch_function(HandlerTests.test_file, ('socket', socket))
HandlerTests.test_cookie_redirect = patcher.patch_function(HandlerTests.test_cookie_redirect, ('urllib2', urllib2)) HandlerTests.test_cookie_redirect = patcher.patch_function(HandlerTests.test_cookie_redirect, ('urllib2', urllib2))
try:
OpenerDirectorTests.test_badly_named_methods = patcher.patch_function(OpenerDirectorTests.test_badly_named_methods, ('urllib2', urllib2)) OpenerDirectorTests.test_badly_named_methods = patcher.patch_function(OpenerDirectorTests.test_badly_named_methods, ('urllib2', urllib2))
except AttributeError:
pass # 2.4 doesn't have this test method
if __name__ == "__main__": if __name__ == "__main__":
test_main() test_main()

View File

@@ -1,19 +1,22 @@
""" Tests with-statement behavior of Timeout class. Don't import when """Tests with-statement behavior of Timeout class."""
using Python 2.4. """
from __future__ import with_statement
import sys import sys
import time
import unittest import unittest
import weakref import weakref
import time
from eventlet import sleep from eventlet import sleep
from eventlet.timeout import Timeout from eventlet.timeout import Timeout
from tests import LimitedTestCase from tests import LimitedTestCase
DELAY = 0.01 DELAY = 0.01
class Error(Exception): class Error(Exception):
pass pass
class Test(LimitedTestCase): class Test(LimitedTestCase):
def test_cancellation(self): def test_cancellation(self):
# Nothing happens if with-block finishes before the timeout expires # Nothing happens if with-block finishes before the timeout expires
@@ -83,7 +86,6 @@ class Test(LimitedTestCase):
timer.cancel() timer.cancel()
sleep(DELAY*2) sleep(DELAY*2)
def test_silent_block(self): def test_silent_block(self):
# To silence the exception before exiting the block, pass # To silence the exception before exiting the block, pass
# False as second parameter. # False as second parameter.

16
tox.ini
View File

@@ -11,7 +11,7 @@ ignore = E261
max-line-length = 101 max-line-length = 101
[tox] [tox]
envlist = py25selects,py25poll,py26selects,py26poll,py26epolls,py27selects,py27poll,py27epolls envlist = py26selects,py26poll,py26epolls,py27selects,py27poll,py27epolls
[testenv] [testenv]
downloadcache = {toxworkdir}/pip_download_cache downloadcache = {toxworkdir}/pip_download_cache
@@ -26,20 +26,6 @@ commands =
eventlet/pool.py eventlet/pools.py eventlet/proc.py \ eventlet/pool.py eventlet/pools.py eventlet/proc.py \
eventlet/queue.py eventlet/timeout.py eventlet/queue.py eventlet/timeout.py
[testenv:py25selects]
basepython = python2.5
setenv = EVENTLET_HUB = selects
deps =
{[testenv]deps}
pyzmq<2.2
[testenv:py25poll]
basepython = python2.5
setenv = EVENTLET_HUB = poll
deps =
{[testenv]deps}
pyzmq<2.2
[testenv:py26selects] [testenv:py26selects]
basepython = python2.6 basepython = python2.6
setenv = EVENTLET_HUB = selects setenv = EVENTLET_HUB = selects