Signal handlers can't be standard class methods because the signal
    callback does not include the object reference (self).  To work
    around this we use a closure the carry the object reference.
5882
def legion_create_handler(obj):
    
    def _handler(sig, frame):
        obj._sig_handler(sig, frame)

    if '_sig_handler' not in dir(obj):                                                          # pragma: no cover
        raise Excecption("Object instance has no _sig_handler method")
    return _handler
446117
def signal_handler_as(sig, handler):
  
  old_handler = signal.signal(sig, handler)
  try:
    yield
  finally:
    signal.signal(sig, old_handler)
685171
@Override
    public Object handle(Message msg) throws Exception {
        if(req_handler != null)
            return req_handler.handle(msg);
        return null;
    }
1154311
def on(self, handler):
        """"""
        if not hasattr(handler, '__call__'):
            raise TypeError('handler is not a callable object.')
        self.handlers.add(handler)
377169
def connect(self, interface, event, object_path, handler):
        
        if object_path:
            def callback(connection, sender_name, object_path,
                         interface_name, signal_name, parameters):
                return handler(*unpack_variant(parameters))
        else:
            def callback(connection, sender_name, object_path,
                         interface_name, signal_name, parameters):
                return handler(object_path, *unpack_variant(parameters))
        return self.connection.signal_subscribe(
            self.bus_name,
            interface,
            event,
            object_path,
            None,
            Gio.DBusSignalFlags.NONE,
            callback,
        )
935455
function Dispatcher(req, res, next) {
            _classCallCheck(this, Dispatcher);

            this.req = req;
            this.res = res;
            this.next = next;
         }
601796
def handler(self, handler_class):
        """"""
        self.handler_class = handler_class
        # we take the docstring from the handler class, not the methods
        if self.docs is None and handler_class.__doc__:
            self.docs = clean_docstring(handler_class.__doc__)
        return handler_class
205185
function wrapHandler(handler) {
  return function (state, ...args) {
    return _.assign({}, state, handler(state, ...args));
  };
}
711539
def _SigSegvHandler(self, signal_number, stack_frame):
    
    self._OnCriticalError()

    # Note that the original SIGSEGV handler can be 0.
    if self._original_sigsegv_handler is not None:
      # Let the original SIGSEGV handler take over.
      signal.signal(signal.SIGSEGV, self._original_sigsegv_handler)
      os.kill(self._pid, signal.SIGSEGV)
762700
function callbackHandler(callback) {
        return function(error, data) {
            if (data === 'OK') {
                callback();
                return;
            }

            callback(error, deserializeResponse(data));
        };
    }
1101156
def method_missing(method_name, *args, &block)
      _, event_name, callback_name = *method_name.to_s.match(/^(\w*?on_\w+?)_(\w+)$/)
      if callback_name && callback_names.include?(callback_name.to_sym)
        public_send(event_name, :"#{callback_name}", *args, &block)
      else
        super
      end
    end
1061044
def CallApiHandler(handler, args, token=None):
    """"""

    result = handler.Handle(args, token=token)

    expected_type = handler.result_type
    if expected_type is None:
      expected_type = None.__class__

    if result.__class__ != expected_type:
      raise UnexpectedResultTypeError(
          "Expected %s, but got %s." %
          (expected_type.__name__, result.__class__.__name__))

    return result
243432
def __handler(self, connection, event):
        
        try:
            # Find local handler
            method = getattr(self, "on_{0}".format(event.type))
        except AttributeError:
            pass
        else:
            try:
                # Call it
                return method(connection, event)
            except Exception as ex:
                logging.exception("Error calling handler: %s", ex)
420637
function () {
  var handlers = {};

  function raise (ev) {
    if (handlers[ ev ]) {
      handlers[ ev ].apply(undefined, Array.prototype.slice.call(arguments, 1));
    }
  }

  function on (ev, handle) {
    handlers[ ev ] = handle;
  }

  function reset () {
    handlers = {};
    this.close = noOp;
    this.createChannel = noOp;
    this.createConfirmChannel = noOp;
    this.release = noOp;
  }

  var instance = {
    acquire: function () {
      this.raise('acquiring');
      return Promise.resolve();
    },
    item: { uri: '' },
    close: noOp,
    createChannel: noOp,
    createConfirmChannel: noOp,
    on: on,
    raise: raise,
    release: noOp,
    reset: reset
  };
  setTimeout(instance.acquire.bind(instance), 0);
  return instance;
}
58715
def emit(self, signal, *args):
        
        callbacks = self._callbacks.get(signal, [])
        new_callbacks = []
        for callback in callbacks:
            # clear out deleted pointers
            if not callback.isValid():
                continue

            new_callbacks.append(callback)

            try:
                callback(*args)
            except StandardError:
                logger.exception('Error occurred during callback.')

        self._callbacks[signal] = new_callbacks
1273601
def _get_handler(self, handler_class):
        """"""
        element = None
        for handler in self.handlers:
            if isinstance(handler, handler_class):
                element = handler
                break
        return element
901823
def unhook(object, type=nil, method=nil)
      raise ArgumentError.new('Object method provided for a Proc') if object.is_a?(Proc) && method
      @lock.synchronize do
        case object
        when Proc
          if(type)
            @hooks[type][:procs].delete(object)
            @hooks[type].delete(:procs) if @hooks[type][:procs].empty?
          else
            @hooks.each_value do |h|
              h[:procs].delete(object)
              h.delete(:procs) if h[:procs].empty?
            end
          end
        else
          if(method.nil? && type.nil?)
            @hooks.each_value{|v|v.delete_if{|k,z| k == object.class}}
          else
            raise ArgumentError.new('Type must be provided') if type.nil?
            const = Splib.find_const(type)
            type = const unless const.nil?
            method = method.to_sym if method
            name = object.class
            raise NameError.new('Uninitialized hook type given') unless @hooks[type]
            raise StandardError.new('No hooks found for given object') unless @hooks[type][name]
            if(method)
              @hooks[type][name].delete_if{|x|x[:method] == method}
              @hooks[type].delete(name) if @hooks[type][name].empty?
            else
              @hooks[type].delete(name)
            end
          end          
        end
        @hooks.delete_if{|k,v|v.empty?}
      end
    end