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
python: 2.7
env:
- TOX_ENV=py25selects
- TOX_ENV=py25poll
- TOX_ENV=py26selects
- TOX_ENV=py26poll
- TOX_ENV=py26epolls
- TOX_ENV=py27selects
- TOX_ENV=py27poll
- TOX_ENV=py27epolls
matrix:
allow_failures:
- env: TOX_ENV=py25selects
- env: TOX_ENV=py25poll
install:
- sudo apt-get update -qq
- sudo apt-get install -qq libssl-dev libmysqlclient-dev libpq-dev

View File

@@ -20,8 +20,7 @@
...
Timeout: 0.1 seconds
In Python 2.5 and newer, you can use the ``with`` statement for additional
convenience::
You can use the ``with`` statement for additional convenience::
with Timeout(seconds, exception) as timeout:
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.
.. 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
--------

View File

@@ -6,7 +6,7 @@ import linecache
import inspect
import warnings
from eventlet.support import greenlets as greenlet, BaseException
from eventlet.support import greenlets as greenlet
from eventlet import hubs
from eventlet import greenthread
from eventlet import debug
@@ -111,10 +111,12 @@ call_after_global = greenthread.call_after_global
class _SilentException(BaseException):
pass
class FakeTimer(object):
def cancel(self):
pass
class timeout(object):
"""Raise an exception in the block after timeout.
@@ -130,10 +132,9 @@ class timeout(object):
try:
with timeout(10, MySpecialError, error_arg_1):
urllib2.open('http://example.com')
except MySpecialError, e:
except MySpecialError as e:
print "special error received"
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:
return True
with_timeout = greenthread.with_timeout
exc_after = greenthread.exc_after

View File

@@ -1,11 +1,12 @@
import sys
from eventlet import greenio
from eventlet import greenthread
from eventlet import greenpool
from eventlet import greenthread
from eventlet.green import socket
from eventlet.support import greenlets as greenlet
def connect(addr, family=socket.AF_INET, bind=None):
"""Convenience function for opening client sockets.
@@ -39,10 +40,12 @@ def listen(addr, family=socket.AF_INET, backlog=50):
sock.listen(backlog)
return sock
class StopServe(Exception):
"""Exception class used for quitting :func:`~eventlet.serve` gracefully."""
pass
def _stop_checker(t, server_gt, conn):
try:
try:
@@ -54,6 +57,7 @@ def _stop_checker(t, server_gt, conn):
except Exception:
greenthread.kill(server_gt, *sys.exc_info())
def serve(sock, handle, concurrency=1000):
"""Runs a server on the supplied socket. Calls the function *handle* in a
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):
"""Convenience function for converting a regular socket into an
SSL socket. Has the same interface as :func:`ssl.wrap_socket`,
but works on 2.5 or earlier, using PyOpenSSL (though note that it
ignores the *cert_reqs*, *ssl_version*, *ca_certs*,
*do_handshake_on_connect*, and *suppress_ragged_eofs* arguments
when using PyOpenSSL).
but can also use PyOpenSSL. Though, note that it ignores the
`cert_reqs`, `ssl_version`, `ca_certs`, `do_handshake_on_connect`,
and `suppress_ragged_eofs` arguments when using PyOpenSSL.
The preferred idiom is to call wrap_ssl directly on the creation
method, e.g., ``wrap_ssl(connect(addr))`` or
@@ -119,15 +122,18 @@ try:
from eventlet.green import ssl
wrap_ssl_impl = ssl.wrap_socket
except ImportError:
# < 2.6, trying PyOpenSSL
# trying PyOpenSSL
try:
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,
cert_reqs=None, ssl_version=None, ca_certs=None,
do_handshake_on_connect=True,
suppress_ragged_eofs=True, ciphers=None):
# theoretically the ssl_version could be respected in this
# next line
# theoretically the ssl_version could be respected in this line
context = SSL.Context(SSL.SSLv23_METHOD)
if certfile is not None:
context.use_certificate_file(certfile)
@@ -141,8 +147,3 @@ except ImportError:
else:
connection.set_connect_state()
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!).
The *resolution* argument governs how long the SIGALARM timeout
waits in seconds. If on Python 2.6 or later, the implementation
uses :func:`signal.setitimer` and can be specified as a
floating-point value. On 2.5 or earlier, 1 second is the minimum.
waits in seconds. The implementation uses :func:`signal.setitimer`
and can be specified as a floating-point value.
The shorter the resolution, the greater the chance of false
positives.
"""
@@ -171,5 +170,5 @@ def hub_blocking_detection(state=False, resolution=1):
assert resolution > 0
hubs.get_hub().debug_blocking = state
hubs.get_hub().debug_blocking_resolution = resolution
if(not state):
if not state:
hubs.get_hub().block_detect_post()

View File

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

View File

@@ -87,24 +87,15 @@ class GreenSSLObject(object):
try:
try:
# >= Python 2.6
from eventlet.green import ssl as ssl_module
sslerror = __socket.sslerror
__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:
# if the real socket module doesn't have the ssl method or sslerror
# exception, we can't emulate them
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
# just want a version that uses eventlet.green.select.select()
# instead of select.select().
try:
_communicate = new.function(subprocess_orig.Popen._communicate.im_func.func_code,
globals())
try:
@@ -89,17 +88,8 @@ class Popen(subprocess_orig.Popen):
globals())
except AttributeError:
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
# OUR Popen class rather than subprocess.Popen.
call = new.function(subprocess_orig.call.func_code, globals())
try:
check_call = new.function(subprocess_orig.check_call.func_code, globals())
except AttributeError:
pass # check_call added in 2.5
check_call = new.function(subprocess_orig.check_call.func_code, globals())

View File

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

View File

@@ -1,38 +1,13 @@
from __future__ import print_function
import collections
from contextlib import contextmanager
from eventlet import queue
__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):
"""
@@ -69,15 +44,6 @@ class Pool(object):
with mypool.item() as thing:
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 :meth:`resize` method.
@@ -126,8 +92,24 @@ class Pool(object):
self.current_size -= 1 # did not create
return self.channel.get()
if item_impl is not None:
item = item_impl
@contextmanager
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):
"""Put an item back into the pool, when done. This may

View File

@@ -22,7 +22,8 @@ def get_errno(exc):
except IndexError:
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
else:
def clear_sys_exc_info():
@@ -30,11 +31,3 @@ else:
Exception information is not visible outside of except statements.
sys.exc_clear became obsolete and removed."""
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
# 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
__all__ = ['Timeout',
@@ -84,10 +84,7 @@ class Timeout(BaseException):
self.timer = None
def __repr__(self):
try:
classname = self.__class__.__name__
except AttributeError: # Python < 2.5
classname = 'Timeout'
if self.pending:
pending = ' pending'
else:

View File

@@ -1,68 +1,31 @@
import socket
import sys
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
from eventlet.support import greenlets as greenlet
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
def tcp_socket():
warnings.warn("eventlet.util.tcp_socket is deprecated."
"Please use the standard socket technique for this instead:"
warnings.warn("eventlet.util.tcp_socket is deprecated. "
"Please use the standard socket technique for this instead: "
"sock = socket.socket()",
DeprecationWarning, stacklevel=2)
s = __original_socket__(socket.AF_INET, socket.SOCK_STREAM)
return s
try:
# if ssl is available, use eventlet.green.ssl for our ssl implementation
from eventlet.green import ssl
def wrap_ssl(sock, certificate=None, private_key=None, server_side=False):
return ssl.wrap_socket(sock,
keyfile=private_key, certfile=certificate,
server_side=server_side, cert_reqs=ssl.CERT_NONE,
ssl_version=ssl.PROTOCOL_SSLv23, ca_certs=None,
do_handshake_on_connect=True,
suppress_ragged_eofs=True)
except ImportError:
# if ssl is not available, use PyOpenSSL
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
# if ssl is available, use eventlet.green.ssl for our ssl implementation
from eventlet.green import ssl
def wrap_ssl(sock, certificate=None, private_key=None, server_side=False):
warnings.warn("eventlet.util.wrap_ssl is deprecated. "
"Please use the eventlet.green.ssl.wrap_socket()",
DeprecationWarning, stacklevel=2)
return ssl.wrap_socket(
sock,
keyfile=private_key,
certfile=certificate,
server_side=server_side,
)
def wrap_socket_with_coroutine_socket(use_thread_pool=None):
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
patcher.monkey_patch(all=False, os=True)
def wrap_select_with_coroutine_select():
warnings.warn("eventlet.util.wrap_select_with_coroutine_select() is now "
"eventlet.patcher.monkey_patch(all=False, select=True)",
@@ -86,6 +50,7 @@ def wrap_select_with_coroutine_select():
from eventlet import patcher
patcher.monkey_patch(all=False, select=True)
def wrap_threading_local_with_coro_local():
"""
monkey patch ``threading.local`` with something that is greenlet aware.

View File

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

View File

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

View File

@@ -4,22 +4,17 @@ import nose
from os.path import dirname, realpath, abspath
import sys
parent_dir = dirname(dirname(realpath(abspath(__file__))))
if parent_dir not in sys.path:
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
zero_status = '--force-zero-status'
if zero_status in argv:
argv.remove(zero_status)
if zero_status in sys.argv:
sys.argv.remove(zero_status)
launch = nose.run
else:
launch = nose.main
launch(argv=argv)
launch(argv=sys.argv)

View File

@@ -1,12 +1,10 @@
from eventlet import patcher
from eventlet.green import select
patcher.inject('test.test_select',
globals(),
('select', select))
if __name__ == "__main__":
try:
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 time
# necessary to initialize the hub before running on 2.5
from eventlet import hubs
hubs.get_hub()
patcher.inject('test.test_thread', globals())
@@ -15,7 +12,4 @@ except NameError:
pass
if __name__ == "__main__":
try:
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_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))
except AttributeError:
pass # 2.4 doesn't have this test method
OpenerDirectorTests.test_badly_named_methods = patcher.patch_function(OpenerDirectorTests.test_badly_named_methods, ('urllib2', urllib2))
if __name__ == "__main__":
test_main()

View File

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

16
tox.ini
View File

@@ -11,7 +11,7 @@ ignore = E261
max-line-length = 101
[tox]
envlist = py25selects,py25poll,py26selects,py26poll,py26epolls,py27selects,py27poll,py27epolls
envlist = py26selects,py26poll,py26epolls,py27selects,py27poll,py27epolls
[testenv]
downloadcache = {toxworkdir}/pip_download_cache
@@ -26,20 +26,6 @@ commands =
eventlet/pool.py eventlet/pools.py eventlet/proc.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]
basepython = python2.6
setenv = EVENTLET_HUB = selects