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