blob: 1a5a833f6bd0f3d9e1017dd1d7e61a74c352a374 [file] [log] [blame]
Tor Norbye3a2425a2013-11-04 10:16:08 -08001from pydevd_constants import * #@UnusedWildImport
2
3try:
4 import cStringIO as StringIO #may not always be available @UnusedImport
5except:
6 try:
7 import StringIO #@Reimport
8 except:
9 import io as StringIO
10
11if USE_LIB_COPY:
12 import _pydev_threading as threading
13else:
14 import threading
15
16import sys #@Reimport
17import traceback
18
19class TracingFunctionHolder:
20 '''This class exists just to keep some variables (so that we don't keep them in the global namespace).
21 '''
22 _original_tracing = None
23 _warn = True
24 _lock = threading.Lock()
25 _traceback_limit = 1
26 _warnings_shown = {}
27
28
29def GetExceptionTracebackStr():
30 exc_info = sys.exc_info()
31 s = StringIO.StringIO()
32 traceback.print_exception(exc_info[0], exc_info[1], exc_info[2], file=s)
33 return s.getvalue()
34
35def _GetStackStr(frame):
36
37 msg = '\nIf this is needed, please check: ' + \
38 '\nhttp://pydev.blogspot.com/2007/06/why-cant-pydev-debugger-work-with.html' + \
39 '\nto see how to restore the debug tracing back correctly.\n'
40
41 if TracingFunctionHolder._traceback_limit:
42 s = StringIO.StringIO()
43 s.write('Call Location:\n')
44 traceback.print_stack(f=frame, limit=TracingFunctionHolder._traceback_limit, file=s)
45 msg = msg + s.getvalue()
46
47 return msg
48
49def _InternalSetTrace(tracing_func):
50 if TracingFunctionHolder._warn:
51 frame = GetFrame()
52 if frame is not None and frame.f_back is not None:
53 if not frame.f_back.f_code.co_filename.lower().endswith('threading.py'):
54
55 message = \
56 '\nPYDEV DEBUGGER WARNING:' + \
57 '\nsys.settrace() should not be used when the debugger is being used.' + \
58 '\nThis may cause the debugger to stop working correctly.' + \
59 '%s' % _GetStackStr(frame.f_back)
60
61 if message not in TracingFunctionHolder._warnings_shown:
62 #only warn about each message once...
63 TracingFunctionHolder._warnings_shown[message] = 1
64 sys.stderr.write('%s\n' % (message,))
65 sys.stderr.flush()
66
67 if TracingFunctionHolder._original_tracing:
68 TracingFunctionHolder._original_tracing(tracing_func)
69
70def SetTrace(tracing_func):
Tor Norbyec667c1f2014-05-28 17:06:51 -070071 if TracingFunctionHolder._original_tracing is None:
72 #This may happen before ReplaceSysSetTraceFunc is called.
73 sys.settrace(tracing_func)
74 return
75
Tor Norbye3a2425a2013-11-04 10:16:08 -080076 TracingFunctionHolder._lock.acquire()
77 try:
78 TracingFunctionHolder._warn = False
79 _InternalSetTrace(tracing_func)
80 TracingFunctionHolder._warn = True
81 finally:
82 TracingFunctionHolder._lock.release()
Tor Norbyec667c1f2014-05-28 17:06:51 -070083
84
Tor Norbye3a2425a2013-11-04 10:16:08 -080085def ReplaceSysSetTraceFunc():
86 if TracingFunctionHolder._original_tracing is None:
87 TracingFunctionHolder._original_tracing = sys.settrace
88 sys.settrace = _InternalSetTrace
89
90def RestoreSysSetTraceFunc():
91 if TracingFunctionHolder._original_tracing is not None:
92 sys.settrace = TracingFunctionHolder._original_tracing
93 TracingFunctionHolder._original_tracing = None
94
95