You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
2.0 KiB

4 years ago
  1. """Module implementing error-catching version of send (sendRobust)"""
  2. from pydispatch.dispatcher import Any, Anonymous, liveReceivers, getAllReceivers
  3. from pydispatch.robustapply import robustApply
  4. def sendRobust(
  5. signal=Any,
  6. sender=Anonymous,
  7. *arguments, **named
  8. ):
  9. """Send signal from sender to all connected receivers catching errors
  10. signal -- (hashable) signal value, see connect for details
  11. sender -- the sender of the signal
  12. if Any, only receivers registered for Any will receive
  13. the message.
  14. if Anonymous, only receivers registered to receive
  15. messages from Anonymous or Any will receive the message
  16. Otherwise can be any python object (normally one
  17. registered with a connect if you actually want
  18. something to occur).
  19. arguments -- positional arguments which will be passed to
  20. *all* receivers. Note that this may raise TypeErrors
  21. if the receivers do not allow the particular arguments.
  22. Note also that arguments are applied before named
  23. arguments, so they should be used with care.
  24. named -- named arguments which will be filtered according
  25. to the parameters of the receivers to only provide those
  26. acceptable to the receiver.
  27. Return a list of tuple pairs [(receiver, response), ... ]
  28. if any receiver raises an error (specifically any subclass of Exception),
  29. the error instance is returned as the result for that receiver.
  30. """
  31. # Call each receiver with whatever arguments it can accept.
  32. # Return a list of tuple pairs [(receiver, response), ... ].
  33. responses = []
  34. for receiver in liveReceivers(getAllReceivers(sender, signal)):
  35. try:
  36. response = robustApply(
  37. receiver,
  38. signal=signal,
  39. sender=sender,
  40. *arguments,
  41. **named
  42. )
  43. except Exception as err:
  44. responses.append((receiver, err))
  45. else:
  46. responses.append((receiver, response))
  47. return responses