| 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 | |
| 44 | #======================================================================================================================= |
| 45 | # Python 3? |
| 46 | #======================================================================================================================= |
| 47 | IS_PY3K = False |
| 48 | IS_PY27 = False |
| 49 | IS_PY24 = False |
| 50 | try: |
| 51 | if sys.version_info[0] >= 3: |
| 52 | IS_PY3K = True |
| 53 | elif sys.version_info[0] == 2 and sys.version_info[1] == 7: |
| 54 | IS_PY27 = True |
| 55 | elif sys.version_info[0] == 2 and sys.version_info[1] == 4: |
| 56 | IS_PY24 = True |
| 57 | except AttributeError: |
| 58 | pass #Not all versions have sys.version_info |
| 59 | |
| 60 | try: |
| 61 | IS_64_BITS = sys.maxsize > 2 ** 32 |
| 62 | except AttributeError: |
| 63 | try: |
| 64 | import struct |
| 65 | IS_64_BITS = struct.calcsize("P") * 8 > 32 |
| 66 | except: |
| 67 | IS_64_BITS = False |
| 68 | |
| 69 | SUPPORT_GEVENT = os.getenv('GEVENT_SUPPORT', 'False') == 'True' |
| 70 | |
| 71 | USE_LIB_COPY = SUPPORT_GEVENT and not IS_PY3K and sys.version_info[1] >= 6 |
| 72 | |
| 73 | if USE_LIB_COPY: |
| 74 | import _pydev_threading as threading |
| 75 | else: |
| 76 | import threading |
| 77 | |
| 78 | _nextThreadIdLock = threading.Lock() |
| 79 | |
| 80 | #======================================================================================================================= |
| 81 | # Jython? |
| 82 | #======================================================================================================================= |
| 83 | try: |
| 84 | import org.python.core.PyDictionary #@UnresolvedImport @UnusedImport -- just to check if it could be valid |
| 85 | |
| 86 | def DictContains(d, key): |
| 87 | return d.has_key(key) |
| 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) |
| 98 | |
| 99 | |
| 100 | try: |
| 101 | xrange |
| 102 | except: |
| 103 | #Python 3k does not have it |
| 104 | xrange = range |
| 105 | |
| 106 | try: |
| 107 | object |
| 108 | except NameError: |
| 109 | class object: |
| 110 | pass |
| 111 | |
| 112 | try: |
| 113 | enumerate |
| 114 | except: |
| 115 | def enumerate(lst): |
| 116 | ret = [] |
| 117 | i=0 |
| 118 | for element in lst: |
| 119 | ret.append((i, element)) |
| 120 | i+=1 |
| 121 | return ret |
| 122 | |
| 123 | #======================================================================================================================= |
| 124 | # StringIO |
| 125 | #======================================================================================================================= |
| 126 | try: |
| 127 | from StringIO import StringIO |
| 128 | except: |
| 129 | from io import StringIO |
| 130 | |
| 131 | |
| 132 | #======================================================================================================================= |
| 133 | # NextId |
| 134 | #======================================================================================================================= |
| 135 | class NextId: |
| 136 | |
| 137 | def __init__(self): |
| 138 | self._id = 0 |
| 139 | |
| 140 | def __call__(self): |
| 141 | #No need to synchronize here |
| 142 | self._id += 1 |
| 143 | return self._id |
| 144 | |
| 145 | _nextThreadId = NextId() |
| 146 | |
| 147 | #======================================================================================================================= |
| 148 | # GetThreadId |
| 149 | #======================================================================================================================= |
| 150 | def GetThreadId(thread): |
| 151 | try: |
| 152 | return thread.__pydevd_id__ |
| 153 | except AttributeError: |
| 154 | _nextThreadIdLock.acquire() |
| 155 | try: |
| 156 | #We do a new check with the lock in place just to be sure that nothing changed |
| 157 | if not hasattr(thread, '__pydevd_id__'): |
| 158 | try: |
| 159 | pid = os.getpid() |
| 160 | except AttributeError: |
| 161 | try: |
| 162 | #Jython does not have it! |
| 163 | import java.lang.management.ManagementFactory #@UnresolvedImport -- just for jython |
| 164 | |
| 165 | pid = java.lang.management.ManagementFactory.getRuntimeMXBean().getName() |
| 166 | pid = pid.replace('@', '_') |
| 167 | except: |
| 168 | #ok, no pid available (will be unable to debug multiple processes) |
| 169 | pid = '000001' |
| 170 | |
| 171 | thread.__pydevd_id__ = 'pid%s_seq%s' % (pid, _nextThreadId()) |
| 172 | finally: |
| 173 | _nextThreadIdLock.release() |
| 174 | |
| 175 | return thread.__pydevd_id__ |
| 176 | |
| 177 | #=============================================================================== |
| 178 | # Null |
| 179 | #=============================================================================== |
| 180 | class Null: |
| 181 | """ |
| 182 | Gotten from: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/68205 |
| 183 | """ |
| 184 | |
| 185 | def __init__(self, *args, **kwargs): |
| 186 | return None |
| 187 | |
| 188 | def __call__(self, *args, **kwargs): |
| 189 | return self |
| 190 | |
| 191 | def __getattr__(self, mname): |
| 192 | return self |
| 193 | |
| 194 | def __setattr__(self, name, value): |
| 195 | return self |
| 196 | |
| 197 | def __delattr__(self, name): |
| 198 | return self |
| 199 | |
| 200 | def __repr__(self): |
| 201 | return "<Null>" |
| 202 | |
| 203 | def __str__(self): |
| 204 | return "Null" |
| 205 | |
| 206 | def __len__(self): |
| 207 | return 0 |
| 208 | |
| 209 | def __getitem__(self): |
| 210 | return self |
| 211 | |
| 212 | def __setitem__(self, *args, **kwargs): |
| 213 | pass |
| 214 | |
| 215 | def write(self, *args, **kwargs): |
| 216 | pass |
| 217 | |
| 218 | def __nonzero__(self): |
| 219 | return 0 |
| 220 | |
| 221 | if __name__ == '__main__': |
| 222 | if Null(): |
| 223 | sys.stdout.write('here\n') |
| 224 | |