python-makefun icon indicating copy to clipboard operation
python-makefun copied to clipboard

functions appear in stack trace with their original name

Open uriva opened this issue 4 years ago • 6 comments

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?

uriva avatar May 09 '20 05:05 uriva

If you care about this, I found a way to do this using eval.

uriva avatar May 10 '20 06:05 uriva

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 ?

smarie avatar May 27 '20 06:05 smarie

Hi @uriva , did the above answer work for you ?

smarie avatar Aug 19 '20 05:08 smarie

Still on my backlog, but I think I will get to it at some point.

uriva avatar Aug 25 '20 17:08 uriva

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).

uriva avatar Oct 27 '20 13:10 uriva

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:

image

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 !

smarie avatar Oct 30 '20 20:10 smarie