1# Copyright: 2013 Paul Traylor
2# These sources are released under the terms of the MIT license: see LICENSE
3
4"""
5Python2.5 and Python3.3 compatibility shim
6
7Heavily inspirted by the "six" library.
8https://pypi.python.org/pypi/six
9"""
10
11import sys
12
13PY2 = sys.version_info[0] == 2
14PY3 = sys.version_info[0] == 3
15
16if PY3:
17	def b(s):
18		if isinstance(s, bytes):
19			return s
20		return s.encode('utf8', 'replace')
21
22	def u(s):
23		if isinstance(s, bytes):
24			return s.decode('utf8', 'replace')
25		return s
26
27	from io import BytesIO as StringIO
28	from configparser import RawConfigParser
29else:
30	def b(s):
31		if isinstance(s, unicode):
32			return s.encode('utf8', 'replace')
33		return s
34
35	def u(s):
36		if isinstance(s, unicode):
37			return s
38		if isinstance(s, int):
39			s = str(s)
40		return unicode(s, "utf8", "replace")
41
42	from StringIO import StringIO
43	from ConfigParser import RawConfigParser
44
45b.__doc__ = "Ensure we have a byte string"
46u.__doc__ = "Ensure we have a unicode string"
47