summaryrefslogtreecommitdiffstats
path: root/pgcli/encodingutils.py
blob: 279da3a882c6e4f3d6199299f5bc0c840dc7a32e (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
import sys

PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] == 3

text_type = unicode if PY2 else str


def unicode2utf8(arg):
    """
    Only in Python 2. Psycopg2 expects the args as bytes not unicode.
    In Python 3 the args are expected as unicode.
    """

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


def utf8tounicode(arg):
    """
    Only in Python 2. Psycopg2 returns the error message as utf-8.
    In Python 3 the errors are returned as unicode.
    """

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