Home / Function/ session_transaction() — flask Function Reference

session_transaction() — flask Function Reference

Architecture documentation for the session_transaction() function in testing.py from the flask codebase.

Entity Profile

Dependency Diagram

graph TD
  eb7c3517_582e_6dd2_c80b_a8be784317c7["session_transaction()"]
  bd3a5eb6_81cb_6392_c9b8_19dcbec7785f["FlaskClient"]
  eb7c3517_582e_6dd2_c80b_a8be784317c7 -->|defined in| bd3a5eb6_81cb_6392_c9b8_19dcbec7785f
  c6ee720e_217f_24d8_42d3_da38bfeebb45["test_request_context()"]
  eb7c3517_582e_6dd2_c80b_a8be784317c7 -->|calls| c6ee720e_217f_24d8_42d3_da38bfeebb45
  1b3ce05d_9f5d_ca0f_d90c_87f0a25a18ba["is_null_session()"]
  eb7c3517_582e_6dd2_c80b_a8be784317c7 -->|calls| 1b3ce05d_9f5d_ca0f_d90c_87f0a25a18ba
  f07e3f84_ffc5_9004_1d81_703a5234af84["open_session()"]
  eb7c3517_582e_6dd2_c80b_a8be784317c7 -->|calls| f07e3f84_ffc5_9004_1d81_703a5234af84
  22e64eec_34fe_f9c3_c75a_bbd842b167e0["save_session()"]
  eb7c3517_582e_6dd2_c80b_a8be784317c7 -->|calls| 22e64eec_34fe_f9c3_c75a_bbd842b167e0
  style eb7c3517_582e_6dd2_c80b_a8be784317c7 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/flask/testing.py lines 136–183

    def session_transaction(
        self, *args: t.Any, **kwargs: t.Any
    ) -> t.Iterator[SessionMixin]:
        """When used in combination with a ``with`` statement this opens a
        session transaction.  This can be used to modify the session that
        the test client uses.  Once the ``with`` block is left the session is
        stored back.

        ::

            with client.session_transaction() as session:
                session['value'] = 42

        Internally this is implemented by going through a temporary test
        request context and since session handling could depend on
        request variables this function accepts the same arguments as
        :meth:`~flask.Flask.test_request_context` which are directly
        passed through.
        """
        if self._cookies is None:
            raise TypeError(
                "Cookies are disabled. Create a client with 'use_cookies=True'."
            )

        app = self.application
        ctx = app.test_request_context(*args, **kwargs)
        self._add_cookies_to_wsgi(ctx.request.environ)

        with ctx:
            sess = app.session_interface.open_session(app, ctx.request)

        if sess is None:
            raise RuntimeError("Session backend did not open a session.")

        yield sess
        resp = app.response_class()

        if app.session_interface.is_null_session(sess):
            return

        with ctx:
            app.session_interface.save_session(app, sess, resp)

        self._update_cookies_from_response(
            ctx.request.host.partition(":")[0],
            ctx.request.path,
            resp.headers.getlist("Set-Cookie"),
        )

Subdomains

Frequently Asked Questions

What does session_transaction() do?
session_transaction() is a function in the flask codebase, defined in src/flask/testing.py.
Where is session_transaction() defined?
session_transaction() is defined in src/flask/testing.py at line 136.
What does session_transaction() call?
session_transaction() calls 4 function(s): is_null_session, open_session, save_session, test_request_context.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free