You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

261 lines
6.5 KiB

4 years ago
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright © 2012-2013 Pierre Raybaut
  4. # Licensed under the terms of the MIT License
  5. # (see spyderlib/__init__.py for details)
  6. """
  7. spyderlib.py3compat
  8. -------------------
  9. Transitional module providing compatibility functions intended to help
  10. migrating from Python 2 to Python 3.
  11. This module should be fully compatible with:
  12. * Python >=v2.6
  13. * Python 3
  14. """
  15. from __future__ import print_function
  16. import sys
  17. import os
  18. PY2 = sys.version[0] == '2'
  19. PY3 = sys.version[0] == '3'
  20. # =============================================================================
  21. # Data types
  22. # =============================================================================
  23. if PY2:
  24. # Python 2
  25. TEXT_TYPES = (str, unicode)
  26. INT_TYPES = (int, long)
  27. else:
  28. # Python 3
  29. TEXT_TYPES = (str,)
  30. INT_TYPES = (int,)
  31. NUMERIC_TYPES = tuple(list(INT_TYPES) + [float, complex])
  32. # =============================================================================
  33. # Renamed/Reorganized modules
  34. # =============================================================================
  35. if PY2:
  36. # Python 2
  37. import __builtin__ as builtins
  38. import ConfigParser as configparser
  39. try:
  40. import _winreg as winreg
  41. except ImportError:
  42. pass
  43. from sys import maxint as maxsize
  44. try:
  45. import CStringIO as io
  46. except ImportError:
  47. import StringIO as io
  48. try:
  49. import cPickle as pickle
  50. except ImportError:
  51. import pickle
  52. from UserDict import DictMixin as MutableMapping
  53. import thread as _thread
  54. import repr as reprlib
  55. else:
  56. # Python 3
  57. import builtins
  58. import configparser
  59. try:
  60. import winreg
  61. except ImportError:
  62. pass
  63. from sys import maxsize
  64. import io
  65. import pickle
  66. from collections import MutableMapping
  67. import _thread
  68. import reprlib
  69. # =============================================================================
  70. # Strings
  71. # =============================================================================
  72. if PY2:
  73. # Python 2
  74. import codecs
  75. def u(obj):
  76. """Make unicode object"""
  77. return codecs.unicode_escape_decode(obj)[0]
  78. else:
  79. # Python 3
  80. def u(obj):
  81. """Return string as it is"""
  82. return obj
  83. def is_text_string(obj):
  84. """Return True if `obj` is a text string, False if it is anything else,
  85. like binary data (Python 3) or QString (Python 2, PyQt API #1)"""
  86. if PY2:
  87. # Python 2
  88. return isinstance(obj, basestring)
  89. else:
  90. # Python 3
  91. return isinstance(obj, str)
  92. def is_binary_string(obj):
  93. """Return True if `obj` is a binary string, False if it is anything else"""
  94. if PY2:
  95. # Python 2
  96. return isinstance(obj, str)
  97. else:
  98. # Python 3
  99. return isinstance(obj, bytes)
  100. def is_string(obj):
  101. """Return True if `obj` is a text or binary Python string object,
  102. False if it is anything else, like a QString (Python 2, PyQt API #1)"""
  103. return is_text_string(obj) or is_binary_string(obj)
  104. def is_unicode(obj):
  105. """Return True if `obj` is unicode"""
  106. if PY2:
  107. # Python 2
  108. return isinstance(obj, unicode)
  109. else:
  110. # Python 3
  111. return isinstance(obj, str)
  112. def to_text_string(obj, encoding=None):
  113. """Convert `obj` to (unicode) text string"""
  114. if PY2:
  115. # Python 2
  116. if encoding is None:
  117. return unicode(obj)
  118. else:
  119. return unicode(obj, encoding)
  120. else:
  121. # Python 3
  122. if encoding is None:
  123. return str(obj)
  124. elif isinstance(obj, str):
  125. # In case this function is not used properly, this could happen
  126. return obj
  127. else:
  128. return str(obj, encoding)
  129. def to_binary_string(obj, encoding=None):
  130. """Convert `obj` to binary string (bytes in Python 3, str in Python 2)"""
  131. if PY2:
  132. # Python 2
  133. if encoding is None:
  134. return str(obj)
  135. else:
  136. return obj.encode(encoding)
  137. else:
  138. # Python 3
  139. return bytes(obj, 'utf-8' if encoding is None else encoding)
  140. # =============================================================================
  141. # Function attributes
  142. # =============================================================================
  143. def get_func_code(func):
  144. """Return function code object"""
  145. if PY2:
  146. # Python 2
  147. return func.func_code
  148. else:
  149. # Python 3
  150. return func.__code__
  151. def get_func_name(func):
  152. """Return function name"""
  153. if PY2:
  154. # Python 2
  155. return func.func_name
  156. else:
  157. # Python 3
  158. return func.__name__
  159. def get_func_defaults(func):
  160. """Return function default argument values"""
  161. if PY2:
  162. # Python 2
  163. return func.func_defaults
  164. else:
  165. # Python 3
  166. return func.__defaults__
  167. # =============================================================================
  168. # Special method attributes
  169. # =============================================================================
  170. def get_meth_func(obj):
  171. """Return method function object"""
  172. if PY2:
  173. # Python 2
  174. return obj.im_func
  175. else:
  176. # Python 3
  177. return obj.__func__
  178. def get_meth_class_inst(obj):
  179. """Return method class instance"""
  180. if PY2:
  181. # Python 2
  182. return obj.im_self
  183. else:
  184. # Python 3
  185. return obj.__self__
  186. def get_meth_class(obj):
  187. """Return method class"""
  188. if PY2:
  189. # Python 2
  190. return obj.im_class
  191. else:
  192. # Python 3
  193. return obj.__self__.__class__
  194. # =============================================================================
  195. # Misc.
  196. # =============================================================================
  197. if PY2:
  198. # Python 2
  199. input = raw_input
  200. getcwd = os.getcwdu
  201. cmp = cmp
  202. import string
  203. str_lower = string.lower
  204. from itertools import izip_longest as zip_longest
  205. else:
  206. # Python 3
  207. input = input
  208. getcwd = os.getcwd
  209. def cmp(a, b):
  210. return (a > b) - (a < b)
  211. str_lower = str.lower
  212. from itertools import zip_longest
  213. def qbytearray_to_str(qba):
  214. """Convert QByteArray object to str in a way compatible with Python 2/3"""
  215. return str(bytes(qba.toHex().data()).decode())
  216. if __name__ == '__main__':
  217. pass