Package logilab :: Package common :: Module compat
[frames] | no frames]

Source Code for Module logilab.common.compat

 1  # pylint: disable=E0601,W0622,W0611 
 2  # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. 
 3  # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr 
 4  # 
 5  # This file is part of logilab-common. 
 6  # 
 7  # logilab-common is free software: you can redistribute it and/or modify it under 
 8  # the terms of the GNU Lesser General Public License as published by the Free 
 9  # Software Foundation, either version 2.1 of the License, or (at your option) any 
10  # later version. 
11  # 
12  # logilab-common is distributed in the hope that it will be useful, but WITHOUT 
13  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
14  # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more 
15  # details. 
16  # 
17  # You should have received a copy of the GNU Lesser General Public License along 
18  # with logilab-common.  If not, see <http://www.gnu.org/licenses/>. 
19  """Wrappers around some builtins introduced in python 2.3, 2.4 and 
20  2.5, making them available in for earlier versions of python. 
21   
22  See another compatibility snippets from other projects: 
23   
24      :mod:`lib2to3.fixes` 
25      :mod:`coverage.backward` 
26      :mod:`unittest2.compatibility` 
27  """ 
28   
29   
30  __docformat__ = "restructuredtext en" 
31   
32  import os 
33  import sys 
34  import types 
35  from warnings import warn 
36   
37  # not used here, but imported to preserve API 
38  from six.moves import builtins 
39   
40  if sys.version_info < (3, 0): 
41      str_to_bytes = str 
42 - def str_encode(string, encoding):
43 if isinstance(string, unicode): 44 return string.encode(encoding) 45 return str(string)
46 else:
47 - def str_to_bytes(string):
48 return str.encode(string)
49 # we have to ignore the encoding in py3k to be able to write a string into a 50 # TextIOWrapper or like object (which expect an unicode string)
51 - def str_encode(string, encoding):
52 return str(string)
53 54 # See also http://bugs.python.org/issue11776 55 if sys.version_info[0] == 3:
56 - def method_type(callable, instance, klass):
57 # api change. klass is no more considered 58 return types.MethodType(callable, instance)
59 else: 60 # alias types otherwise 61 method_type = types.MethodType 62 63 # Pythons 2 and 3 differ on where to get StringIO 64 if sys.version_info < (3, 0): 65 from cStringIO import StringIO 66 FileIO = file 67 BytesIO = StringIO 68 reload = reload 69 else: 70 from io import FileIO, BytesIO, StringIO 71 from imp import reload 72 73 from logilab.common.deprecation import deprecated 74 75 # Other projects import these from here, keep providing them for 76 # backwards compat 77 any = deprecated('use builtin "any"')(any) 78 all = deprecated('use builtin "all"')(all) 79