Merge asyncio into trollius

This commit is contained in:
Victor Stinner
2015-07-11 12:01:55 +02:00
11 changed files with 108 additions and 53 deletions

71
trollius/compat.py Normal file
View File

@@ -0,0 +1,71 @@
"""Compatibility helpers for the different Python versions."""
import sys
# Python 2.6 or older?
PY26 = (sys.version_info < (2, 7))
# Python 3.0 or newer?
PY3 = (sys.version_info >= (3,))
# Python 3.3 or newer?
PY33 = (sys.version_info >= (3, 3))
# Python 3.4 or newer?
PY34 = sys.version_info >= (3, 4)
# Python 3.5 or newer?
PY35 = sys.version_info >= (3, 5)
if PY3:
integer_types = (int,)
bytes_type = bytes
text_type = str
string_types = (bytes, str)
BYTES_TYPES = (bytes, bytearray, memoryview)
else:
integer_types = (int, long,)
bytes_type = str
text_type = unicode
string_types = basestring
if PY26:
BYTES_TYPES = (str, bytearray, buffer)
else: # Python 2.7
BYTES_TYPES = (str, bytearray, memoryview, buffer)
if PY3:
def reraise(tp, value, tb=None):
if value.__traceback__ is not tb:
raise value.with_traceback(tb)
raise value
else:
exec("""def reraise(tp, value, tb=None): raise tp, value, tb""")
def flatten_bytes(data):
"""
Convert bytes-like objects (bytes, bytearray, memoryview, buffer) to
a bytes string.
"""
if not isinstance(data, BYTES_TYPES):
raise TypeError('data argument must be byte-ish (%r)',
type(data))
if PY34:
# In Python 3.4, socket.send() and bytes.join() accept memoryview
# and bytearray
return data
if not data:
return b''
if not PY3 and isinstance(data, (buffer, bytearray)):
return str(data)
elif not PY26 and isinstance(data, memoryview):
return data.tobytes()
else:
return data
def flatten_list_bytes(data):
"""Concatenate a sequence of bytes-like objects."""
data = map(flatten_bytes, data)
return b''.join(data)

View File

@@ -16,9 +16,6 @@ from . import futures
from .log import logger
_PY35 = sys.version_info >= (3, 5)
# Opcode of "yield from" instruction
_YIELD_FROM = opcode.opmap.get('YIELD_FROM', None)
@@ -209,7 +206,7 @@ class CoroWrapper:
def gi_code(self):
return self.gen.gi_code
if _PY35:
if compat.PY35:
__await__ = __iter__ # make compatible with 'await' expression

View File

@@ -28,12 +28,11 @@ except (ImportError, SyntaxError):
# from" if asyncio module is in the Python path
asyncio = None
_PY34 = sys.version_info >= (3, 4)
from trollius import compat
def _get_function_source(func):
if _PY34:
if compat.PY34:
func = inspect.unwrap(func)
elif hasattr(func, '__wrapped__'):
func = func.__wrapped__
@@ -42,7 +41,7 @@ def _get_function_source(func):
return (code.co_filename, code.co_firstlineno)
if isinstance(func, functools.partial):
return _get_function_source(func.func)
if _PY34 and isinstance(func, functools.partialmethod):
if compat.PY34 and isinstance(func, functools.partialmethod):
return _get_function_source(func.func)
return None

View File

@@ -22,9 +22,6 @@ _PENDING = 'PENDING'
_CANCELLED = 'CANCELLED'
_FINISHED = 'FINISHED'
_PY34 = sys.version_info >= (3, 4)
_PY35 = sys.version_info >= (3, 5)
Error = executor.Error
CancelledError = executor.CancelledError
TimeoutError = executor.TimeoutError
@@ -207,7 +204,7 @@ class Future(object):
# On Python 3.3 and older, objects with a destructor part of a reference
# cycle are never destroyed. It's not more the case on Python 3.4 thanks
# to the PEP 442.
if _PY34:
if compat.PY34:
def __del__(self):
if not self._log_traceback:
# set_exception() was not called, or result() or exception()
@@ -377,7 +374,7 @@ class Future(object):
self._exception_tb = sys.exc_info()[2]
self._state = _FINISHED
self._schedule_callbacks()
if _PY34:
if compat.PY34:
self._log_traceback = True
else:
self._tb_logger = _TracebackLogger(self, exception)
@@ -425,9 +422,8 @@ class Future(object):
result = other.result()
self.set_result(result)
#if _PY35:
# __await__ = __iter__ # make compatible with 'await' expression
if compat.PY35:
__await__ = __iter__ # make compatible with 'await' expression
if events.asyncio is not None:
# Accept also asyncio Future objects for interoperability

View File

