1# -*- coding: utf-8 -*-
2from __future__ import unicode_literals
3
4from litecli.compat import PY2
5
6
7if PY2:
8    binary_type = str
9    string_types = basestring
10    text_type = unicode
11else:
12    binary_type = bytes
13    string_types = str
14    text_type = str
15
16
17def unicode2utf8(arg):
18    """Convert strings to UTF8-encoded bytes.
19
20    Only in Python 2. In Python 3 the args are expected as unicode.
21
22    """
23
24    if PY2 and isinstance(arg, text_type):
25        return arg.encode("utf-8")
26    return arg
27
28
29def utf8tounicode(arg):
30    """Convert UTF8-encoded bytes to strings.
31
32    Only in Python 2. In Python 3 the errors are returned as strings.
33
34    """
35
36    if PY2 and isinstance(arg, binary_type):
37        return arg.decode("utf-8")
38    return arg
39