def code_to_debug(): from dbgimporter import import_and_enable_debugger import_and_enable_debugger() import sys exit_code = eval(sys.argv[1]) print('sys.exit(%r)' % (exit_code, )) sys.exit(exit_code)
def code_to_debug(): from dbgimporter import import_and_enable_debugger import_and_enable_debugger() a = 1 b = {"one": 1, "two": 2} c = 3 print([a, b, c])
def code_to_debug(): from dbgimporter import import_and_enable_debugger import_and_enable_debugger() for i in range(0, 10): print(i) import backchannel backchannel.read_json()
def ptvsd_launcher(): # import_and_enable_debugger import ptvsd.debugger import backchannel args = tuple(backchannel.read_json()) print('debug%r' % (args, )) ptvsd.debugger.debug(*args)
def code_to_debug(): from dbgimporter import import_and_enable_debugger import_and_enable_debugger() a = 100 b = [1, 10, 100] c = {10: 10, 100: 100, 1000: 1000} d = {(1, 10, 100): (10000, 100000, 100000)} print((a, b, c, d)) # @bp
def code_to_debug(): from dbgimporter import import_and_enable_debugger import_and_enable_debugger() import ptvsd # Since Unicode variable name is a SyntaxError at parse time in Python 2, # this needs to do a roundabout way of setting it to avoid parse issues. globals()[u'\u16A0'] = 123 ptvsd.break_into_debugger() print('break')
def wait_until_realized(self, expectation, freeze=None, explain=True, observe=True): if explain: print(colors.LIGHT_MAGENTA + 'Waiting for ' + colors.RESET + colors.color_repr(expectation)) return self._wait_until_realized(expectation, freeze, explain, observe)
def code_to_debug(): from dbgimporter import import_and_enable_debugger import_and_enable_debugger() a = 10 for i in range(1, a): print('value: %d' % i) # Break at end too so that we're sure we get all output # events before the break. a = 10
def code_to_debug(): from dbgimporter import import_and_enable_debugger import_and_enable_debugger() def func(): print(1) # @inner1 print(2) # @inner2 print(3) # @outer3 func()
def wait_for_next(self, expectation, freeze=True, explain=True, observe=True): if explain: print(colors.LIGHT_MAGENTA + 'Waiting for next ' + colors.RESET + colors.color_repr(expectation)) return self._wait_until_realized(self._proceeding_from >> expectation, freeze, explain, observe)
def code_to_debug(): from dbgimporter import import_and_enable_debugger import_and_enable_debugger() import sys exit_code = eval(sys.argv[1]) print('sys.exit(%r)' % (exit_code, )) try: sys.exit(exit_code) # @handled except SystemExit: pass sys.exit(exit_code) # @unhandled
def code_to_debug(): import os import sys import backchannel from dbgimporter import import_and_enable_debugger import_and_enable_debugger() print('begin') assert backchannel.read_json() == 'continue' backchannel.write_json(os.path.abspath(sys.modules['ptvsd'].__file__)) print('end')
def wait_for(self, expectation, freeze=None, explain=True): assert expectation.has_lower_bound, ( 'Expectation must have a lower time bound to be used with wait_for()! ' 'Use >> to sequence an expectation against an occurrence to establish a lower bound, ' 'or wait_for_next() to wait for the next expectation since the timeline was last ' 'frozen, or wait_until_realized() when a lower bound is really not necessary.' ) if explain: print(colors.LIGHT_MAGENTA + 'Waiting for ' + colors.RESET + colors.color_repr(expectation)) return self._wait_until_realized(expectation, freeze, explain=explain)
def code_to_debug(): from dbgimporter import import_and_enable_debugger import_and_enable_debugger() class MyClass(object): def do_something(self): return 'did something' def my_func(): return 'did more things' MyClass().do_something() # @bp my_func() print('done')
def _explain_how_realized(self, expectation, reasons): message = (colors.LIGHT_MAGENTA + 'Realized ' + colors.RESET + colors.color_repr(expectation)) # For the breakdown, we want to skip any expectations that were exact occurrences, # since there's no point explaining that occurrence was realized by itself. skip = [exp for exp in reasons.keys() if isinstance(exp, Occurrence)] for exp in skip: reasons.pop(exp, None) if reasons: message += colors.LIGHT_MAGENTA + ':' + colors.RESET for exp, reason in reasons.items(): message += ('\n ' + colors.color_repr(exp) + colors.LIGHT_MAGENTA + ' by ' + colors.RESET + colors.color_repr(reason)) print(message)
def code_to_debug(): from dbgimporter import import_and_enable_debugger import_and_enable_debugger() b_test = {"spam": "A", "eggs": "B", "abcd": "C"} # noqa _b_test = 12 # noqa __b_test = 13 # noqa __b_test__ = 14 # noqa a_test = 1 # noqa _a_test = 2 # noqa __a_test = 3 # noqa __a_test__ = 4 # noqa c_test = {1: "one", 2: "two", 10: "ten"} # noqa _c_test = 22 # noqa __c_test = 23 # noqa __c_test__ = 24 # noqa d = 3 # noqa print('done')
def expect_no_unobserved(self): if not self: return # print('Checking for unobserved since %s' % colors.color_repr(self[0])) unobserved = [ occ for occ in self if not occ.observed and all( exp != occ for exp in self.timeline.ignore_unobserved) ] if not unobserved: return print(colors.LIGHT_RED + 'Unobserved occurrences detected:' + colors.RESET) for occ in unobserved: print(' ' + colors.color_repr(occ)) raise Exception('Unobserved occurrences detected')
def code_to_debug(): from dbgimporter import import_and_enable_debugger import_and_enable_debugger() import backchannel import sys json = backchannel.read_json() call_me_back_dir = json['call_me_back_dir'] sys.path.append(call_me_back_dir) import call_me_back def call_func(): raise RuntimeError('unhandled error') # @raise_line call_me_back.call_me_back(call_func) # @call_me_back_line print('done')
def finalize(self): if self.is_final: return print(colors.LIGHT_MAGENTA + 'Finalizing' + colors.RESET) with self.unfrozen(): self.mark('finalized') with self.unfrozen(): self._finalized.set() # Drain the record queue. self._record_queue.join() # Tell the recorder to shut itself down. self._record_queue.put(None) self._recorder_thread.join() assert self._record_queue.empty( ), 'Finalized timeline had pending records'
def _expect_realized(self, expectation, first, explain=True, observe=True): self.expect_frozen() try: reasons = next(expectation.test(first, self.last)) except StopIteration: print(colors.LIGHT_RED + 'No matching ' + colors.RESET + colors.color_repr(expectation)) # The weird always-false assert is to make pytest print occurrences nicely. occurrences = list(first.and_following()) assert occurrences is ('not matching expectation', expectation) occs = tuple(reasons.values()) assert occs if observe: self.observe(*occs) if explain: self._explain_how_realized(expectation, reasons) return occs if len(occs) > 1 else occs[0]
def func(expected_tracing): assert ptvsd.tracing() == expected_tracing, ( "inside func({0!r})".format(expected_tracing) ) print(1) # @inner1 # Test nested change/restore. Going from False to True only works entirely # correctly on Python 3.6+; on earlier versions, if tracing wasn't enabled # when the function is entered, re-enabling it later will not cause the # breakpoints in this function to light up. However, it will allow hitting # breakpoints in functions called from here. def inner2(): print(2) # @inner2 with ptvsd.tracing(not expected_tracing): assert ptvsd.tracing() != expected_tracing, "inside with-statement" inner2() assert ptvsd.tracing() == expected_tracing, "after with-statement" print(3) # @inner3
def code_to_debug(): from dbgimporter import import_and_enable_debugger import_and_enable_debugger() b = True while b: #@bp1-expected pass #@bp1-requested break print() #@bp2-expected [ #@bp2-requested 1, 2, 3, #@bp3-expected ] #@bp3-requested # Python 2.7 only. print() #@bp4-expected print( 1, #@bp4-requested-1 2, 3, #@bp4-requested-2 4, 5, 6)
def __init__(self, *expectations): self.expectations = expectations assert len(expectations) > 0 assert all(isinstance(exp, Expectation) for exp in expectations) timelines = {id(exp.timeline): exp.timeline for exp in expectations} timelines.pop(id(None), None) if len(timelines) > 1: print(colors.RED + 'Cannot mix expectations from multiple timelines:' + colors.RESET) for tl_id, tl in timelines.items(): print('\n %d: %r' % (tl_id, tl)) print() raise ValueError('Cannot mix expectations from multiple timelines') for tl in timelines.values(): self.timeline = tl
def code_to_debug(): from dbgimporter import import_and_enable_debugger import_and_enable_debugger() print('break here') #@bp
def code_to_debug(): from dbgimporter import import_and_enable_debugger import_and_enable_debugger() for i in range(0, 10): print(i)
def deep_stack(level): if level <= 0: print('done') #@bp return level deep_stack(level - 1)
def test_invalid_breakpoints(pyfile, run_as, start_method): @pyfile def code_to_debug(): from dbgimporter import import_and_enable_debugger import_and_enable_debugger() b = True while b: #@bp1-expected pass #@bp1-requested break print() #@bp2-expected [ #@bp2-requested 1, 2, 3, #@bp3-expected ] #@bp3-requested # Python 2.7 only. print() #@bp4-expected print(1, #@bp4-requested-1 2, 3, #@bp4-requested-2 4, 5, 6) line_numbers = get_marked_line_numbers(code_to_debug) from tests.helpers import print print(line_numbers) with DebugSession() as session: session.initialize( target=(run_as, code_to_debug), start_method=start_method, ignore_unobserved=[Event('continued')], ) requested_bps = [ line_numbers['bp1-requested'], line_numbers['bp2-requested'], line_numbers['bp3-requested'], ] if sys.version_info < (3,): requested_bps += [ line_numbers['bp4-requested-1'], line_numbers['bp4-requested-2'], ] actual_bps = session.set_breakpoints(code_to_debug, requested_bps) actual_bps = [bp['line'] for bp in actual_bps] expected_bps = [ line_numbers['bp1-expected'], line_numbers['bp2-expected'], line_numbers['bp3-expected'], ] if sys.version_info < (3,): expected_bps += [ line_numbers['bp4-expected'], line_numbers['bp4-expected'], ] assert expected_bps == actual_bps # Now let's make sure that we hit all of the expected breakpoints, # and stop where we expect them to be. session.start_debugging() # If there's multiple breakpoints on the same line, we only stop once, # so remove duplicates first. expected_bps = sorted(set(expected_bps)) while expected_bps: hit = session.wait_for_thread_stopped() frames = hit.stacktrace.body['stackFrames'] line = frames[0]['line'] assert line == expected_bps[0] del expected_bps[0] session.send_request('continue').wait_for_response() assert not expected_bps session.wait_for_exit()
def script2(): from dbgimporter import import_and_enable_debugger import_and_enable_debugger() import script1 script1.do_something() print('Done')
def do_something(): print('do something')
def code_to_debug(): # import_and_enable_debugger import backchannel backchannel.read_json() print('ok')