code: def capture_events(receiver, dest):
      
      # the following code is a copy of the formal definition of `yield from`
      # in PEP 380, with modifications to capture the value sent during yield
      _i = iter(receiver)
      try:
          _y = next(_i)
      except StopIteration as _e:
          return _e.value
  
      try:
          while True:
              try:
                  _s = yield _y
              except GeneratorExit as _e:
                  try:
                      _m = _i.close
                  except AttributeError:
                      pass
                  else:
                      _m()
                      raise _e
              except BaseException as _e:
                  _x = sys.exc_info()
                  try:
                      _m = _i.throw
                  except AttributeError:
                      raise _e
                  else:
                      try:
                          _y = _m(*_x)
                      except StopIteration as _e:
                          _r = _e.value
                          break
              else:
                  dest.append(_s)
                  try:
                      if _s is None:
                          _y = next(_i)
                      else:
                          _y = _i.send(_s)
                  except StopIteration as _e:
                      _r = _e.value
                      break
      except:  # NOQA
          dest.clear()
          raise
      return _r
docs: Capture all events sent to `receiver` in the sequence `dest`. This is a
      generator, and it is best used with ``yield from``. The observable effect
      of using this generator with ``yield from`` is identical to the effect of
      using `receiver` with ``yield from`` directly (including the return value),
      but in addition, the values which are *sent* to the receiver are captured
      in `dest`.
  
      If `receiver` raises an exception or the generator is closed prematurely
      using its :meth:`close`, `dest` is cleared.
  
      This is used to implement :class:`CapturingXSO`. See the documentation
      there for use cases.
  
      .. versionadded:: 0.5