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
[0] == '2'
25 PY3
= sys
.version
[0] == '3'
28 # =============================================================================
30 # =============================================================================
33 TEXT_TYPES
= (str, unicode)
34 INT_TYPES
= (int, long)
39 NUMERIC_TYPES
= tuple(list(INT_TYPES
) + [float, complex])
42 # =============================================================================
43 # Renamed/Reorganized modules
44 # =============================================================================
47 import __builtin__
as builtins
48 import ConfigParser
as configparser
50 import _winreg
as winreg
53 from sys
import maxint
as maxsize
55 import CStringIO
as io
59 import cPickle
as pickle
62 from UserDict
import DictMixin
as MutableMapping
63 import thread
as _thread
64 import repr as reprlib
73 from sys
import maxsize
76 from collections
import MutableMapping
81 # =============================================================================
83 # =============================================================================
89 """Make unicode object"""
90 return codecs
.unicode_escape_decode(obj
)[0]
94 """Return string as it is"""
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)"""
103 return isinstance(obj
, basestring
)
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"""
113 return isinstance(obj
, str)
116 return isinstance(obj
, bytes
)
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
)
126 """Return True if `obj` is unicode"""
129 return isinstance(obj
, unicode)
132 return isinstance(obj
, str)
135 def to_text_string(obj
, encoding
=None):
136 """Convert `obj` to (unicode) text string"""
142 return unicode(obj
, encoding
)
147 elif isinstance(obj
, str):
148 # In case this function is not used properly, this could happen
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)"""
161 return obj
.encode(encoding
)
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"""
174 return func
.func_code
180 def get_func_name(func
):
181 """Return function name"""
184 return func
.func_name
190 def get_func_defaults(func
):
191 """Return function default argument values"""
194 return func
.func_defaults
197 return func
.__defaults
__
200 # =============================================================================
201 # Special method attributes
202 # =============================================================================
203 def get_meth_func(obj
):
204 """Return method function object"""
213 def get_meth_class_inst(obj
):
214 """Return method class instance"""
223 def get_meth_class(obj
):
224 """Return method class"""
230 return obj
.__self
__.__class
__
233 # =============================================================================
235 # =============================================================================
242 str_lower
= string
.lower
243 from itertools
import izip_longest
as zip_longest
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__':