dag: update column allocation algorithm description
[git-cola.git] / cola / compat.py
bloba39b469c23ae113d7269bcd39839bb93c104b934
1 from __future__ import absolute_import, division, unicode_literals
2 import os
3 import sys
4 try:
5 import urllib2 as parse
6 except ImportError:
7 # Python 3
8 from urllib import parse
11 PY2 = sys.version_info[0] == 2
12 PY3 = sys.version_info[0] >= 3
13 PY26_PLUS = PY2 and sys.version_info[1] >= 6
14 WIN32 = sys.platform == 'win32' or sys.platform == 'cygwin'
16 if PY3:
17 def bstr(x):
18 # pylint: disable=bytes-builtin
19 return bytes(x, encoding='utf8')
20 elif PY26_PLUS:
21 # pylint: disable=bytes-builtin
22 bstr = bytes
23 else:
24 # Python <= 2.5
25 bstr = str
27 if PY3:
28 def bchr(i):
29 return bytes([i])
31 int_types = (int,)
32 maxsize = sys.maxsize
33 ustr = str
34 unichr = chr
35 else:
36 bchr = chr
37 maxsize = sys.maxint
38 # pylint: disable=unicode-builtin
39 ustr = unicode
40 # pylint: disable=unichr-builtin
41 unichr = unichr
42 int_types = (int, long) # pylint: disable=long-builtin
45 def setenv(key, value):
46 """Compatibility wrapper for setting environment variables
48 Why? win32 requires putenv(). UNIX only requires os.environ.
50 """
51 if not PY3 and type(value) is ustr:
52 value = value.encode('utf-8', 'replace')
53 os.environ[key] = value
54 os.putenv(key, value)
57 def unsetenv(key):
58 """Compatibility wrapper for unsetting environment variables"""
59 try:
60 del os.environ[key]
61 except:
62 pass
63 if hasattr(os, 'unsetenv'):
64 os.unsetenv(key)