Last active
August 29, 2015 14:15
-
-
Save carlos-jenkins/35630a786136e6dab43b to your computer and use it in GitHub Desktop.
Python debug breakpoint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
# | |
# When executed, this file shows to following behavior: | |
# | |
# $ python debug.py | |
# * Break: debug.py ::: Line 72 | |
# * Continue with Ctrl+D... | |
# >>> a | |
# 10 | |
# >>> | |
# * Break: debug.py ::: Line 80 | |
# * Continue with Ctrl+D... | |
# >>> b | |
# 'Hello' | |
# >>> mylist | |
# [20, 'Hello', 20] | |
# >>> mylist.append(a) | |
# >>> | |
# * Break: debug.py ::: Line 86 | |
# * Continue with Ctrl+D... | |
# >>> a | |
# '1_one' | |
# >>> mylist | |
# [20, 'Hello', 20, 20] | |
# >>> | |
# Traceback (most recent call last): | |
# File "debug.py", line 92, in <module> | |
# tuple()[0] | |
# IndexError: tuple index out of range | |
# * Break: debug.py ::: Line 94 | |
# * Continue with Ctrl+D... | |
# >>> | |
def debug_breakpoint(): | |
""" | |
Python debug breakpoint. | |
""" | |
from sys import exc_info | |
from code import InteractiveConsole | |
from inspect import currentframe | |
from traceback import print_exception | |
try: | |
import readline # noqa | |
except ImportError: | |
pass | |
caller = currentframe().f_back | |
env = {} | |
env.update(caller.f_globals) | |
env.update(caller.f_locals) | |
exc = exc_info() | |
if all(exc): | |
print_exception(*exc) | |
del exc | |
shell = InteractiveConsole(env) | |
shell.interact( | |
'* Break: {} ::: Line {}\n' | |
'* Continue with Ctrl+D...'.format( | |
caller.f_code.co_filename, caller.f_lineno | |
) | |
) | |
a = 10 | |
b = 20 | |
c = 'Hello' | |
debug_breakpoint() | |
a = 20 | |
b = c | |
c = a | |
mylist = [a, b, c] | |
debug_breakpoint() | |
def bar(): | |
a = '1_one' | |
b = '2+2' | |
debug_breakpoint() | |
bar() | |
try: | |
tuple()[0] | |
except: | |
debug_breakpoint() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment