Created
February 26, 2020 01:23
-
-
Save joonhwan/7127201ab5ef3beb283d1787de5efe64 to your computer and use it in GitHub Desktop.
Simplest python Bdb extension(just for study)
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
import pdb | |
import wdb | |
from mibdb import MiBdb | |
d = MiBdb() | |
d.set_trace() | |
d.set_breakpoint("simplecall.py", 12) | |
d.set_breakpoint("simplecall.py", 16) | |
def a(): | |
double_value = 2 | |
return b() * double_value | |
def b(): | |
return 'response from b' | |
print('hello') | |
a() |
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
from bdb import Bdb | |
import inspect | |
import sys | |
def default_trace(frame): | |
print("breakpoint log : ", frame.f_locals) | |
local_vars: dict = frame.f_locals | |
ui = input("MiBDB> ") | |
class MiBdb(Bdb): | |
def do_clear(self, arg): | |
pass | |
def __init__(self): | |
Bdb.__init__(self) | |
self.breakpoints = dict() | |
self.set_trace() | |
def end(self): | |
self.reset() | |
# bdb does not seem to actually support clearing, so we do it manually | |
sys.settrace(None) | |
def set_breakpoint(self, filename=None, lineno=None, method=None): | |
if filename is None and lineno is None and method is None: | |
return | |
self.set_break(filename, lineno) | |
self.breakpoints.get((filename, lineno), {}) | |
if method is None: | |
method = default_trace | |
try : | |
self.breakpoints[(filename, lineno)].add(method) | |
except KeyError: | |
self.breakpoints[(filename, lineno)] = [method] | |
def unset_breakpoint(self, filename=None, lineno=None): | |
if filename is None and lineno is None: | |
return | |
del self.breakpoints[(filename, lineno)] | |
def user_line(self, frame): | |
if not self.break_here(frame): | |
return | |
(filename, lineno, function, code_context, index) = inspect.getframeinfo(frame) | |
filename = filename.split("/")[-1] | |
callbacks = self.breakpoints[((filename, lineno))] | |
for callback in callbacks: | |
callback(frame) | |
# | |
# class MiDbg(Bdb): | |
# def user_call(self, frame, argument_list): | |
# print('user call') | |
# def user_exception(self, frame, exc_info): | |
# print('user exception') | |
# def user_line(self, frame): | |
# print('user line') | |
# def user_return(self, frame, return_value): | |
# print('user return') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment