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