dgrunwald / rust-cpython

Rust <-> Python bindings
MIT License
1.82k stars 136 forks source link

Using decorators or getting a function's decorator? #83

Open ghost opened 7 years ago

ghost commented 7 years ago

Is it currently possible to retrieve the decorator/decorator's data of a Python 3 function when interfacing Python code in Rust?

ssokolow commented 7 years ago

Wouldn't the decorator be what you receive a reference to?

Unless I'm fundamentally misunderstanding something about what you're asking or how decorators work in Python, decorating a function is storing a reference to it in a closure's scope and then reassigning the variable binding from the original function to the decorator.

The harder question would be getting a reference to the function the decorator wraps, since you'd need to poke at the scope captured within the closure.

ghost commented 7 years ago

Sorry, I'll be more clear. I'll use an event handler example. Say I have a decorator which specifies a key and a value specific to this function:

@event("example_event")
def event_handler(event):
  event.do_something()

This says, hey, this function handles example_event (implemented by the rust code) this function then is called every time example_event fires and the python code does stuff with the event

I'm curious as to how I can get the context of every function with that specific @event("example_event") decorator.

Thanks!

ghost commented 7 years ago

I'm thinking I'll just do this on the python side for now