code: def stop(self):
          
          if self._state != Bundle.ACTIVE:
              # Invalid state
              return
  
          exception = None
          with self._lock:
              # Store the bundle current state
              previous_state = self._state
  
              # Stopping...
              self._state = Bundle.STOPPING
              self._fire_bundle_event(BundleEvent.STOPPING)
  
              # Call the activator, if any
              stopper = self.__get_activator_method("stop")
              if stopper is not None:
                  try:
                      # Call the start method
                      stopper(self.__context)
                  except (FrameworkException, BundleException) as ex:
                      # Restore previous state
                      self._state = previous_state
  
                      # Re-raise directly Pelix exceptions
                      _logger.exception(
                          "Pelix error raised by %s while stopping", self.__name
                      )
                      exception = ex
                  except Exception as ex:
                      _logger.exception(
                          "Error raised by %s while stopping", self.__name
                      )
                      # Store the exception (raised after service clean up)
                      exception = BundleException(ex)
  
              # Hide remaining services
              self.__framework._hide_bundle_services(self)
  
              # Intermediate bundle event : activator should have cleaned up
              # everything, but some element could stay (iPOPO components, ...)
              self._fire_bundle_event(BundleEvent.STOPPING_PRECLEAN)
  
              # Remove remaining services (the hard way)
              self.__unregister_services()
  
              # Cleanup service usages
              self.__framework._unget_used_services(self)
  
              # Bundle is now stopped and all its services have been unregistered
              self._state = Bundle.RESOLVED
              self._fire_bundle_event(BundleEvent.STOPPED)
  
          # Raise the exception, if any
          # pylint: disable=E0702
          # Pylint seems to miss the "is not None" check below
          if exception is not None:
              raise exception
docs: Stops the bundle. Does nothing if the bundle is already stopped.
  
          :raise BundleException: The bundle activator failed.