blob: 71fe4aed25052bb511469f308ff3b552f2c1a46c [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:
Tor Norbyec667c1f2014-05-28 17:06:51 -070062 pass #Not all versions have sys.version_info
Tor Norbye3a2425a2013-11-04 10:16:08 -080063
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:
Tor Norbyec667c1f2014-05-28 17:06:51 -070088 DictContains = dict.has_key
Tor Norbye3a2425a2013-11-04 10:16:08 -080089except:
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 Norbyec667c1f2014-05-28 17:06:51 -070099#=======================================================================================================================
100# Jython?
101#=======================================================================================================================
102try:
103 DictPop = dict.pop
104except:
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 Norbye3a2425a2013-11-04 10:16:08 -0800112
113
114try:
115 xrange
116except:
117 #Python 3k does not have it
118 xrange = range
119
120try:
121 object
122except NameError:
123 class object:
124 pass
125
126try:
127 enumerate
128except:
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#=======================================================================================================================
140try:
141 from StringIO import StringIO
142except:
143 from io import StringIO
144
145
146#=======================================================================================================================
147# NextId
148#=======================================================================================================================
149class 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#=======================================================================================================================
164def 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#===============================================================================
194class 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 Norbyec667c1f2014-05-28 17:06:51 -0700206 if len(mname) > 4 and mname[:2] == '__' and mname[-2:] == '__':
207 # Don't pretend to implement special method names.
208 raise AttributeError(mname)
Tor Norbye3a2425a2013-11-04 10:16:08 -0800209 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 Norbyec667c1f2014-05-28 17:06:51 -0700238 def __iter__(self):
239 return iter(())
240
241
242def 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 Norbye3a2425a2013-11-04 10:16:08 -0800262if __name__ == '__main__':
263 if Null():
264 sys.stdout.write('here\n')
265