55# (see spyderlib/__init__.py for details)
66
77"""
8- winpython.py3compat (exact copy of spyderlib.py3compat)
9- -------------------------------------------------------
8+ spyderlib.py3compat
9+ -------------------
1010
11- Transitional module providing compatibility functions intended to help
11+ Transitional module providing compatibility functions intended to help
1212migrating from Python 2 to Python 3.
1313
1414This module should be fully compatible with:
2121import sys
2222import os
2323
24- PY2 = sys .version_info [0 ] == 2
25- PY3 = sys .version_info [0 ] == 3
24+ PY2 = sys .version [0 ] == '2'
25+ PY3 = sys .version [0 ] == '3'
2626
2727
28- # =============================================================================
28+ #= =============================================================================
2929# Data types
30- # =============================================================================
30+ #= =============================================================================
3131if PY2 :
3232 # Python 2
3333 TEXT_TYPES = (str , unicode )
3939NUMERIC_TYPES = tuple (list (INT_TYPES ) + [float , complex ])
4040
4141
42- # =============================================================================
42+ #= =============================================================================
4343# Renamed/Reorganized modules
44- # =============================================================================
44+ #= =============================================================================
4545if PY2 :
4646 # Python 2
4747 import __builtin__ as builtins
6060 except ImportError :
6161 import pickle
6262 from UserDict import DictMixin as MutableMapping
63+ import thread as _thread
64+ import repr as reprlib
6365else :
6466 # Python 3
6567 import builtins
7274 import io
7375 import pickle
7476 from collections import MutableMapping
77+ import _thread
78+ import reprlib
7579
7680
77- # =============================================================================
81+ #= =============================================================================
7882# Strings
79- # =============================================================================
83+ #==============================================================================
84+ if PY2 :
85+ # Python 2
86+ import codecs
87+ def u (obj ):
88+ """Make unicode object"""
89+ return codecs .unicode_escape_decode (obj )[0 ]
90+ else :
91+ # Python 3
92+ def u (obj ):
93+ """Return string as it is"""
94+ return obj
95+
8096def is_text_string (obj ):
8197 """Return True if `obj` is a text string, False if it is anything else,
8298 like binary data (Python 3) or QString (Python 2, PyQt API #1)"""
@@ -87,7 +103,6 @@ def is_text_string(obj):
87103 # Python 3
88104 return isinstance (obj , str )
89105
90-
91106def is_binary_string (obj ):
92107 """Return True if `obj` is a binary string, False if it is anything else"""
93108 if PY2 :
@@ -97,13 +112,11 @@ def is_binary_string(obj):
97112 # Python 3
98113 return isinstance (obj , bytes )
99114
100-
101115def is_string (obj ):
102116 """Return True if `obj` is a text or binary Python string object,
103117 False if it is anything else, like a QString (Python 2, PyQt API #1)"""
104118 return is_text_string (obj ) or is_binary_string (obj )
105119
106-
107120def is_unicode (obj ):
108121 """Return True if `obj` is unicode"""
109122 if PY2 :
@@ -113,7 +126,6 @@ def is_unicode(obj):
113126 # Python 3
114127 return isinstance (obj , str )
115128
116-
117129def to_text_string (obj , encoding = None ):
118130 """Convert `obj` to (unicode) text string"""
119131 if PY2 :
@@ -132,7 +144,6 @@ def to_text_string(obj, encoding=None):
132144 else :
133145 return str (obj , encoding )
134146
135-
136147def to_binary_string (obj , encoding = None ):
137148 """Convert `obj` to binary string (bytes in Python 3, str in Python 2)"""
138149 if PY2 :
@@ -146,9 +157,9 @@ def to_binary_string(obj, encoding=None):
146157 return bytes (obj , 'utf-8' if encoding is None else encoding )
147158
148159
149- # =============================================================================
160+ #= =============================================================================
150161# Function attributes
151- # =============================================================================
162+ #= =============================================================================
152163def get_func_code (func ):
153164 """Return function code object"""
154165 if PY2 :
@@ -158,7 +169,6 @@ def get_func_code(func):
158169 # Python 3
159170 return func .__code__
160171
161-
162172def get_func_name (func ):
163173 """Return function name"""
164174 if PY2 :
@@ -168,7 +178,6 @@ def get_func_name(func):
168178 # Python 3
169179 return func .__name__
170180
171-
172181def get_func_defaults (func ):
173182 """Return function default argument values"""
174183 if PY2 :
@@ -179,9 +188,9 @@ def get_func_defaults(func):
179188 return func .__defaults__
180189
181190
182- # =============================================================================
191+ #= =============================================================================
183192# Special method attributes
184- # =============================================================================
193+ #= =============================================================================
185194def get_meth_func (obj ):
186195 """Return method function object"""
187196 if PY2 :
@@ -191,7 +200,6 @@ def get_meth_func(obj):
191200 # Python 3
192201 return obj .__func__
193202
194-
195203def get_meth_class_inst (obj ):
196204 """Return method class instance"""
197205 if PY2 :
@@ -201,7 +209,6 @@ def get_meth_class_inst(obj):
201209 # Python 3
202210 return obj .__self__
203211
204-
205212def get_meth_class (obj ):
206213 """Return method class"""
207214 if PY2 :
@@ -212,29 +219,29 @@ def get_meth_class(obj):
212219 return obj .__self__ .__class__
213220
214221
215- # =============================================================================
222+ #= =============================================================================
216223# Misc.
217- # =============================================================================
224+ #= =============================================================================
218225if PY2 :
219226 # Python 2
220227 input = raw_input
221228 getcwd = os .getcwdu
222229 cmp = cmp
223230 import string
224231 str_lower = string .lower
232+ from itertools import izip_longest as zip_longest
225233else :
226234 # Python 3
227235 input = input
228236 getcwd = os .getcwd
229-
230237 def cmp (a , b ):
231238 return (a > b ) - (a < b )
232239 str_lower = str .lower
233-
240+ from itertools import zip_longest
234241
235242def qbytearray_to_str (qba ):
236243 """Convert QByteArray object to str in a way compatible with Python 2/3"""
237- return str (bytes (qba .toHex ()).decode ())
244+ return str (bytes (qba .toHex (). data () ).decode ())
238245
239246
240247if __name__ == '__main__' :
0 commit comments