File: encoding.py

package info (click to toggle)
python-gitdb 2.0.0-2~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 740 kB
  • sloc: python: 3,249; makefile: 13
file content (31 lines) | stat: -rw-r--r-- 617 bytes parent folder | download | duplicates (3)
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
from gitdb.utils import compat

if compat.PY3:
    string_types = (str, )
    text_type = str
else:
    string_types = (basestring, )
    text_type = unicode


def force_bytes(data, encoding="ascii"):
    if isinstance(data, bytes):
        return data

    if isinstance(data, string_types):
        return data.encode(encoding)

    return data


def force_text(data, encoding="utf-8"):
    if isinstance(data, text_type):
        return data

    if isinstance(data, bytes):
        return data.decode(encoding)

    if compat.PY3:
        return text_type(data, encoding)
    else:
        return text_type(data)