doc: add 0xflotus to the credits
[git-cola.git] / cola / compat.py
blob47862402498a4ee35e000136f0ace87c2d43399f
1 # pylint: disable=unused-import,redefined-builtin,undefined-variable
2 from __future__ import absolute_import, division, unicode_literals
3 import os
4 import sys
5 try:
6 import urllib2 as parse # noqa
7 except ImportError:
8 # Python 3
9 from urllib import parse # noqa
11 try:
12 # Python 2.7+
13 from collections import OrderedDict as odict # noqa
14 except ImportError:
15 from .ordered_dict import OrderedDict as odict # noqa
18 PY2 = sys.version_info[0] == 2
19 PY3 = sys.version_info[0] >= 3
20 PY26_PLUS = PY2 and sys.version_info[1] >= 6
21 WIN32 = sys.platform == 'win32' or sys.platform == 'cygwin'
22 ENCODING = 'utf-8'
25 if PY3:
26 def bstr(x, encoding=ENCODING):
27 return bytes(x, encoding=encoding)
29 elif PY26_PLUS:
30 bstr = bytes
31 else:
32 # Python <= 2.5
33 bstr = str
35 if PY3:
36 def bchr(i):
37 return bytes([i])
39 int_types = (int,)
40 maxsize = sys.maxsize
41 ustr = str
42 uchr = chr
43 else:
44 bchr = chr
45 maxsize = 2 ** 31
46 # pylint: disable=unicode-builtin
47 ustr = unicode # noqa
48 # pylint: disable=unichr-builtin
49 uchr = unichr # noqa
50 # pylint: disable=long-builtin
51 int_types = (int, long) # noqa
54 def setenv(key, value):
55 """Compatibility wrapper for setting environment variables
57 Why? win32 requires putenv(). UNIX only requires os.environ.
59 """
60 if not PY3 and isinstance(value, ustr):
61 value = value.encode(ENCODING, 'replace')
62 os.environ[key] = value
63 os.putenv(key, value)
66 def unsetenv(key):
67 """Compatibility wrapper for unsetting environment variables"""
68 try:
69 del os.environ[key]
70 except KeyError:
71 pass
72 if hasattr(os, 'unsetenv'):
73 os.unsetenv(key)