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

Support launch_ipdb_on_exception as a function decorator #215

Closed
alanbernstein opened this issue Jan 28, 2021 · 1 comment · Fixed by #226
Closed

Support launch_ipdb_on_exception as a function decorator #215

alanbernstein opened this issue Jan 28, 2021 · 1 comment · Fixed by #226

Comments

@alanbernstein
Copy link
Contributor

For some time I have used this in my personal debug tools:

import ipdb

class Decontext(object):
    """
    Makes a context manager also act as decorator
    """
    def __init__(self, context_manager):
        self._cm = context_manager

    def __enter__(self):
        return self._cm.__enter__()

    def __exit__(self, *args, **kwds):
        return self._cm.__exit__(*args, **kwds)

    def __call__(self, func):
        def wrapper(*args, **kwds):
            with self:
                return func(*args, **kwds)
        return wrapper

pm = Decontext(ipdb.launch_ipdb_on_exception())

It allows me to do this:

from mydebugtools import pm

@pm
def main():
    # whatever

And ipdb is triggered on an exception at any level below that function in the call stack.

This is kind of a silly convenience thing, maybe just personal preference, but I thought others might benefit from seeing it. Would it be reasonable to add this functionality to ipdb? If so, would it be sensible to use this (not named pm), or is there a better way?

@gotcha
Copy link
Owner

gotcha commented Mar 2, 2021

It would be reasonable 😄

I'll let you come up with a good name and a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants