pynvim.compat.find_module()   B
last analyzed

Complexity

Conditions 6

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 34.317

Importance

Changes 0
Metric Value
cc 6
eloc 13
nop 2
dl 0
loc 19
rs 8.6666
c 0
b 0
f 0
ccs 1
cts 13
cp 0.0769
crap 34.317
1
"""Code for compatibility across Python versions."""
2
3 6
import sys
4 6
import warnings
5 6
from imp import find_module as original_find_module
6
7
8 6
IS_PYTHON3 = sys.version_info >= (3, 0)
9
10
11 6
if IS_PYTHON3:
12 4
    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
    # There is no 'long' type in Python3 just int
33 4
    long = int
34 4
    unicode_errors_default = 'surrogateescape'
35
else:
36 2
    find_module = original_find_module
37 2
    unicode_errors_default = 'strict'
38
39 6
NUM_TYPES = (int, long, float)
0 ignored issues
show
introduced by
The variable long does not seem to be defined for all execution paths.
Loading history...
40
41
42 6
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 6
    if async_ is not None:
49 6
        return async_
50 6
    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