Package proton ::
Module _compat
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 """
21 Utilities to help Proton support both python2 and python3.
22 """
23
24 import sys
25 import types
26 IS_PY2 = sys.version_info[0] == 2
27 IS_PY3 = sys.version_info[0] == 3
28
29 if IS_PY3:
30 INT_TYPES = (int,)
31 TEXT_TYPES = (str,)
32 STRING_TYPES = (str,)
33 BINARY_TYPES = (bytes,)
34 CLASS_TYPES = (type,)
35
36 - def raise_(t, v=None, tb=None):
37 """Mimic the old 2.x raise behavior:
38 Raise an exception of type t with value v using optional traceback tb
39 """
40 if v is None:
41 v = t()
42 if tb is None:
43 raise v
44 else:
45 raise v.with_traceback(tb)
46
48 return iter(d.items())
49
52
53 - def str2bin(s, encoding='latin-1'):
54 """Convert str to binary type"""
55 return s.encode(encoding)
56
59
60 else:
61 INT_TYPES = (int, long)
62 TEXT_TYPES = (unicode,)
63
64 STRING_TYPES = (basestring,)
65 BINARY_TYPES = (str,)
66 CLASS_TYPES = (type, types.ClassType)
67
68
69
70 exec("""def raise_(t, v=None, tb=None):
71 raise t, v, tb
72 """)
73
76
79
80 - def str2bin(s, encoding='latin-1'):
82
84 return unicode(s, "unicode_escape")
85