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