| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame^] | 1 | from pydevd_constants import * #@UnusedWildImport |
| 2 | |
| 3 | try: |
| 4 | import cStringIO as StringIO #may not always be available @UnusedImport |
| 5 | except: |
| 6 | try: |
| 7 | import StringIO #@Reimport |
| 8 | except: |
| 9 | import io as StringIO |
| 10 | |
| 11 | if USE_LIB_COPY: |
| 12 | import _pydev_threading as threading |
| 13 | else: |
| 14 | import threading |
| 15 | |
| 16 | import sys #@Reimport |
| 17 | import traceback |
| 18 | |
| 19 | class 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 | |
| 29 | def 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 | |
| 35 | def _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 | |
| 49 | def _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 | |
| 70 | def 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 | |
| 80 | def ReplaceSysSetTraceFunc(): |
| 81 | if TracingFunctionHolder._original_tracing is None: |
| 82 | TracingFunctionHolder._original_tracing = sys.settrace |
| 83 | sys.settrace = _InternalSetTrace |
| 84 | |
| 85 | def RestoreSysSetTraceFunc(): |
| 86 | if TracingFunctionHolder._original_tracing is not None: |
| 87 | sys.settrace = TracingFunctionHolder._original_tracing |
| 88 | TracingFunctionHolder._original_tracing = None |
| 89 | |
| 90 | |