blob: 74e897461458b0f6cd6bc7084d831751e322db0a [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'''
Tor Norbye3a2425a2013-11-04 10:16:08 -08004STATE_RUN = 1
5STATE_SUSPEND = 2
6
7PYTHON_SUSPEND = 1
8DJANGO_SUSPEND = 2
9
10try:
11 __setFalse = False
12except:
13 import __builtin__
14
15 setattr(__builtin__, 'True', 1)
16 setattr(__builtin__, 'False', 0)
17
18class DebugInfoHolder:
Tor Norbye1aa2e092014-08-20 17:01:23 -070019 #we have to put it here because it can be set through the command line (so, the
Tor Norbye3a2425a2013-11-04 10:16:08 -080020 #already imported references would not have it).
21 DEBUG_RECORD_SOCKET_READS = False
22 DEBUG_TRACE_LEVEL = -1
23 DEBUG_TRACE_BREAKPOINTS = -1
24
Tor Norbye1aa2e092014-08-20 17:01:23 -070025#Optimize with psyco? This gave a 50% speedup in the debugger in tests
Tor Norbye3a2425a2013-11-04 10:16:08 -080026USE_PSYCO_OPTIMIZATION = True
27
28#Hold a reference to the original _getframe (because psyco will change that as soon as it's imported)
29import sys #Note: the sys import must be here anyways (others depend on it)
30try:
31 GetFrame = sys._getframe
32except AttributeError:
33 def GetFrame():
34 raise AssertionError('sys._getframe not available (possible causes: enable -X:Frames on IronPython?)')
35
36#Used to determine the maximum size of each variable passed to eclipse -- having a big value here may make
37#the communication slower -- as the variables are being gathered lazily in the latest version of eclipse,
38#this value was raised from 200 to 1000.
39MAXIMUM_VARIABLE_REPRESENTATION_SIZE = 1000
40
41import os
42
Tor Norbye8668e1b2013-12-20 09:14:04 -080043import pydevd_vm_type
44
45IS_JYTHON = pydevd_vm_type.GetVmType() == pydevd_vm_type.PydevdVmType.JYTHON
46
Tor Norbye3a2425a2013-11-04 10:16:08 -080047#=======================================================================================================================
48# Python 3?
49#=======================================================================================================================
50IS_PY3K = False
51IS_PY27 = False
52IS_PY24 = False
53try:
54 if sys.version_info[0] >= 3:
55 IS_PY3K = True
56 elif sys.version_info[0] == 2 and sys.version_info[1] == 7:
57 IS_PY27 = True
58 elif sys.version_info[0] == 2 and sys.version_info[1] == 4:
59 IS_PY24 = True
60except AttributeError:
Tor Norbyec667c1f2014-05-28 17:06:51 -070061 pass #Not all versions have sys.version_info
Tor Norbye3a2425a2013-11-04 10:16:08 -080062
63try:
64 IS_64_BITS = sys.maxsize > 2 ** 32
65except AttributeError:
66 try:
67 import struct
68 IS_64_BITS = struct.calcsize("P") * 8 > 32
69 except:
70 IS_64_BITS = False
71
72SUPPORT_GEVENT = os.getenv('GEVENT_SUPPORT', 'False') == 'True'
73
74USE_LIB_COPY = SUPPORT_GEVENT and not IS_PY3K and sys.version_info[1] >= 6
75
76if USE_LIB_COPY:
77 import _pydev_threading as threading
78else:
79 import threading
80
81_nextThreadIdLock = threading.Lock()
82
83#=======================================================================================================================
84# Jython?
85#=======================================================================================================================
86try:
Tor Norbyec667c1f2014-05-28 17:06:51 -070087 DictContains = dict.has_key
Tor Norbye3a2425a2013-11-04 10:16:08 -080088except:
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)
Tor Norbyec667c1f2014-05-28 17:06:51 -070098#=======================================================================================================================
99# Jython?
100#=======================================================================================================================
101try:
102 DictPop = dict.pop
103except:
104 def DictPop(d, key, default=None):
105 try:
106 ret = d[key]
107 del d[key]
108 return ret
109 except:
110 return default
Tor Norbye3a2425a2013-11-04 10:16:08 -0800111
112
Tor Norbye1aa2e092014-08-20 17:01:23 -0700113if IS_PY3K:
114 def DictKeys(d):
115 return list(d.keys())
116
117 def DictValues(d):
118 return list(d.values())
119
120 DictIterValues = dict.values
121
122 def DictIterItems(d):
123 return d.items()
124
125 def DictItems(d):
126 return list(d.items())
127
128else:
129 DictKeys = dict.keys
130 try:
131 DictIterValues = dict.itervalues
132 except:
133 DictIterValues = dict.values #Older versions don't have the itervalues
134
135 DictValues = dict.values
136
137 def DictIterItems(d):
138 return d.iteritems()
139
140 def DictItems(d):
141 return d.items()
142
143
Tor Norbye3a2425a2013-11-04 10:16:08 -0800144try:
Tor Norbye1aa2e092014-08-20 17:01:23 -0700145 xrange = xrange
Tor Norbye3a2425a2013-11-04 10:16:08 -0800146except:
147 #Python 3k does not have it
148 xrange = range
Tor Norbye1aa2e092014-08-20 17:01:23 -0700149
150try:
151 import itertools
152 izip = itertools.izip
153except:
154 izip = zip
Tor Norbye3a2425a2013-11-04 10:16:08 -0800155
156try:
157 object
158except NameError:
159 class object:
160 pass
161
162try:
163 enumerate
164except:
165 def enumerate(lst):
166 ret = []
Tor Norbye1aa2e092014-08-20 17:01:23 -0700167 i = 0
Tor Norbye3a2425a2013-11-04 10:16:08 -0800168 for element in lst:
169 ret.append((i, element))
Tor Norbye1aa2e092014-08-20 17:01:23 -0700170 i += 1
Tor Norbye3a2425a2013-11-04 10:16:08 -0800171 return ret
172
173#=======================================================================================================================
174# StringIO
175#=======================================================================================================================
176try:
177 from StringIO import StringIO
178except:
179 from io import StringIO
180
181
182#=======================================================================================================================
183# NextId
184#=======================================================================================================================
185class NextId:
186
187 def __init__(self):
188 self._id = 0
189
190 def __call__(self):
191 #No need to synchronize here
192 self._id += 1
193 return self._id
194
195_nextThreadId = NextId()
196
197#=======================================================================================================================
198# GetThreadId
199#=======================================================================================================================
200def GetThreadId(thread):
201 try:
202 return thread.__pydevd_id__
203 except AttributeError:
204 _nextThreadIdLock.acquire()
205 try:
206 #We do a new check with the lock in place just to be sure that nothing changed
207 if not hasattr(thread, '__pydevd_id__'):
208 try:
209 pid = os.getpid()
210 except AttributeError:
211 try:
212 #Jython does not have it!
Tor Norbye1aa2e092014-08-20 17:01:23 -0700213 import java.lang.management.ManagementFactory #@UnresolvedImport -- just for jython
Tor Norbye3a2425a2013-11-04 10:16:08 -0800214 pid = java.lang.management.ManagementFactory.getRuntimeMXBean().getName()
215 pid = pid.replace('@', '_')
216 except:
217 #ok, no pid available (will be unable to debug multiple processes)
218 pid = '000001'
219
220 thread.__pydevd_id__ = 'pid%s_seq%s' % (pid, _nextThreadId())
221 finally:
222 _nextThreadIdLock.release()
223
224 return thread.__pydevd_id__
225
226#===============================================================================
227# Null
228#===============================================================================
229class Null:
230 """
231 Gotten from: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/68205
232 """
233
234 def __init__(self, *args, **kwargs):
235 return None
236
237 def __call__(self, *args, **kwargs):
238 return self
239
240 def __getattr__(self, mname):
Tor Norbyec667c1f2014-05-28 17:06:51 -0700241 if len(mname) > 4 and mname[:2] == '__' and mname[-2:] == '__':
242 # Don't pretend to implement special method names.
243 raise AttributeError(mname)
Tor Norbye3a2425a2013-11-04 10:16:08 -0800244 return self
245
246 def __setattr__(self, name, value):
247 return self
248
249 def __delattr__(self, name):
250 return self
251
252 def __repr__(self):
253 return "<Null>"
254
255 def __str__(self):
256 return "Null"
257
258 def __len__(self):
259 return 0
260
261 def __getitem__(self):
262 return self
263
264 def __setitem__(self, *args, **kwargs):
265 pass
266
267 def write(self, *args, **kwargs):
268 pass
269
270 def __nonzero__(self):
271 return 0
272
Tor Norbyec667c1f2014-05-28 17:06:51 -0700273 def __iter__(self):
274 return iter(())
275
276
277def call_only_once(func):
278 '''
279 To be used as a decorator
280
281 @call_only_once
282 def func():
283 print 'Calling func only this time'
284
285 Actually, in PyDev it must be called as:
286
287 func = call_only_once(func) to support older versions of Python.
288 '''
289 def new_func(*args, **kwargs):
290 if not new_func._called:
291 new_func._called = True
292 return func(*args, **kwargs)
293
294 new_func._called = False
295 return new_func
296
Tor Norbye3a2425a2013-11-04 10:16:08 -0800297if __name__ == '__main__':
298 if Null():
299 sys.stdout.write('here\n')
Tor Norbye1aa2e092014-08-20 17:01:23 -0700300