New submission from Louie Lu:

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):
        self.set_cmd('')
        sys.setprofile(self.dispatcher)
        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
        sys.setprofile(pr.dispatcher)
        fib(5)
        # 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

    foo()

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.

----------
components: Library (Lib)
messages: 291957
nosy: louielu, ncoghlan
priority: normal
severity: normal
status: open
title: Add profile test case for trace_dispatch_return assertion
type: enhancement
versions: Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue30113>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to