55# (see spyderlib/__init__.py for details)
66
77"""
8- winpython.py3compat (exact copy of spyderlib.py3compat)
9- -------------------------------------------------------
10-
11- Transitional module providing compatibility functions intended to help
8+ spyderlib.py3compat
9+ -------------------
10+ Transitional module providing compatibility functions intended to help
1211migrating from Python 2 to Python 3.
13-
1412This module should be fully compatible with:
1513 * Python >=v2.6
1614 * Python 3
2119import sys
2220import os
2321
24- PY2 = sys .version_info [0 ] == 2
25- PY3 = sys .version_info [0 ] == 3
22+ PY2 = sys .version [0 ] == '2'
23+ PY3 = sys .version [0 ] == '3'
2624
2725
28- # =============================================================================
26+ #= =============================================================================
2927# Data types
30- # =============================================================================
28+ #= =============================================================================
3129if PY2 :
3230 # Python 2
3331 TEXT_TYPES = (str , unicode )
3937NUMERIC_TYPES = tuple (list (INT_TYPES ) + [float , complex ])
4038
4139
42- # =============================================================================
40+ #= =============================================================================
4341# Renamed/Reorganized modules
44- # =============================================================================
42+ #= =============================================================================
4543if PY2 :
4644 # Python 2
4745 import __builtin__ as builtins
6058 except ImportError :
6159 import pickle
6260 from UserDict import DictMixin as MutableMapping
61+ import thread as _thread
62+ import repr as reprlib
6363else :
6464 # Python 3
6565 import builtins
7272 import io
7373 import pickle
7474 from collections import MutableMapping
75+ import _thread
76+ import reprlib
7577
7678
77- # =============================================================================
79+ #= =============================================================================
7880# Strings
79- # =============================================================================
81+ #==============================================================================
82+ if PY2 :
83+ # Python 2
84+ import codecs
85+ def u (obj ):
86+ """Make unicode object"""
87+ return codecs .unicode_escape_decode (obj )[0 ]
88+ else :
89+ # Python 3
90+ def u (obj ):
91+ """Return string as it is"""
92+ return obj
93+
8094def is_text_string (obj ):
8195 """Return True if `obj` is a text string, False if it is anything else,
8296 like binary data (Python 3) or QString (Python 2, PyQt API #1)"""
@@ -87,7 +101,6 @@ def is_text_string(obj):
87101 # Python 3
88102 return isinstance (obj , str )
89103
90-
91104def is_binary_string (obj ):
92105 """Return True if `obj` is a binary string, False if it is anything else"""
93106 if PY2 :
@@ -97,13 +110,11 @@ def is_binary_string(obj):
97110 # Python 3
98111 return isinstance (obj , bytes )
99112
100-
101113def is_string (obj ):
102114 """Return True if `obj` is a text or binary Python string object,
103115 False if it is anything else, like a QString (Python 2, PyQt API #1)"""
104116 return is_text_string (obj ) or is_binary_string (obj )
105117
106-
107118def is_unicode (obj ):
108119 """Return True if `obj` is unicode"""
109120 if PY2 :
@@ -113,7 +124,6 @@ def is_unicode(obj):
113124 # Python 3
114125 return isinstance (obj , str )
115126
116-
117127def to_text_string (obj , encoding = None ):
118128 """Convert `obj` to (unicode) text string"""
119129 if PY2 :
@@ -132,7 +142,6 @@ def to_text_string(obj, encoding=None):
132142 else :
133143 return str (obj , encoding )
134144
135-
136145def to_binary_string (obj , encoding = None ):
137146 """Convert `obj` to binary string (bytes in Python 3, str in Python 2)"""
138147 if PY2 :
@@ -146,9 +155,9 @@ def to_binary_string(obj, encoding=None):
146155 return bytes (obj , 'utf-8' if encoding is None else encoding )
147156
148157
149- # =============================================================================
158+ #= =============================================================================
150159# Function attributes
151- # =============================================================================
160+ #= =============================================================================
152161def get_func_code (func ):
153162 """Return function code object"""
154163 if PY2 :
@@ -158,7 +167,6 @@ def get_func_code(func):
158167 # Python 3
159168 return func .__code__
160169
161-
162170def get_func_name (func ):
163171 """Return function name"""
164172 if PY2 :
@@ -168,7 +176,6 @@ def get_func_name(func):
168176 # Python 3
169177 return func .__name__
170178
171-
172179def get_func_defaults (func ):
173180 """Return function default argument values"""
174181 if PY2 :
@@ -179,9 +186,9 @@ def get_func_defaults(func):
179186 return func .__defaults__
180187
181188
182- # =============================================================================
189+ #= =============================================================================
183190# Special method attributes
184- # =============================================================================
191+ #= =============================================================================
185192def get_meth_func (obj ):
186193 """Return method function object"""
187194 if PY2 :
@@ -191,7 +198,6 @@ def get_meth_func(obj):
191198 # Python 3
192199 return obj .__func__
193200
194-
195201def get_meth_class_inst (obj ):
196202 """Return method class instance"""
197203 if PY2 :
@@ -201,7 +207,6 @@ def get_meth_class_inst(obj):
201207 # Python 3
202208 return obj .__self__
203209
204-
205210def get_meth_class (obj ):
206211 """Return method class"""
207212 if PY2 :
@@ -212,29 +217,29 @@ def get_meth_class(obj):
212217 return obj .__self__ .__class__
213218
214219
215- # =============================================================================
220+ #= =============================================================================
216221# Misc.
217- # =============================================================================
222+ #= =============================================================================
218223if PY2 :
219224 # Python 2
220225 input = raw_input
221226 getcwd = os .getcwdu
222227 cmp = cmp
223228 import string
224229 str_lower = string .lower
230+ from itertools import izip_longest as zip_longest
225231else :
226232 # Python 3
227233 input = input
228234 getcwd = os .getcwd
229-
230235 def cmp (a , b ):
231236 return (a > b ) - (a < b )
232237 str_lower = str .lower
233-
238+ from itertools import zip_longest
234239
235240def qbytearray_to_str (qba ):
236241 """Convert QByteArray object to str in a way compatible with Python 2/3"""
237- return str (bytes (qba .toHex ()).decode ())
242+ return str (bytes (qba .toHex (). data () ).decode ())
238243
239244
240245if __name__ == '__main__' :
0 commit comments