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

A signal handler cannot be a method on an object #2581

Closed
todddialpad opened this issue Oct 21, 2022 · 4 comments · Fixed by #2608
Closed

A signal handler cannot be a method on an object #2581

todddialpad opened this issue Oct 21, 2022 · 4 comments · Fixed by #2608

Comments

@todddialpad
Copy link

todddialpad commented Oct 21, 2022

Describe the bug

If I try and register a method on an object as a signal handler, an exception is thrown because it is not possible to set an attribute on a bound method

    self.add_signal(self.after_routing, 'http.routing.after')
  File "sanic/lib/python3.7/site-packages/sanic/mixins/signals.py", line 77, in add_signal
    handler
  File "sanic/lib/python3.7/site-packages/sanic/mixins/signals.py", line 57, in decorator
    self._apply_signal(future_signal)
  File "sanic/lib/python3.7/site-packages/sanic/app.py", line 420, in _apply_signal
    return self.signal_router.add(*signal)
  File "sanic/lib/python3.7/site-packages/sanic/signals.py", line 222, in add
    handler.__requirements__ = condition  # type: ignore
AttributeError: 'method' object has no attribute '__requirements__'

Code snippet

import sanic

class MyApp(sanic.Sanic):
  def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.add_signal(self.after_routing, 'http.routing.after')

  def after_routing(self, request, route, kwargs, handler):
    pass

app = MyApp(name='method_signal_handler')

Expected behavior

I would expect the bound method to be registered, and then called when the signal is dispatched.

Environment (please complete the following information):

  • OS: MacOS
  • Sanic Version: 22.9

Additional context

@Tronic
Copy link
Member

Tronic commented Oct 21, 2022

Added code tags for readability.

@ChihweiLHBird
Copy link
Member

ChihweiLHBird commented Nov 27, 2022

image

I looked into it and found this was because method object doesn't support being injected arbitrary variables. I would like to purpose saving the conditions and triggers in dict instead of the handler (function) object. One part that confused me is that __trigger__ saved in the function object seems never used. What do you guys think about this solution? @ahopkins @Tronic @sanic-org/framework

@ahopkins
Copy link
Member

Yes, that is correct. IIRC, there is not a great way to pass this info. I'd have to look. Happy to hear a proposal.

@ChihweiLHBird
Copy link
Member

Yes, that is correct. IIRC, there is not a great way to pass this info. I'd have to look. Happy to hear a proposal.

How do you think about the draft? #2608

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