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

functions appear in stack trace with their original name #55

Open
uriva opened this issue May 9, 2020 · 6 comments
Open

functions appear in stack trace with their original name #55

uriva opened this issue May 9, 2020 · 6 comments

Comments

@uriva
Copy link

uriva commented May 9, 2020

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
Copy link
Author

uriva commented May 10, 2020

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

@smarie
Copy link
Owner

smarie commented May 27, 2020

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
Copy link
Owner

smarie commented Aug 19, 2020

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

smarie pushed a commit that referenced this issue Aug 19, 2020
@uriva
Copy link
Author

uriva commented Aug 25, 2020

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

@uriva
Copy link
Author

uriva commented Oct 27, 2020

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

@smarie
Copy link
Owner

smarie commented Oct 30, 2020

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 !

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

No branches or pull requests

2 participants