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