docs: def _remove_session_save_objects(self):
          """Used during exception handling in case we need to remove() session:
  
          keep instances and merge them in the new session.
          """
          if self.testing:
              return
          # Before destroying the session, get all instances to be attached to the
          # new session. Without this, we get DetachedInstance errors, like when
          # tryin to get user's attribute in the error page...
          old_session = db.session()
          g_objs = []
          for key in iter(g):
              obj = getattr(g, key)
              if isinstance(obj, db.Model) and sa.orm.object_session(obj) in (
                  None,
                  old_session,
              ):
                  g_objs.append((key, obj, obj in old_session.dirty))
  
          db.session.remove()
          session = db.session()
  
          for key, obj, load in g_objs:
              # replace obj instance in bad session by new instance in fresh
              # session
              setattr(g, key, session.merge(obj, load=load))
  
          # refresh `current_user`
          user = getattr(_request_ctx_stack.top, "user", None)
          if user is not None and isinstance(user, db.Model):
              _request_ctx_stack.top.user = session.merge(user, load=load)