diff options
author | Victor Stinner <vstinner@redhat.com> | 2015-07-11 12:01:55 +0200 |
---|---|---|
committer | Victor Stinner <vstinner@redhat.com> | 2015-07-11 12:01:55 +0200 |
commit | 16b46b383ca84dcdc3aecc6ccf735499d497fef0 (patch) | |
tree | 656e53bfdab043ce00f26b23dc43cc123bc29193 | |
parent | 61199a49a0782b1cf83f7dc32719b456d950ba3f (diff) | |
parent | 1f85dc7d6ba07d1a8be18446d251ebe5b7575d34 (diff) | |
download | trollius-git-16b46b383ca84dcdc3aecc6ccf735499d497fef0.tar.gz |
Merge asyncio into trollius
-rw-r--r-- | trollius/compat.py | 71 | ||||
-rw-r--r-- | trollius/coroutines.py | 5 | ||||
-rw-r--r-- | trollius/events.py | 7 | ||||
-rw-r--r-- | trollius/futures.py | 12 | ||||
-rw-r--r-- | trollius/locks.py | 38 | ||||
-rw-r--r-- | trollius/queues.py | 10 | ||||
-rw-r--r-- | trollius/streams.py | 6 | ||||
-rw-r--r-- | trollius/tasks.py | 4 | ||||
-rw-r--r-- | trollius/transports.py | 7 | ||||
-rwxr-xr-x | update-asyncio-step2.sh | 5 | ||||
-rwxr-xr-x | update-asyncio-step3.sh | 2 |
11 files changed, 111 insertions, 56 deletions
diff --git a/trollius/compat.py b/trollius/compat.py new file mode 100644 index 0000000..220a90f --- /dev/null +++ b/trollius/compat.py @@ -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) diff --git a/trollius/coroutines.py b/trollius/coroutines.py index 8bd0b7e..eea8c60 100644 --- a/trollius/coroutines.py +++ b/trollius/coroutines.py @@ -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 diff --git a/trollius/events.py b/trollius/events.py index 859ac07..5261161 100644 --- a/trollius/events.py +++ b/trollius/events.py @@ -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 diff --git a/trollius/futures.py b/trollius/futures.py index 48225b9..9899ae5 100644 --- a/trollius/futures.py +++ b/trollius/futures.py @@ -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 diff --git a/trollius/locks.py b/trollius/locks.py index 43a6500..0d6de65 100644 --- a/trollius/locks.py +++ b/trollius/locks.py @@ -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: - - # 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 __aexit__(self, exc_type, exc, tb): - # self.release() + # if compat.PY35: + + # 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 __aexit__(self, exc_type, exc, tb): + # self.release() class Lock(_ContextManagerMixin): diff --git a/trollius/queues.py b/trollius/queues.py index 49a23d9..9c77c60 100644 --- a/trollius/queues.py +++ b/trollius/queues.py @@ -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') diff --git a/trollius/streams.py b/trollius/streams.py index c9532dc..b7ba4c5 100644 --- a/trollius/streams.py +++ b/trollius/streams.py @@ -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() diff --git a/trollius/tasks.py b/trollius/tasks.py index 2f461b3..8de3e62 100644 --- a/trollius/tasks.py +++ b/trollius/tasks.py @@ -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 = { diff --git a/trollius/transports.py b/trollius/transports.py index 5bdbdaf..10bad51 100644 --- a/trollius/transports.py +++ b/trollius/transports.py @@ -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. diff --git a/update-asyncio-step2.sh b/update-asyncio-step2.sh index 558940b..f813b6d 100755 --- a/update-asyncio-step2.sh +++ b/update-asyncio-step2.sh @@ -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 diff --git a/update-asyncio-step3.sh b/update-asyncio-step3.sh index d7d63ba..cdd3208 100755 --- a/update-asyncio-step3.sh +++ b/update-asyncio-step3.sh @@ -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' |