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