Merge asyncio into trollius
This commit is contained in:
71
trollius/compat.py
Normal file
71
trollius/compat.py
Normal 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)
|
||||||
@@ -16,9 +16,6 @@ from . import futures
|
|||||||
from .log import logger
|
from .log import logger
|
||||||
|
|
||||||
|
|
||||||
_PY35 = sys.version_info >= (3, 5)
|
|
||||||
|
|
||||||
|
|
||||||
# Opcode of "yield from" instruction
|
# Opcode of "yield from" instruction
|
||||||
_YIELD_FROM = opcode.opmap.get('YIELD_FROM', None)
|
_YIELD_FROM = opcode.opmap.get('YIELD_FROM', None)
|
||||||
|
|
||||||
@@ -209,7 +206,7 @@ class CoroWrapper:
|
|||||||
def gi_code(self):
|
def gi_code(self):
|
||||||
return self.gen.gi_code
|
return self.gen.gi_code
|
||||||
|
|
||||||
if _PY35:
|
if compat.PY35:
|
||||||
|
|
||||||
__await__ = __iter__ # make compatible with 'await' expression
|
__await__ = __iter__ # make compatible with 'await' expression
|
||||||
|
|
||||||
|
|||||||
@@ -28,12 +28,11 @@ except (ImportError, SyntaxError):
|
|||||||
# from" if asyncio module is in the Python path
|
# from" if asyncio module is in the Python path
|
||||||
asyncio = None
|
asyncio = None
|
||||||
|
|
||||||
|
from trollius import compat
|
||||||
_PY34 = sys.version_info >= (3, 4)
|
|
||||||
|
|
||||||
|
|
||||||
def _get_function_source(func):
|
def _get_function_source(func):
|
||||||
if _PY34:
|
if compat.PY34:
|
||||||
func = inspect.unwrap(func)
|
func = inspect.unwrap(func)
|
||||||
elif hasattr(func, '__wrapped__'):
|
elif hasattr(func, '__wrapped__'):
|
||||||
func = func.__wrapped__
|
func = func.__wrapped__
|
||||||
@@ -42,7 +41,7 @@ def _get_function_source(func):
|
|||||||
return (code.co_filename, code.co_firstlineno)
|
return (code.co_filename, code.co_firstlineno)
|
||||||
if isinstance(func, functools.partial):
|
if isinstance(func, functools.partial):
|
||||||
return _get_function_source(func.func)
|
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 _get_function_source(func.func)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|||||||
@@ -22,9 +22,6 @@ _PENDING = 'PENDING'
|
|||||||
_CANCELLED = 'CANCELLED'
|
_CANCELLED = 'CANCELLED'
|
||||||
_FINISHED = 'FINISHED'
|
_FINISHED = 'FINISHED'
|
||||||
|
|
||||||
_PY34 = sys.version_info >= (3, 4)
|
|
||||||
_PY35 = sys.version_info >= (3, 5)
|
|
||||||
|
|
||||||
Error = executor.Error
|
Error = executor.Error
|
||||||
CancelledError = executor.CancelledError
|
CancelledError = executor.CancelledError
|
||||||
TimeoutError = executor.TimeoutError
|
TimeoutError = executor.TimeoutError
|
||||||
@@ -207,7 +204,7 @@ class Future(object):
|
|||||||
# On Python 3.3 and older, objects with a destructor part of a reference
|
# 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
|
# cycle are never destroyed. It's not more the case on Python 3.4 thanks
|
||||||
# to the PEP 442.
|
# to the PEP 442.
|
||||||
if _PY34:
|
if compat.PY34:
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
if not self._log_traceback:
|
if not self._log_traceback:
|
||||||
# set_exception() was not called, or result() or exception()
|
# set_exception() was not called, or result() or exception()
|
||||||
@@ -377,7 +374,7 @@ class Future(object):
|
|||||||
self._exception_tb = sys.exc_info()[2]
|
self._exception_tb = sys.exc_info()[2]
|
||||||
self._state = _FINISHED
|
self._state = _FINISHED
|
||||||
self._schedule_callbacks()
|
self._schedule_callbacks()
|
||||||
if _PY34:
|
if compat.PY34:
|
||||||
self._log_traceback = True
|
self._log_traceback = True
|
||||||
else:
|
else:
|
||||||
self._tb_logger = _TracebackLogger(self, exception)
|
self._tb_logger = _TracebackLogger(self, exception)
|
||||||
@@ -425,9 +422,8 @@ class Future(object):
|
|||||||
result = other.result()
|
result = other.result()
|
||||||
self.set_result(result)
|
self.set_result(result)
|
||||||
|
|
||||||
#if _PY35:
|
if compat.PY35:
|
||||||
# __await__ = __iter__ # make compatible with 'await' expression
|
__await__ = __iter__ # make compatible with 'await' expression
|
||||||
|
|
||||||
|
|
||||||
if events.asyncio is not None:
|
if events.asyncio is not None:
|
||||||
# Accept also asyncio Future objects for interoperability
|
# Accept also asyncio Future objects for interoperability
|
||||||
|
|||||||
@@ -5,14 +5,12 @@ __all__ = ['Lock', 'Event', 'Condition', 'Semaphore', 'BoundedSemaphore']
|
|||||||
import collections
|
import collections
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from . import compat
|
||||||
from . import events
|
from . import events
|
||||||
from . import futures
|
from . import futures
|
||||||
from .coroutines import coroutine, From, Return
|
from .coroutines import coroutine, From, Return
|
||||||
|
|
||||||
|
|
||||||
_PY35 = sys.version_info >= (3, 5)
|
|
||||||
|
|
||||||
|
|
||||||
class _ContextManager:
|
class _ContextManager:
|
||||||
"""Context manager.
|
"""Context manager.
|
||||||
|
|
||||||
@@ -54,7 +52,7 @@ class _ContextManagerMixin(object):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
# FIXME: support PEP 492?
|
# FIXME: support PEP 492?
|
||||||
#if _PY35:
|
# if compat.PY35:
|
||||||
|
|
||||||
# def __await__(self):
|
# def __await__(self):
|
||||||
# # To make "with await lock" work.
|
# # To make "with await lock" work.
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
"""Queues"""
|
"""Queues"""
|
||||||
|
|
||||||
__all__ = ['Queue', 'PriorityQueue', 'LifoQueue', 'QueueFull', 'QueueEmpty',
|
__all__ = ['Queue', 'PriorityQueue', 'LifoQueue', 'QueueFull', 'QueueEmpty']
|
||||||
'JoinableQueue']
|
|
||||||
|
|
||||||
import collections
|
import collections
|
||||||
import heapq
|
import heapq
|
||||||
|
|
||||||
|
from . import compat
|
||||||
from . import events
|
from . import events
|
||||||
from . import futures
|
from . import futures
|
||||||
from . import locks
|
from . import locks
|
||||||
@@ -290,5 +290,7 @@ class LifoQueue(Queue):
|
|||||||
return self._queue.pop()
|
return self._queue.pop()
|
||||||
|
|
||||||
|
|
||||||
JoinableQueue = Queue
|
if not compat.PY35:
|
||||||
"""Deprecated alias for Queue."""
|
JoinableQueue = Queue
|
||||||
|
"""Deprecated alias for Queue."""
|
||||||
|
__all__.append('JoinableQueue')
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ if hasattr(socket, 'AF_UNIX'):
|
|||||||
__all__.extend(['open_unix_connection', 'start_unix_server'])
|
__all__.extend(['open_unix_connection', 'start_unix_server'])
|
||||||
|
|
||||||
from . import coroutines
|
from . import coroutines
|
||||||
|
from . import compat
|
||||||
from . import events
|
from . import events
|
||||||
from . import futures
|
from . import futures
|
||||||
from . import protocols
|
from . import protocols
|
||||||
@@ -21,7 +22,6 @@ from .log import logger
|
|||||||
|
|
||||||
|
|
||||||
_DEFAULT_LIMIT = 2**16
|
_DEFAULT_LIMIT = 2**16
|
||||||
_PY35 = sys.version_info >= (3, 5)
|
|
||||||
|
|
||||||
|
|
||||||
class IncompleteReadError(EOFError):
|
class IncompleteReadError(EOFError):
|
||||||
@@ -499,11 +499,11 @@ class StreamReader(object):
|
|||||||
raise Return(b''.join(blocks))
|
raise Return(b''.join(blocks))
|
||||||
|
|
||||||
# FIXME: should we support __aiter__ and __anext__ in Trollius?
|
# FIXME: should we support __aiter__ and __anext__ in Trollius?
|
||||||
#if _PY35:
|
#if compat.PY35:
|
||||||
# @coroutine
|
# @coroutine
|
||||||
# def __aiter__(self):
|
# def __aiter__(self):
|
||||||
# return self
|
# return self
|
||||||
|
#
|
||||||
# @coroutine
|
# @coroutine
|
||||||
# def __anext__(self):
|
# def __anext__(self):
|
||||||
# val = yield from self.readline()
|
# val = yield from self.readline()
|
||||||
|
|||||||
@@ -27,8 +27,6 @@ from .locks import Lock, Condition, Semaphore, _ContextManager
|
|||||||
from .coroutines import coroutine, From, Return
|
from .coroutines import coroutine, From, Return
|
||||||
|
|
||||||
|
|
||||||
_PY34 = (sys.version_info >= (3, 4))
|
|
||||||
|
|
||||||
|
|
||||||
@coroutine
|
@coroutine
|
||||||
def _lock_coroutine(lock):
|
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
|
# 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
|
# reference cycle are never destroyed. That's not the case any more on
|
||||||
# Python 3.4 thanks to the PEP 442.
|
# Python 3.4 thanks to the PEP 442.
|
||||||
if _PY34:
|
if compat.PY34:
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
if self._state == futures._PENDING and self._log_destroy_pending:
|
if self._state == futures._PENDING and self._log_destroy_pending:
|
||||||
context = {
|
context = {
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
"""Abstract Transport class."""
|
"""Abstract Transport class."""
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
from .compat import flatten_bytes
|
|
||||||
|
|
||||||
_PY34 = sys.version_info >= (3, 4)
|
from trollius import compat
|
||||||
|
|
||||||
__all__ = ['BaseTransport', 'ReadTransport', 'WriteTransport',
|
__all__ = ['BaseTransport', 'ReadTransport', 'WriteTransport',
|
||||||
'Transport', 'DatagramTransport', 'SubprocessTransport',
|
'Transport', 'DatagramTransport', 'SubprocessTransport',
|
||||||
@@ -95,8 +94,8 @@ class WriteTransport(BaseTransport):
|
|||||||
The default implementation concatenates the arguments and
|
The default implementation concatenates the arguments and
|
||||||
calls write() on the result.
|
calls write() on the result.
|
||||||
"""
|
"""
|
||||||
data = map(flatten_bytes, list_of_data)
|
data = compat.flatten_list_bytes(list_of_data)
|
||||||
self.write(b''.join(data))
|
self.write(data)
|
||||||
|
|
||||||
def write_eof(self):
|
def write_eof(self):
|
||||||
"""Close the write end after flushing buffered data.
|
"""Close the write end after flushing buffered data.
|
||||||
|
|||||||
@@ -27,11 +27,6 @@ if $(grep -q -E '\{[^0-9].*format' */*.py); then
|
|||||||
grep -E '\{[^0-9].*format' */*.py
|
grep -E '\{[^0-9].*format' */*.py
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
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
|
if $(grep -q -F 'super()' */*.py); then
|
||||||
echo "Issues with Python 2.6 compatibility:"
|
echo "Issues with Python 2.6 compatibility:"
|
||||||
grep -F 'super()' */*.py
|
grep -F 'super()' */*.py
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
set -e -x
|
set -e -x
|
||||||
./update-tulip-step2.sh
|
./update-asyncio-step2.sh
|
||||||
tox -e py27,py34
|
tox -e py27,py34
|
||||||
git commit -m 'Merge asyncio into trollius'
|
git commit -m 'Merge asyncio into trollius'
|
||||||
|
|||||||
Reference in New Issue
Block a user