python-makefun
python-makefun copied to clipboard
functions appear in stack trace with their original name
When running this:
def original_foo(): raise Exception()
makefun.create_function("foo()", original_foo)()
I want foo
to be the name of the function in the stack trace. Any way I can achieve that using this lib?
If you care about this, I found a way to do this using eval.
Hello @uriva I am deeply sorry to only see your message just now. With github switching to the "new notifications experience" it seems that a few notifications got lost in the way, or at least I missed some of them.
So the answer is yes, this is why makefun was created.
from makefun import with_signature
@with_signature('foo()')
def original_foo():
raise Exception()
print(original_foo)
yields
<function foo at 0x0000018DAB2E8268>
You can also apply the decorator programmatically of course:
original_foo = with_signature('foo()')(original_foo)
Note that using with_signature(None, func_name='foo')
instead will only change the metadata, so only original_foo.__name__
will be changed
Does that answer your question ?
Hi @uriva , did the above answer work for you ?
Still on my backlog, but I think I will get to it at some point.
Hey, looking into this today. I have a working solution (which is slow): https://gist.github.com/uriva/04333004f2379e959197993ed416b1e1
Can I do this using this lib? i.e. given a function, just rename it, but don't change its signature?
When I same "rename" I mean rename so stack trace shows its new name, I don't care about other stuff (so just changing __name__
will not work).
Ok I understand your problem : you would like the frame in the stack trace to be available with the appropriate name. So with the code below and putting a breakpoint inside the original function :
from makefun import with_signature
@with_signature('foo()')
def original_foo():
raise Exception()
print(original_foo)
original_foo()
you do not like this:
Apparently the absence of frame for the created wrapper is due to the fact that I create it with compile
and exec
, like what is done in decorator
or attrs
libs.
If you find a way to add something to this function so that it does what you want do not hesitate to propose a PR !