• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import sys
2
3__all__ = ['b', 'basestring_', 'bytes', 'next', 'is_unicode']
4
5if sys.version < "3":
6    b = bytes = str
7    basestring_ = basestring
8else:
9
10    def b(s):
11        if isinstance(s, str):
12            return s.encode('latin1')
13        return bytes(s)
14    basestring_ = (bytes, str)
15    bytes = bytes
16text = str
17
18if sys.version < "3":
19
20    def next(obj):
21        return obj.next()
22else:
23    next = next
24
25if sys.version < "3":
26
27    def is_unicode(obj):
28        return isinstance(obj, unicode)
29else:
30
31    def is_unicode(obj):
32        return isinstance(obj, str)
33
34
35def coerce_text(v):
36    if not isinstance(v, basestring_):
37        if sys.version < "3":
38            attr = '__unicode__'
39        else:
40            attr = '__str__'
41        if hasattr(v, attr):
42            return unicode(v)
43        else:
44            return bytes(v)
45    return v
46