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)
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:
19 from __future__
import print_function
24 PY2
= sys
.version_info
[0] == 2
25 PY3
= sys
.version_info
[0] == 3
26 PY33
= PY3
and sys
.version_info
[1] >= 3
29 # =============================================================================
31 # =============================================================================
34 TEXT_TYPES
= (str, unicode)
35 INT_TYPES
= (int, long)
40 NUMERIC_TYPES
= tuple(list(INT_TYPES
) + [float, complex])
43 # =============================================================================
44 # Renamed/Reorganized modules
45 # =============================================================================
48 import __builtin__
as builtins
49 import ConfigParser
as configparser
51 import _winreg
as winreg
54 from sys
import maxint
as maxsize
56 import CStringIO
as io
60 import cPickle
as pickle
63 from UserDict
import DictMixin
as MutableMapping
64 import thread
as _thread
65 import repr as reprlib
74 from sys
import maxsize
78 from collections
.abc
import MutableMapping
80 from collections
import MutableMapping
85 # =============================================================================
87 # =============================================================================
93 """Make unicode object"""
94 return codecs
.unicode_escape_decode(obj
)[0]
98 """Return string as it is"""
102 def is_text_string(obj
):
103 """Return True if `obj` is a text string, False if it is anything else,
104 like binary data (Python 3) or QString (Python 2, PyQt API #1)"""
107 return isinstance(obj
, basestring
)
110 return isinstance(obj
, str)
113 def is_binary_string(obj
):
114 """Return True if `obj` is a binary string, False if it is anything else"""
117 return isinstance(obj
, str)
120 return isinstance(obj
, bytes
)
124 """Return True if `obj` is a text or binary Python string object,
125 False if it is anything else, like a QString (Python 2, PyQt API #1)"""
126 return is_text_string(obj
) or is_binary_string(obj
)
130 """Return True if `obj` is unicode"""
133 return isinstance(obj
, unicode)
136 return isinstance(obj
, str)
139 def to_text_string(obj
, encoding
=None):
140 """Convert `obj` to (unicode) text string"""
146 return unicode(obj
, encoding
)
151 elif isinstance(obj
, str):
152 # In case this function is not used properly, this could happen
155 return str(obj
, encoding
)
158 def to_binary_string(obj
, encoding
=None):
159 """Convert `obj` to binary string (bytes in Python 3, str in Python 2)"""
165 return obj
.encode(encoding
)
168 return bytes(obj
, 'utf-8' if encoding
is None else encoding
)
171 # =============================================================================
172 # Function attributes
173 # =============================================================================
174 def get_func_code(func
):
175 """Return function code object"""
178 return func
.func_code
184 def get_func_name(func
):
185 """Return function name"""
188 return func
.func_name
194 def get_func_defaults(func
):
195 """Return function default argument values"""
198 return func
.func_defaults
201 return func
.__defaults
__
204 # =============================================================================
205 # Special method attributes
206 # =============================================================================
207 def get_meth_func(obj
):
208 """Return method function object"""
217 def get_meth_class_inst(obj
):
218 """Return method class instance"""
227 def get_meth_class(obj
):
228 """Return method class"""
234 return obj
.__self
__.__class
__
237 # =============================================================================
239 # =============================================================================
246 str_lower
= string
.lower
247 from itertools
import izip_longest
as zip_longest
254 return (a
> b
) - (a
< b
)
255 str_lower
= str.lower
256 from itertools
import zip_longest
259 def qbytearray_to_str(qba
):
260 """Convert QByteArray object to str in a way compatible with Python 2/3"""
261 return str(bytes(qba
.toHex().data()).decode())