compat.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. See another compatibility snippets from other projects:
  22. :mod:`lib2to3.fixes`
  23. :mod:`coverage.backward`
  24. :mod:`unittest2.compatibility`
  25. """
  26. from __future__ import generators
  27. __docformat__ = "restructuredtext en"
  28. import os
  29. import sys
  30. import types
  31. from warnings import warn
  32. import __builtin__ as builtins # 2to3 will tranform '__builtin__' to 'builtins'
  33. if sys.version_info < (3, 0):
  34. str_to_bytes = str
  35. def str_encode(string, encoding):
  36. if isinstance(string, unicode):
  37. return string.encode(encoding)
  38. return str(string)
  39. else:
  40. def str_to_bytes(string):
  41. return str.encode(string)
  42. # we have to ignore the encoding in py3k to be able to write a string into a
  43. # TextIOWrapper or like object (which expect an unicode string)
  44. def str_encode(string, encoding):
  45. return str(string)
  46. # XXX callable built-in seems back in all python versions
  47. try:
  48. callable = builtins.callable
  49. except AttributeError:
  50. from collections import Callable
  51. def callable(something):
  52. return isinstance(something, Callable)
  53. del Callable
  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. if sys.version_info < (3, 0):
  63. raw_input = raw_input
  64. else:
  65. raw_input = input
  66. # Pythons 2 and 3 differ on where to get StringIO
  67. if sys.version_info < (3, 0):
  68. from cStringIO import StringIO
  69. FileIO = file
  70. BytesIO = StringIO
  71. reload = reload
  72. else:
  73. from io import FileIO, BytesIO, StringIO
  74. from imp import reload
  75. # Where do pickles come from?
  76. try:
  77. import cPickle as pickle
  78. except ImportError:
  79. import pickle
  80. from logilab.common.deprecation import deprecated
  81. from itertools import izip, chain, imap
  82. if sys.version_info < (3, 0):# 2to3 will remove the imports
  83. izip = deprecated('izip exists in itertools since py2.3')(izip)
  84. imap = deprecated('imap exists in itertools since py2.3')(imap)
  85. chain = deprecated('chain exists in itertools since py2.3')(chain)
  86. sum = deprecated('sum exists in builtins since py2.3')(sum)
  87. enumerate = deprecated('enumerate exists in builtins since py2.3')(enumerate)
  88. frozenset = deprecated('frozenset exists in builtins since py2.4')(frozenset)
  89. reversed = deprecated('reversed exists in builtins since py2.4')(reversed)
  90. sorted = deprecated('sorted exists in builtins since py2.4')(sorted)
  91. max = deprecated('max exists in builtins since py2.4')(max)
  92. # Python2.5 builtins
  93. try:
  94. any = any
  95. all = all
  96. except NameError:
  97. def any(iterable):
  98. """any(iterable) -> bool
  99. Return True if bool(x) is True for any x in the iterable.
  100. """
  101. for elt in iterable:
  102. if elt:
  103. return True
  104. return False
  105. def all(iterable):
  106. """all(iterable) -> bool
  107. Return True if bool(x) is True for all values x in the iterable.
  108. """
  109. for elt in iterable:
  110. if not elt:
  111. return False
  112. return True
  113. # Python2.5 subprocess added functions and exceptions
  114. try:
  115. from subprocess import Popen
  116. except ImportError:
  117. # gae or python < 2.3
  118. class CalledProcessError(Exception):
  119. """This exception is raised when a process run by check_call() returns
  120. a non-zero exit status. The exit status will be stored in the
  121. returncode attribute."""
  122. def __init__(self, returncode, cmd):
  123. self.returncode = returncode
  124. self.cmd = cmd
  125. def __str__(self):
  126. return "Command '%s' returned non-zero exit status %d" % (self.cmd,
  127. self.returncode)
  128. def call(*popenargs, **kwargs):
  129. """Run command with arguments. Wait for command to complete, then
  130. return the returncode attribute.
  131. The arguments are the same as for the Popen constructor. Example:
  132. retcode = call(["ls", "-l"])
  133. """
  134. # workaround: subprocess.Popen(cmd, stdout=sys.stdout) fails
  135. # see http://bugs.python.org/issue1531862
  136. if "stdout" in kwargs:
  137. fileno = kwargs.get("stdout").fileno()
  138. del kwargs['stdout']
  139. return Popen(stdout=os.dup(fileno), *popenargs, **kwargs).wait()
  140. return Popen(*popenargs, **kwargs).wait()
  141. def check_call(*popenargs, **kwargs):
  142. """Run command with arguments. Wait for command to complete. If
  143. the exit code was zero then return, otherwise raise
  144. CalledProcessError. The CalledProcessError object will have the
  145. return code in the returncode attribute.
  146. The arguments are the same as for the Popen constructor. Example:
  147. check_call(["ls", "-l"])
  148. """
  149. retcode = call(*popenargs, **kwargs)
  150. cmd = kwargs.get("args")
  151. if cmd is None:
  152. cmd = popenargs[0]
  153. if retcode:
  154. raise CalledProcessError(retcode, cmd)
  155. return retcode
  156. try:
  157. from os.path import relpath
  158. except ImportError: # python < 2.6
  159. from os.path import curdir, abspath, sep, commonprefix, pardir, join
  160. def relpath(path, start=curdir):
  161. """Return a relative version of a path"""
  162. if not path:
  163. raise ValueError("no path specified")
  164. start_list = abspath(start).split(sep)
  165. path_list = abspath(path).split(sep)
  166. # Work out how much of the filepath is shared by start and path.
  167. i = len(commonprefix([start_list, path_list]))
  168. rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
  169. if not rel_list:
  170. return curdir
  171. return join(*rel_list)
  172. # XXX don't know why tests don't pass if I don't do that :
  173. _real_set, set = set, deprecated('set exists in builtins since py2.4')(set)
  174. if (2, 5) <= sys.version_info[:2]:
  175. InheritableSet = _real_set
  176. else:
  177. class InheritableSet(_real_set):
  178. """hacked resolving inheritancy issue from old style class in 2.4"""
  179. def __new__(cls, *args, **kwargs):
  180. if args:
  181. new_args = (args[0], )
  182. else:
  183. new_args = ()
  184. obj = _real_set.__new__(cls, *new_args)
  185. obj.__init__(*args, **kwargs)
  186. return obj
  187. # XXX shouldn't we remove this and just let 2to3 do his job ?
  188. # range or xrange?
  189. try:
  190. range = xrange
  191. except NameError:
  192. range = range
  193. # ConfigParser was renamed to the more-standard configparser
  194. try:
  195. import configparser
  196. except ImportError:
  197. import ConfigParser as configparser
  198. try:
  199. import json
  200. except ImportError:
  201. try:
  202. import simplejson as json
  203. except ImportError:
  204. json = None