1
|
|
|
"""Code for supporting compatibility across python versions.""" |
2
|
|
|
|
3
|
5 |
|
import sys |
4
|
5 |
|
import warnings |
5
|
|
|
from imp import find_module as original_find_module |
6
|
|
|
|
7
|
5 |
|
|
8
|
|
|
IS_PYTHON3 = sys.version_info >= (3, 0) |
9
|
|
|
|
10
|
5 |
|
|
11
|
3 |
|
if IS_PYTHON3: |
12
|
|
|
def find_module(fullname, path): |
13
|
|
|
"""Compatibility wrapper for imp.find_module. |
14
|
|
|
|
15
|
|
|
Automatically decodes arguments of find_module, in Python3 |
16
|
|
|
they must be Unicode |
17
|
|
|
""" |
18
|
|
|
if isinstance(fullname, bytes): |
19
|
|
|
fullname = fullname.decode() |
20
|
|
|
if isinstance(path, bytes): |
21
|
|
|
path = path.decode() |
22
|
|
|
elif isinstance(path, list): |
23
|
|
|
newpath = [] |
24
|
|
|
for element in path: |
25
|
|
|
if isinstance(element, bytes): |
26
|
|
|
newpath.append(element.decode()) |
27
|
|
|
else: |
28
|
|
|
newpath.append(element) |
29
|
|
|
path = newpath |
30
|
|
|
return original_find_module(fullname, path) |
31
|
|
|
|
32
|
3 |
|
# There is no 'long' type in Python3 just int |
33
|
3 |
|
long = int |
34
|
|
|
unicode_errors_default = 'surrogateescape' |
35
|
2 |
|
else: |
36
|
2 |
|
find_module = original_find_module |
37
|
|
|
unicode_errors_default = 'strict' |
38
|
5 |
|
|
39
|
|
|
NUM_TYPES = (int, long, float) |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
def check_async(async_, kwargs, default): |
43
|
|
|
"""Return a value of 'async' in kwargs or default when async_ is None |
44
|
|
|
|
45
|
|
|
This helper function exists for backward compatibility (See #274). |
46
|
|
|
It shows a warning message when 'async' in kwargs is used to note users. |
47
|
|
|
""" |
48
|
|
|
if async_ is not None: |
49
|
|
|
return async_ |
50
|
|
|
elif 'async' in kwargs: |
51
|
|
|
warnings.warn( |
52
|
|
|
'"async" attribute is deprecated. Use "async_" instead.', |
53
|
|
|
DeprecationWarning, |
54
|
|
|
) |
55
|
|
|
return kwargs.pop('async') |
56
|
|
|
else: |
57
|
|
|
return default |
58
|
|
|
|