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 from collections
import Callable
, MutableMapping
50 import ConfigParser
as configparser
52 import _winreg
as winreg
55 from sys
import maxint
as maxsize
57 import CStringIO
as io
61 import cPickle
as pickle
64 from UserDict
import DictMixin
as MutableMapping
65 import thread
as _thread
66 import repr as reprlib
75 from sys
import maxsize
79 from collections
.abc
import Callable
, MutableMapping
81 from collections
import Callable
, MutableMapping
86 # =============================================================================
88 # =============================================================================
94 """Make unicode object"""
95 return codecs
.unicode_escape_decode(obj
)[0]
99 """Return string as it is"""
103 def is_text_string(obj
):
104 """Return True if `obj` is a text string, False if it is anything else,
105 like binary data (Python 3) or QString (Python 2, PyQt API #1)"""
108 return isinstance(obj
, basestring
)
111 return isinstance(obj
, str)
114 def is_binary_string(obj
):
115 """Return True if `obj` is a binary string, False if it is anything else"""
118 return isinstance(obj
, str)
121 return isinstance(obj
, bytes
)
125 """Return True if `obj` is a text or binary Python string object,
126 False if it is anything else, like a QString (Python 2, PyQt API #1)"""
127 return is_text_string(obj
) or is_binary_string(obj
)
131 """Return True if `obj` is unicode"""
134 return isinstance(obj
, unicode)
137 return isinstance(obj
, str)
140 def to_text_string(obj
, encoding
=None):
141 """Convert `obj` to (unicode) text string"""
147 return unicode(obj
, encoding
)
152 elif isinstance(obj
, str):
153 # In case this function is not used properly, this could happen
156 return str(obj
, encoding
)
159 def to_binary_string(obj
, encoding
=None):
160 """Convert `obj` to binary string (bytes in Python 3, str in Python 2)"""
166 return obj
.encode(encoding
)
169 return bytes(obj
, 'utf-8' if encoding
is None else encoding
)
172 # =============================================================================
173 # Function attributes
174 # =============================================================================
175 def get_func_code(func
):
176 """Return function code object"""
179 return func
.func_code
185 def get_func_name(func
):
186 """Return function name"""
189 return func
.func_name
195 def get_func_defaults(func
):
196 """Return function default argument values"""
199 return func
.func_defaults
202 return func
.__defaults
__
205 # =============================================================================
206 # Special method attributes
207 # =============================================================================
208 def get_meth_func(obj
):
209 """Return method function object"""
218 def get_meth_class_inst(obj
):
219 """Return method class instance"""
228 def get_meth_class(obj
):
229 """Return method class"""
235 return obj
.__self
__.__class
__
238 # =============================================================================
240 # =============================================================================
247 str_lower
= string
.lower
248 from itertools
import izip_longest
as zip_longest
255 return (a
> b
) - (a
< b
)
256 str_lower
= str.lower
257 from itertools
import zip_longest
260 def qbytearray_to_str(qba
):
261 """Convert QByteArray object to str in a way compatible with Python 2/3"""
262 return str(bytes(qba
.toHex().data()).decode())