| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 1 | ''' |
| 2 | This module holds the constants used for specifying the states of the debugger. |
| 3 | ''' |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 4 | STATE_RUN = 1 |
| 5 | STATE_SUSPEND = 2 |
| 6 | |
| 7 | PYTHON_SUSPEND = 1 |
| 8 | DJANGO_SUSPEND = 2 |
| 9 | |
| 10 | try: |
| 11 | __setFalse = False |
| 12 | except: |
| 13 | import __builtin__ |
| 14 | |
| 15 | setattr(__builtin__, 'True', 1) |
| 16 | setattr(__builtin__, 'False', 0) |
| 17 | |
| 18 | class DebugInfoHolder: |
| Tor Norbye | 1aa2e09 | 2014-08-20 17:01:23 -0700 | [diff] [blame^] | 19 | #we have to put it here because it can be set through the command line (so, the |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 20 | #already imported references would not have it). |
| 21 | DEBUG_RECORD_SOCKET_READS = False |
| 22 | DEBUG_TRACE_LEVEL = -1 |
| 23 | DEBUG_TRACE_BREAKPOINTS = -1 |
| 24 | |
| Tor Norbye | 1aa2e09 | 2014-08-20 17:01:23 -0700 | [diff] [blame^] | 25 | #Optimize with psyco? This gave a 50% speedup in the debugger in tests |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 26 | USE_PSYCO_OPTIMIZATION = True |
| 27 | |
| 28 | #Hold a reference to the original _getframe (because psyco will change that as soon as it's imported) |
| 29 | import sys #Note: the sys import must be here anyways (others depend on it) |
| 30 | try: |
| 31 | GetFrame = sys._getframe |
| 32 | except AttributeError: |
| 33 | def GetFrame(): |
| 34 | raise AssertionError('sys._getframe not available (possible causes: enable -X:Frames on IronPython?)') |
| 35 | |
| 36 | #Used to determine the maximum size of each variable passed to eclipse -- having a big value here may make |
| 37 | #the communication slower -- as the variables are being gathered lazily in the latest version of eclipse, |
| 38 | #this value was raised from 200 to 1000. |
| 39 | MAXIMUM_VARIABLE_REPRESENTATION_SIZE = 1000 |
| 40 | |
| 41 | import os |
| 42 | |
| Tor Norbye | 8668e1b | 2013-12-20 09:14:04 -0800 | [diff] [blame] | 43 | import pydevd_vm_type |
| 44 | |
| 45 | IS_JYTHON = pydevd_vm_type.GetVmType() == pydevd_vm_type.PydevdVmType.JYTHON |
| 46 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 47 | #======================================================================================================================= |
| 48 | # Python 3? |
| 49 | #======================================================================================================================= |
| 50 | IS_PY3K = False |
| 51 | IS_PY27 = False |
| 52 | IS_PY24 = False |
| 53 | try: |
| 54 | if sys.version_info[0] >= 3: |
| 55 | IS_PY3K = True |
| 56 | elif sys.version_info[0] == 2 and sys.version_info[1] == 7: |
| 57 | IS_PY27 = True |
| 58 | elif sys.version_info[0] == 2 and sys.version_info[1] == 4: |
| 59 | IS_PY24 = True |
| 60 | except AttributeError: |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame] | 61 | pass #Not all versions have sys.version_info |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 62 | |
| 63 | try: |
| 64 | IS_64_BITS = sys.maxsize > 2 ** 32 |
| 65 | except AttributeError: |
| 66 | try: |
| 67 | import struct |
| 68 | IS_64_BITS = struct.calcsize("P") * 8 > 32 |
| 69 | except: |
| 70 | IS_64_BITS = False |
| 71 | |
| 72 | SUPPORT_GEVENT = os.getenv('GEVENT_SUPPORT', 'False') == 'True' |
| 73 | |
| 74 | USE_LIB_COPY = SUPPORT_GEVENT and not IS_PY3K and sys.version_info[1] >= 6 |
| 75 | |
| 76 | if USE_LIB_COPY: |
| 77 | import _pydev_threading as threading |
| 78 | else: |
| 79 | import threading |
| 80 | |
| 81 | _nextThreadIdLock = threading.Lock() |
| 82 | |
| 83 | #======================================================================================================================= |
| 84 | # Jython? |
| 85 | #======================================================================================================================= |
| 86 | try: |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame] | 87 | DictContains = dict.has_key |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 88 | except: |
| 89 | try: |
| 90 | #Py3k does not have has_key anymore, and older versions don't have __contains__ |
| 91 | DictContains = dict.__contains__ |
| 92 | except: |
| 93 | try: |
| 94 | DictContains = dict.has_key |
| 95 | except NameError: |
| 96 | def DictContains(d, key): |
| 97 | return d.has_key(key) |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame] | 98 | #======================================================================================================================= |
| 99 | # Jython? |
| 100 | #======================================================================================================================= |
| 101 | try: |
| 102 | DictPop = dict.pop |
| 103 | except: |
| 104 | def DictPop(d, key, default=None): |
| 105 | try: |
| 106 | ret = d[key] |
| 107 | del d[key] |
| 108 | return ret |
| 109 | except: |
| 110 | return default |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 111 | |
| 112 | |
| Tor Norbye | 1aa2e09 | 2014-08-20 17:01:23 -0700 | [diff] [blame^] | 113 | if IS_PY3K: |
| 114 | def DictKeys(d): |
| 115 | return list(d.keys()) |
| 116 | |
| 117 | def DictValues(d): |
| 118 | return list(d.values()) |
| 119 | |
| 120 | DictIterValues = dict.values |
| 121 | |
| 122 | def DictIterItems(d): |
| 123 | return d.items() |
| 124 | |
| 125 | def DictItems(d): |
| 126 | return list(d.items()) |
| 127 | |
| 128 | else: |
| 129 | DictKeys = dict.keys |
| 130 | try: |
| 131 | DictIterValues = dict.itervalues |
| 132 | except: |
| 133 | DictIterValues = dict.values #Older versions don't have the itervalues |
| 134 | |
| 135 | DictValues = dict.values |
| 136 | |
| 137 | def DictIterItems(d): |
| 138 | return d.iteritems() |
| 139 | |
| 140 | def DictItems(d): |
| 141 | return d.items() |
| 142 | |
| 143 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 144 | try: |
| Tor Norbye | 1aa2e09 | 2014-08-20 17:01:23 -0700 | [diff] [blame^] | 145 | xrange = xrange |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 146 | except: |
| 147 | #Python 3k does not have it |
| 148 | xrange = range |
| Tor Norbye | 1aa2e09 | 2014-08-20 17:01:23 -0700 | [diff] [blame^] | 149 | |
| 150 | try: |
| 151 | import itertools |
| 152 | izip = itertools.izip |
| 153 | except: |
| 154 | izip = zip |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 155 | |
| 156 | try: |
| 157 | object |
| 158 | except NameError: |
| 159 | class object: |
| 160 | pass |
| 161 | |
| 162 | try: |
| 163 | enumerate |
| 164 | except: |
| 165 | def enumerate(lst): |
| 166 | ret = [] |
| Tor Norbye | 1aa2e09 | 2014-08-20 17:01:23 -0700 | [diff] [blame^] | 167 | i = 0 |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 168 | for element in lst: |
| 169 | ret.append((i, element)) |
| Tor Norbye | 1aa2e09 | 2014-08-20 17:01:23 -0700 | [diff] [blame^] | 170 | i += 1 |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 171 | return ret |
| 172 | |
| 173 | #======================================================================================================================= |
| 174 | # StringIO |
| 175 | #======================================================================================================================= |
| 176 | try: |
| 177 | from StringIO import StringIO |
| 178 | except: |
| 179 | from io import StringIO |
| 180 | |
| 181 | |
| 182 | #======================================================================================================================= |
| 183 | # NextId |
| 184 | #======================================================================================================================= |
| 185 | class NextId: |
| 186 | |
| 187 | def __init__(self): |
| 188 | self._id = 0 |
| 189 | |
| 190 | def __call__(self): |
| 191 | #No need to synchronize here |
| 192 | self._id += 1 |
| 193 | return self._id |
| 194 | |
| 195 | _nextThreadId = NextId() |
| 196 | |
| 197 | #======================================================================================================================= |
| 198 | # GetThreadId |
| 199 | #======================================================================================================================= |
| 200 | def GetThreadId(thread): |
| 201 | try: |
| 202 | return thread.__pydevd_id__ |
| 203 | except AttributeError: |
| 204 | _nextThreadIdLock.acquire() |
| 205 | try: |
| 206 | #We do a new check with the lock in place just to be sure that nothing changed |
| 207 | if not hasattr(thread, '__pydevd_id__'): |
| 208 | try: |
| 209 | pid = os.getpid() |
| 210 | except AttributeError: |
| 211 | try: |
| 212 | #Jython does not have it! |
| Tor Norbye | 1aa2e09 | 2014-08-20 17:01:23 -0700 | [diff] [blame^] | 213 | import java.lang.management.ManagementFactory #@UnresolvedImport -- just for jython |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 214 | pid = java.lang.management.ManagementFactory.getRuntimeMXBean().getName() |
| 215 | pid = pid.replace('@', '_') |
| 216 | except: |
| 217 | #ok, no pid available (will be unable to debug multiple processes) |
| 218 | pid = '000001' |
| 219 | |
| 220 | thread.__pydevd_id__ = 'pid%s_seq%s' % (pid, _nextThreadId()) |
| 221 | finally: |
| 222 | _nextThreadIdLock.release() |
| 223 | |
| 224 | return thread.__pydevd_id__ |
| 225 | |
| 226 | #=============================================================================== |
| 227 | # Null |
| 228 | #=============================================================================== |
| 229 | class Null: |
| 230 | """ |
| 231 | Gotten from: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/68205 |
| 232 | """ |
| 233 | |
| 234 | def __init__(self, *args, **kwargs): |
| 235 | return None |
| 236 | |
| 237 | def __call__(self, *args, **kwargs): |
| 238 | return self |
| 239 | |
| 240 | def __getattr__(self, mname): |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame] | 241 | if len(mname) > 4 and mname[:2] == '__' and mname[-2:] == '__': |
| 242 | # Don't pretend to implement special method names. |
| 243 | raise AttributeError(mname) |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 244 | return self |
| 245 | |
| 246 | def __setattr__(self, name, value): |
| 247 | return self |
| 248 | |
| 249 | def __delattr__(self, name): |
| 250 | return self |
| 251 | |
| 252 | def __repr__(self): |
| 253 | return "<Null>" |
| 254 | |
| 255 | def __str__(self): |
| 256 | return "Null" |
| 257 | |
| 258 | def __len__(self): |
| 259 | return 0 |
| 260 | |
| 261 | def __getitem__(self): |
| 262 | return self |
| 263 | |
| 264 | def __setitem__(self, *args, **kwargs): |
| 265 | pass |
| 266 | |
| 267 | def write(self, *args, **kwargs): |
| 268 | pass |
| 269 | |
| 270 | def __nonzero__(self): |
| 271 | return 0 |
| 272 | |
| Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame] | 273 | def __iter__(self): |
| 274 | return iter(()) |
| 275 | |
| 276 | |
| 277 | def call_only_once(func): |
| 278 | ''' |
| 279 | To be used as a decorator |
| 280 | |
| 281 | @call_only_once |
| 282 | def func(): |
| 283 | print 'Calling func only this time' |
| 284 | |
| 285 | Actually, in PyDev it must be called as: |
| 286 | |
| 287 | func = call_only_once(func) to support older versions of Python. |
| 288 | ''' |
| 289 | def new_func(*args, **kwargs): |
| 290 | if not new_func._called: |
| 291 | new_func._called = True |
| 292 | return func(*args, **kwargs) |
| 293 | |
| 294 | new_func._called = False |
| 295 | return new_func |
| 296 | |
| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 297 | if __name__ == '__main__': |
| 298 | if Null(): |
| 299 | sys.stdout.write('here\n') |
| Tor Norbye | 1aa2e09 | 2014-08-20 17:01:23 -0700 | [diff] [blame^] | 300 | |