Completed
Push — master ( 7380c8...c7125a )
by Bai
12s
created

qiniu.u()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
cc 1
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
1
# -*- coding: utf-8 -*-
2
3 1
"""
4
pythoncompat
5
"""
6
7 1
import sys
8
9 1
try:
10 1
    import simplejson as json
11 1
except (ImportError, SyntaxError):
12
    # simplejson does not support Python 3.2, it thows a SyntaxError
13
    # because of u'...' Unicode literals.
14 1
    import json  # noqa
15
16
17
# -------
18
# Pythons
19
# -------
20
21 1
_ver = sys.version_info
22
23
#: Python 2.x?
24 1
is_py2 = (_ver[0] == 2)
25
26
#: Python 3.x?
27 1
is_py3 = (_ver[0] == 3)
28
29
30
# ---------
31
# Specifics
32
# ---------
33
34 1
if is_py2:
35 1
    from urlparse import urlparse  # noqa
36 1
    import StringIO
37 1
    StringIO = BytesIO = StringIO.StringIO
38
39 1
    builtin_str = str
40 1
    bytes = str
41 1
    str = unicode  # noqa
42 1
    basestring = basestring  # noqa
43 1
    numeric_types = (int, long, float)  # noqa
44
45 1
    def b(data):
46 1
        return bytes(data)
47
48 1
    def s(data):
49 1
        return bytes(data)
50
51 1
    def u(data):
52
        return unicode(data, 'unicode_escape')  # noqa
53
54
elif is_py3:
55
    from urllib.parse import urlparse  # noqa
56
    import io
57
    StringIO = io.StringIO
58
    BytesIO = io.BytesIO
59
60
    builtin_str = str
61
    str = str
62
    bytes = bytes
63
    basestring = (str, bytes)
64
    numeric_types = (int, float)
65
66
    def b(data):
67
        if isinstance(data, str):
68
            return data.encode('utf-8')
69
        return data
70
71
    def s(data):
72
        if isinstance(data, bytes):
73
            data = data.decode('utf-8')
74
        return data
75
76
    def u(data):
77
        return data
78