code: def cleanup(logger, *args):
      
      for obj in args:
          if obj is not None and hasattr(obj, 'cleanup'):
              try:
                  obj.cleanup()
              except NotImplementedError:
                  pass
              except Exception:
                  logger.exception("Unable to cleanup %s object", obj)
docs: Environment's cleanup routine.