@@ -5,14 +5,12 @@ __all__ = ['Lock', 'Event', 'Condition', 'Semaphore', 'BoundedSemaphore']
import collections
import sys
from . import compat
from . import events
from . import futures
from .coroutines import coroutine, From, Return
_PY35 = sys.version_info >= (3, 5)
class _ContextManager:
"""Context manager.
@@ -54,23 +52,23 @@ class _ContextManagerMixin(object):
pass
# FIXME: support PEP 492?
#if _PY35:
# if compat.PY35:
# def __await__(self):
# # To make "with await lock" work.
# yield from self.acquire()
# return _ContextManager(self)
# def __await__(self):
# # To make "with await lock" work.
# yield from self.acquire()
# return _ContextManager(self)
# @coroutine
# def __aenter__(self):
# yield from self.acquire()
# # We have no use for the "as ..." clause in the with
# # statement for locks.
# return None
# @coroutine
# def __aenter__(self):
# yield from self.acquire()
# # We have no use for the "as ..." clause in the with
# # statement for locks.
# return None
# @coroutine
# def __aexit__(self, exc_type, exc, tb):
# self.release()
# @coroutine
# def __aexit__(self, exc_type, exc, tb):
# self.release()
class Lock(_ContextManagerMixin):

View File

@@ -1,11 +1,11 @@
"""Queues"""
__all__ = ['Queue', 'PriorityQueue', 'LifoQueue', 'QueueFull', 'QueueEmpty',
'JoinableQueue']
__all__ = ['Queue', 'PriorityQueue', 'LifoQueue', 'QueueFull', 'QueueEmpty']
import collections
import heapq
from . import compat
from . import events
from . import futures
from . import locks
@@ -290,5 +290,7 @@ class LifoQueue(Queue):
return self._queue.pop()
JoinableQueue = Queue
"""Deprecated alias for Queue."""
if not compat.PY35:
JoinableQueue = Queue
"""Deprecated alias for Queue."""
__all__.append('JoinableQueue')

View File

@@ -12,6 +12,7 @@ if hasattr(socket, 'AF_UNIX'):
__all__.extend(['open_unix_connection', 'start_unix_server'])
from . import coroutines
from . import compat
from . import events
from . import futures
from . import protocols
@@ -21,7 +22,6 @@ from .log import logger
_DEFAULT_LIMIT = 2**16
_PY35 = sys.version_info >= (3, 5)
class IncompleteReadError(EOFError):
@@ -499,11 +499,11 @@ class StreamReader(object):
raise Return(b''.join(blocks))
# FIXME: should we support __aiter__ and __anext__ in Trollius?
#if _PY35:
#if compat.PY35:
# @coroutine
# def __aiter__(self):
# return self
#
# @coroutine
# def __anext__(self):
# val = yield from self.readline()

View File

@@ -27,8 +27,6 @@ from .locks import Lock, Condition, Semaphore, _ContextManager
from .coroutines import coroutine, From, Return
_PY34 = (sys.version_info >= (3, 4))
@coroutine
def _lock_coroutine(lock):
@@ -95,7 +93,7 @@ class Task(futures.Future):
# On Python 3.3 or older, objects with a destructor that are part of a
# reference cycle are never destroyed. That's not the case any more on
# Python 3.4 thanks to the PEP 442.
if _PY34:
if compat.PY34:
def __del__(self):
if self._state == futures._PENDING and self._log_destroy_pending:
context = {

View File

@@ -1,9 +1,8 @@
"""Abstract Transport class."""
import sys
from .compat import flatten_bytes
_PY34 = sys.version_info >= (3, 4)
from trollius import compat
__all__ = ['BaseTransport', 'ReadTransport', 'WriteTransport',
'Transport', 'DatagramTransport', 'SubprocessTransport',
@@ -95,8 +94,8 @@ class WriteTransport(BaseTransport):
The default implementation concatenates the arguments and
calls write() on the result.
"""
data = map(flatten_bytes, list_of_data)
self.write(b''.join(data))
data = compat.flatten_list_bytes(list_of_data)
self.write(data)
def write_eof(self):
"""Close the write end after flushing buffered data.

View File

@@ -27,11 +27,6 @@ if $(grep -q -E '\{[^0-9].*format' */*.py); then
grep -E '\{[^0-9].*format' */*.py
exit 1
fi
if $(grep -q -E 'unittest\.skip' tests/*.py); then
echo "Issues with Python 2.6 compatibility:"
grep -E 'unittest\.skip' tests/*.py
exit 1
fi
if $(grep -q -F 'super()' */*.py); then
echo "Issues with Python 2.6 compatibility:"
grep -F 'super()' */*.py

View File

@@ -1,4 +1,4 @@
set -e -x
./update-tulip-step2.sh
./update-asyncio-step2.sh
tox -e py27,py34
git commit -m 'Merge asyncio into trollius'