blob: 34759f7f999dcaac11cb6bea518828f5ea5293d3 [file] [log] [blame]
Tor Norbye3a2425a2013-11-04 10:16:08 -08001'''
2This module holds the constants used for specifying the states of the debugger.
3'''
4
5STATE_RUN = 1
6STATE_SUSPEND = 2
7
8PYTHON_SUSPEND = 1
9DJANGO_SUSPEND = 2
10
11try:
12 __setFalse = False
13except:
14 import __builtin__
15
16 setattr(__builtin__, 'True', 1)
17 setattr(__builtin__, 'False', 0)
18
19class 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
27USE_PSYCO_OPTIMIZATION = True
28
29#Hold a reference to the original _getframe (because psyco will change that as soon as it's imported)
30import sys #Note: the sys import must be here anyways (others depend on it)
31try:
32 GetFrame = sys._getframe
33except 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.
40MAXIMUM_VARIABLE_REPRESENTATION_SIZE = 1000
41
42import os
43
44#=======================================================================================================================
45# Python 3?
46#=======================================================================================================================
47IS_PY3K = False
48IS_PY27 = False
49IS_PY24 = False
50try:
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
57except AttributeError:
58 pass #Not all versions have sys.version_info
59
60try:
61 IS_64_BITS = sys.maxsize > 2 ** 32
62except AttributeError:
63 try:
64 import struct
65 IS_64_BITS = struct.calcsize("P") * 8 > 32
66 except:
67 IS_64_BITS = False
68
69SUPPORT_GEVENT = os.getenv('GEVENT_SUPPORT', 'False') == 'True'
70
71USE_LIB_COPY = SUPPORT_GEVENT and not IS_PY3K and sys.version_info[1] >= 6
72
73if USE_LIB_COPY:
74 import _pydev_threading as threading
75else:
76 import threading
77
78_nextThreadIdLock = threading.Lock()
79
80#=======================================================================================================================
81# Jython?
82#=======================================================================================================================
83try:
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)
88except:
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
100try:
101 xrange
102except:
103 #Python 3k does not have it
104 xrange = range
105
106try:
107 object
108except NameError:
109 class object:
110 pass
111
112try:
113 enumerate
114except:
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#=======================================================================================================================
126try:
127 from StringIO import StringIO
128except:
129 from io import StringIO
130
131
132#=======================================================================================================================
133# NextId
134#=======================================================================================================================
135class 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#=======================================================================================================================
150def 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#===============================================================================
180class 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
221if __name__ == '__main__':
222 if Null():
223 sys.stdout.write('here\n')
224