Author louielu
Recipients louielu, ncoghlan
Date 2017-04-20.09:36:32
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
This is a sub-problem of #9285, in #9285, we aim to provide cProfile and profile a context manager, this will need to add code like this:

    def __enter__(self):
        return self

Unfortunately, when setting up profiler via `sys.setprofile`, it will immediately work on next line `return self`, which cause the assertion inside `trace_dispatch_return` claim this is a "Bad return".

Technically, `profile.Profile` can not go return upper than it's frame inside the frame stack. This behavior can be observed by this code:

    def fib(n):
        if n > 2:
            return fib(n - 1) + fib(n - 2)
        return n

    def foo():
        pr = profile.Profile()
        # Profile was set in the `foo` frame, it can't get more upper than this
        # that means, we can't return to global frame when this profile is set
        # We didn't stop the profile here via sys.setprofile(None)
        # So it will return 0xDEADBEAF to global frame and cause a bad return
        return 0xDEADBEAF


Here this issue will provide the test of this behavior, then will make some modify in #9285 to prevent this situation when using profile as a context manager.
Date User Action Args
2017-04-20 09:36:32louielusetrecipients: + louielu, ncoghlan
2017-04-20 09:36:32louielusetmessageid: <>
2017-04-20 09:36:32louielulinkissue30113 messages
2017-04-20 09:36:32louielucreate