blob: 7c197efc2002cc9f74b385d90e0d981c36766701 [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):
71 TracingFunctionHolder._lock.acquire()
72 try:
73 TracingFunctionHolder._warn = False
74 _InternalSetTrace(tracing_func)
75 TracingFunctionHolder._warn = True
76 finally:
77 TracingFunctionHolder._lock.release()
78
79
80def ReplaceSysSetTraceFunc():
81 if TracingFunctionHolder._original_tracing is None:
82 TracingFunctionHolder._original_tracing = sys.settrace
83 sys.settrace = _InternalSetTrace
84
85def RestoreSysSetTraceFunc():
86 if TracingFunctionHolder._original_tracing is not None:
87 sys.settrace = TracingFunctionHolder._original_tracing
88 TracingFunctionHolder._original_tracing = None
89
90