Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added trace_module to debugutils #78

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion boltons/debugutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
except ImportError:
_UNSET = object()

__all__ = ['pdb_on_signal', 'pdb_on_exception']
__all__ = ['pdb_on_signal', 'pdb_on_exception', 'trace_module']


def pdb_on_signal(signalnum=None):
Expand Down Expand Up @@ -82,6 +82,37 @@ def pdb_excepthook(exc_type, exc_val, exc_tb):
sys.excepthook = pdb_excepthook
return


def trace_module(modules):
'''Prints lines of code as they are executed only within the
given modules, in the current thread.

Compare to '-t' option of trace from the standard library, made usable
by having the condition inverted. Only specifc modules are invluded
in the output, instead of having to itemize modules to exclude.

(Uses sys.settrace() so will interfere with other modules that want to own
the trace function such as coverage, profile, and trace.)
'''
import sys

if type(modules) is not list:
modules = [modules]

globalses = set()
for module in modules:
globalses.add(id(module.__dict__))

def trace(frame, event, arg):
if event == 'line':
print(frame.f_code.co_filename, frame.f_code.co_name, frame.f_lineno)
if event == 'call':
if id(frame.f_globals) in globalses:
return trace

sys.settrace(trace)


_repr_obj = Repr()
_repr_obj.maxstring = 50
_repr_obj.maxother = 50
Expand Down