summaryrefslogtreecommitdiffstats
path: root/litecli/encodingutils.py
blob: 6caf14d5055244e7fa6819572d9afc88303cb917 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from litecli.compat import PY2


if PY2:
    binary_type = str
    string_types = basestring
    text_type = unicode
else:
    binary_type = bytes
    string_types = str
    text_type = str


def unicode2utf8(arg):
    """Convert strings to UTF8-encoded bytes.

    Only in Python 2. In Python 3 the args are expected as unicode.

    """

    if PY2 and isinstance(arg, text_type):
        return arg.encode("utf-8")
    return arg


def utf8tounicode(arg):
    """Convert UTF8-encoded bytes to strings.

    Only in Python 2. In Python 3 the errors are returned as strings.

    """

    if PY2 and isinstance(arg, binary_type):
        return arg.decode("utf-8")
    return arg