5
5
# (see spyderlib/__init__.py for details)
6
6
7
7
"""
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
12
11
migrating from Python 2 to Python 3.
13
-
14
12
This module should be fully compatible with:
15
13
* Python >=v2.6
16
14
* Python 3
21
19
import sys
22
20
import os
23
21
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'
26
24
27
25
28
- # =============================================================================
26
+ #= =============================================================================
29
27
# Data types
30
- # =============================================================================
28
+ #= =============================================================================
31
29
if PY2 :
32
30
# Python 2
33
31
TEXT_TYPES = (str , unicode )
39
37
NUMERIC_TYPES = tuple (list (INT_TYPES ) + [float , complex ])
40
38
41
39
42
- # =============================================================================
40
+ #= =============================================================================
43
41
# Renamed/Reorganized modules
44
- # =============================================================================
42
+ #= =============================================================================
45
43
if PY2 :
46
44
# Python 2
47
45
import __builtin__ as builtins
60
58
except ImportError :
61
59
import pickle
62
60
from UserDict import DictMixin as MutableMapping
61
+ import thread as _thread
62
+ import repr as reprlib
63
63
else :
64
64
# Python 3
65
65
import builtins
72
72
import io
73
73
import pickle
74
74
from collections import MutableMapping
75
+ import _thread
76
+ import reprlib
75
77
76
78
77
- # =============================================================================
79
+ #= =============================================================================
78
80
# 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
+
80
94
def is_text_string (obj ):
81
95
"""Return True if `obj` is a text string, False if it is anything else,
82
96
like binary data (Python 3) or QString (Python 2, PyQt API #1)"""
@@ -87,7 +101,6 @@ def is_text_string(obj):
87
101
# Python 3
88
102
return isinstance (obj , str )
89
103
90
-
91
104
def is_binary_string (obj ):
92
105
"""Return True if `obj` is a binary string, False if it is anything else"""
93
106
if PY2 :
@@ -97,13 +110,11 @@ def is_binary_string(obj):
97
110
# Python 3
98
111
return isinstance (obj , bytes )
99
112
100
-
101
113
def is_string (obj ):
102
114
"""Return True if `obj` is a text or binary Python string object,
103
115
False if it is anything else, like a QString (Python 2, PyQt API #1)"""
104
116
return is_text_string (obj ) or is_binary_string (obj )
105
117
106
-
107
118
def is_unicode (obj ):
108
119
"""Return True if `obj` is unicode"""
109
120
if PY2 :
@@ -113,7 +124,6 @@ def is_unicode(obj):
113
124
# Python 3
114
125
return isinstance (obj , str )
115
126
116
-
117
127
def to_text_string (obj , encoding = None ):
118
128
"""Convert `obj` to (unicode) text string"""
119
129
if PY2 :
@@ -132,7 +142,6 @@ def to_text_string(obj, encoding=None):
132
142
else :
133
143
return str (obj , encoding )
134
144
135
-
136
145
def to_binary_string (obj , encoding = None ):
137
146
"""Convert `obj` to binary string (bytes in Python 3, str in Python 2)"""
138
147
if PY2 :
@@ -146,9 +155,9 @@ def to_binary_string(obj, encoding=None):
146
155
return bytes (obj , 'utf-8' if encoding is None else encoding )
147
156
148
157
149
- # =============================================================================
158
+ #= =============================================================================
150
159
# Function attributes
151
- # =============================================================================
160
+ #= =============================================================================
152
161
def get_func_code (func ):
153
162
"""Return function code object"""
154
163
if PY2 :
@@ -158,7 +167,6 @@ def get_func_code(func):
158
167
# Python 3
159
168
return func .__code__
160
169
161
-
162
170
def get_func_name (func ):
163
171
"""Return function name"""
164
172
if PY2 :
@@ -168,7 +176,6 @@ def get_func_name(func):
168
176
# Python 3
169
177
return func .__name__
170
178
171
-
172
179
def get_func_defaults (func ):
173
180
"""Return function default argument values"""
174
181
if PY2 :
@@ -179,9 +186,9 @@ def get_func_defaults(func):
179
186
return func .__defaults__
180
187
181
188
182
- # =============================================================================
189
+ #= =============================================================================
183
190
# Special method attributes
184
- # =============================================================================
191
+ #= =============================================================================
185
192
def get_meth_func (obj ):
186
193
"""Return method function object"""
187
194
if PY2 :
@@ -191,7 +198,6 @@ def get_meth_func(obj):
191
198
# Python 3
192
199
return obj .__func__
193
200
194
-
195
201
def get_meth_class_inst (obj ):
196
202
"""Return method class instance"""
197
203
if PY2 :
@@ -201,7 +207,6 @@ def get_meth_class_inst(obj):
201
207
# Python 3
202
208
return obj .__self__
203
209
204
-
205
210
def get_meth_class (obj ):
206
211
"""Return method class"""
207
212
if PY2 :
@@ -212,29 +217,29 @@ def get_meth_class(obj):
212
217
return obj .__self__ .__class__
213
218
214
219
215
- # =============================================================================
220
+ #= =============================================================================
216
221
# Misc.
217
- # =============================================================================
222
+ #= =============================================================================
218
223
if PY2 :
219
224
# Python 2
220
225
input = raw_input
221
226
getcwd = os .getcwdu
222
227
cmp = cmp
223
228
import string
224
229
str_lower = string .lower
230
+ from itertools import izip_longest as zip_longest
225
231
else :
226
232
# Python 3
227
233
input = input
228
234
getcwd = os .getcwd
229
-
230
235
def cmp (a , b ):
231
236
return (a > b ) - (a < b )
232
237
str_lower = str .lower
233
-
238
+ from itertools import zip_longest
234
239
235
240
def qbytearray_to_str (qba ):
236
241
"""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 ())
238
243
239
244
240
245
if __name__ == '__main__' :
0 commit comments