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 iteritems(d):
48 return iter(d.items())
49
50 - def unichar(i):
51 return chr(i)
52
53 - def str2bin(s, encoding='latin-1'):
54 """Convert str to binary type""" 55 return s.encode(encoding)
56
57 - def str2unicode(s):
58 return s
59 60 else: 61 INT_TYPES = (int, long) 62 TEXT_TYPES = (unicode,) 63 # includes both unicode and non-unicode strings: 64 STRING_TYPES = (basestring,) 65 BINARY_TYPES = (str,) 66 CLASS_TYPES = (type, types.ClassType) 67 68 # the raise syntax will cause a parse error in Py3, so 'sneak' in a 69 # definition that won't cause the parser to barf 70 exec("""def raise_(t, v=None, tb=None): 71 raise t, v, tb 72 """) 73
74 - def iteritems(d, **kw):
75 return d.iteritems()
76
77 - def unichar(i):
78 return unichr(i)
79
80 - def str2bin(s, encoding='latin-1'):
81 return s
82
83 - def str2unicode(s):
84 return unicode(s, "unicode_escape")
85