88winpython.py3compat (exact copy of spyderlib.py3compat)
99-------------------------------------------------------
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:
2525PY3 = 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
7474 from collections import MutableMapping
7575
7676
77- #= =============================================================================
77+ # =============================================================================
7878# Strings
79- #= =============================================================================
79+ # =============================================================================
8080def is_text_string (obj ):
8181 """Return True if `obj` is a text string, False if it is anything else,
8282 like binary data (Python 3) or QString (Python 2, PyQt API #1)"""
@@ -87,6 +87,7 @@ def is_text_string(obj):
8787 # Python 3
8888 return isinstance (obj , str )
8989
90+
9091def is_binary_string (obj ):
9192 """Return True if `obj` is a binary string, False if it is anything else"""
9293 if PY2 :
@@ -96,11 +97,13 @@ def is_binary_string(obj):
9697 # Python 3
9798 return isinstance (obj , bytes )
9899
100+
99101def is_string (obj ):
100102 """Return True if `obj` is a text or binary Python string object,
101103 False if it is anything else, like a QString (Python 2, PyQt API #1)"""
102104 return is_text_string (obj ) or is_binary_string (obj )
103105
106+
104107def is_unicode (obj ):
105108 """Return True if `obj` is unicode"""
106109 if PY2 :
@@ -110,6 +113,7 @@ def is_unicode(obj):
110113 # Python 3
111114 return isinstance (obj , str )
112115
116+
113117def to_text_string (obj , encoding = None ):
114118 """Convert `obj` to (unicode) text string"""
115119 if PY2 :
@@ -128,6 +132,7 @@ def to_text_string(obj, encoding=None):
128132 else :
129133 return str (obj , encoding )
130134
135+
131136def to_binary_string (obj , encoding = None ):
132137 """Convert `obj` to binary string (bytes in Python 3, str in Python 2)"""
133138 if PY2 :
@@ -141,9 +146,9 @@ def to_binary_string(obj, encoding=None):
141146 return bytes (obj , 'utf-8' if encoding is None else encoding )
142147
143148
144- #= =============================================================================
149+ # =============================================================================
145150# Function attributes
146- #= =============================================================================
151+ # =============================================================================
147152def get_func_code (func ):
148153 """Return function code object"""
149154 if PY2 :
@@ -153,6 +158,7 @@ def get_func_code(func):
153158 # Python 3
154159 return func .__code__
155160
161+
156162def get_func_name (func ):
157163 """Return function name"""
158164 if PY2 :
@@ -162,6 +168,7 @@ def get_func_name(func):
162168 # Python 3
163169 return func .__name__
164170
171+
165172def get_func_defaults (func ):
166173 """Return function default argument values"""
167174 if PY2 :
@@ -172,9 +179,9 @@ def get_func_defaults(func):
172179 return func .__defaults__
173180
174181
175- #= =============================================================================
182+ # =============================================================================
176183# Special method attributes
177- #= =============================================================================
184+ # =============================================================================
178185def get_meth_func (obj ):
179186 """Return method function object"""
180187 if PY2 :
@@ -184,6 +191,7 @@ def get_meth_func(obj):
184191 # Python 3
185192 return obj .__func__
186193
194+
187195def get_meth_class_inst (obj ):
188196 """Return method class instance"""
189197 if PY2 :
@@ -193,6 +201,7 @@ def get_meth_class_inst(obj):
193201 # Python 3
194202 return obj .__self__
195203
204+
196205def get_meth_class (obj ):
197206 """Return method class"""
198207 if PY2 :
@@ -203,9 +212,9 @@ def get_meth_class(obj):
203212 return obj .__self__ .__class__
204213
205214
206- #= =============================================================================
215+ # =============================================================================
207216# Misc.
208- #= =============================================================================
217+ # =============================================================================
209218if PY2 :
210219 # Python 2
211220 input = raw_input
@@ -217,10 +226,12 @@ def get_meth_class(obj):
217226 # Python 3
218227 input = input
219228 getcwd = os .getcwd
229+
220230 def cmp (a , b ):
221231 return (a > b ) - (a < b )
222232 str_lower = str .lower
223233
234+
224235def qbytearray_to_str (qba ):
225236 """Convert QByteArray object to str in a way compatible with Python 2/3"""
226237 return str (bytes (qba .toHex ()).decode ())
0 commit comments