code: def test_event_removable_handle(event1, event2):
      # Removable handle removes event from engine.
      engine = Engine(lambda e, b: None)
      handler = create_autospec(spec=lambda x: None)
      assert not hasattr(handler, "_parent")
  
      removable_handle = engine.add_event_handler(event1, handler)
      assert engine.has_event_handler(handler, event1)
  
      engine.run([1, 2])
      handler.assert_any_call(engine)
      num_calls = handler.call_count
  
      removable_handle.remove()
      assert not engine.has_event_handler(handler, event1)
  
      # Second engine pass does not fire handle again.
      engine.run([1, 2])
      # Assert that handler wasn't call
      assert handler.call_count == num_calls
  
      # Removable handle can be used as a context manager
      handler = create_autospec(spec=lambda x: None)
  
      with engine.add_event_handler(event1, handler):
          assert engine.has_event_handler(handler, event1)
          engine.run([1, 2])
  
      assert not engine.has_event_handler(handler, event1)
      handler.assert_any_call(engine)
      num_calls = handler.call_count
  
      engine.run([1, 2])
      # Assert that handler wasn't call
      assert handler.call_count == num_calls
  
      # Removeable handle only effects a single event registration
      handler = MagicMock(spec_set=True)
  
      with engine.add_event_handler(event1, handler):
          with engine.add_event_handler(event2, handler):
              assert engine.has_event_handler(handler, event1)
              assert engine.has_event_handler(handler, event2)
          assert engine.has_event_handler(handler, event1)
          assert not engine.has_event_handler(handler, event2)
      assert not engine.has_event_handler(handler, event1)
      assert not engine.has_event_handler(handler, event2)
  
      # Removeable handle is re-enter and re-exitable
  
      handler = MagicMock(spec_set=True)
  
      remove = engine.add_event_handler(event1, handler)
  
      with remove:
          with remove:
              assert engine.has_event_handler(handler, event1)
          assert not engine.has_event_handler(handler, event1)
      assert not engine.has_event_handler(handler, event1)
  
      # Removeable handle is a weakref, does not keep engine or event alive
      def _add_in_closure():
          _engine = Engine(lambda e, b: None)
  
          def _handler(_):
              pass
  
          _handle = _engine.add_event_handler(event1, _handler)
          assert _handle.engine() is _engine
  
          if event1.filter is None:
              assert _handle.handler() is _handler
          else:
              assert _handle.handler()._parent() is _handler
  
          return _handle
  
      removable_handle = _add_in_closure()
  
      # gc.collect, resolving reference cycles in engine/state
      # required to ensure object deletion in python2
      gc.collect()
  
      assert removable_handle.engine() is None
      assert removable_handle.handler() is None
  
imports: import functools
  import gc
  from unittest.mock import call, create_autospec, MagicMock
  import pytest
  from pytest import raises
  from ignite.engine import Engine, Events, State
  from ignite.engine.events import EventsList
label_desc: Use image node_id 6 for calling a global function with example usage: test_event_removable_handle(event1, event2) and returns: _handle