Package proton :: Module _compat
[frames] | no frames]

Source Code for Module proton._compat

 1  # 
 2  # Licensed to the Apache Software Foundation (ASF) under one 
 3  # or more contributor license agreements.  See the NOTICE file 
 4  # distributed with this work for additional information 
 5  # regarding copyright ownership.  The ASF licenses this file 
 6  # to you under the Apache License, Version 2.0 (the 
 7  # "License"); you may not use this file except in compliance 
 8  # with the License.  You may obtain a copy of the License at 
 9  # 
10  #   http://www.apache.org/licenses/LICENSE-2.0 
11  # 
12  # Unless required by applicable law or agreed to in writing, 
13  # software distributed under the License is distributed on an 
14  # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
15  # KIND, either express or implied.  See the License for the 
16  # specific language governing permissions and limitations 
17  # under the License. 
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
47 - def bin2str(s, encoding='utf-8'):
48 return s
49
50 - def iteritems(d):
51 return iter(d.items())
52
53 - def unichar(i):
54 return chr(i)
55
56 - def str2bin(s, encoding='latin-1'):
57 """Convert str to binary type""" 58 return s.encode(encoding)
59
60 - def str2unicode(s):
61 return s
62 63 else: 64 INT_TYPES = (int, long) 65 TEXT_TYPES = (unicode,) 66 # includes both unicode and non-unicode strings: 67 STRING_TYPES = (basestring,) 68 BINARY_TYPES = (str,) 69 CLASS_TYPES = (type, types.ClassType) 70 71 # the raise syntax will cause a parse error in Py3, so 'sneak' in a 72 # definition that won't cause the parser to barf 73 exec("""def raise_(t, v=None, tb=None): 74 raise t, v, tb 75 """) 76
77 - def bin2str(s, encoding='utf-8'):
78 return s.decode(encoding)
79
80 - def iteritems(d, **kw):
81 return d.iteritems()
82
83 - def unichar(i):
84 return unichr(i)
85
86 - def str2bin(s, encoding='latin-1'):
87 return s
88
89 - def str2unicode(s):
90 return unicode(s, "unicode_escape")
91