code: def _tmp_session(self, session, close=True):
          
          if session:
              # Don't call end_session.
              yield session
              return
  
          s = self._ensure_session(session)
          if s and close:
              with s:
                  # Call end_session when we exit this scope.
                  yield s
          elif s:
              try:
                  # Only call end_session on error.
                  yield s
              except Exception:
                  s.end_session()
                  raise
          else:
              yield None
docs: If provided session is None, lend a temporary session.