Skip to content

Commit

Permalink
python3 compat fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vikt0rs authored and temoto committed Mar 28, 2014
1 parent cfdd9a9 commit 2b2f0a9
Show file tree
Hide file tree
Showing 35 changed files with 327 additions and 279 deletions.
2 changes: 2 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Contributors
* Floris Bruynooghe
* Paul Oppenheim
* Jakub Stasiak
* Aldona Majorek
* Victor Sergeyev

Linden Lab Contributors
-----------------------
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def measure_best(repeat, iters,
funcs = list(funcs)
results = dict([(f,[]) for f in funcs])

for i in xrange(repeat):
for i in six.moves.range(repeat):
random.shuffle(funcs)
for func in funcs:
gc.collect()
Expand Down
5 changes: 4 additions & 1 deletion benchmarks/hub_timers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import eventlet
import random
import time

from eventlet.hubs import timer, get_hub
from eventlet.support import six


timer_count = 100000

Expand All @@ -19,7 +22,7 @@
def work(n):
l.append(n)

timeouts = [random.uniform(0, 10) for x in xrange(timer_count)]
timeouts = [random.uniform(0, 10) for x in six.moves.range(timer_count)]

hub = get_hub()

Expand Down
9 changes: 5 additions & 4 deletions benchmarks/localhost_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import time

import benchmarks
from eventlet.support import six


BYTES=1000
Expand All @@ -30,13 +31,13 @@ def writer(addr, socket_impl):


def green_accepter(server_sock, pool):
for i in xrange(CONCURRENCY):
for i in six.moves.range(CONCURRENCY):
sock, addr = server_sock.accept()
pool.spawn_n(reader, sock)


def heavy_accepter(server_sock, pool):
for i in xrange(CONCURRENCY):
for i in six.moves.range(CONCURRENCY):
sock, addr = server_sock.accept()
t = threading.Thread(None, reader, "reader thread", (sock,))
t.start()
Expand All @@ -57,7 +58,7 @@ def launch_green_threads():
server_sock.listen(50)
addr = ('localhost', server_sock.getsockname()[1])
pool.spawn_n(green_accepter, server_sock, pool)
for i in xrange(CONCURRENCY):
for i in six.moves.range(CONCURRENCY):
pool.spawn_n(writer, addr, eventlet.green.socket.socket)
pool.waitall()

Expand All @@ -75,7 +76,7 @@ def launch_heavy_threads():
accepter_thread = threading.Thread(None, heavy_accepter, "accepter thread", (server_sock, threads))
accepter_thread.start()
threads.append(accepter_thread)
for i in xrange(CONCURRENCY):
for i in six.moves.range(CONCURRENCY):
client_thread = threading.Thread(None, writer, "writer thread", (addr, socket.socket))
client_thread.start()
threads.append(client_thread)
Expand Down
2 changes: 1 addition & 1 deletion doc/modules/timeout.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
timeout = Timeout(1)
try:
...
except Timeout, t:
except Timeout as t:
if t is not timeout:
raise # not my timeout

Expand Down
3 changes: 2 additions & 1 deletion eventlet/backdoor.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ def backdoor_server(sock, locals=None):
sock.close()


def backdoor((conn, addr), locals=None):
def backdoor(conn_info, locals=None):
"""Sets up an interactive console on a socket with a single connected
client. This does not block the caller, as it spawns a new greenlet to
handle the console. This is meant to be called from within an accept loop
(such as backdoor_server).
"""
conn, addr = conn_info
host, port = addr
print("backdoor to %s:%s" % (host, port))
fl = conn.makefile("rw")
Expand Down
8 changes: 6 additions & 2 deletions eventlet/green/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
error = __select.error
from eventlet.greenthread import getcurrent
from eventlet.hubs import get_hub
from eventlet.support import six


__patched__ = ['select']


def get_fileno(obj):
# The purpose of this function is to exactly replicate
# the behavior of the select module when confronted with
Expand All @@ -13,15 +16,16 @@ def get_fileno(obj):
try:
f = obj.fileno
except AttributeError:
if not isinstance(obj, (int, long)):
if not isinstance(obj, six.integer_types):
raise TypeError("Expected int or long, got " + type(obj))
return obj
else:
rv = f()
if not isinstance(rv, (int, long)):
if not isinstance(rv, six.integer_types):
raise TypeError("Expected int or long, got " + type(rv))
return rv


def select(read_list, write_list, error_list, timeout=None):
# error checking like this is required by the stdlib unit tests
if timeout is not None:
Expand Down
27 changes: 12 additions & 15 deletions eventlet/greenio.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from eventlet.support import get_errno
from eventlet.support import get_errno, six
from eventlet.hubs import trampoline
BUFFER_SIZE = 4096

Expand All @@ -18,13 +18,11 @@
if sys.platform[:3] == "win":
CONNECT_ERR.add(errno.WSAEINVAL) # Bug 67

# Emulate _fileobject class in 3.x implementation
# Eventually this internal socket structure could be replaced with makefile calls.
try:
if six.PY3:
from io import IOBase as file
_fileobject = socket.SocketIO
elif six.PY2:
_fileobject = socket._fileobject
except AttributeError:
def _fileobject(sock, *args, **kwargs):
return _original_socket.makefile(sock, *args, **kwargs)


def socket_connect(descriptor, address):
Expand Down Expand Up @@ -123,7 +121,7 @@ class GreenSocket(object):
"""
def __init__(self, family_or_realsock=socket.AF_INET, *args, **kwargs):
should_set_nonblocking = kwargs.pop('set_nonblocking', True)
if isinstance(family_or_realsock, (int, long)):
if isinstance(family_or_realsock, six.integer_types):
fd = _original_socket(family_or_realsock, *args, **kwargs)
else:
fd = family_or_realsock
Expand Down Expand Up @@ -427,10 +425,10 @@ class GreenPipe(_fileobject):
- file argument can be descriptor, file name or file object.
"""
def __init__(self, f, mode='r', bufsize=-1):
if not isinstance(f, (basestring, int, file)):
if not isinstance(f, six.string_types + (int, file)):
raise TypeError('f(ile) should be int, str, unicode or file, not %r' % f)

if isinstance(f, basestring):
if isinstance(f, six.string_types):
f = open(f, mode, 0)

if isinstance(f, int):
Expand Down Expand Up @@ -467,12 +465,11 @@ def close(self):
'write', 'xreadlines', '__iter__', 'writelines']:
setattr(self, method, _operationOnClosedFile)

if getattr(file, '__enter__', None):
def __enter__(self):
return self
def __enter__(self):
return self

def __exit__(self, *args):
self.close()
def __exit__(self, *args):
self.close()

def readinto(self, buf):
data = self.read(len(buf)) # FIXME could it be done without allocating intermediate?
Expand Down
Loading

0 comments on commit 2b2f0a9

Please sign in to comment.