Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 71080fc

Browse files
committed
asyncio: Add asyncio.compat module
Move compatibility helpers for the different Python versions to a new asyncio.compat module.
1 parent f05b79d commit 71080fc

8 files changed

Lines changed: 35 additions & 30 deletions

File tree

Lib/asyncio/compat.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Compatibility helpers for the different Python versions."""
2+
3+
import sys
4+
5+
PY34 = sys.version_info >= (3, 4)
6+
PY35 = sys.version_info >= (3, 5)
7+
8+
9+
def flatten_list_bytes(list_of_data):
10+
"""Concatenate a sequence of bytes-like objects."""
11+
if not PY34:
12+
# On Python 3.3 and older, bytes.join() doesn't handle
13+
# memoryview.
14+
list_of_data = (
15+
bytes(data) if isinstance(data, memoryview) else data
16+
for data in list_of_data)
17+
return b''.join(list_of_data)

Lib/asyncio/coroutines.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@
99
import traceback
1010
import types
1111

12+
from . import compat
1213
from . import events
1314
from . import futures
1415
from .log import logger
1516

1617

17-
_PY35 = sys.version_info >= (3, 5)
18-
19-
2018
# Opcode of "yield from" instruction
2119
_YIELD_FROM = opcode.opmap['YIELD_FROM']
2220

@@ -140,7 +138,7 @@ def gi_running(self):
140138
def gi_code(self):
141139
return self.gen.gi_code
142140

143-
if _PY35:
141+
if compat.PY35:
144142

145143
__await__ = __iter__ # make compatible with 'await' expression
146144

Lib/asyncio/events.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@
1717
import threading
1818
import traceback
1919

20-
21-
_PY34 = sys.version_info >= (3, 4)
20+
from asyncio import compat
2221

2322

2423
def _get_function_source(func):
25-
if _PY34:
24+
if compat.PY34:
2625
func = inspect.unwrap(func)
2726
elif hasattr(func, '__wrapped__'):
2827
func = func.__wrapped__
@@ -31,7 +30,7 @@ def _get_function_source(func):
3130
return (code.co_filename, code.co_firstlineno)
3231
if isinstance(func, functools.partial):
3332
return _get_function_source(func.func)
34-
if _PY34 and isinstance(func, functools.partialmethod):
33+
if compat.PY34 and isinstance(func, functools.partialmethod):
3534
return _get_function_source(func.func)
3635
return None
3736

Lib/asyncio/futures.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@
1111
import sys
1212
import traceback
1313

14+
from . import compat
1415
from . import events
1516

1617
# States for Future.
1718
_PENDING = 'PENDING'
1819
_CANCELLED = 'CANCELLED'
1920
_FINISHED = 'FINISHED'
2021

21-
_PY34 = sys.version_info >= (3, 4)
22-
_PY35 = sys.version_info >= (3, 5)
23-
2422
Error = concurrent.futures._base.Error
2523
CancelledError = concurrent.futures.CancelledError
2624
TimeoutError = concurrent.futures.TimeoutError
@@ -199,7 +197,7 @@ def __repr__(self):
199197
# On Python 3.3 and older, objects with a destructor part of a reference
200198
# cycle are never destroyed. It's not more the case on Python 3.4 thanks
201199
# to the PEP 442.
202-
if _PY34:
200+
if compat.PY34:
203201
def __del__(self):
204202
if not self._log_traceback:
205203
# set_exception() was not called, or result() or exception()
@@ -352,7 +350,7 @@ def set_exception(self, exception):
352350
self._exception = exception
353351
self._state = _FINISHED
354352
self._schedule_callbacks()
355-
if _PY34:
353+
if compat.PY34:
356354
self._log_traceback = True
357355
else:
358356
self._tb_logger = _TracebackLogger(self, exception)
@@ -388,7 +386,7 @@ def __iter__(self):
388386
assert self.done(), "yield from wasn't used with future"
389387
return self.result() # May raise too.
390388

391-
if _PY35:
389+
if compat.PY35:
392390
__await__ = __iter__ # make compatible with 'await' expression
393391

394392

Lib/asyncio/locks.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
import collections
66
import sys
77

8+
from . import compat
89
from . import events
910
from . import futures
1011
from .coroutines import coroutine
1112

1213

13-
_PY35 = sys.version_info >= (3, 5)
14-
15-
1614
class _ContextManager:
1715
"""Context manager.
1816
@@ -70,7 +68,7 @@ def __iter__(self):
7068
yield from self.acquire()
7169
return _ContextManager(self)
7270

73-
if _PY35:
71+
if compat.PY35:
7472

7573
def __await__(self):
7674
# To make "with await lock" work.

Lib/asyncio/streams.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
__all__.extend(['open_unix_connection', 'start_unix_server'])
1313

1414
from . import coroutines
15+
from . import compat
1516
from . import events
1617
from . import futures
1718
from . import protocols
@@ -20,7 +21,6 @@
2021

2122

2223
_DEFAULT_LIMIT = 2**16
23-
_PY35 = sys.version_info >= (3, 5)
2424

2525

2626
class IncompleteReadError(EOFError):
@@ -488,7 +488,7 @@ def readexactly(self, n):
488488

489489
return b''.join(blocks)
490490

491-
if _PY35:
491+
if compat.PY35:
492492
@coroutine
493493
def __aiter__(self):
494494
return self

Lib/asyncio/tasks.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@
1616
import warnings
1717
import weakref
1818

19+
from . import compat
1920
from . import coroutines
2021
from . import events
2122
from . import futures
2223
from .coroutines import coroutine
2324

24-
_PY34 = (sys.version_info >= (3, 4))
25-
2625

2726
class Task(futures.Future):
2827
"""A coroutine wrapped in a Future."""
@@ -83,7 +82,7 @@ def __init__(self, coro, *, loop=None):
8382
# On Python 3.3 or older, objects with a destructor that are part of a
8483
# reference cycle are never destroyed. That's not the case any more on
8584
# Python 3.4 thanks to the PEP 442.
86-
if _PY34:
85+
if compat.PY34:
8786
def __del__(self):
8887
if self._state == futures._PENDING and self._log_destroy_pending:
8988
context = {

Lib/asyncio/transports.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import sys
44

5-
_PY34 = sys.version_info >= (3, 4)
5+
from asyncio import compat
66

77
__all__ = ['BaseTransport', 'ReadTransport', 'WriteTransport',
88
'Transport', 'DatagramTransport', 'SubprocessTransport',
@@ -94,12 +94,8 @@ def writelines(self, list_of_data):
9494
The default implementation concatenates the arguments and
9595
calls write() on the result.
9696
"""
97-
if not _PY34:
98-
# In Python 3.3, bytes.join() doesn't handle memoryview.
99-
list_of_data = (
100-
bytes(data) if isinstance(data, memoryview) else data
101-
for data in list_of_data)
102-
self.write(b''.join(list_of_data))
97+
data = compat.flatten_list_bytes(list_of_data)
98+
self.write(data)
10399

104100
def write_eof(self):
105101
"""Close the write end after flushing buffered data.

0 commit comments

Comments
 (0)