Skip to content

Commit 7e6690b

Browse files
author
Pierre Raybaut
committed
Added module py3compat for Python2/Python3 compatiblity
1 parent 27f894a commit 7e6690b

File tree

2 files changed

+232
-3
lines changed

2 files changed

+232
-3
lines changed

qwt/py3compat.py

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright © 2012-2013 Pierre Raybaut
4+
# Licensed under the terms of the MIT License
5+
# (see spyderlib/__init__.py for details)
6+
7+
"""
8+
guidata.py3compat (exact copy of spyderlib.py3compat)
9+
-----------------------------------------------------
10+
11+
Transitional module providing compatibility functions intended to help
12+
migrating from Python 2 to Python 3.
13+
14+
This module should be fully compatible with:
15+
* Python >=v2.6
16+
* Python 3
17+
"""
18+
19+
from __future__ import print_function
20+
21+
import sys
22+
import os
23+
24+
PY2 = sys.version[0] == '2'
25+
PY3 = sys.version[0] == '3'
26+
27+
28+
#==============================================================================
29+
# Data types
30+
#==============================================================================
31+
if PY2:
32+
# Python 2
33+
TEXT_TYPES = (str, unicode)
34+
INT_TYPES = (int, long)
35+
else:
36+
# Python 3
37+
TEXT_TYPES = (str,)
38+
INT_TYPES = (int,)
39+
NUMERIC_TYPES = tuple(list(INT_TYPES) + [float, complex])
40+
41+
42+
#==============================================================================
43+
# Renamed/Reorganized modules
44+
#==============================================================================
45+
if PY2:
46+
# Python 2
47+
import __builtin__ as builtins
48+
import ConfigParser as configparser
49+
try:
50+
import _winreg as winreg
51+
except ImportError:
52+
pass
53+
from sys import maxint as maxsize
54+
try:
55+
import CStringIO as io
56+
except ImportError:
57+
import StringIO as io
58+
try:
59+
import cPickle as pickle
60+
except ImportError:
61+
import pickle
62+
from UserDict import DictMixin as MutableMapping
63+
else:
64+
# Python 3
65+
import builtins
66+
import configparser
67+
try:
68+
import winreg
69+
except ImportError:
70+
pass
71+
from sys import maxsize
72+
import io
73+
import pickle
74+
from collections import MutableMapping
75+
76+
77+
#==============================================================================
78+
# Strings
79+
#==============================================================================
80+
def is_text_string(obj):
81+
"""Return True if `obj` is a text string, False if it is anything else,
82+
like binary data (Python 3) or QString (Python 2, PyQt API #1)"""
83+
if PY2:
84+
# Python 2
85+
return isinstance(obj, basestring)
86+
else:
87+
# Python 3
88+
return isinstance(obj, str)
89+
90+
def is_binary_string(obj):
91+
"""Return True if `obj` is a binary string, False if it is anything else"""
92+
if PY2:
93+
# Python 2
94+
return isinstance(obj, str)
95+
else:
96+
# Python 3
97+
return isinstance(obj, bytes)
98+
99+
def is_string(obj):
100+
"""Return True if `obj` is a text or binary Python string object,
101+
False if it is anything else, like a QString (Python 2, PyQt API #1)"""
102+
return is_text_string(obj) or is_binary_string(obj)
103+
104+
def is_unicode(obj):
105+
"""Return True if `obj` is unicode"""
106+
if PY2:
107+
# Python 2
108+
return isinstance(obj, unicode)
109+
else:
110+
# Python 3
111+
return isinstance(obj, str)
112+
113+
def to_text_string(obj, encoding=None):
114+
"""Convert `obj` to (unicode) text string"""
115+
if PY2:
116+
# Python 2
117+
if encoding is None:
118+
return unicode(obj)
119+
else:
120+
return unicode(obj, encoding)
121+
else:
122+
# Python 3
123+
if encoding is None:
124+
return str(obj)
125+
elif isinstance(obj, str):
126+
# In case this function is not used properly, this could happen
127+
return obj
128+
else:
129+
return str(obj, encoding)
130+
131+
def to_binary_string(obj, encoding=None):
132+
"""Convert `obj` to binary string (bytes in Python 3, str in Python 2)"""
133+
if PY2:
134+
# Python 2
135+
if encoding is None:
136+
return str(obj)
137+
else:
138+
return obj.encode(encoding)
139+
else:
140+
# Python 3
141+
return bytes(obj, 'utf-8' if encoding is None else encoding)
142+
143+
144+
#==============================================================================
145+
# Function attributes
146+
#==============================================================================
147+
def get_func_code(func):
148+
"""Return function code object"""
149+
if PY2:
150+
# Python 2
151+
return func.func_code
152+
else:
153+
# Python 3
154+
return func.__code__
155+
156+
def get_func_name(func):
157+
"""Return function name"""
158+
if PY2:
159+
# Python 2
160+
return func.func_name
161+
else:
162+
# Python 3
163+
return func.__name__
164+
165+
def get_func_defaults(func):
166+
"""Return function default argument values"""
167+
if PY2:
168+
# Python 2
169+
return func.func_defaults
170+
else:
171+
# Python 3
172+
return func.__defaults__
173+
174+
175+
#==============================================================================
176+
# Special method attributes
177+
#==============================================================================
178+
def get_meth_func(obj):
179+
"""Return method function object"""
180+
if PY2:
181+
# Python 2
182+
return obj.im_func
183+
else:
184+
# Python 3
185+
return obj.__func__
186+
187+
def get_meth_class_inst(obj):
188+
"""Return method class instance"""
189+
if PY2:
190+
# Python 2
191+
return obj.im_self
192+
else:
193+
# Python 3
194+
return obj.__self__
195+
196+
def get_meth_class(obj):
197+
"""Return method class"""
198+
if PY2:
199+
# Python 2
200+
return obj.im_class
201+
else:
202+
# Python 3
203+
return obj.__self__.__class__
204+
205+
206+
#==============================================================================
207+
# Misc.
208+
#==============================================================================
209+
if PY2:
210+
# Python 2
211+
input = raw_input
212+
getcwd = os.getcwdu
213+
cmp = cmp
214+
import string
215+
str_lower = string.lower
216+
else:
217+
# Python 3
218+
input = input
219+
getcwd = os.getcwd
220+
def cmp(a, b):
221+
return (a > b) - (a < b)
222+
str_lower = str.lower
223+
224+
def qbytearray_to_str(qba):
225+
"""Convert QByteArray object to str in a way compatible with Python 2/3"""
226+
return str(bytes(qba.toHex()).decode())
227+
228+
229+
if __name__ == '__main__':
230+
pass

qwt/qt/compat.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@
2323
import sys
2424
import collections
2525

26-
from spyderlib.qt.QtGui import QFileDialog
27-
28-
from spyderlib.py3compat import is_text_string, to_text_string, TEXT_TYPES
26+
from qwt.qt.QtGui import QFileDialog
27+
from qwt.py3compat import is_text_string, to_text_string, TEXT_TYPES
2928

3029
#==============================================================================
3130
# QVariant conversion utilities

0 commit comments

Comments
 (0)