[
  {
    "path": ".devcontainer/devcontainer.json",
    "content": "{\n  \"name\": \"pallets/jinja\",\n  \"image\": \"mcr.microsoft.com/devcontainers/python:3\",\n  \"customizations\": {\n    \"vscode\": {\n      \"settings\": {\n        \"python.defaultInterpreterPath\": \"${workspaceFolder}/.venv\",\n        \"python.terminal.activateEnvInCurrentTerminal\": true,\n        \"python.terminal.launchArgs\": [\n          \"-X\",\n          \"dev\"\n        ]\n      }\n    }\n  },\n  \"onCreateCommand\": \".devcontainer/on-create-command.sh\"\n}\n"
  },
  {
    "path": ".devcontainer/on-create-command.sh",
    "content": "#!/bin/bash\nset -e\n\n# Install uv if not already installed\nif ! command -v uv &> /dev/null; then\n    echo \"Installing uv...\"\n    curl -LsSf https://astral.sh/uv/install.sh | sh\n    export PATH=\"$HOME/.cargo/bin:$PATH\"\nfi\n\n# Create venv using uv and install dependencies\necho \"Creating virtual environment and installing dependencies...\"\nuv sync\n\n# Install pre-commit hooks\necho \"Installing pre-commit hooks...\"\npre-commit install --install-hooks\n"
  },
  {
    "path": ".editorconfig",
    "content": "root = true\n\n[*]\nindent_style = space\nindent_size = 4\ninsert_final_newline = true\ntrim_trailing_whitespace = true\nend_of_line = lf\ncharset = utf-8\nmax_line_length = 88\n\n[*.{css,html,js,json,jsx,scss,ts,tsx,yaml,yml}]\nindent_size = 2\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/bug-report.md",
    "content": "---\nname: Bug report\nabout: Report a bug in Jinja (not other projects which depend on Jinja)\n---\n\n<!--\nThis issue tracker is a tool to address bugs in Jinja itself. Please use\nGitHub Discussions or the Pallets Discord for questions about your own code.\n\nReplace this comment with a clear outline of what the bug is.\n-->\n\n<!--\nDescribe how to replicate the bug.\n\nInclude a minimal reproducible example that demonstrates the bug.\nInclude the full traceback if there was an exception.\n-->\n\n<!--\nDescribe the expected behavior that should have happened but didn't.\n-->\n\nEnvironment:\n\n- Python version:\n- Jinja version:\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/config.yml",
    "content": "blank_issues_enabled: false\ncontact_links:\n  - name: Questions on Discussions\n    url: https://github.com/pallets/jinja/discussions/\n    about: Ask questions about your own code on the Discussions tab.\n  - name: Questions on Chat\n    url: https://discord.gg/pallets\n    about: Ask questions about your own code on our Discord chat.\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/feature-request.md",
    "content": "---\nname: Feature request\nabout: Suggest a new feature for Jinja\n---\n\n<!--\nReplace this comment with a description of what the feature should do.\nInclude details such as links to relevant specs or previous discussions.\n-->\n\n<!--\nReplace this comment with an example of the problem which this feature\nwould resolve. Is this problem solvable without changes to Jinja, such\nas by subclassing or using an extension?\n-->\n"
  },
  {
    "path": ".github/pull_request_template.md",
    "content": "<!--\nBefore opening a PR, open a ticket describing the issue or feature the\nPR will address. An issue is not required for fixing typos in\ndocumentation, or other simple non-code changes.\n\nReplace this comment with a description of the change. Describe how it\naddresses the linked ticket.\n-->\n\n<!--\nLink to relevant issues or previous PRs, one per line. Use \"fixes\" to\nautomatically close an issue.\n\nfixes #<issue number>\n-->\n\n<!--\nEnsure each step in CONTRIBUTING.rst is complete, especially the following:\n\n- Add tests that demonstrate the correct behavior of the change. Tests\n  should fail without the change.\n- Add or update relevant docs, in the docs folder and in code.\n- Add an entry in CHANGES.rst summarizing the change and linking to the issue.\n- Add `.. versionchanged::` entries in any relevant code docs.\n-->\n"
  },
  {
    "path": ".github/workflows/lock.yaml",
    "content": "name: Lock inactive closed issues\n# Lock closed issues that have not received any further activity for two weeks.\n# This does not close open issues, only humans may do that. It is easier to\n# respond to new issues with fresh examples rather than continuing discussions\n# on old issues.\n\non:\n  schedule:\n    - cron: '0 0 * * *'\npermissions:\n  issues: write\n  pull-requests: write\n  discussions: write\nconcurrency:\n  group: lock\njobs:\n  lock:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: dessant/lock-threads@1bf7ec25051fe7c00bdd17e6a7cf3d7bfb7dc771 # v5.0.1\n        with:\n          issue-inactive-days: 14\n          pr-inactive-days: 14\n          discussion-inactive-days: 14\n"
  },
  {
    "path": ".github/workflows/pre-commit.yaml",
    "content": "name: pre-commit\non:\n  pull_request:\n  push:\n    branches: [main, stable]\njobs:\n  main:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2\n      - uses: astral-sh/setup-uv@f0ec1fc3b38f5e7cd731bb6ce540c5af426746bb # v6.1.0\n        with:\n          enable-cache: true\n          prune-cache: false\n      - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0\n        id: setup-python\n        with:\n          python-version-file: pyproject.toml\n      - uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3\n        with:\n          path: ~/.cache/pre-commit\n          key: pre-commit|${{ hashFiles('pyproject.toml', '.pre-commit-config.yaml') }}\n      - run: uv run --locked --group pre-commit pre-commit run --show-diff-on-failure --color=always --all-files\n      - uses: pre-commit-ci/lite-action@5d6cc0eb514c891a40562a58a8e71576c5c7fb43 # v1.1.0\n        if: ${{ !cancelled() }}\n"
  },
  {
    "path": ".github/workflows/publish.yaml",
    "content": "name: Publish\non:\n  push:\n    tags: ['*']\njobs:\n  build:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2\n      - uses: astral-sh/setup-uv@f0ec1fc3b38f5e7cd731bb6ce540c5af426746bb # v6.1.0\n        with:\n          enable-cache: true\n          prune-cache: false\n      - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0\n        with:\n          python-version-file: pyproject.toml\n      - run: echo \"SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct)\" >> $GITHUB_ENV\n      - run: uv build\n      - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2\n        with:\n          path: ./dist\n  create-release:\n    needs: [build]\n    runs-on: ubuntu-latest\n    permissions:\n      contents: write\n    steps:\n      - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0\n      - name: create release\n        run: >\n          gh release create --draft --repo ${{ github.repository }}\n          ${{ github.ref_name }} artifact/*\n        env:\n          GH_TOKEN: ${{ github.token }}\n  publish-pypi:\n    needs: [build]\n    environment:\n      name: publish\n      url: https://pypi.org/project/Jinja2/${{ github.ref_name }}\n    runs-on: ubuntu-latest\n    permissions:\n      id-token: write\n    steps:\n      - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0\n      - uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc # v1.12.4\n        with:\n          packages-dir: artifact/\n"
  },
  {
    "path": ".github/workflows/tests.yaml",
    "content": "name: Tests\non:\n  pull_request:\n    paths-ignore: ['docs/**', 'README.md']\n  push:\n    branches: [main, stable]\n    paths-ignore: ['docs/**', 'README.md']\njobs:\n  tests:\n    name: ${{ matrix.name || matrix.python }}\n    runs-on: ${{ matrix.os || 'ubuntu-latest' }}\n    strategy:\n      fail-fast: false\n      matrix:\n        include:\n          - {python: '3.13'}\n          - {name: Windows, python: '3.13', os: windows-latest}\n          - {name: Mac, python: '3.13', os: macos-latest}\n          - {python: '3.12'}\n          - {python: '3.11'}\n          - {python: '3.10'}\n          - {name: PyPy, python: 'pypy-3.11', tox: pypy3.11}\n    steps:\n      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2\n      - uses: astral-sh/setup-uv@f0ec1fc3b38f5e7cd731bb6ce540c5af426746bb # v6.1.0\n        with:\n          enable-cache: true\n          prune-cache: false\n      - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0\n        with:\n          python-version: ${{ matrix.python }}\n      - run: uv run --locked tox run -e ${{ matrix.tox || format('py{0}', matrix.python) }}\n  typing:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2\n      - uses: astral-sh/setup-uv@f0ec1fc3b38f5e7cd731bb6ce540c5af426746bb # v6.1.0\n        with:\n          enable-cache: true\n          prune-cache: false\n      - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0\n        with:\n          python-version-file: pyproject.toml\n      - name: cache mypy\n        uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3\n        with:\n          path: ./.mypy_cache\n          key: mypy|${{ hashFiles('pyproject.toml') }}\n      - run: uv run --locked tox run -e typing\n"
  },
  {
    "path": ".gitignore",
    "content": ".idea/\n.vscode/\n__pycache__/\ndist/\n.coverage*\nhtmlcov/\n.tox/\ndocs/_build/\n"
  },
  {
    "path": ".pre-commit-config.yaml",
    "content": "repos:\n  - repo: https://github.com/astral-sh/ruff-pre-commit\n    rev: 76e47323a83cd9795e4ff9a1de1c0d2eef610f17  # frozen: v0.11.11\n    hooks:\n      - id: ruff\n      - id: ruff-format\n  - repo: https://github.com/astral-sh/uv-pre-commit\n    rev: 648bdbfd6bb1a82f132ecc2c666e0d1b2e4b0d94  # frozen: 0.7.8\n    hooks:\n      - id: uv-lock\n  - repo: https://github.com/pre-commit/pre-commit-hooks\n    rev: cef0300fd0fc4d2a87a85fa2093c6b283ea36f4b  # frozen: v5.0.0\n    hooks:\n      - id: check-merge-conflict\n      - id: debug-statements\n      - id: fix-byte-order-marker\n      - id: trailing-whitespace\n      - id: end-of-file-fixer\n"
  },
  {
    "path": ".readthedocs.yaml",
    "content": "version: 2\nbuild:\n  os: ubuntu-24.04\n  tools:\n    python: '3.13'\n  commands:\n    - asdf plugin add uv\n    - asdf install uv latest\n    - asdf global uv latest\n    - uv run --group docs sphinx-build -W -b dirhtml docs $READTHEDOCS_OUTPUT/html\n"
  },
  {
    "path": "CHANGES.rst",
    "content": ".. currentmodule:: jinja2\n\nVersion 3.2.0\n-------------\n\nUnreleased\n\n-   Drop support for Python 3.7, 3.8, and 3.9.\n-   Update minimum MarkupSafe version to >= 3.0.\n-   Update minimum Babel version to >= 2.17.\n-   Deprecate the ``__version__`` attribute. Use feature detection or\n    ``importlib.metadata.version(\"jinja2\")`` instead.\n-   Use modern packaging metadata with ``pyproject.toml`` instead of ``setup.cfg``.\n    :pr:`1793`\n-   Use ``flit_core`` instead of ``setuptools`` as build backend.\n\n\nVersion 3.1.6\n-------------\n\nReleased 2025-03-05\n\n-   The ``|attr`` filter does not bypass the environment's attribute lookup,\n    allowing the sandbox to apply its checks. :ghsa:`cpwx-vrp4-4pq7`\n\n\nVersion 3.1.5\n-------------\n\nReleased 2024-12-21\n\n-   The sandboxed environment handles indirect calls to ``str.format``, such as\n    by passing a stored reference to a filter that calls its argument.\n    :ghsa:`q2x7-8rv6-6q7h`\n-   Escape template name before formatting it into error messages, to avoid\n    issues with names that contain f-string syntax.\n    :issue:`1792`, :ghsa:`gmj6-6f8f-6699`\n-   Sandbox does not allow ``clear`` and ``pop`` on known mutable sequence\n    types. :issue:`2032`\n-   Calling sync ``render`` for an async template uses ``asyncio.run``.\n    :pr:`1952`\n-   Avoid unclosed ``auto_aiter`` warnings. :pr:`1960`\n-   Return an ``aclose``-able ``AsyncGenerator`` from\n    ``Template.generate_async``. :pr:`1960`\n-   Avoid leaving ``root_render_func()`` unclosed in\n    ``Template.generate_async``. :pr:`1960`\n-   Avoid leaving async generators unclosed in blocks, includes and extends.\n    :pr:`1960`\n-   The runtime uses the correct ``concat`` function for the current environment\n    when calling block references. :issue:`1701`\n-   Make ``|unique`` async-aware, allowing it to be used after another\n    async-aware filter. :issue:`1781`\n-   ``|int`` filter handles ``OverflowError`` from scientific notation.\n    :issue:`1921`\n-   Make compiling deterministic for tuple unpacking in a ``{% set ... %}``\n    call. :issue:`2021`\n-   Fix dunder protocol (`copy`/`pickle`/etc) interaction with ``Undefined``\n    objects. :issue:`2025`\n-   Fix `copy`/`pickle` support for the internal ``missing`` object.\n    :issue:`2027`\n-   ``Environment.overlay(enable_async)`` is applied correctly. :pr:`2061`\n-   The error message from ``FileSystemLoader`` includes the paths that were\n    searched. :issue:`1661`\n-   ``PackageLoader`` shows a clearer error message when the package does not\n    contain the templates directory. :issue:`1705`\n-   Improve annotations for methods returning copies. :pr:`1880`\n-   ``urlize`` does not add ``mailto:`` to values like `@a@b`. :pr:`1870`\n-   Tests decorated with `@pass_context`` can be used with the ``|select``\n    filter. :issue:`1624`\n-   Using ``set`` for multiple assignment (``a, b = 1, 2``) does not fail when the\n    target is a namespace attribute. :issue:`1413`\n-   Using ``set`` in all branches of ``{% if %}{% elif %}{% else %}`` blocks\n    does not cause the variable to be considered initially undefined.\n    :issue:`1253`\n\n\nVersion 3.1.4\n-------------\n\nReleased 2024-05-05\n\n-   The ``xmlattr`` filter does not allow keys with ``/`` solidus, ``>``\n    greater-than sign, or ``=`` equals sign, in addition to disallowing spaces.\n    Regardless of any validation done by Jinja, user input should never be used\n    as keys to this filter, or must be separately validated first.\n    :ghsa:`h75v-3vvj-5mfj`\n\n\nVersion 3.1.3\n-------------\n\nReleased 2024-01-10\n\n-   Fix compiler error when checking if required blocks in parent templates are\n    empty. :pr:`1858`\n-   ``xmlattr`` filter does not allow keys with spaces. :ghsa:`h5c8-rqwp-cp95`\n-   Make error messages stemming from invalid nesting of ``{% trans %}`` blocks\n    more helpful. :pr:`1918`\n\n\nVersion 3.1.2\n-------------\n\nReleased 2022-04-28\n\n-   Add parameters to ``Environment.overlay`` to match ``__init__``.\n    :issue:`1645`\n-   Handle race condition in ``FileSystemBytecodeCache``. :issue:`1654`\n\n\nVersion 3.1.1\n-------------\n\nReleased 2022-03-25\n\n-   The template filename on Windows uses the primary path separator.\n    :issue:`1637`\n\n\nVersion 3.1.0\n-------------\n\nReleased 2022-03-24\n\n-   Drop support for Python 3.6. :pr:`1534`\n-   Remove previously deprecated code. :pr:`1544`\n\n    -   ``WithExtension`` and ``AutoEscapeExtension`` are built-in now.\n    -   ``contextfilter`` and ``contextfunction`` are replaced by\n        ``pass_context``. ``evalcontextfilter`` and\n        ``evalcontextfunction`` are replaced by ``pass_eval_context``.\n        ``environmentfilter`` and ``environmentfunction`` are replaced\n        by ``pass_environment``.\n    -   ``Markup`` and ``escape`` should be imported from MarkupSafe.\n    -   Compiled templates from very old Jinja versions may need to be\n        recompiled.\n    -   Legacy resolve mode for ``Context`` subclasses is no longer\n        supported. Override ``resolve_or_missing`` instead of\n        ``resolve``.\n    -   ``unicode_urlencode`` is renamed to ``url_quote``.\n\n-   Add support for native types in macros. :issue:`1510`\n-   The ``{% trans %}`` tag can use ``pgettext`` and ``npgettext`` by\n    passing a context string as the first token in the tag, like\n    ``{% trans \"title\" %}``. :issue:`1430`\n-   Update valid identifier characters from Python 3.6 to 3.7.\n    :pr:`1571`\n-   Filters and tests decorated with ``@async_variant`` are pickleable.\n    :pr:`1612`\n-   Add ``items`` filter. :issue:`1561`\n-   Subscriptions (``[0]``, etc.) can be used after filters, tests, and\n    calls when the environment is in async mode. :issue:`1573`\n-   The ``groupby`` filter is case-insensitive by default, matching\n    other comparison filters. Added the ``case_sensitive`` parameter to\n    control this. :issue:`1463`\n-   Windows drive-relative path segments in template names will not\n    result in ``FileSystemLoader`` and ``PackageLoader`` loading from\n    drive-relative paths. :pr:`1621`\n\n\nVersion 3.0.3\n-------------\n\nReleased 2021-11-09\n\n-   Fix traceback rewriting internals for Python 3.10 and 3.11.\n    :issue:`1535`\n-   Fix how the native environment treats leading and trailing spaces\n    when parsing values on Python 3.10. :pr:`1537`\n-   Improve async performance by avoiding checks for common types.\n    :issue:`1514`\n-   Revert change to ``hash(Node)`` behavior. Nodes are hashed by id\n    again :issue:`1521`\n-   ``PackageLoader`` works when the package is a single module file.\n    :issue:`1512`\n\n\nVersion 3.0.2\n-------------\n\nReleased 2021-10-04\n\n-   Fix a loop scoping bug that caused assignments in nested loops\n    to still be referenced outside of it. :issue:`1427`\n-   Make ``compile_templates`` deterministic for filter and import\n    names. :issue:`1452, 1453`\n-   Revert an unintended change that caused ``Undefined`` to act like\n    ``StrictUndefined`` for the ``in`` operator. :issue:`1448`\n-   Imported macros have access to the current template globals in async\n    environments. :issue:`1494`\n-   ``PackageLoader`` will not include a current directory (.) path\n    segment. This allows loading templates from the root of a zip\n    import. :issue:`1467`\n\n\nVersion 3.0.1\n-------------\n\nReleased 2021-05-18\n\n-   Update MarkupSafe dependency to >= 2.0. :pr:`1418`\n-   Mark top-level names as exported so type checking understands\n    imports in user projects. :issue:`1426`\n-   Fix some types that weren't available in Python 3.6.0. :issue:`1433`\n-   The deprecation warning for unneeded ``autoescape`` and ``with_``\n    extensions shows more relevant context. :issue:`1429`\n-   Fixed calling deprecated ``jinja2.Markup`` without an argument.\n    Use ``markupsafe.Markup`` instead. :issue:`1438`\n-   Calling sync ``render`` for an async template uses ``asyncio.new_event_loop``\n    This fixes a deprecation that Python 3.10 introduces. :issue:`1443`\n\n\nVersion 3.0.0\n-------------\n\nReleased 2021-05-11\n\n-   Drop support for Python 2.7 and 3.5.\n-   Bump MarkupSafe dependency to >=1.1.\n-   Bump Babel optional dependency to >=2.1.\n-   Remove code that was marked deprecated.\n-   Add type hinting. :pr:`1412`\n-   Use :pep:`451` API to load templates with\n    :class:`~loaders.PackageLoader`. :issue:`1168`\n-   Fix a bug that caused imported macros to not have access to the\n    current template's globals. :issue:`688`\n-   Add ability to ignore ``trim_blocks`` using ``+%}``. :issue:`1036`\n-   Fix a bug that caused custom async-only filters to fail with\n    constant input. :issue:`1279`\n-   Fix UndefinedError incorrectly being thrown on an undefined variable\n    instead of ``Undefined`` being returned on\n    ``NativeEnvironment`` on Python 3.10. :issue:`1335`\n-   Blocks can be marked as ``required``. They must be overridden at\n    some point, but not necessarily by the direct child. :issue:`1147`\n-   Deprecate the ``autoescape`` and ``with`` extensions, they are\n    built-in to the compiler. :issue:`1203`\n-   The ``urlize`` filter recognizes ``mailto:`` links and takes\n    ``extra_schemes`` (or ``env.policies[\"urlize.extra_schemes\"]``) to\n    recognize other schemes. It tries to balance parentheses within a\n    URL instead of ignoring trailing characters. The parsing in general\n    has been updated to be more efficient and match more cases. URLs\n    without a scheme are linked as ``https://`` instead of ``http://``.\n    :issue:`522, 827, 1172`, :pr:`1195`\n-   Filters that get attributes, such as ``map`` and ``groupby``, can\n    use a false or empty value as a default. :issue:`1331`\n-   Fix a bug that prevented variables set in blocks or loops from\n    being accessed in custom context functions. :issue:`768`\n-   Fix a bug that caused scoped blocks from accessing special loop\n    variables. :issue:`1088`\n-   Update the template globals when calling\n    ``Environment.get_template(globals=...)`` even if the template was\n    already loaded. :issue:`295`\n-   Do not raise an error for undefined filters in unexecuted\n    if-statements and conditional expressions. :issue:`842`\n-   Add ``is filter`` and ``is test`` tests to test if a name is a\n    registered filter or test. This allows checking if a filter is\n    available in a template before using it. Test functions can be\n    decorated with ``@pass_environment``, ``@pass_eval_context``,\n    or ``@pass_context``. :issue:`842`, :pr:`1248`\n-   Support ``pgettext`` and ``npgettext`` (message contexts) in i18n\n    extension. :issue:`441`\n-   The ``|indent`` filter's ``width`` argument can be a string to\n    indent by. :pr:`1167`\n-   The parser understands hex, octal, and binary integer literals.\n    :issue:`1170`\n-   ``Undefined.__contains__`` (``in``) raises an ``UndefinedError``\n    instead of a ``TypeError``. :issue:`1198`\n-   ``Undefined`` is iterable in an async environment. :issue:`1294`\n-   ``NativeEnvironment`` supports async mode. :issue:`1362`\n-   Template rendering only treats ``\\n``, ``\\r\\n`` and ``\\r`` as line\n    breaks. Other characters are left unchanged. :issue:`769, 952, 1313`\n-   ``|groupby`` filter takes an optional ``default`` argument.\n    :issue:`1359`\n-   The function and filter decorators have been renamed and unified.\n    The old names are deprecated. :issue:`1381`\n\n    -   ``pass_context`` replaces ``contextfunction`` and\n        ``contextfilter``.\n    -   ``pass_eval_context`` replaces ``evalcontextfunction`` and\n        ``evalcontextfilter``\n    -   ``pass_environment`` replaces ``environmentfunction`` and\n        ``environmentfilter``.\n\n-   Async support no longer requires Jinja to patch itself. It must\n    still be enabled with ``Environment(enable_async=True)``.\n    :issue:`1390`\n-   Overriding ``Context.resolve`` is deprecated, override\n    ``resolve_or_missing`` instead. :issue:`1380`\n\n\nVersion 2.11.3\n--------------\n\nReleased 2021-01-31\n\n-   Improve the speed of the ``urlize`` filter by reducing regex\n    backtracking. Email matching requires a word character at the start\n    of the domain part, and only word characters in the TLD. :pr:`1343`\n\n\nVersion 2.11.2\n--------------\n\nReleased 2020-04-13\n\n-   Fix a bug that caused callable objects with ``__getattr__``, like\n    :class:`~unittest.mock.Mock` to be treated as a\n    :func:`contextfunction`. :issue:`1145`\n-   Update ``wordcount`` filter to trigger :class:`Undefined` methods\n    by wrapping the input in :func:`soft_str`. :pr:`1160`\n-   Fix a hang when displaying tracebacks on Python 32-bit.\n    :issue:`1162`\n-   Showing an undefined error for an object that raises\n    ``AttributeError`` on access doesn't cause a recursion error.\n    :issue:`1177`\n-   Revert changes to :class:`~loaders.PackageLoader` from 2.10 which\n    removed the dependency on setuptools and pkg_resources, and added\n    limited support for namespace packages. The changes caused issues\n    when using Pytest. Due to the difficulty in supporting Python 2 and\n    :pep:`451` simultaneously, the changes are reverted until 3.0.\n    :pr:`1182`\n-   Fix line numbers in error messages when newlines are stripped.\n    :pr:`1178`\n-   The special ``namespace()`` assignment object in templates works in\n    async environments. :issue:`1180`\n-   Fix whitespace being removed before tags in the middle of lines when\n    ``lstrip_blocks`` is enabled. :issue:`1138`\n-   :class:`~nativetypes.NativeEnvironment` doesn't evaluate\n    intermediate strings during rendering. This prevents early\n    evaluation which could change the value of an expression.\n    :issue:`1186`\n\n\nVersion 2.11.1\n--------------\n\nReleased 2020-01-30\n\n-   Fix a bug that prevented looking up a key after an attribute\n    (``{{ data.items[1:] }}``) in an async template. :issue:`1141`\n\n\nVersion 2.11.0\n--------------\n\nReleased 2020-01-27\n\n-   Drop support for Python 2.6, 3.3, and 3.4. This will be the last\n    version to support Python 2.7 and 3.5.\n-   Added a new ``ChainableUndefined`` class to support getitem and\n    getattr on an undefined object. :issue:`977`\n-   Allow ``{%+`` syntax (with NOP behavior) when ``lstrip_blocks`` is\n    disabled. :issue:`748`\n-   Added a ``default`` parameter for the ``map`` filter. :issue:`557`\n-   Exclude environment globals from\n    :func:`meta.find_undeclared_variables`. :issue:`931`\n-   Float literals can be written with scientific notation, like\n    2.56e-3. :issue:`912`, :pr:`922`\n-   Int and float literals can be written with the '_' separator for\n    legibility, like 12_345. :pr:`923`\n-   Fix a bug causing deadlocks in ``LRUCache.setdefault``. :pr:`1000`\n-   The ``trim`` filter takes an optional string of characters to trim.\n    :pr:`828`\n-   A new ``jinja2.ext.debug`` extension adds a ``{% debug %}`` tag to\n    quickly dump the current context and available filters and tests.\n    :issue:`174`, :pr:`798, 983`\n-   Lexing templates with large amounts of whitespace is much faster.\n    :issue:`857`, :pr:`858`\n-   Parentheses around comparisons are preserved, so\n    ``{{ 2 * (3 < 5) }}`` outputs \"2\" instead of \"False\".\n    :issue:`755`, :pr:`938`\n-   Add new ``boolean``, ``false``, ``true``, ``integer`` and ``float``\n    tests. :pr:`824`\n-   The environment's ``finalize`` function is only applied to the\n    output of expressions (constant or not), not static template data.\n    :issue:`63`\n-   When providing multiple paths to ``FileSystemLoader``, a template\n    can have the same name as a directory. :issue:`821`\n-   Always return :class:`Undefined` when omitting the ``else`` clause\n    in a ``{{ 'foo' if bar }}`` expression, regardless of the\n    environment's ``undefined`` class. Omitting the ``else`` clause is a\n    valid shortcut and should not raise an error when using\n    :class:`StrictUndefined`. :issue:`710`, :pr:`1079`\n-   Fix behavior of ``loop`` control variables such as ``length`` and\n    ``revindex0`` when looping over a generator. :issue:`459, 751, 794`,\n    :pr:`993`\n-   Async support is only loaded the first time an environment enables\n    it, in order to avoid a slow initial import. :issue:`765`\n-   In async environments, the ``|map`` filter will await the filter\n    call if needed. :pr:`913`\n-   In for loops that access ``loop`` attributes, the iterator is not\n    advanced ahead of the current iteration unless ``length``,\n    ``revindex``, ``nextitem``, or ``last`` are accessed. This makes it\n    less likely to break ``groupby`` results. :issue:`555`, :pr:`1101`\n-   In async environments, the ``loop`` attributes ``length`` and\n    ``revindex`` work for async iterators. :pr:`1101`\n-   In async environments, values from attribute/property access will\n    be awaited if needed. :pr:`1101`\n-   :class:`~loader.PackageLoader` doesn't depend on setuptools or\n    pkg_resources. :issue:`970`\n-   ``PackageLoader`` has limited support for :pep:`420` namespace\n    packages. :issue:`1097`\n-   Support :class:`os.PathLike` objects in\n    :class:`~loader.FileSystemLoader` and :class:`~loader.ModuleLoader`.\n    :issue:`870`\n-   :class:`~nativetypes.NativeTemplate` correctly handles quotes\n    between expressions. ``\"'{{ a }}', '{{ b }}'\"`` renders as the tuple\n    ``('1', '2')`` rather than the string ``'1, 2'``. :issue:`1020`\n-   Creating a :class:`~nativetypes.NativeTemplate` directly creates a\n    :class:`~nativetypes.NativeEnvironment` instead of a default\n    :class:`Environment`. :issue:`1091`\n-   After calling ``LRUCache.copy()``, the copy's queue methods point to\n    the correct queue. :issue:`843`\n-   Compiling templates always writes UTF-8 instead of defaulting to the\n    system encoding. :issue:`889`\n-   ``|wordwrap`` filter treats existing newlines as separate paragraphs\n    to be wrapped individually, rather than creating short intermediate\n    lines. :issue:`175`\n-   Add ``break_on_hyphens`` parameter to ``|wordwrap`` filter.\n    :issue:`550`\n-   Cython compiled functions decorated as context functions will be\n    passed the context. :pr:`1108`\n-   When chained comparisons of constants are evaluated at compile time,\n    the result follows Python's behavior of returning ``False`` if any\n    comparison returns ``False``, rather than only the last one.\n    :issue:`1102`\n-   Tracebacks for exceptions in templates show the correct line numbers\n    and source for Python >= 3.7. :issue:`1104`\n-   Tracebacks for template syntax errors in Python 3 no longer show\n    internal compiler frames. :issue:`763`\n-   Add a ``DerivedContextReference`` node that can be used by\n    extensions to get the current context and local variables such as\n    ``loop``. :issue:`860`\n-   Constant folding during compilation is applied to some node types\n    that were previously overlooked. :issue:`733`\n-   ``TemplateSyntaxError.source`` is not empty when raised from an\n    included template. :issue:`457`\n-   Passing an ``Undefined`` value to ``get_template`` (such as through\n    ``extends``, ``import``, or ``include``), raises an\n    ``UndefinedError`` consistently. ``select_template`` will show the\n    undefined message in the list of attempts rather than the empty\n    string. :issue:`1037`\n-   ``TemplateSyntaxError`` can be pickled. :pr:`1117`\n\n\nVersion 2.10.3\n--------------\n\nReleased 2019-10-04\n\n-   Fix a typo in Babel entry point in ``setup.py`` that was preventing\n    installation.\n\n\nVersion 2.10.2\n--------------\n\nReleased 2019-10-04\n\n-   Fix Python 3.7 deprecation warnings.\n-   Using ``range`` in the sandboxed environment uses ``xrange`` on\n    Python 2 to avoid memory use. :issue:`933`\n-   Use Python 3.7's better traceback support to avoid a core dump when\n    using debug builds of Python 3.7. :issue:`1050`\n\n\nVersion 2.10.1\n--------------\n\nReleased 2019-04-06\n\n-   ``SandboxedEnvironment`` securely handles ``str.format_map`` in\n    order to prevent code execution through untrusted format strings.\n    The sandbox already handled ``str.format``.\n\n\nVersion 2.10\n------------\n\nReleased 2017-11-08\n\n-   Added a new extension node called ``OverlayScope`` which can be used\n    to create an unoptimized scope that will look up all variables from\n    a derived context.\n-   Added an ``in`` test that works like the in operator. This can be\n    used in combination with ``reject`` and ``select``.\n-   Added ``previtem`` and ``nextitem`` to loop contexts, providing\n    access to the previous/next item in the loop. If such an item does\n    not exist, the value is undefined.\n-   Added ``changed(*values)`` to loop contexts, providing an easy way\n    of checking whether a value has changed since the last iteration (or\n    rather since the last call of the method)\n-   Added a ``namespace`` function that creates a special object which\n    allows attribute assignment using the ``set`` tag. This can be used\n    to carry data across scopes, e.g. from a loop body to code that\n    comes after the loop.\n-   Added a ``trimmed`` modifier to ``{% trans %}`` to strip linebreaks\n    and surrounding whitespace. Also added a new policy to enable this\n    for all ``trans`` blocks.\n-   The ``random`` filter is no longer incorrectly constant folded and\n    will produce a new random choice each time the template is rendered.\n    :pr:`478`\n-   Added a ``unique`` filter. :pr:`469`\n-   Added ``min`` and ``max`` filters. :pr:`475`\n-   Added tests for all comparison operators: ``eq``, ``ne``, ``lt``,\n    ``le``, ``gt``, ``ge``. :pr:`665`\n-   ``import`` statement cannot end with a trailing comma. :pr:`617`,\n    :pr:`618`\n-   ``indent`` filter will not indent blank lines by default. :pr:`685`\n-   Add ``reverse`` argument for ``dictsort`` filter. :pr:`692`\n-   Add a ``NativeEnvironment`` that renders templates to native Python\n    types instead of strings. :pr:`708`\n-   Added filter support to the block ``set`` tag. :pr:`489`\n-   ``tojson`` filter marks output as safe to match documented behavior.\n    :pr:`718`\n-   Resolved a bug where getting debug locals for tracebacks could\n    modify template context.\n-   Fixed a bug where having many ``{% elif ... %}`` blocks resulted in\n    a \"too many levels of indentation\" error. These blocks now compile\n    to native ``elif ..:`` instead of ``else: if ..:`` :issue:`759`\n\n\nVersion 2.9.6\n-------------\n\nReleased 2017-04-03\n\n-   Fixed custom context behavior in fast resolve mode :issue:`675`\n\n\nVersion 2.9.5\n-------------\n\nReleased 2017-01-28\n\n-   Restored the original repr of the internal ``_GroupTuple`` because\n    this caused issues with ansible and it was an unintended change.\n    :issue:`654`\n-   Added back support for custom contexts that override the old\n    ``resolve`` method since it was hard for people to spot that this\n    could cause a regression.\n-   Correctly use the buffer for the else block of for loops. This\n    caused invalid syntax errors to be caused on 2.x and completely\n    wrong behavior on Python 3 :issue:`669`\n-   Resolve an issue where the ``{% extends %}`` tag could not be used\n    with async environments. :issue:`668`\n-   Reduce memory footprint slightly by reducing our unicode database\n    dump we use for identifier matching on Python 3 :issue:`666`\n-   Fixed autoescaping not working for macros in async compilation mode.\n    :issue:`671`\n\n\nVersion 2.9.4\n-------------\n\nReleased 2017-01-10\n\n-   Solved some warnings for string literals. :issue:`646`\n-   Increment the bytecode cache version which was not done due to an\n    oversight before.\n-   Corrected bad code generation and scoping for filtered loops.\n    :issue:`649`\n-   Resolved an issue where top-level output silencing after known\n    extend blocks could generate invalid code when blocks where\n    contained in if statements. :issue:`651`\n-   Made the ``truncate.leeway`` default configurable to improve\n    compatibility with older templates.\n\n\nVersion 2.9.3\n-------------\n\nReleased 2017-01-08\n\n-   Restored the use of blocks in macros to the extend that was possible\n    before. On Python 3 it would render a generator repr instead of the\n    block contents. :issue:`645`\n-   Set a consistent behavior for assigning of variables in inner scopes\n    when the variable is also read from an outer scope. This now sets\n    the intended behavior in all situations however it does not restore\n    the old behavior where limited assignments to outer scopes was\n    possible. For more information and a discussion see :issue:`641`\n-   Resolved an issue where ``block scoped`` would not take advantage of\n    the new scoping rules. In some more exotic cases a variable\n    overridden in a local scope would not make it into a block.\n-   Change the code generation of the ``with`` statement to be in line\n    with the new scoping rules. This resolves some unlikely bugs in edge\n    cases. This also introduces a new internal ``With`` node that can be\n    used by extensions.\n\n\nVersion 2.9.2\n-------------\n\nReleased 2017-01-08\n\n-   Fixed a regression that caused for loops to not be able to use the\n    same variable for the target as well as source iterator.\n    :issue:`640`\n-   Add support for a previously unknown behavior of macros. It used to\n    be possible in some circumstances to explicitly provide a caller\n    argument to macros. While badly buggy and unintended it turns out\n    that this is a common case that gets copy pasted around. To not\n    completely break backwards compatibility with the most common cases\n    it's now possible to provide an explicit keyword argument for caller\n    if it's given an explicit default. :issue:`642`\n\n\nVersion 2.9.1\n-------------\n\nReleased 2017-01-07\n\n-   Resolved a regression with call block scoping for macros. Nested\n    caller blocks that used the same identifiers as outer macros could\n    refer to the wrong variable incorrectly.\n\n\nVersion 2.9\n-----------\n\nReleased 2017-01-07, codename Derivation\n\n-   Change cache key definition in environment. This fixes a performance\n    regression introduced in 2.8.\n-   Added support for ``generator_stop`` on supported Python versions\n    (Python 3.5 and later)\n-   Corrected a long standing issue with operator precedence of math\n    operations not being what was expected.\n-   Added support for Python 3.6 async iterators through a new async\n    mode.\n-   Added policies for filter defaults and similar things.\n-   Urlize now sets \"rel noopener\" by default.\n-   Support attribute fallback for old-style classes in 2.x.\n-   Support toplevel set statements in extend situations.\n-   Restored behavior of Cycler for Python 3 users.\n-   Subtraction now follows the same behavior as other operators on\n    undefined values.\n-   ``map`` and friends will now give better error messages if you\n    forgot to quote the parameter.\n-   Depend on MarkupSafe 0.23 or higher.\n-   Improved the ``truncate`` filter to support better truncation in\n    case the string is barely truncated at all.\n-   Change the logic for macro autoescaping to be based on the runtime\n    autoescaping information at call time instead of macro define time.\n-   Ported a modified version of the ``tojson`` filter from Flask to\n    Jinja and hooked it up with the new policy framework.\n-   Block sets are now marked ``safe`` by default.\n-   On Python 2 the asciification of ASCII strings can now be disabled\n    with the ``compiler.ascii_str`` policy.\n-   Tests now no longer accept an arbitrary expression as first argument\n    but a restricted one. This means that you can now properly use\n    multiple tests in one expression without extra parentheses. In\n    particular you can now write ``foo is divisibleby 2 or foo is\n    divisibleby 3`` as you would expect.\n-   Greatly changed the scoping system to be more consistent with what\n    template designers and developers expect. There is now no more magic\n    difference between the different include and import constructs.\n    Context is now always propagated the same way. The only remaining\n    differences is the defaults for ``with context`` and ``without\n    context``.\n-   The ``with`` and ``autoescape`` tags are now built-in.\n-   Added the new ``select_autoescape`` function which helps configuring\n    better autoescaping easier.\n-   Fixed a runtime error in the sandbox when attributes of async\n    generators were accessed.\n\n\nVersion 2.8.1\n-------------\n\nReleased 2016-12-29\n\n-   Fixed the ``for_qs`` flag for ``urlencode``.\n-   Fixed regression when applying ``int`` to non-string values.\n-   SECURITY: if the sandbox mode is used format expressions are now\n    sandboxed with the same rules as in Jinja. This solves various\n    information leakage problems that can occur with format strings.\n\n\nVersion 2.8\n-----------\n\nReleased 2015-07-26, codename Replacement\n\n-   Added ``target`` parameter to urlize function.\n-   Added support for ``followsymlinks`` to the file system loader.\n-   The truncate filter now counts the length.\n-   Added equalto filter that helps with select filters.\n-   Changed cache keys to use absolute file names if available instead\n    of load names.\n-   Fixed loop length calculation for some iterators.\n-   Changed how Jinja enforces strings to be native strings in Python 2\n    to work when people break their default encoding.\n-   Added ``make_logging_undefined`` which returns an undefined\n    object that logs failures into a logger.\n-   If unmarshalling of cached data fails the template will be reloaded\n    now.\n-   Implemented a block ``set`` tag.\n-   Default cache size was increased to 400 from a low 50.\n-   Fixed ``is number`` test to accept long integers in all Python\n    versions.\n-   Changed ``is number`` to accept Decimal as a number.\n-   Added a check for default arguments followed by non-default\n    arguments. This change makes ``{% macro m(x, y=1, z) %}`` a syntax\n    error. The previous behavior for this code was broken anyway\n    (resulting in the default value being applied to ``y``).\n-   Add ability to use custom subclasses of\n    ``jinja2.compiler.CodeGenerator`` and ``jinja2.runtime.Context`` by\n    adding two new attributes to the environment\n    (``code_generator_class`` and ``context_class``). :pr:`404`\n-   Added support for context/environment/evalctx decorator functions on\n    the finalize callback of the environment.\n-   Escape query strings for urlencode properly. Previously slashes were\n    not escaped in that place.\n-   Add 'base' parameter to 'int' filter.\n\n\nVersion 2.7.3\n-------------\n\nReleased 2014-06-06\n\n-   Security issue: Corrected the security fix for the cache folder.\n    This fix was provided by RedHat.\n\n\nVersion 2.7.2\n-------------\n\nReleased 2014-01-10\n\n-   Prefix loader was not forwarding the locals properly to inner\n    loaders. This is now fixed.\n-   Security issue: Changed the default folder for the filesystem cache\n    to be user specific and read and write protected on UNIX systems.\n    See `Debian bug 734747`_ for more information.\n\n.. _Debian bug 734747: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=734747\n\n\nVersion 2.7.1\n-------------\n\nReleased 2013-08-07\n\n-   Fixed a bug with ``call_filter`` not working properly on environment\n    and context filters.\n-   Fixed lack of Python 3 support for bytecode caches.\n-   Reverted support for defining blocks in included templates as this\n    broke existing templates for users.\n-   Fixed some warnings with hashing of undefineds and nodes if Python\n    is run with warnings for Python 3.\n-   Added support for properly hashing undefined objects.\n-   Fixed a bug with the title filter not working on already uppercase\n    strings.\n\n\nVersion 2.7\n-----------\n\nReleased 2013-05-20, codename Translation\n\n-   Choice and prefix loaders now dispatch source and template lookup\n    separately in order to work in combination with module loaders as\n    advertised.\n-   Fixed filesizeformat.\n-   Added a non-silent option for babel extraction.\n-   Added ``urlencode`` filter that automatically quotes values for URL\n    safe usage with utf-8 as only supported encoding. If applications\n    want to change this encoding they can override the filter.\n-   Added ``keep-trailing-newline`` configuration to environments and\n    templates to optionally preserve the final trailing newline.\n-   Accessing ``last`` on the loop context no longer causes the iterator\n    to be consumed into a list.\n-   Python requirement changed: 2.6, 2.7 or >= 3.3 are required now,\n    supported by same source code, using the \"six\" compatibility\n    library.\n-   Allow ``contextfunction`` and other decorators to be applied to\n    ``__call__``.\n-   Added support for changing from newline to different signs in the\n    ``wordwrap`` filter.\n-   Added support for ignoring memcache errors silently.\n-   Added support for keeping the trailing newline in templates.\n-   Added finer grained support for stripping whitespace on the left\n    side of blocks.\n-   Added ``map``, ``select``, ``reject``, ``selectattr`` and\n    ``rejectattr`` filters.\n-   Added support for ``loop.depth`` to figure out how deep inside a\n    recursive loop the code is.\n-   Disabled py_compile for pypy and python 3.\n\n\nVersion 2.6\n-----------\n\nReleased 2011-07-24, codename Convolution\n\n-   Internal attributes now raise an internal attribute error now\n    instead of returning an undefined. This fixes problems when passing\n    undefined objects to Python semantics expecting APIs.\n-   Traceback support now works properly for PyPy. (Tested with 1.4)\n-   Implemented operator intercepting for sandboxed environments. This\n    allows application developers to disable builtin operators for\n    better security. (For instance limit the mathematical operators to\n    actual integers instead of longs)\n-   Groupby filter now supports dotted notation for grouping by\n    attributes of attributes.\n-   Scoped blocks now properly treat toplevel assignments and imports.\n    Previously an import suddenly \"disappeared\" in a scoped block.\n-   Automatically detect newer Python interpreter versions before\n    loading code from bytecode caches to prevent segfaults on invalid\n    opcodes. The segfault in earlier Jinja versions here was not a\n    Jinja bug but a limitation in the underlying Python interpreter. If\n    you notice Jinja segfaulting in earlier versions after an upgrade\n    of the Python interpreter you don't have to upgrade, it's enough to\n    flush the bytecode cache. This just no longer makes this necessary,\n    Jinja will automatically detect these cases now.\n-   The sum filter can now sum up values by attribute. This is a\n    backwards incompatible change. The argument to the filter previously\n    was the optional starting index which defaults to zero. This now\n    became the second argument to the function because it's rarely used.\n-   Like sum, sort now also makes it possible to order items by\n    attribute.\n-   Like sum and sort, join now also is able to join attributes of\n    objects as string.\n-   The internal eval context now has a reference to the environment.\n-   Added a mapping test to see if an object is a dict or an object with\n    a similar interface.\n\n\nVersion 2.5.5\n-------------\n\nReleased 2010-10-18\n\n-   Built documentation is no longer part of release.\n\n\nVersion 2.5.4\n-------------\n\nReleased 2010-10-17\n\n-   Fixed extensions not loading properly with overlays.\n-   Work around a bug in cpython for the debugger that causes segfaults\n    on 64bit big-endian architectures.\n\n\nVersion 2.5.3\n-------------\n\nReleased 2010-10-17\n\n-   Fixed an operator precedence error introduced in 2.5.2. Statements\n    like \"-foo.bar\" had their implicit parentheses applied around the\n    first part of the expression (\"(-foo).bar\") instead of the more\n    correct \"-(foo.bar)\".\n\n\nVersion 2.5.2\n-------------\n\nReleased 2010-08-18\n\n-   Improved setup.py script to better work with assumptions people\n    might still have from it (``--with-speedups``).\n-   Fixed a packaging error that excluded the new debug support.\n\n\nVersion 2.5.1\n-------------\n\nReleased 2010-08-17\n\n-   StopIteration exceptions raised by functions called from templates\n    are now intercepted and converted to undefineds. This solves a lot\n    of debugging grief. (StopIteration is used internally to abort\n    template execution)\n-   Improved performance of macro calls slightly.\n-   Babel extraction can now properly extract newstyle gettext calls.\n-   Using the variable ``num`` in newstyle gettext for something else\n    than the pluralize count will no longer raise a :exc:`KeyError`.\n-   Removed builtin markup class and switched to markupsafe. For\n    backwards compatibility the pure Python implementation still exists\n    but is pulled from markupsafe by the Jinja developers. The debug\n    support went into a separate feature called \"debugsupport\" and is\n    disabled by default because it is only relevant for Python 2.4\n-   Fixed an issue with unary operators having the wrong precedence.\n\n\nVersion 2.5\n-----------\n\nReleased 2010-05-29, codename Incoherence\n\n-   Improved the sort filter (should have worked like this for a long\n    time) by adding support for case insensitive searches.\n-   Fixed a bug for getattribute constant folding.\n-   Support for newstyle gettext translations which result in a nicer\n    in-template user interface and more consistent catalogs.\n-   It's now possible to register extensions after an environment was\n    created.\n\n\nVersion 2.4.1\n-------------\n\nReleased 2010-04-20\n\n-   Fixed an error reporting bug for undefined.\n\n\nVersion 2.4\n-----------\n\nReleased 2010-04-13, codename Correlation\n\n-   The environment template loading functions now transparently pass\n    through a template object if it was passed to it. This makes it\n    possible to import or extend from a template object that was passed\n    to the template.\n-   Added a ``ModuleLoader`` that can load templates from\n    precompiled sources. The environment now features a method to\n    compile the templates from a configured loader into a zip file or\n    folder.\n-   The _speedups C extension now supports Python 3.\n-   Added support for autoescaping toggling sections and support for\n    evaluation contexts.\n-   Extensions have a priority now.\n\n\nVersion 2.3.1\n-------------\n\nReleased 2010-02-19\n\n-   Fixed an error reporting bug on all python versions\n-   Fixed an error reporting bug on Python 2.4\n\n\nVersion 2.3\n-----------\n\nReleased 2010-02-10, codename 3000 Pythons\n\n-   Fixes issue with code generator that causes unbound variables to be\n    generated if set was used in if-blocks and other small identifier\n    problems.\n-   Include tags are now able to select between multiple templates and\n    take the first that exists, if a list of templates is given.\n-   Fixed a problem with having call blocks in outer scopes that have an\n    argument that is also used as local variable in an inner frame\n    :issue:`360`.\n-   Greatly improved error message reporting :pr:`339`\n-   Implicit tuple expressions can no longer be totally empty. This\n    change makes ``{% if %}`` a syntax error now. :issue:`364`\n-   Added support for translator comments if extracted via babel.\n-   Added with-statement extension.\n-   Experimental Python 3 support.\n\n\nVersion 2.2.1\n-------------\n\nReleased 2009-09-14\n\n-   Fixes some smaller problems for Jinja on Jython.\n\n\nVersion 2.2\n-----------\n\nReleased 2009-09-13, codename Kong\n\n-   Include statements can now be marked with ``ignore missing`` to skip\n    non existing templates.\n-   Priority of ``not`` raised. It's now possible to write ``not foo in\n    bar`` as an alias to ``foo not in bar`` like in python. Previously\n    the grammar required parentheses (``not (foo in bar)``) which was\n    odd.\n-   Fixed a bug that caused syntax errors when defining macros or using\n    the ``{% call %}`` tag inside loops.\n-   Fixed a bug in the parser that made ``{{ foo[1, 2] }}`` impossible.\n-   Made it possible to refer to names from outer scopes in included\n    templates that were unused in the callers frame :issue:`327`\n-   Fixed a bug that caused internal errors if names where used as\n    iteration variable and regular variable *after* the loop if that\n    variable was unused *before* the loop. :pr:`331`\n-   Added support for optional ``scoped`` modifier to blocks.\n-   Added support for line-comments.\n-   Added the ``meta`` module.\n-   Renamed (undocumented) attribute \"overlay\" to \"overlayed\" on the\n    environment because it was clashing with a method of the same name.\n-   Speedup extension is now disabled by default.\n\n\nVersion 2.1.1\n-------------\n\nReleased 2008-12-25\n\n-   Fixed a translation error caused by looping over empty recursive\n    loops.\n\n\nVersion 2.1\n-----------\n\nReleased 2008-11-23, codename Yasuzō\n\n-   Fixed a bug with nested loops and the special loop variable. Before\n    the change an inner loop overwrote the loop variable from the outer\n    one after iteration.\n-   Fixed a bug with the i18n extension that caused the explicit\n    pluralization block to look up the wrong variable.\n-   Fixed a limitation in the lexer that made ``{{ foo.0.0 }}``\n    impossible.\n-   Index based subscribing of variables with a constant value returns\n    an undefined object now instead of raising an index error. This was\n    a bug caused by eager optimizing.\n-   The i18n extension looks up ``foo.ugettext`` now followed by\n    ``foo.gettext`` if an translations object is installed. This makes\n    dealing with custom translations classes easier.\n-   Fixed a confusing behavior with conditional extending. loops were\n    partially executed under some conditions even though they were not\n    part of a visible area.\n-   Added ``sort`` filter that works like ``dictsort`` but for arbitrary\n    sequences.\n-   Fixed a bug with empty statements in macros.\n-   Implemented a bytecode cache system.\n-   The template context is now weakref-able\n-   Inclusions and imports \"with context\" forward all variables now, not\n    only the initial context.\n-   Added a cycle helper called ``cycler``.\n-   Added a joining helper called ``joiner``.\n-   Added a ``compile_expression`` method to the environment that allows\n    compiling of Jinja expressions into callable Python objects.\n-   Fixed an escaping bug in urlize\n\n\nVersion 2.0\n-----------\n\nReleased 2008-07-17, codename Jinjavitus\n\n-   The subscribing of objects (looking up attributes and items) changed\n    from slightly. It's now possible to give attributes or items a\n    higher priority by either using dot-notation lookup or the bracket\n    syntax. This also changed the AST slightly. ``Subscript`` is gone\n    and was replaced with ``Getitem`` and ``Getattr``.\n-   Added support for preprocessing and token stream filtering for\n    extensions. This would allow extensions to allow simplified gettext\n    calls in template data and something similar.\n-   Added ``TemplateStream.dump``.\n-   Added missing support for implicit string literal concatenation.\n    ``{{ \"foo\" \"bar\" }}`` is equivalent to ``{{ \"foobar\" }}``\n-   ``else`` is optional for conditional expressions. If not given it\n    evaluates to ``false``.\n-   Improved error reporting for undefined values by providing a\n    position.\n-   ``filesizeformat`` filter uses decimal prefixes now by default and\n    can be set to binary mode with the second parameter.\n-   Fixed bug in finalizer\n\n\nVersion 2.0rc1\n--------------\n\nReleased 2008-06-09\n\n-   First release of Jinja 2.\n"
  },
  {
    "path": "LICENSE.txt",
    "content": "Copyright 2007 Pallets\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are\nmet:\n\n1.  Redistributions of source code must retain the above copyright\n    notice, this list of conditions and the following disclaimer.\n\n2.  Redistributions in binary form must reproduce the above copyright\n    notice, this list of conditions and the following disclaimer in the\n    documentation and/or other materials provided with the distribution.\n\n3.  Neither the name of the copyright holder nor the names of its\n    contributors may be used to endorse or promote products derived from\n    this software without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\nLIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A\nPARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\nHOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\nSPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED\nTO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\nPROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\nLIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\nNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
  },
  {
    "path": "README.md",
    "content": "<div align=\"center\"><img src=\"https://raw.githubusercontent.com/pallets/jinja/refs/heads/stable/docs/_static/jinja-name.svg\" alt=\"\" height=\"150\"></div>\n\n# Jinja\n\nJinja is a fast, expressive, extensible templating engine. Special\nplaceholders in the template allow writing code similar to Python\nsyntax. Then the template is passed data to render the final document.\n\nIt includes:\n\n-   Template inheritance and inclusion.\n-   Define and import macros within templates.\n-   HTML templates can use autoescaping to prevent XSS from untrusted\n    user input.\n-   A sandboxed environment can safely render untrusted templates.\n-   AsyncIO support for generating templates and calling async\n    functions.\n-   I18N support with Babel.\n-   Templates are compiled to optimized Python code just-in-time and\n    cached, or can be compiled ahead-of-time.\n-   Exceptions point to the correct line in templates to make debugging\n    easier.\n-   Extensible filters, tests, functions, and even syntax.\n\nJinja's philosophy is that while application logic belongs in Python if\npossible, it shouldn't make the template designer's job difficult by\nrestricting functionality too much.\n\n\n## In A Nutshell\n\n```jinja\n{% extends \"base.html\" %}\n{% block title %}Members{% endblock %}\n{% block content %}\n  <ul>\n  {% for user in users %}\n    <li><a href=\"{{ user.url }}\">{{ user.username }}</a></li>\n  {% endfor %}\n  </ul>\n{% endblock %}\n```\n\n## Donate\n\nThe Pallets organization develops and supports Jinja and other popular\npackages. In order to grow the community of contributors and users, and\nallow the maintainers to devote more time to the projects, [please\ndonate today][].\n\n[please donate today]: https://palletsprojects.com/donate\n\n## Contributing\n\nSee our [detailed contributing documentation][contrib] for many ways to\ncontribute, including reporting issues, requesting features, asking or answering\nquestions, and making PRs.\n\n[contrib]: https://palletsprojects.com/contributing/\n"
  },
  {
    "path": "docs/Makefile",
    "content": "# Minimal makefile for Sphinx documentation\n#\n\n# You can set these variables from the command line.\nSPHINXOPTS    =\nSPHINXBUILD   = sphinx-build\nSOURCEDIR     = .\nBUILDDIR      = _build\n\n# Put it first so that \"make\" without argument is like \"make help\".\nhelp:\n\t@$(SPHINXBUILD) -M help \"$(SOURCEDIR)\" \"$(BUILDDIR)\" $(SPHINXOPTS) $(O)\n\n.PHONY: help Makefile\n\n# Catch-all target: route all unknown targets to Sphinx using the new\n# \"make mode\" option.  $(O) is meant as a shortcut for $(SPHINXOPTS).\n%: Makefile\n\t@$(SPHINXBUILD) -M $@ \"$(SOURCEDIR)\" \"$(BUILDDIR)\" $(SPHINXOPTS) $(O)\n"
  },
  {
    "path": "docs/api.rst",
    "content": "API\n===\n\n.. module:: jinja2\n    :noindex:\n    :synopsis: public Jinja API\n\nThis document describes the API to Jinja and not the template language\n(for that, see :doc:`/templates`). It will be most useful as reference\nto those implementing the template interface to the application and not\nthose who are creating Jinja templates.\n\nBasics\n------\n\nJinja uses a central object called the template :class:`Environment`.\nInstances of this class are used to store the configuration and global objects,\nand are used to load templates from the file system or other locations.\nEven if you are creating templates from strings by using the constructor of\n:class:`Template` class, an environment is created automatically for you,\nalbeit a shared one.\n\nMost applications will create one :class:`Environment` object on application\ninitialization and use that to load templates.  In some cases however, it's\nuseful to have multiple environments side by side, if different configurations\nare in use.\n\nThe simplest way to configure Jinja to load templates for your\napplication is to use :class:`~loaders.PackageLoader`.\n\n.. code-block:: python\n\n    from jinja2 import Environment, PackageLoader, select_autoescape\n    env = Environment(\n        loader=PackageLoader(\"yourapp\"),\n        autoescape=select_autoescape()\n    )\n\nThis will create a template environment with a loader that looks up\ntemplates in the ``templates`` folder inside the ``yourapp`` Python\npackage (or next to the ``yourapp.py`` Python module). It also enables\nautoescaping for HTML files. This loader only requires that ``yourapp``\nis importable, it figures out the absolute path to the folder for you.\n\nDifferent loaders are available to load templates in other ways or from\nother locations. They're listed in the `Loaders`_ section below. You can\nalso write your own if you want to load templates from a source that's\nmore specialized to your project.\n\nTo load a template from this environment, call the :meth:`get_template`\nmethod, which returns the loaded :class:`Template`.\n\n.. code-block:: python\n\n    template = env.get_template(\"mytemplate.html\")\n\nTo render it with some variables, call the :meth:`render` method.\n\n.. code-block:: python\n\n    print(template.render(the=\"variables\", go=\"here\"))\n\nUsing a template loader rather than passing strings to :class:`Template`\nor :meth:`Environment.from_string` has multiple advantages.  Besides being\na lot easier to use it also enables template inheritance.\n\n.. admonition:: Notes on Autoescaping\n\n   In future versions of Jinja we might enable autoescaping by default\n   for security reasons.  As such you are encouraged to explicitly\n   configure autoescaping now instead of relying on the default.\n\n\nHigh Level API\n--------------\n\nThe high-level API is the API you will use in the application to load and\nrender Jinja templates.  The :ref:`low-level-api` on the other side is only\nuseful if you want to dig deeper into Jinja or :ref:`develop extensions\n<jinja-extensions>`.\n\n.. autoclass:: Environment([options])\n    :members: from_string, get_template, select_template,\n              get_or_select_template, join_path, extend, compile_expression,\n              compile_templates, list_templates, add_extension\n\n    .. attribute:: shared\n\n        If a template was created by using the :class:`Template` constructor\n        an environment is created automatically.  These environments are\n        created as shared environments which means that multiple templates\n        may have the same anonymous environment.  For all shared environments\n        this attribute is `True`, else `False`.\n\n    .. attribute:: sandboxed\n\n        If the environment is sandboxed this attribute is `True`.  For the\n        sandbox mode have a look at the documentation for the\n        :class:`~jinja2.sandbox.SandboxedEnvironment`.\n\n    .. attribute:: filters\n\n        A dict of filters for this environment.  As long as no template was\n        loaded it's safe to add new filters or remove old.  For custom filters\n        see :ref:`writing-filters`.  For valid filter names have a look at\n        :ref:`identifier-naming`.\n\n    .. attribute:: tests\n\n        A dict of test functions for this environment.  As long as no\n        template was loaded it's safe to modify this dict.  For custom tests\n        see :ref:`writing-tests`.  For valid test names have a look at\n        :ref:`identifier-naming`.\n\n    .. attribute:: globals\n\n        A dict of variables that are available in every template loaded\n        by the environment. As long as no template was loaded it's safe\n        to modify this. For more details see :ref:`global-namespace`.\n        For valid object names see :ref:`identifier-naming`.\n\n    .. attribute:: policies\n\n        A dictionary with :ref:`policies`.  These can be reconfigured to\n        change the runtime behavior or certain template features.  Usually\n        these are security related.\n\n    .. attribute:: code_generator_class\n\n       The class used for code generation.  This should not be changed\n       in most cases, unless you need to modify the Python code a\n       template compiles to.\n\n    .. attribute:: context_class\n\n       The context used for templates.  This should not be changed\n       in most cases, unless you need to modify internals of how\n       template variables are handled.  For details, see\n       :class:`~jinja2.runtime.Context`.\n\n    .. automethod:: overlay([options])\n\n    .. method:: undefined([hint, obj, name, exc])\n\n        Creates a new :class:`Undefined` object for `name`.  This is useful\n        for filters or functions that may return undefined objects for\n        some operations.  All parameters except of `hint` should be provided\n        as keyword parameters for better readability.  The `hint` is used as\n        error message for the exception if provided, otherwise the error\n        message will be generated from `obj` and `name` automatically.  The exception\n        provided as `exc` is raised if something with the generated undefined\n        object is done that the undefined object does not allow.  The default\n        exception is :exc:`UndefinedError`.  If a `hint` is provided the\n        `name` may be omitted.\n\n        The most common way to create an undefined object is by providing\n        a name only::\n\n            return environment.undefined(name='some_name')\n\n        This means that the name `some_name` is not defined.  If the name\n        was from an attribute of an object it makes sense to tell the\n        undefined object the holder object to improve the error message::\n\n            if not hasattr(obj, 'attr'):\n                return environment.undefined(obj=obj, name='attr')\n\n        For a more complex example you can provide a hint.  For example\n        the :func:`first` filter creates an undefined object that way::\n\n            return environment.undefined('no first item, sequence was empty')\n\n        If it the `name` or `obj` is known (for example because an attribute\n        was accessed) it should be passed to the undefined object, even if\n        a custom `hint` is provided.  This gives undefined objects the\n        possibility to enhance the error message.\n\n.. autoclass:: Template\n    :members: module, make_module\n\n    .. attribute:: globals\n\n        A dict of variables that are available every time the template\n        is rendered, without needing to pass them during render. This\n        should not be modified, as depending on how the template was\n        loaded it may be shared with the environment and other\n        templates.\n\n        Defaults to :attr:`Environment.globals` unless extra values are\n        passed to :meth:`Environment.get_template`.\n\n        Globals are only intended for data that is common to every\n        render of the template. Specific data should be passed to\n        :meth:`render`.\n\n        See :ref:`global-namespace`.\n\n    .. attribute:: name\n\n        The loading name of the template.  If the template was loaded from a\n        string this is `None`.\n\n    .. attribute:: filename\n\n        The filename of the template on the file system if it was loaded from\n        there.  Otherwise this is `None`.\n\n    .. automethod:: render([context])\n\n    .. automethod:: generate([context])\n\n    .. automethod:: stream([context])\n\n    .. automethod:: render_async([context])\n\n    .. automethod:: generate_async([context])\n\n\n.. autoclass:: jinja2.environment.TemplateStream()\n    :members: disable_buffering, enable_buffering, dump\n\n\nAutoescaping\n------------\n\n.. versionchanged:: 2.4\n\nJinja now comes with autoescaping support.  As of Jinja 2.9 the\nautoescape extension is removed and built-in.  However autoescaping is\nnot yet enabled by default though this will most likely change in the\nfuture.  It's recommended to configure a sensible default for\nautoescaping.  This makes it possible to enable and disable autoescaping\non a per-template basis (HTML versus text for instance).\n\n.. autofunction:: jinja2.select_autoescape\n\nHere a recommended setup that enables autoescaping for templates ending\nin ``'.html'``, ``'.htm'`` and ``'.xml'`` and disabling it by default\nfor all other extensions.  You can use the :func:`~jinja2.select_autoescape`\nfunction for this::\n\n    from jinja2 import Environment, PackageLoader, select_autoescape\n    env = Environment(autoescape=select_autoescape(['html', 'htm', 'xml']),\n                      loader=PackageLoader('mypackage'))\n\nThe :func:`~jinja.select_autoescape` function returns a function that\nworks roughly like this::\n\n    def autoescape(template_name):\n        if template_name is None:\n            return False\n        if template_name.endswith(('.html', '.htm', '.xml'))\n\nWhen implementing a guessing autoescape function, make sure you also\naccept `None` as valid template name.  This will be passed when generating\ntemplates from strings.  You should always configure autoescaping as\ndefaults in the future might change.\n\nInside the templates the behaviour can be temporarily changed by using\nthe `autoescape` block (see :ref:`autoescape-overrides`).\n\n\n.. _identifier-naming:\n\nNotes on Identifiers\n--------------------\n\nJinja uses Python naming rules. Valid identifiers can be any combination\nof characters accepted by Python.\n\nFilters and tests are looked up in separate namespaces and have slightly\nmodified identifier syntax.  Filters and tests may contain dots to group\nfilters and tests by topic.  For example it's perfectly valid to add a\nfunction into the filter dict and call it `to.str`.  The regular\nexpression for filter and test identifiers is\n``[a-zA-Z_][a-zA-Z0-9_]*(\\.[a-zA-Z_][a-zA-Z0-9_]*)*``.\n\n\nUndefined Types\n---------------\n\nThese classes can be used as undefined types.  The :class:`Environment`\nconstructor takes an `undefined` parameter that can be one of those classes\nor a custom subclass of :class:`Undefined`.  Whenever the template engine is\nunable to look up a name or access an attribute one of those objects is\ncreated and returned.  Some operations on undefined values are then allowed,\nothers fail.\n\nThe closest to regular Python behavior is the :class:`StrictUndefined` which\ndisallows all operations beside testing if it's an undefined object.\n\n.. autoclass:: jinja2.Undefined()\n\n    .. attribute:: _undefined_hint\n\n        Either `None` or a string with the error message for the\n        undefined object.\n\n    .. attribute:: _undefined_obj\n\n        Either `None` or the owner object that caused the undefined object\n        to be created (for example because an attribute does not exist).\n\n    .. attribute:: _undefined_name\n\n        The name for the undefined variable / attribute or just `None`\n        if no such information exists.\n\n    .. attribute:: _undefined_exception\n\n        The exception that the undefined object wants to raise.  This\n        is usually one of :exc:`UndefinedError` or :exc:`SecurityError`.\n\n    .. method:: _fail_with_undefined_error(\\*args, \\**kwargs)\n\n        When called with any arguments this method raises\n        :attr:`_undefined_exception` with an error message generated\n        from the undefined hints stored on the undefined object.\n\n.. autoclass:: jinja2.ChainableUndefined()\n\n.. autoclass:: jinja2.DebugUndefined()\n\n.. autoclass:: jinja2.StrictUndefined()\n\nThere is also a factory function that can decorate undefined objects to\nimplement logging on failures:\n\n.. autofunction:: jinja2.make_logging_undefined\n\nUndefined objects are created by calling :attr:`undefined`.\n\n.. admonition:: Implementation\n\n    :class:`Undefined` is implemented by overriding the special\n    ``__underscore__`` methods. For example the default\n    :class:`Undefined` class implements ``__str__`` to returns an empty\n    string, while ``__int__`` and others fail with an exception. To\n    allow conversion to int by returning ``0`` you can implement your\n    own subclass.\n\n    .. code-block:: python\n\n        class NullUndefined(Undefined):\n            def __int__(self):\n                return 0\n\n            def __float__(self):\n                return 0.0\n\n    To disallow a method, override it and raise\n    :attr:`~Undefined._undefined_exception`.  Because this is very\n    common there is the helper method\n    :meth:`~Undefined._fail_with_undefined_error` that raises the error\n    with the correct information. Here's a class that works like the\n    regular :class:`Undefined` but fails on iteration::\n\n        class NonIterableUndefined(Undefined):\n            def __iter__(self):\n                self._fail_with_undefined_error()\n\n\nThe Context\n-----------\n\n.. autoclass:: jinja2.runtime.Context()\n    :members: get, resolve, resolve_or_missing, get_exported, get_all\n\n    .. attribute:: parent\n\n        A dict of read only, global variables the template looks up.  These\n        can either come from another :class:`Context`, from the\n        :attr:`Environment.globals` or :attr:`Template.globals` or points\n        to a dict created by combining the globals with the variables\n        passed to the render function.  It must not be altered.\n\n    .. attribute:: vars\n\n        The template local variables.  This list contains environment and\n        context functions from the :attr:`parent` scope as well as local\n        modifications and exported variables from the template.  The template\n        will modify this dict during template evaluation but filters and\n        context functions are not allowed to modify it.\n\n    .. attribute:: environment\n\n        The environment that loaded the template.\n\n    .. attribute:: exported_vars\n\n        This set contains all the names the template exports.  The values for\n        the names are in the :attr:`vars` dict.  In order to get a copy of the\n        exported variables as dict, :meth:`get_exported` can be used.\n\n    .. attribute:: name\n\n        The load name of the template owning this context.\n\n    .. attribute:: blocks\n\n        A dict with the current mapping of blocks in the template.  The keys\n        in this dict are the names of the blocks, and the values a list of\n        blocks registered.  The last item in each list is the current active\n        block (latest in the inheritance chain).\n\n    .. attribute:: eval_ctx\n\n        The current :ref:`eval-context`.\n\n    .. automethod:: jinja2.runtime.Context.call(callable, \\*args, \\**kwargs)\n\n\nThe context is immutable, it prevents modifications, and if it is\nmodified somehow despite that those changes may not show up. For\nperformance, Jinja does not use the context as data storage for, only as\na primary data source. Variables that the template does not define are\nlooked up in the context, but variables the template does define are\nstored locally.\n\nInstead of modifying the context directly, a function should return\na value that can be assigned to a variable within the template itself.\n\n.. code-block:: jinja\n\n    {% set comments = get_latest_comments() %}\n\n\n.. _loaders:\n\nLoaders\n-------\n\nLoaders are responsible for loading templates from a resource such as the\nfile system.  The environment will keep the compiled modules in memory like\nPython's `sys.modules`.  Unlike `sys.modules` however this cache is limited in\nsize by default and templates are automatically reloaded.\nAll loaders are subclasses of :class:`BaseLoader`.  If you want to create your\nown loader, subclass :class:`BaseLoader` and override `get_source`.\n\n.. autoclass:: jinja2.BaseLoader\n    :members: get_source, load\n\nHere a list of the builtin loaders Jinja provides:\n\n.. autoclass:: jinja2.FileSystemLoader\n\n.. autoclass:: jinja2.PackageLoader\n\n.. autoclass:: jinja2.DictLoader\n\n.. autoclass:: jinja2.FunctionLoader\n\n.. autoclass:: jinja2.PrefixLoader\n\n.. autoclass:: jinja2.ChoiceLoader\n\n.. autoclass:: jinja2.ModuleLoader\n\n\n.. _bytecode-cache:\n\nBytecode Cache\n--------------\n\nJinja 2.1 and higher support external bytecode caching.  Bytecode caches make\nit possible to store the generated bytecode on the file system or a different\nlocation to avoid parsing the templates on first use.\n\nThis is especially useful if you have a web application that is initialized on\nthe first request and Jinja compiles many templates at once which slows down\nthe application.\n\nTo use a bytecode cache, instantiate it and pass it to the :class:`Environment`.\n\n.. autoclass:: jinja2.BytecodeCache\n    :members: load_bytecode, dump_bytecode, clear\n\n.. autoclass:: jinja2.bccache.Bucket\n    :members: write_bytecode, load_bytecode, bytecode_from_string,\n              bytecode_to_string, reset\n\n    .. attribute:: environment\n\n        The :class:`Environment` that created the bucket.\n\n    .. attribute:: key\n\n        The unique cache key for this bucket\n\n    .. attribute:: code\n\n        The bytecode if it's loaded, otherwise `None`.\n\n\nBuiltin bytecode caches:\n\n.. autoclass:: jinja2.FileSystemBytecodeCache\n\n.. autoclass:: jinja2.MemcachedBytecodeCache\n\n\nAsync Support\n-------------\n\n.. versionadded:: 2.9\n\nJinja supports the Python ``async`` and ``await`` syntax. For the\ntemplate designer, this support (when enabled) is entirely transparent,\ntemplates continue to look exactly the same. However, developers should\nbe aware of the implementation as it affects what types of APIs you can\nuse.\n\nBy default, async support is disabled. Enabling it will cause the\nenvironment to compile different code behind the scenes in order to\nhandle async and sync code in an asyncio event loop. This has the\nfollowing implications:\n\n-   The compiled code uses ``await`` for functions and attributes, and\n    uses ``async for`` loops. In order to support using both async and\n    sync functions in this context, a small wrapper is placed around\n    all calls and access, which adds overhead compared to purely async\n    code.\n-   Sync methods and filters become wrappers around their corresponding\n    async implementations where needed. For example, ``render`` invokes\n    ``async_render``, and ``|map`` supports async iterables.\n\nAwaitable objects can be returned from functions in templates and any\nfunction call in a template will automatically await the result. The\n``await`` you would normally add in Python is implied. For example, you\ncan provide a method that asynchronously loads data from a database, and\nfrom the template designer's point of view it can be called like any\nother function.\n\n\n.. _policies:\n\nPolicies\n--------\n\nStarting with Jinja 2.9 policies can be configured on the environment\nwhich can slightly influence how filters and other template constructs\nbehave.  They can be configured with the\n:attr:`~jinja2.Environment.policies` attribute.\n\nExample::\n\n    env.policies['urlize.rel'] = 'nofollow noopener'\n\n``truncate.leeway``:\n    Configures the leeway default for the `truncate` filter.  Leeway as\n    introduced in 2.9 but to restore compatibility with older templates\n    it can be configured to `0` to get the old behavior back.  The default\n    is `5`.\n\n``urlize.rel``:\n    A string that defines the items for the `rel` attribute of generated\n    links with the `urlize` filter.  These items are always added.  The\n    default is `noopener`.\n\n``urlize.target``:\n    The default target that is issued for links from the `urlize` filter\n    if no other target is defined by the call explicitly.\n\n``urlize.extra_schemes``:\n    Recognize URLs that start with these schemes in addition to the\n    default ``http://``, ``https://``, and ``mailto:``.\n\n``json.dumps_function``:\n    If this is set to a value other than `None` then the `tojson` filter\n    will dump with this function instead of the default one.  Note that\n    this function should accept arbitrary extra arguments which might be\n    passed in the future from the filter.  Currently the only argument\n    that might be passed is `indent`.  The default dump function is\n    ``json.dumps``.\n\n``json.dumps_kwargs``:\n    Keyword arguments to be passed to the dump function.  The default is\n    ``{'sort_keys': True}``.\n\n.. _ext-i18n-trimmed:\n\n``ext.i18n.trimmed``:\n    If this is set to `True`, ``{% trans %}`` blocks of the\n    :ref:`i18n-extension` will always unify linebreaks and surrounding\n    whitespace as if the `trimmed` modifier was used.\n\n\nUtilities\n---------\n\nThese helper functions and classes are useful if you add custom filters or\nfunctions to a Jinja environment.\n\n.. autofunction:: jinja2.pass_context\n\n.. autofunction:: jinja2.pass_eval_context\n\n.. autofunction:: jinja2.pass_environment\n\n.. autofunction:: jinja2.clear_caches\n\n.. autofunction:: jinja2.is_undefined\n\n\nExceptions\n----------\n\n.. autoexception:: jinja2.TemplateError\n\n.. autoexception:: jinja2.UndefinedError\n\n.. autoexception:: jinja2.TemplateNotFound\n\n.. autoexception:: jinja2.TemplatesNotFound\n\n.. autoexception:: jinja2.TemplateSyntaxError\n\n    .. attribute:: message\n\n        The error message.\n\n    .. attribute:: lineno\n\n        The line number where the error occurred.\n\n    .. attribute:: name\n\n        The load name for the template.\n\n    .. attribute:: filename\n\n        The filename that loaded the template in the encoding of the\n        file system (most likely utf-8, or mbcs on Windows systems).\n\n.. autoexception:: jinja2.TemplateRuntimeError\n\n.. autoexception:: jinja2.TemplateAssertionError\n\n\n.. _writing-filters:\n\nCustom Filters\n--------------\n\nFilters are Python functions that take the value to the left of the\nfilter as the first argument and produce a new value. Arguments passed\nto the filter are passed after the value.\n\nFor example, the filter ``{{ 42|myfilter(23) }}`` is called behind the\nscenes as ``myfilter(42, 23)``.\n\nJinja comes with some :ref:`built-in filters <builtin-filters>`. To use\na custom filter, write a function that takes at least a ``value``\nargument, then register it in :attr:`Environment.filters`.\n\nHere's a filter that formats datetime objects:\n\n.. code-block:: python\n\n    def datetime_format(value, format=\"%H:%M %d-%m-%y\"):\n        return value.strftime(format)\n\n    environment.filters[\"datetime_format\"] = datetime_format\n\nNow it can be used in templates:\n\n.. sourcecode:: jinja\n\n    {{ article.pub_date|datetime_format }}\n    {{ article.pub_date|datetime_format(\"%B %Y\") }}\n\nSome decorators are available to tell Jinja to pass extra information to\nthe filter. The object is passed as the first argument, making the value\nbeing filtered the second argument.\n\n-   :func:`pass_environment` passes the :class:`Environment`.\n-   :func:`pass_eval_context` passes the :ref:`eval-context`.\n-   :func:`pass_context` passes the current\n    :class:`~jinja2.runtime.Context`.\n\nHere's a filter that converts line breaks into HTML ``<br>`` and ``<p>``\ntags. It uses the eval context to check if autoescape is currently\nenabled before escaping the input and marking the output safe.\n\n.. code-block:: python\n\n    import re\n    from jinja2 import pass_eval_context\n    from markupsafe import Markup, escape\n\n    @pass_eval_context\n    def nl2br(eval_ctx, value):\n        br = \"<br>\\n\"\n\n        if eval_ctx.autoescape:\n            value = escape(value)\n            br = Markup(br)\n\n        result = \"\\n\\n\".join(\n            f\"<p>{br.join(p.splitlines())}</p>\"\n            for p in re.split(r\"(?:\\r\\n|\\r(?!\\n)|\\n){2,}\", value)\n        )\n        return Markup(result) if eval_ctx.autoescape else result\n\n\n.. _writing-tests:\n\nCustom Tests\n------------\n\nTest are Python functions that take the value to the left of the test as\nthe first argument, and return ``True`` or ``False``. Arguments passed\nto the test are passed after the value.\n\nFor example, the test ``{{ 42 is even }}`` is called behind the scenes\nas ``is_even(42)``.\n\nJinja comes with some :ref:`built-in tests <builtin-tests>`. To use a\ncustom tests, write a function that takes at least a ``value`` argument,\nthen register it in :attr:`Environment.tests`.\n\nHere's a test that checks if a value is a prime number:\n\n.. code-block:: python\n\n    import math\n\n    def is_prime(n):\n        if n == 2:\n            return True\n\n        for i in range(2, int(math.ceil(math.sqrt(n))) + 1):\n            if n % i == 0:\n                return False\n\n        return True\n\n    environment.tests[\"prime\"] = is_prime\n\nNow it can be used in templates:\n\n.. sourcecode:: jinja\n\n    {% if value is prime %}\n        {{ value }} is a prime number\n    {% else %}\n        {{ value }} is not a prime number\n    {% endif %}\n\nSome decorators are available to tell Jinja to pass extra information to\nthe test. The object is passed as the first argument, making the value\nbeing tested the second argument.\n\n-   :func:`pass_environment` passes the :class:`Environment`.\n-   :func:`pass_eval_context` passes the :ref:`eval-context`.\n-   :func:`pass_context` passes the current\n    :class:`~jinja2.runtime.Context`.\n\n\n.. _eval-context:\n\nEvaluation Context\n------------------\n\nThe evaluation context (short eval context or eval ctx) makes it\npossible to activate and deactivate compiled features at runtime.\n\nCurrently it is only used to enable and disable automatic escaping, but\nit can be used by extensions as well.\n\nThe ``autoescape`` setting should be checked on the evaluation context,\nnot the environment. The evaluation context will have the computed value\nfor the current template.\n\nInstead of ``pass_environment``:\n\n.. code-block:: python\n\n    @pass_environment\n    def filter(env, value):\n        result = do_something(value)\n\n        if env.autoescape:\n            result = Markup(result)\n\n        return result\n\nUse ``pass_eval_context`` if you only need the setting:\n\n.. code-block:: python\n\n    @pass_eval_context\n    def filter(eval_ctx, value):\n        result = do_something(value)\n\n        if eval_ctx.autoescape:\n            result = Markup(result)\n\n        return result\n\nOr use ``pass_context`` if you need other context behavior as well:\n\n.. code-block:: python\n\n    @pass_context\n    def filter(context, value):\n        result = do_something(value)\n\n        if context.eval_ctx.autoescape:\n            result = Markup(result)\n\n        return result\n\nThe evaluation context must not be modified at runtime.  Modifications\nmust only happen with a :class:`nodes.EvalContextModifier` and\n:class:`nodes.ScopedEvalContextModifier` from an extension, not on the\neval context object itself.\n\n.. autoclass:: jinja2.nodes.EvalContext\n\n   .. attribute:: autoescape\n\n      `True` or `False` depending on if autoescaping is active or not.\n\n   .. attribute:: volatile\n\n      `True` if the compiler cannot evaluate some expressions at compile\n      time.  At runtime this should always be `False`.\n\n\n.. _global-namespace:\n\nThe Global Namespace\n--------------------\n\nThe global namespace stores variables and functions that should be\navailable without needing to pass them to :meth:`Template.render`. They\nare also available to templates that are imported or included without\ncontext. Most applications should only use :attr:`Environment.globals`.\n\n:attr:`Environment.globals` are intended for data that is common to all\ntemplates loaded by that environment. :attr:`Template.globals` are\nintended for data that is common to all renders of that template, and\ndefault to :attr:`Environment.globals` unless they're given in\n:meth:`Environment.get_template`, etc. Data that is specific to a\nrender should be passed as context to :meth:`Template.render`.\n\nOnly one set of globals is used during any specific rendering. If\ntemplates A and B both have template globals, and B extends A, then\nonly B's globals are used for both when using ``b.render()``.\n\nEnvironment globals should not be changed after loading any templates,\nand template globals should not be changed at any time after loading the\ntemplate. Changing globals after loading a template will result in\nunexpected behavior as they may be shared between the environment and\nother templates.\n\n\n.. _low-level-api:\n\nLow Level API\n-------------\n\nThe low level API exposes functionality that can be useful to understand some\nimplementation details, debugging purposes or advanced :ref:`extension\n<jinja-extensions>` techniques.  Unless you know exactly what you are doing we\ndon't recommend using any of those.\n\n.. automethod:: Environment.lex\n\n.. automethod:: Environment.parse\n\n.. automethod:: Environment.preprocess\n\n.. automethod:: Template.new_context\n\n.. method:: Template.root_render_func(context)\n\n    This is the low level render function.  It's passed a :class:`Context`\n    that has to be created by :meth:`new_context` of the same template or\n    a compatible template.  This render function is generated by the\n    compiler from the template code and returns a generator that yields\n    strings.\n\n    If an exception in the template code happens the template engine will\n    not rewrite the exception but pass through the original one.  As a\n    matter of fact this function should only be called from within a\n    :meth:`render` / :meth:`generate` / :meth:`stream` call.\n\n.. attribute:: Template.blocks\n\n    A dict of block render functions.  Each of these functions works exactly\n    like the :meth:`root_render_func` with the same limitations.\n\n.. attribute:: Template.is_up_to_date\n\n    This attribute is `False` if there is a newer version of the template\n    available, otherwise `True`.\n\n.. admonition:: Note\n\n    The low-level API is fragile.  Future Jinja versions will try not to\n    change it in a backwards incompatible way but modifications in the Jinja\n    core may shine through.  For example if Jinja introduces a new AST node\n    in later versions that may be returned by :meth:`~Environment.parse`.\n\nThe Meta API\n------------\n\n.. versionadded:: 2.2\n\nThe meta API returns some information about abstract syntax trees that\ncould help applications to implement more advanced template concepts.  All\nthe functions of the meta API operate on an abstract syntax tree as\nreturned by the :meth:`Environment.parse` method.\n\n.. autofunction:: jinja2.meta.find_undeclared_variables\n\n.. autofunction:: jinja2.meta.find_referenced_templates\n"
  },
  {
    "path": "docs/changes.rst",
    "content": "Changes\n=======\n\n.. include:: ../CHANGES.rst\n"
  },
  {
    "path": "docs/conf.py",
    "content": "from pallets_sphinx_themes import get_version\nfrom pallets_sphinx_themes import ProjectLink\n\n# Project --------------------------------------------------------------\n\nproject = \"Jinja\"\ncopyright = \"2007 Pallets\"\nauthor = \"Pallets\"\nrelease, version = get_version(\"Jinja2\")\n\n# General --------------------------------------------------------------\n\ndefault_role = \"code\"\nextensions = [\n    \"sphinx.ext.autodoc\",\n    \"sphinx.ext.extlinks\",\n    \"sphinx.ext.intersphinx\",\n    \"sphinxcontrib.log_cabinet\",\n    \"pallets_sphinx_themes\",\n]\nautodoc_member_order = \"bysource\"\nautodoc_typehints = \"description\"\nautodoc_preserve_defaults = True\nextlinks = {\n    \"issue\": (\"https://github.com/pallets/jinja/issues/%s\", \"#%s\"),\n    \"pr\": (\"https://github.com/pallets/jinja/pull/%s\", \"#%s\"),\n    \"ghsa\": (\"https://github.com/pallets/jinja/security/advisories/GHSA-%s\", \"GHSA-%s\"),\n}\nintersphinx_mapping = {\n    \"python\": (\"https://docs.python.org/3/\", None),\n}\n\n# HTML -----------------------------------------------------------------\n\nhtml_theme = \"jinja\"\nhtml_theme_options = {\"index_sidebar_logo\": False}\nhtml_context = {\n    \"project_links\": [\n        ProjectLink(\"Donate\", \"https://palletsprojects.com/donate\"),\n        ProjectLink(\"PyPI Releases\", \"https://pypi.org/project/Jinja2/\"),\n        ProjectLink(\"Source Code\", \"https://github.com/pallets/jinja/\"),\n        ProjectLink(\"Issue Tracker\", \"https://github.com/pallets/jinja/issues/\"),\n        ProjectLink(\"Chat\", \"https://discord.gg/pallets\"),\n    ]\n}\nhtml_sidebars = {\n    \"index\": [\"project.html\", \"localtoc.html\", \"searchbox.html\", \"ethicalads.html\"],\n    \"**\": [\"localtoc.html\", \"relations.html\", \"searchbox.html\", \"ethicalads.html\"],\n}\nsinglehtml_sidebars = {\"index\": [\"project.html\", \"localtoc.html\", \"ethicalads.html\"]}\nhtml_static_path = [\"_static\"]\nhtml_favicon = \"_static/jinja-icon.svg\"\nhtml_logo = \"_static/jinja-logo.svg\"\nhtml_title = f\"Jinja Documentation ({version})\"\nhtml_show_sourcelink = False\n"
  },
  {
    "path": "docs/examples/cache_extension.py",
    "content": "from jinja2 import nodes\nfrom jinja2.ext import Extension\n\n\nclass FragmentCacheExtension(Extension):\n    # a set of names that trigger the extension.\n    tags = {\"cache\"}\n\n    def __init__(self, environment):\n        super().__init__(environment)\n\n        # add the defaults to the environment\n        environment.extend(fragment_cache_prefix=\"\", fragment_cache=None)\n\n    def parse(self, parser):\n        # the first token is the token that started the tag.  In our case\n        # we only listen to ``'cache'`` so this will be a name token with\n        # `cache` as value.  We get the line number so that we can give\n        # that line number to the nodes we create by hand.\n        lineno = next(parser.stream).lineno\n\n        # now we parse a single expression that is used as cache key.\n        args = [parser.parse_expression()]\n\n        # if there is a comma, the user provided a timeout.  If not use\n        # None as second parameter.\n        if parser.stream.skip_if(\"comma\"):\n            args.append(parser.parse_expression())\n        else:\n            args.append(nodes.Const(None))\n\n        # now we parse the body of the cache block up to `endcache` and\n        # drop the needle (which would always be `endcache` in that case)\n        body = parser.parse_statements([\"name:endcache\"], drop_needle=True)\n\n        # now return a `CallBlock` node that calls our _cache_support\n        # helper method on this extension.\n        return nodes.CallBlock(\n            self.call_method(\"_cache_support\", args), [], [], body\n        ).set_lineno(lineno)\n\n    def _cache_support(self, name, timeout, caller):\n        \"\"\"Helper callback.\"\"\"\n        key = self.environment.fragment_cache_prefix + name\n\n        # try to load the block from the cache\n        # if there is no fragment in the cache, render it and store\n        # it in the cache.\n        rv = self.environment.fragment_cache.get(key)\n        if rv is not None:\n            return rv\n        rv = caller()\n        self.environment.fragment_cache.add(key, rv, timeout)\n        return rv\n"
  },
  {
    "path": "docs/examples/inline_gettext_extension.py",
    "content": "import re\n\nfrom jinja2.exceptions import TemplateSyntaxError\nfrom jinja2.ext import Extension\nfrom jinja2.lexer import count_newlines\nfrom jinja2.lexer import Token\n\n_outside_re = re.compile(r\"\\\\?(gettext|_)\\(\")\n_inside_re = re.compile(r\"\\\\?[()]\")\n\n\nclass InlineGettext(Extension):\n    \"\"\"This extension implements support for inline gettext blocks::\n\n        <h1>_(Welcome)</h1>\n        <p>_(This is a paragraph)</p>\n\n    Requires the i18n extension to be loaded and configured.\n    \"\"\"\n\n    def filter_stream(self, stream):\n        paren_stack = 0\n\n        for token in stream:\n            if token.type != \"data\":\n                yield token\n                continue\n\n            pos = 0\n            lineno = token.lineno\n\n            while True:\n                if not paren_stack:\n                    match = _outside_re.search(token.value, pos)\n                else:\n                    match = _inside_re.search(token.value, pos)\n                if match is None:\n                    break\n                new_pos = match.start()\n                if new_pos > pos:\n                    preval = token.value[pos:new_pos]\n                    yield Token(lineno, \"data\", preval)\n                    lineno += count_newlines(preval)\n                gtok = match.group()\n                if gtok[0] == \"\\\\\":\n                    yield Token(lineno, \"data\", gtok[1:])\n                elif not paren_stack:\n                    yield Token(lineno, \"block_begin\", None)\n                    yield Token(lineno, \"name\", \"trans\")\n                    yield Token(lineno, \"block_end\", None)\n                    paren_stack = 1\n                else:\n                    if gtok == \"(\" or paren_stack > 1:\n                        yield Token(lineno, \"data\", gtok)\n                    paren_stack += -1 if gtok == \")\" else 1\n                    if not paren_stack:\n                        yield Token(lineno, \"block_begin\", None)\n                        yield Token(lineno, \"name\", \"endtrans\")\n                        yield Token(lineno, \"block_end\", None)\n                pos = match.end()\n\n            if pos < len(token.value):\n                yield Token(lineno, \"data\", token.value[pos:])\n\n        if paren_stack:\n            raise TemplateSyntaxError(\n                \"unclosed gettext expression\",\n                token.lineno,\n                stream.name,\n                stream.filename,\n            )\n"
  },
  {
    "path": "docs/extensions.rst",
    "content": ".. _jinja-extensions:\n\nExtensions\n==========\n\nJinja supports extensions that can add extra filters, tests, globals or even\nextend the parser.  The main motivation of extensions is to move often used\ncode into a reusable class like adding support for internationalization.\n\n\nAdding Extensions\n-----------------\n\nExtensions are added to the Jinja environment at creation time.  To add an\nextension pass a list of extension classes or import paths to the\n``extensions`` parameter of the :class:`~jinja2.Environment` constructor.  The following\nexample creates a Jinja environment with the i18n extension loaded::\n\n    jinja_env = Environment(extensions=['jinja2.ext.i18n'])\n\nTo add extensions after creation time, use the :meth:`~jinja2.Environment.add_extension` method::\n\n    jinja_env.add_extension('jinja2.ext.debug')\n\n\n.. _i18n-extension:\n\ni18n Extension\n--------------\n\n**Import name:** ``jinja2.ext.i18n``\n\nThe i18n extension can be used in combination with `gettext`_ or\n`Babel`_.  When it's enabled, Jinja provides a ``trans`` statement that\nmarks a block as translatable and calls ``gettext``.\n\nAfter enabling, an application has to provide functions for ``gettext``,\n``ngettext``, and optionally ``pgettext`` and ``npgettext``, either\nglobally or when rendering. A ``_()`` function is added as an alias to\nthe ``gettext`` function.\n\nA convenient way to provide these functions is to call one of the below\nmethods depending on the translation system in use. If you do not require\nactual translation, use ``Environment.install_null_translations`` to\ninstall no-op functions.\n\nEnvironment Methods\n~~~~~~~~~~~~~~~~~~~\n\nAfter enabling the extension, the environment provides the following\nadditional methods:\n\n.. method:: jinja2.Environment.install_gettext_translations(translations, newstyle=False)\n\n    Installs a translation globally for the environment. The\n    ``translations`` object must implement ``gettext``, ``ngettext``,\n    and optionally ``pgettext`` and ``npgettext``.\n    :class:`gettext.NullTranslations`, :class:`gettext.GNUTranslations`,\n    and `Babel`_\\s ``Translations`` are supported.\n\n    .. versionchanged:: 3.0\n        Added ``pgettext`` and ``npgettext``.\n\n    .. versionchanged:: 2.5\n        Added new-style gettext support.\n\n.. method:: jinja2.Environment.install_null_translations(newstyle=False)\n\n    Install no-op gettext functions. This is useful if you want to\n    prepare the application for internationalization but don't want to\n    implement the full system yet.\n\n    .. versionchanged:: 2.5 Added new-style gettext support.\n\n.. method:: jinja2.Environment.install_gettext_callables(gettext, ngettext, newstyle=False, pgettext=None, npgettext=None)\n\n    Install the given ``gettext``, ``ngettext``, ``pgettext``, and\n    ``npgettext`` callables into the environment. They should behave\n    exactly like :func:`gettext.gettext`, :func:`gettext.ngettext`,\n    :func:`gettext.pgettext` and :func:`gettext.npgettext`.\n\n    If ``newstyle`` is activated, the callables are wrapped to work like\n    newstyle callables.  See :ref:`newstyle-gettext` for more information.\n\n    .. versionchanged:: 3.0\n        Added ``pgettext`` and ``npgettext``.\n\n    .. versionadded:: 2.5\n        Added new-style gettext support.\n\n.. method:: jinja2.Environment.uninstall_gettext_translations()\n\n    Uninstall the environment's globally installed translation.\n\n.. method:: jinja2.Environment.extract_translations(source)\n\n    Extract localizable strings from the given template node or source.\n\n    For every string found this function yields a ``(lineno, function,\n    message)`` tuple, where:\n\n    -   ``lineno`` is the number of the line on which the string was\n        found.\n    -   ``function`` is the name of the ``gettext`` function used (if\n        the string was extracted from embedded Python code).\n    -   ``message`` is the string itself, or a tuple of strings for\n        functions with multiple arguments.\n\n    If `Babel`_ is installed, see :ref:`babel-integration` to extract\n    the strings.\n\nFor a web application that is available in multiple languages but gives\nall the users the same language (for example, multilingual forum\nsoftware installed for a French community), the translation may be\ninstalled when the environment is created.\n\n.. code-block:: python\n\n    translations = get_gettext_translations()\n    env = Environment(extensions=[\"jinja2.ext.i18n\"])\n    env.install_gettext_translations(translations)\n\nThe ``get_gettext_translations`` function would return the translator\nfor the current configuration, for example by using ``gettext.find``.\n\nThe usage of the ``i18n`` extension for template designers is covered in\n:ref:`the template documentation <i18n-in-templates>`.\n\n.. _gettext: https://docs.python.org/3/library/gettext.html\n.. _Babel: https://babel.pocoo.org/\n\n\nWhitespace Trimming\n~~~~~~~~~~~~~~~~~~~\n\n.. versionadded:: 2.10\n\nWithin ``{% trans %}`` blocks, it can be useful to trim line breaks and\nwhitespace so that the block of text looks like a simple string with\nsingle spaces in the translation file.\n\nLinebreaks and surrounding whitespace can be automatically trimmed by\nenabling the ``ext.i18n.trimmed`` :ref:`policy <ext-i18n-trimmed>`.\n\n\n.. _newstyle-gettext:\n\nNew Style Gettext\n~~~~~~~~~~~~~~~~~\n\n.. versionadded:: 2.5\n\nNew style gettext calls are less to type, less error prone, and support\nautoescaping better.\n\nYou can use \"new style\" gettext calls by setting\n``env.newstyle_gettext = True`` or passing ``newstyle=True`` to\n``env.install_translations``. They are fully supported by the Babel\nextraction tool, but might not work as expected with other extraction\ntools.\n\nWith standard ``gettext`` calls, string formatting is a separate step\ndone with the ``|format`` filter. This requires duplicating work for\n``ngettext`` calls.\n\n.. sourcecode:: jinja\n\n    {{ gettext(\"Hello, World!\") }}\n    {{ gettext(\"Hello, %(name)s!\")|format(name=name) }}\n    {{ ngettext(\n           \"%(num)d apple\", \"%(num)d apples\", apples|count\n       )|format(num=apples|count) }}\n    {{ pgettext(\"greeting\", \"Hello, World!\") }}\n    {{ npgettext(\n           \"fruit\", \"%(num)d apple\", \"%(num)d apples\", apples|count\n       )|format(num=apples|count) }}\n\nNew style ``gettext`` make formatting part of the call, and behind the\nscenes enforce more consistency.\n\n.. sourcecode:: jinja\n\n    {{ gettext(\"Hello, World!\") }}\n    {{ gettext(\"Hello, %(name)s!\", name=name) }}\n    {{ ngettext(\"%(num)d apple\", \"%(num)d apples\", apples|count) }}\n    {{ pgettext(\"greeting\", \"Hello, World!\") }}\n    {{ npgettext(\"fruit\", \"%(num)d apple\", \"%(num)d apples\", apples|count) }}\n\nThe advantages of newstyle gettext are:\n\n-   There's no separate formatting step, you don't have to remember to\n    use the ``|format`` filter.\n-   Only named placeholders are allowed. This solves a common problem\n    translators face because positional placeholders can't switch\n    positions meaningfully. Named placeholders always carry semantic\n    information about what value goes where.\n-   String formatting is used even if no placeholders are used, which\n    makes all strings use a consistent format. Remember to escape any\n    raw percent signs as ``%%``, such as ``100%%``.\n-   The translated string is marked safe, formatting performs escaping\n    as needed. Mark a parameter as ``|safe`` if it has already been\n    escaped.\n\n\nExpression Statement\n--------------------\n\n**Import name:** ``jinja2.ext.do``\n\nThe \"do\" aka expression-statement extension adds a simple ``do`` tag to the\ntemplate engine that works like a variable expression but ignores the\nreturn value.\n\n.. _loopcontrols-extension:\n\nLoop Controls\n-------------\n\n**Import name:** ``jinja2.ext.loopcontrols``\n\nThis extension adds support for ``break`` and ``continue`` in loops.  After\nenabling, Jinja provides those two keywords which work exactly like in\nPython.\n\n.. _with-extension:\n\nWith Statement\n--------------\n\n**Import name:** ``jinja2.ext.with_``\n\n.. versionchanged:: 2.9\n\n    This extension is now built-in and no longer does anything.\n\n.. _autoescape-extension:\n\nAutoescape Extension\n--------------------\n\n**Import name:** ``jinja2.ext.autoescape``\n\n.. versionchanged:: 2.9\n\n    This extension was removed and is now built-in. Enabling the\n    extension no longer does anything.\n\n\n.. _debug-extension:\n\nDebug Extension\n---------------\n\n**Import name:** ``jinja2.ext.debug``\n\nAdds a ``{% debug %}`` tag to dump the current context as well as the\navailable filters and tests. This is useful to see what's available to\nuse in the template without setting up a debugger.\n\n\n.. _writing-extensions:\n\nWriting Extensions\n------------------\n\n.. module:: jinja2.ext\n\nBy writing extensions you can add custom tags to Jinja.  This is a non-trivial\ntask and usually not needed as the default tags and expressions cover all\ncommon use cases.  The i18n extension is a good example of why extensions are\nuseful. Another one would be fragment caching.\n\nWhen writing extensions you have to keep in mind that you are working with the\nJinja template compiler which does not validate the node tree you are passing\nto it.  If the AST is malformed you will get all kinds of compiler or runtime\nerrors that are horrible to debug.  Always make sure you are using the nodes\nyou create correctly.  The API documentation below shows which nodes exist and\nhow to use them.\n\n\nExample Extensions\n------------------\n\nCache\n~~~~~\n\nThe following example implements a ``cache`` tag for Jinja by using the\n`cachelib`_ library:\n\n.. literalinclude:: examples/cache_extension.py\n    :language: python\n\nAnd here is how you use it in an environment::\n\n    from jinja2 import Environment\n    from cachelib import SimpleCache\n\n    env = Environment(extensions=[FragmentCacheExtension])\n    env.fragment_cache = SimpleCache()\n\nInside the template it's then possible to mark blocks as cacheable.  The\nfollowing example caches a sidebar for 300 seconds:\n\n.. sourcecode:: html+jinja\n\n    {% cache 'sidebar', 300 %}\n    <div class=\"sidebar\">\n        ...\n    </div>\n    {% endcache %}\n\n.. _cachelib: https://github.com/pallets/cachelib\n\n\nInline ``gettext``\n~~~~~~~~~~~~~~~~~~\n\nThe following example demonstrates using :meth:`Extension.filter_stream`\nto parse calls to the ``_()`` gettext function inline with static data\nwithout needing Jinja blocks.\n\n.. code-block:: html\n\n        <h1>_(Welcome)</h1>\n        <p>_(This is a paragraph)</p>\n\nIt requires the i18n extension to be loaded and configured.\n\n.. literalinclude:: examples/inline_gettext_extension.py\n    :language: python\n\n\nExtension API\n-------------\n\nExtension\n~~~~~~~~~\n\nExtensions always have to extend the :class:`jinja2.ext.Extension` class:\n\n.. autoclass:: Extension\n    :members: preprocess, filter_stream, parse, attr, call_method\n\n    .. attribute:: identifier\n\n        The identifier of the extension.  This is always the true import name\n        of the extension class and must not be changed.\n\n    .. attribute:: tags\n\n        If the extension implements custom tags this is a set of tag names\n        the extension is listening for.\n\n\nParser\n~~~~~~\n\nThe parser passed to :meth:`Extension.parse` provides ways to parse\nexpressions of different types.  The following methods may be used by\nextensions:\n\n.. autoclass:: jinja2.parser.Parser\n    :members: parse_expression, parse_tuple, parse_assign_target,\n              parse_statements, free_identifier, fail\n\n    .. attribute:: filename\n\n        The filename of the template the parser processes.  This is **not**\n        the load name of the template.  For the load name see :attr:`name`.\n        For templates that were not loaded form the file system this is\n        ``None``.\n\n    .. attribute:: name\n\n        The load name of the template.\n\n    .. attribute:: stream\n\n        The current :class:`~jinja2.lexer.TokenStream`\n\n.. autoclass:: jinja2.lexer.TokenStream\n   :members: push, look, eos, skip, __next__, next_if, skip_if, expect\n\n   .. attribute:: current\n\n        The current :class:`~jinja2.lexer.Token`.\n\n.. autoclass:: jinja2.lexer.Token\n    :members: test, test_any\n\n    .. attribute:: lineno\n\n        The line number of the token\n\n    .. attribute:: type\n\n        The type of the token.  This string is interned so you may compare\n        it with arbitrary strings using the ``is`` operator.\n\n    .. attribute:: value\n\n        The value of the token.\n\nThere is also a utility function in the lexer module that can count newline\ncharacters in strings:\n\n.. autofunction:: jinja2.lexer.count_newlines\n\n\nAST\n~~~\n\nThe AST (Abstract Syntax Tree) is used to represent a template after parsing.\nIt's build of nodes that the compiler then converts into executable Python\ncode objects.  Extensions that provide custom statements can return nodes to\nexecute custom Python code.\n\nThe list below describes all nodes that are currently available.  The AST may\nchange between Jinja versions but will stay backwards compatible.\n\nFor more information have a look at the repr of :meth:`jinja2.Environment.parse`.\n\n.. module:: jinja2.nodes\n\n.. jinja:nodes:: jinja2.nodes.Node\n\n.. autoexception:: Impossible\n"
  },
  {
    "path": "docs/faq.rst",
    "content": "Frequently Asked Questions\n==========================\n\n\nWhy is it called Jinja?\n-----------------------\n\n\"Jinja\" is a Japanese `Shinto shrine`_, or temple, and temple and\ntemplate share a similar English pronunciation. It is not named after\nthe `city in Uganda`_.\n\n.. _Shinto shrine: https://en.wikipedia.org/wiki/Shinto_shrine\n.. _city in Uganda: https://en.wikipedia.org/wiki/Jinja%2C_Uganda\n\n\nHow fast is Jinja?\n------------------\n\nJinja is relatively fast among template engines because it compiles and\ncaches template code to Python code, so that the template does not need\nto be parsed and interpreted each time. Rendering a template becomes as\nclose to executing a Python function as possible.\n\nJinja also makes extensive use of caching. Templates are cached by name\nafter loading, so future uses of the template avoid loading. The\ntemplate loading itself uses a bytecode cache to avoid repeated\ncompiling. The caches can be external to persist across restarts.\nTemplates can also be precompiled and loaded as fast Python imports.\n\nWe dislike benchmarks because they don't reflect real use. Performance\ndepends on many factors. Different engines have different default\nconfigurations and tradeoffs that make it unclear how to set up a useful\ncomparison. Often, database access, API calls, and data processing have\na much larger effect on performance than the template engine.\n\n\nIsn't it a bad idea to put logic in templates?\n----------------------------------------------\n\nWithout a doubt you should try to remove as much logic from templates as\npossible. With less logic, the template is easier to understand, has\nfewer potential side effects, and is faster to compile and render. But a\ntemplate without any logic means processing must be done in code before\nrendering. A template engine that does that is shipped with Python,\ncalled :class:`string.Template`, and while it's definitely fast it's not\nconvenient.\n\nJinja's features such as blocks, statements, filters, and function calls\nmake it much easier to write expressive templates, with very few\nrestrictions. Jinja doesn't allow arbitrary Python code in templates, or\nevery feature available in the Python language. This keeps the engine\neasier to maintain, and keeps templates more readable.\n\nSome amount of logic is required in templates to keep everyone happy.\nToo much logic in the template can make it complex to reason about and\nmaintain. It's up to you to decide how your application will work and\nbalance how much logic you want to put in the template.\n\n\nWhy is HTML escaping not the default?\n-------------------------------------\n\nJinja provides a feature that can be enabled to escape HTML syntax in\nrendered templates. However, it is disabled by default.\n\nJinja is a general purpose template engine, it is not only used for HTML\ndocuments. You can generate plain text, LaTeX, emails, CSS, JavaScript,\nconfiguration files, etc. HTML escaping wouldn't make sense for any of\nthese document types.\n\nWhile automatic escaping means that you are less likely have an XSS\nproblem, it also requires significant extra processing during compiling\nand rendering, which can reduce performance. Jinja uses `MarkupSafe`_ for\nescaping, which provides optimized C code for speed, but it still\nintroduces overhead to track escaping across methods and formatting.\n\n.. _MarkupSafe: https://markupsafe.palletsprojects.com/\n"
  },
  {
    "path": "docs/index.rst",
    "content": ".. rst-class:: hide-header\n\nJinja\n=====\n\n.. image:: _static/jinja-name.svg\n    :align: center\n    :height: 200px\n\nJinja is a fast, expressive, extensible templating engine. Special\nplaceholders in the template allow writing code similar to Python\nsyntax. Then the template is passed data to render the final document.\n\n.. toctree::\n    :maxdepth: 2\n    :caption: Contents:\n\n    intro\n    api\n    sandbox\n    nativetypes\n    templates\n    extensions\n    integration\n    switching\n    tricks\n    faq\n    license\n    changes\n"
  },
  {
    "path": "docs/integration.rst",
    "content": "Integration\n===========\n\n\nFlask\n-----\n\nThe `Flask`_ web application framework, also maintained by Pallets, uses\nJinja templates by default. Flask sets up a Jinja environment and\ntemplate loader for you, and provides functions to easily render\ntemplates from view functions.\n\n.. _Flask: https://flask.palletsprojects.com\n\n\nDjango\n------\n\nDjango supports using Jinja as its template engine, see\nhttps://docs.djangoproject.com/en/stable/topics/templates/#support-for-template-engines.\n\n\n.. _babel-integration:\n\nBabel\n-----\n\nJinja provides support for extracting gettext messages from templates\nvia a `Babel`_ extractor entry point called\n``jinja2.ext.babel_extract``. The support is implemented as part of the\n:ref:`i18n-extension` extension.\n\nGettext messages are extracted from both ``trans`` tags and code\nexpressions.\n\nTo extract gettext messages from templates, the project needs a Jinja\nsection in its Babel extraction method `mapping file`_:\n\n.. sourcecode:: ini\n\n    [jinja2: **/templates/**.html]\n    encoding = utf-8\n\nThe syntax related options of the :class:`Environment` are also\navailable as configuration values in the mapping file. For example, to\ntell the extractor that templates use ``%`` as\n``line_statement_prefix`` you can use this code:\n\n.. sourcecode:: ini\n\n    [jinja2: **/templates/**.html]\n    encoding = utf-8\n    line_statement_prefix = %\n\n:ref:`jinja-extensions` may also be defined by passing a comma separated\nlist of import paths as the ``extensions`` value. The i18n extension is\nadded automatically.\n\nTemplate syntax errors are ignored by default. The assumption is that\ntests will catch syntax errors in templates. If you don't want to ignore\nerrors, add ``silent = false`` to the settings.\n\n.. _Babel: https://babel.readthedocs.io/\n.. _mapping file: https://babel.readthedocs.io/en/latest/messages.html#extraction-method-mapping-and-configuration\n\n\nPylons\n------\n\nIt's easy to integrate Jinja into a `Pylons`_ application.\n\nThe template engine is configured in ``config/environment.py``. The\nconfiguration for Jinja looks something like this:\n\n.. code-block:: python\n\n    from jinja2 import Environment, PackageLoader\n    config['pylons.app_globals'].jinja_env = Environment(\n        loader=PackageLoader('yourapplication', 'templates')\n    )\n\nAfter that you can render Jinja templates by using the ``render_jinja``\nfunction from the ``pylons.templating`` module.\n\nAdditionally it's a good idea to set the Pylons ``c`` object to strict\nmode. By default attribute access on missing attributes on the ``c``\nobject returns an empty string and not an undefined object. To change\nthis add this to ``config/environment.py``:\n\n.. code-block:: python\n\n    config['pylons.strict_c'] = True\n\n.. _Pylons: https://pylonsproject.org/\n"
  },
  {
    "path": "docs/intro.rst",
    "content": "Introduction\n============\n\nJinja is a fast, expressive, extensible templating engine. Special\nplaceholders in the template allow writing code similar to Python\nsyntax. Then the template is passed data to render the final document.\n\nIt includes:\n\n-   Template inheritance and inclusion.\n-   Define and import macros within templates.\n-   HTML templates can use autoescaping to prevent XSS from untrusted\n    user input.\n-   A sandboxed environment can safely render untrusted templates.\n-   Async support for generating templates that automatically handle\n    sync and async functions without extra syntax.\n-   I18N support with Babel.\n-   Templates are compiled to optimized Python code just-in-time and\n    cached, or can be compiled ahead-of-time.\n-   Exceptions point to the correct line in templates to make debugging\n    easier.\n-   Extensible filters, tests, functions, and even syntax.\n\nJinja's philosophy is that while application logic belongs in Python if\npossible, it shouldn't make the template designer's job difficult by\nrestricting functionality too much.\n\n\nInstallation\n------------\n\nWe recommend using the latest version of Python. Jinja supports Python\n3.10 and newer. We also recommend using a `virtual environment`_ in order\nto isolate your project dependencies from other projects and the system.\n\n.. _virtual environment: https://packaging.python.org/tutorials/installing-packages/#creating-virtual-environments\n\nInstall the most recent Jinja version using pip:\n\n.. code-block:: text\n\n    $ pip install Jinja2\n\n\nDependencies\n~~~~~~~~~~~~\n\nThese will be installed automatically when installing Jinja.\n\n-   `MarkupSafe`_ escapes untrusted input when rendering templates to\n    avoid injection attacks.\n\n.. _MarkupSafe: https://markupsafe.palletsprojects.com/\n\n\nOptional Dependencies\n~~~~~~~~~~~~~~~~~~~~~\n\nThese distributions will not be installed automatically.\n\n-   `Babel`_ provides translation support in templates.\n\n.. _Babel: https://babel.pocoo.org/\n"
  },
  {
    "path": "docs/license.rst",
    "content": "BSD-3-Clause License\n====================\n\n.. literalinclude:: ../LICENSE.txt\n    :language: text\n"
  },
  {
    "path": "docs/make.bat",
    "content": "@ECHO OFF\n\npushd %~dp0\n\nREM Command file for Sphinx documentation\n\nif \"%SPHINXBUILD%\" == \"\" (\n\tset SPHINXBUILD=sphinx-build\n)\nset SOURCEDIR=.\nset BUILDDIR=_build\n\nif \"%1\" == \"\" goto help\n\n%SPHINXBUILD% >NUL 2>NUL\nif errorlevel 9009 (\n\techo.\n\techo.The 'sphinx-build' command was not found. Make sure you have Sphinx\n\techo.installed, then set the SPHINXBUILD environment variable to point\n\techo.to the full path of the 'sphinx-build' executable. Alternatively you\n\techo.may add the Sphinx directory to PATH.\n\techo.\n\techo.If you don't have Sphinx installed, grab it from\n\techo.https://www.sphinx-doc.org/\n\texit /b 1\n)\n\n%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS%\ngoto end\n\n:help\n%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS%\n\n:end\npopd\n"
  },
  {
    "path": "docs/nativetypes.rst",
    "content": ".. module:: jinja2.nativetypes\n\n.. _nativetypes:\n\nNative Python Types\n===================\n\nThe default :class:`~jinja2.Environment` renders templates to strings. With\n:class:`NativeEnvironment`, rendering a template produces a native Python type.\nThis is useful if you are using Jinja outside the context of creating text\nfiles. For example, your code may have an intermediate step where users may use\ntemplates to define values that will then be passed to a traditional string\nenvironment.\n\nExamples\n--------\n\nAdding two values results in an integer, not a string with a number:\n\n>>> env = NativeEnvironment()\n>>> t = env.from_string('{{ x + y }}')\n>>> result = t.render(x=4, y=2)\n>>> print(result)\n6\n>>> print(type(result))\nint\n\nRendering list syntax produces a list:\n\n>>> t = env.from_string('[{% for item in data %}{{ item + 1 }},{% endfor %}]')\n>>> result = t.render(data=range(5))\n>>> print(result)\n[1, 2, 3, 4, 5]\n>>> print(type(result))\nlist\n\nRendering something that doesn't look like a Python literal produces a string:\n\n>>> t = env.from_string('{{ x }} * {{ y }}')\n>>> result = t.render(x=4, y=2)\n>>> print(result)\n4 * 2\n>>> print(type(result))\nstr\n\nRendering a Python object produces that object as long as it is the only node:\n\n>>> class Foo:\n...     def __init__(self, value):\n...         self.value = value\n...\n>>> result = env.from_string('{{ x }}').render(x=Foo(15))\n>>> print(type(result).__name__)\nFoo\n>>> print(result.value)\n15\n\nSandboxed Native Environment\n----------------------------\n\nYou can combine :class:`.SandboxedEnvironment` and :class:`NativeEnvironment` to\nget both behaviors.\n\n.. code-block:: python\n\n    class SandboxedNativeEnvironment(SandboxedEnvironment, NativeEnvironment):\n        pass\n\nAPI\n---\n\n.. autoclass:: NativeEnvironment([options])\n\n.. autoclass:: NativeTemplate([options])\n    :members: render\n"
  },
  {
    "path": "docs/sandbox.rst",
    "content": "Sandbox\n=======\n\nThe Jinja sandbox can be used to render untrusted templates. Access to\nattributes, method calls, operators, mutating data structures, and\nstring formatting can be intercepted and prohibited.\n\n.. code-block:: pycon\n\n    >>> from jinja2.sandbox import SandboxedEnvironment\n    >>> env = SandboxedEnvironment()\n    >>> func = lambda: \"Hello, Sandbox!\"\n    >>> env.from_string(\"{{ func() }}\").render(func=func)\n    'Hello, Sandbox!'\n    >>> env.from_string(\"{{ func.__code__.co_code }}\").render(func=func)\n    Traceback (most recent call last):\n      ...\n    SecurityError: access to attribute '__code__' of 'function' object is unsafe.\n\nA sandboxed environment can be useful, for example, to allow users of an\ninternal reporting system to create custom emails. You would document\nwhat data is available in the templates, then the user would write a\ntemplate using that information. Your code would generate the report\ndata and pass it to the user's sandboxed template to render.\n\n\nSecurity Considerations\n-----------------------\n\nThe sandbox alone is not a solution for perfect security. Keep these\nthings in mind when using the sandbox.\n\nTemplates can still raise errors when compiled or rendered. Your code\nshould attempt to catch errors instead of crashing.\n\nIt is possible to construct a relatively small template that renders to\na very large amount of output, which could correspond to a high use of\nCPU or memory. You should run your application with limits on resources\nsuch as CPU and memory to mitigate this.\n\nJinja only renders text, it does not understand, for example, JavaScript\ncode. Depending on how the rendered template will be used, you may need\nto do other postprocessing to restrict the output.\n\nPass only the data that is relevant to the template. Avoid passing\nglobal data, or objects with methods that have side effects. By default\nthe sandbox prevents private and internal attribute access. You can\noverride :meth:`~SandboxedEnvironment.is_safe_attribute` to further\nrestrict attributes access. Decorate methods with :func:`unsafe` to\nprevent calling them from templates when passing objects as data. Use\n:class:`ImmutableSandboxedEnvironment` to prevent modifying lists and\ndictionaries.\n\n\nAPI\n---\n\n.. module:: jinja2.sandbox\n\n.. autoclass:: SandboxedEnvironment([options])\n    :members: is_safe_attribute, is_safe_callable, default_binop_table,\n              default_unop_table, intercepted_binops, intercepted_unops,\n              call_binop, call_unop\n\n.. autoclass:: ImmutableSandboxedEnvironment([options])\n\n.. autoexception:: SecurityError\n\n.. autofunction:: unsafe\n\n.. autofunction:: is_internal_attribute\n\n.. autofunction:: modifies_known_mutable\n\n\nOperator Intercepting\n---------------------\n\nFor performance, Jinja outputs operators directly when compiling. This\nmeans it's not possible to intercept operator behavior by overriding\n:meth:`SandboxEnvironment.call <Environment.call>` by default, because\noperator special methods are handled by the Python interpreter, and\nmight not correspond with exactly one method depending on the operator's\nuse.\n\nThe sandbox can instruct the compiler to output a function to intercept\ncertain operators instead. Override\n:attr:`SandboxedEnvironment.intercepted_binops` and\n:attr:`SandboxedEnvironment.intercepted_unops` with the operator symbols\nyou want to intercept. The compiler will replace the symbols with calls\nto :meth:`SandboxedEnvironment.call_binop` and\n:meth:`SandboxedEnvironment.call_unop` instead. The default\nimplementation of those methods will use\n:attr:`SandboxedEnvironment.binop_table` and\n:attr:`SandboxedEnvironment.unop_table` to translate operator symbols\ninto :mod:`operator` functions.\n\nFor example, the power (``**``) operator can be disabled:\n\n.. code-block:: python\n\n    from jinja2.sandbox import SandboxedEnvironment\n\n    class MyEnvironment(SandboxedEnvironment):\n        intercepted_binops = frozenset([\"**\"])\n\n        def call_binop(self, context, operator, left, right):\n            if operator == \"**\":\n                return self.undefined(\"The power (**) operator is unavailable.\")\n\n            return super().call_binop(self, context, operator, left, right)\n"
  },
  {
    "path": "docs/switching.rst",
    "content": "Switching From Other Template Engines\n=====================================\n\nThis is a brief guide on some of the differences between Jinja syntax\nand other template languages. See :doc:`/templates` for a comprehensive\nguide to Jinja syntax and features.\n\n\nDjango\n------\n\nIf you have previously worked with Django templates, you should find\nJinja very familiar. Many of the syntax elements look and work the same.\nHowever, Jinja provides some more syntax elements, and some work a bit\ndifferently.\n\nThis section covers the template changes. The API, including extension\nsupport, is fundamentally different so it won't be covered here.\n\nDjango supports using Jinja as its template engine, see\nhttps://docs.djangoproject.com/en/stable/topics/templates/#support-for-template-engines.\n\n\nMethod Calls\n~~~~~~~~~~~~\n\nIn Django, methods are called implicitly, without parentheses.\n\n.. code-block:: django\n\n    {% for page in user.get_created_pages %}\n        ...\n    {% endfor %}\n\nIn Jinja, using parentheses is required for calls, like in Python. This\nallows you to pass variables to the method, which is not possible\nin Django. This syntax is also used for calling macros.\n\n.. code-block:: jinja\n\n    {% for page in user.get_created_pages() %}\n        ...\n    {% endfor %}\n\n\nFilter Arguments\n~~~~~~~~~~~~~~~~\n\nIn Django, one literal value can be passed to a filter after a colon.\n\n.. code-block:: django\n\n    {{ items|join:\", \" }}\n\nIn Jinja, filters can take any number of positional and keyword\narguments in parentheses, like function calls. Arguments can also be\nvariables instead of literal values.\n\n.. code-block:: jinja\n\n    {{ items|join(\", \") }}\n\n\nTests\n~~~~~\n\nIn addition to filters, Jinja also has \"tests\" used with the ``is``\noperator. This operator is not the same as the Python operator.\n\n.. code-block:: jinja\n\n    {% if user.user_id is odd %}\n        {{ user.username|e }} is odd\n    {% else %}\n        hmm. {{ user.username|e }} looks pretty normal\n    {% endif %}\n\nLoops\n~~~~~\n\nIn Django, the special variable for the loop context is called\n``forloop``, and the ``empty`` is used for no loop items.\n\n.. code-block:: django\n\n    {% for item in items %}\n        {{ forloop.counter }}. {{ item }}\n    {% empty %}\n        No items!\n    {% endfor %}\n\nIn Jinja, the special variable for the loop context is called ``loop``,\nand the ``else`` block is used for no loop items.\n\n.. code-block:: jinja\n\n    {% for item in items %}\n        {{ loop.index }}. {{ item }}\n    {% else %}\n        No items!\n    {% endfor %}\n\n\nCycle\n~~~~~\n\nIn Django, the ``{% cycle %}`` can be used in a for loop to alternate\nbetween values per loop.\n\n.. code-block:: django\n\n    {% for user in users %}\n        <li class=\"{% cycle 'odd' 'even' %}\">{{ user }}</li>\n    {% endfor %}\n\nIn Jinja, the ``loop`` context has a ``cycle`` method.\n\n.. code-block:: jinja\n\n    {% for user in users %}\n        <li class=\"{{ loop.cycle('odd', 'even') }}\">{{ user }}</li>\n    {% endfor %}\n\nA cycler can also be assigned to a variable and used outside or across\nloops with the ``cycle()`` global function.\n\n\nMako\n----\n\nYou can configure Jinja to look more like Mako:\n\n.. code-block:: python\n\n    env = Environment(\n        block_start_string=\"<%\",\n        block_end_string=\"%>\",\n        variable_start_string=\"${\",\n        variable_end_string=\"}\",\n        comment_start_string=\"<%doc>\",\n        commend_end_string=\"</%doc>\",\n        line_statement_prefix=\"%\",\n        line_comment_prefix=\"##\",\n    )\n\nWith an environment configured like that, Jinja should be able to\ninterpret a small subset of Mako templates without any changes.\n\nJinja does not support embedded Python code, so you would have to move\nthat out of the template. You could either process the data with the\nsame code before rendering, or add a global function or filter to the\nJinja environment.\n\nThe syntax for defs (which are called macros in Jinja) and template\ninheritance is different too.\n\nThe following Mako template:\n\n.. code-block:: mako\n\n    <%inherit file=\"layout.html\" />\n    <%def name=\"title()\">Page Title</%def>\n    <ul>\n    % for item in list:\n        <li>${item}</li>\n    % endfor\n    </ul>\n\nLooks like this in Jinja with the above configuration:\n\n.. code-block:: jinja\n\n    <% extends \"layout.html\" %>\n    <% block title %>Page Title<% endblock %>\n    <% block body %>\n    <ul>\n    % for item in list:\n        <li>${item}</li>\n    % endfor\n    </ul>\n    <% endblock %>\n"
  },
  {
    "path": "docs/templates.rst",
    "content": ".. py:currentmodule:: jinja2\n.. highlight:: html+jinja\n\nTemplate Designer Documentation\n===============================\n\nThis document describes the syntax and semantics of the template engine and\nwill be most useful as reference to those creating Jinja templates.  As the\ntemplate engine is very flexible, the configuration from the application can\nbe slightly different from the code presented here in terms of delimiters and\nbehavior of undefined values.\n\n\nSynopsis\n--------\n\nA Jinja template is simply a text file. Jinja can generate any text-based\nformat (HTML, XML, CSV, LaTeX, etc.).  A Jinja template doesn't need to have a\nspecific extension: ``.html``, ``.xml``, or any other extension is just fine.\n\nA template contains **variables** and/or **expressions**, which get replaced\nwith values when a template is *rendered*; and **tags**, which control the\nlogic of the template.  The template syntax is heavily inspired by Django and\nPython.\n\nBelow is a minimal template that illustrates a few basics using the default\nJinja configuration.  We will cover the details later in this document::\n\n    <!DOCTYPE html>\n    <html lang=\"en\">\n    <head>\n        <title>My Webpage</title>\n    </head>\n    <body>\n        <ul id=\"navigation\">\n        {% for item in navigation %}\n            <li><a href=\"{{ item.href }}\">{{ item.caption }}</a></li>\n        {% endfor %}\n        </ul>\n\n        <h1>My Webpage</h1>\n        {{ a_variable }}\n\n        {# a comment #}\n    </body>\n    </html>\n\nThe following example shows the default configuration settings.  An application\ndeveloper can change the syntax configuration from ``{% foo %}`` to ``<% foo\n%>``, or something similar.\n\nThere are a few kinds of delimiters. The default Jinja delimiters are\nconfigured as follows:\n\n* ``{% ... %}`` for :ref:`Statements <list-of-control-structures>`\n* ``{{ ... }}`` for :ref:`Expressions` to print to the template output\n* ``{# ... #}`` for :ref:`Comments` not included in the template output\n\n:ref:`Line Statements and Comments <line-statements>` are also possible,\nthough they don't have default prefix characters. To use them, set\n``line_statement_prefix`` and ``line_comment_prefix`` when creating the\n:class:`~jinja2.Environment`.\n\n\nTemplate File Extension\n~~~~~~~~~~~~~~~~~~~~~~~\n\nAs stated above, any file can be loaded as a template, regardless of\nfile extension. Adding a ``.jinja`` extension, like ``user.html.jinja``\nmay make it easier for some IDEs or editor plugins, but is not required.\nAutoescaping, introduced later, can be applied based on file extension,\nso you'll need to take the extra suffix into account in that case.\n\nAnother good heuristic for identifying templates is that they are in a\n``templates`` folder, regardless of extension. This is a common layout\nfor projects.\n\n\n.. _variables:\n\nVariables\n---------\n\nTemplate variables are defined by the context dictionary passed to the\ntemplate.\n\nYou can mess around with the variables in templates provided they are passed in\nby the application.  Variables may have attributes or elements on them you can\naccess too.  What attributes a variable has depends heavily on the application\nproviding that variable.\n\nYou can use a dot (``.``) to access attributes of a variable in addition\nto the standard Python ``__getitem__`` \"subscript\" syntax (``[]``).\n\nThe following lines do the same thing::\n\n    {{ foo.bar }}\n    {{ foo['bar'] }}\n\nIt's important to know that the outer double-curly braces are *not* part of the\nvariable, but the print statement.  If you access variables inside tags don't\nput the braces around them.\n\nIf a variable or attribute does not exist, you will get back an undefined\nvalue.  What you can do with that kind of value depends on the application\nconfiguration: the default behavior is to evaluate to an empty string if\nprinted or iterated over, and to fail for every other operation.\n\n.. _notes-on-subscriptions:\n\n.. admonition:: Implementation\n\n    For the sake of convenience, ``foo.bar`` in Jinja does the following\n    things on the Python layer:\n\n    -   check for an attribute called `bar` on `foo`\n        (``getattr(foo, 'bar')``)\n    -   if there is not, check for an item ``'bar'`` in `foo`\n        (``foo.__getitem__('bar')``)\n    -   if there is not, return an undefined object.\n\n    ``foo['bar']`` works mostly the same with a small difference in sequence:\n\n    -   check for an item ``'bar'`` in `foo`.\n        (``foo.__getitem__('bar')``)\n    -   if there is not, check for an attribute called `bar` on `foo`.\n        (``getattr(foo, 'bar')``)\n    -   if there is not, return an undefined object.\n\n    This is important if an object has an item and attribute with the same\n    name.  Additionally, the :func:`attr` filter only looks up attributes.\n\n.. _filters:\n\nFilters\n-------\n\nVariables can be modified by **filters**.  Filters are separated from the\nvariable by a pipe symbol (``|``) and may have optional arguments in\nparentheses.  Multiple filters can be chained.  The output of one filter is\napplied to the next.\n\nFor example, ``{{ name|striptags|title }}`` will remove all HTML Tags from\nvariable `name` and title-case the output (``title(striptags(name))``).\n\nFilters that accept arguments have parentheses around the arguments, just like\na function call.  For example: ``{{ listx|join(', ') }}`` will join a list with\ncommas (``str.join(', ', listx)``).\n\nThe :ref:`builtin-filters` below describes all the builtin filters.\n\n.. _tests:\n\nTests\n-----\n\nBeside filters, there are also so-called \"tests\" available.  Tests can be used\nto test a variable against a common expression.  To test a variable or\nexpression, you add `is` plus the name of the test after the variable.  For\nexample, to find out if a variable is defined, you can do ``name is defined``,\nwhich will then return true or false depending on whether `name` is defined\nin the current template context.\n\nTests can accept arguments, too.  If the test only takes one argument, you can\nleave out the parentheses.  For example, the following two\nexpressions do the same thing::\n\n    {% if loop.index is divisibleby 3 %}\n    {% if loop.index is divisibleby(3) %}\n\nThe :ref:`builtin-tests` below describes all the builtin tests.\n\n\n.. _comments:\n\nComments\n--------\n\nTo comment-out part of a line in a template, use the comment syntax which is\nby default set to ``{# ... #}``.  This is useful to comment out parts of the\ntemplate for debugging or to add information for other template designers or\nyourself::\n\n    {# note: commented-out template because we no longer use this\n        {% for user in users %}\n            ...\n        {% endfor %}\n    #}\n\n\nWhitespace Control\n------------------\n\nIn the default configuration:\n\n* a single trailing newline is stripped if present\n* other whitespace (spaces, tabs, newlines etc.) is returned unchanged\n\nIf an application configures Jinja to `trim_blocks`, the first newline after a\ntemplate tag is removed automatically (like in PHP). The `lstrip_blocks`\noption can also be set to strip tabs and spaces from the beginning of a\nline to the start of a block. (Nothing will be stripped if there are\nother characters before the start of the block.)\n\nWith both ``trim_blocks`` and ``lstrip_blocks`` disabled (the default), block\ntags on their own lines will be removed, but a blank line will remain and the\nspaces in the content will be preserved. For example, this template:\n\n.. code-block:: jinja\n\n    <div>\n        {% if True %}\n            yay\n        {% endif %}\n    </div>\n\nWith both ``trim_blocks`` and ``lstrip_blocks`` disabled, the template is\nrendered with blank lines inside the div:\n\n.. code-block:: text\n\n    <div>\n\n            yay\n\n    </div>\n\nWith both ``trim_blocks`` and ``lstrip_blocks`` enabled, the template block\nlines are completely removed:\n\n.. code-block:: text\n\n    <div>\n            yay\n    </div>\n\nYou can manually disable the `lstrip_blocks` behavior by putting a\nplus sign (``+``) at the start of a block::\n\n    <div>\n            {%+ if something %}yay{% endif %}\n    </div>\n\nSimilarly, you can manually disable the ``trim_blocks`` behavior by\nputting a plus sign (``+``) at the end of a block::\n\n    <div>\n        {% if something +%}\n            yay\n        {% endif %}\n    </div>\n\nYou can also strip whitespace in templates by hand.  If you add a minus\nsign (``-``) to the start or end of a block (e.g. a :ref:`for-loop` tag), a\ncomment, or a variable expression, the whitespaces before or after\nthat block will be removed::\n\n    {% for item in seq -%}\n        {{ item }}\n    {%- endfor %}\n\nThis will yield all elements without whitespace between them.  If `seq` was\na list of numbers from ``1`` to ``9``, the output would be ``123456789``.\n\nIf :ref:`line-statements` are enabled, they strip leading whitespace\nautomatically up to the beginning of the line.\n\nBy default, Jinja also removes trailing newlines.  To keep single\ntrailing newlines, configure Jinja to `keep_trailing_newline`.\n\n.. admonition:: Note\n\n    You must not add whitespace between the tag and the minus sign.\n\n    **valid**::\n\n        {%- if foo -%}...{% endif %}\n\n    **invalid**::\n\n        {% - if foo - %}...{% endif %}\n\n\nEscaping\n--------\n\nIt is sometimes desirable -- even necessary -- to have Jinja ignore parts\nit would otherwise handle as variables or blocks.  For example, if, with\nthe default syntax, you want to use ``{{`` as a raw string in a template and\nnot start a variable, you have to use a trick.\n\nThe easiest way to output a literal variable delimiter (``{{``) is by using a\nvariable expression::\n\n    {{ '{{' }}\n\nFor bigger sections, it makes sense to mark a block `raw`.  For example, to\ninclude example Jinja syntax in a template, you can use this snippet::\n\n    {% raw %}\n        <ul>\n        {% for item in seq %}\n            <li>{{ item }}</li>\n        {% endfor %}\n        </ul>\n    {% endraw %}\n\n.. admonition:: Note\n\n    Minus sign at the end of ``{% raw -%}`` tag cleans all the spaces and newlines\n    preceding the first character of your raw data.\n\n\n.. _line-statements:\n\nLine Statements\n---------------\n\nIf line statements are enabled by the application, it's possible to mark a\nline as a statement.  For example, if the line statement prefix is configured\nto ``#``, the following two examples are equivalent::\n\n    <ul>\n    # for item in seq\n        <li>{{ item }}</li>\n    # endfor\n    </ul>\n\n    <ul>\n    {% for item in seq %}\n        <li>{{ item }}</li>\n    {% endfor %}\n    </ul>\n\nThe line statement prefix can appear anywhere on the line as long as no text\nprecedes it.  For better readability, statements that start a block (such as\n`for`, `if`, `elif` etc.) may end with a colon::\n\n    # for item in seq:\n        ...\n    # endfor\n\n\n.. admonition:: Note\n\n    Line statements can span multiple lines if there are open parentheses,\n    braces or brackets::\n\n        <ul>\n        # for href, caption in [('index.html', 'Index'),\n                                ('about.html', 'About')]:\n            <li><a href=\"{{ href }}\">{{ caption }}</a></li>\n        # endfor\n        </ul>\n\nSince Jinja 2.2, line-based comments are available as well.  For example, if\nthe line-comment prefix is configured to be ``##``, everything from ``##`` to\nthe end of the line is ignored (excluding the newline sign)::\n\n    # for item in seq:\n        <li>{{ item }}</li>     ## this comment is ignored\n    # endfor\n\n\n.. _template-inheritance:\n\nTemplate Inheritance\n--------------------\n\nThe most powerful part of Jinja is template inheritance. Template inheritance\nallows you to build a base \"skeleton\" template that contains all the common\nelements of your site and defines **blocks** that child templates can override.\n\nSounds complicated but is very basic. It's easiest to understand it by starting\nwith an example.\n\n\nBase Template\n~~~~~~~~~~~~~\n\nThis template, which we'll call ``base.html``, defines a simple HTML skeleton\ndocument that you might use for a simple two-column page. It's the job of\n\"child\" templates to fill the empty blocks with content::\n\n    <!DOCTYPE html>\n    <html lang=\"en\">\n    <head>\n        {% block head %}\n        <link rel=\"stylesheet\" href=\"style.css\" />\n        <title>{% block title %}{% endblock %} - My Webpage</title>\n        {% endblock %}\n    </head>\n    <body>\n        <div id=\"content\">{% block content %}{% endblock %}</div>\n        <div id=\"footer\">\n            {% block footer %}\n            &copy; Copyright 2008 by <a href=\"http://domain.invalid/\">you</a>.\n            {% endblock %}\n        </div>\n    </body>\n    </html>\n\nIn this example, the ``{% block %}`` tags define four blocks that child templates\ncan fill in. All the `block` tag does is tell the template engine that a\nchild template may override those placeholders in the template.\n\n``block`` tags can be inside other blocks such as ``if``, but they will\nalways be executed regardless of if the ``if`` block is actually\nrendered.\n\nChild Template\n~~~~~~~~~~~~~~\n\nA child template might look like this::\n\n    {% extends \"base.html\" %}\n    {% block title %}Index{% endblock %}\n    {% block head %}\n        {{ super() }}\n        <style type=\"text/css\">\n            .important { color: #336699; }\n        </style>\n    {% endblock %}\n    {% block content %}\n        <h1>Index</h1>\n        <p class=\"important\">\n          Welcome to my awesome homepage.\n        </p>\n    {% endblock %}\n\nThe ``{% extends %}`` tag is the key here. It tells the template engine that\nthis template \"extends\" another template.  When the template system evaluates\nthis template, it first locates the parent.  The extends tag should be the\nfirst tag in the template.  Everything before it is printed out normally and\nmay cause confusion.  For details about this behavior and how to take\nadvantage of it, see :ref:`null-default-fallback`. Also a block will always be\nfilled in regardless of whether the surrounding condition is evaluated to be true\nor false.\n\nThe filename of the template depends on the template loader.  For example, the\n:class:`FileSystemLoader` allows you to access other templates by giving the\nfilename.  You can access templates in subdirectories with a slash::\n\n    {% extends \"layout/default.html\" %}\n\nBut this behavior can depend on the application embedding Jinja.  Note that\nsince the child template doesn't define the ``footer`` block, the value from\nthe parent template is used instead.\n\nYou can't define multiple ``{% block %}`` tags with the same name in the\nsame template.  This limitation exists because a block tag works in \"both\"\ndirections.  That is, a block tag doesn't just provide a placeholder to fill\n- it also defines the content that fills the placeholder in the *parent*.\nIf there were two similarly-named ``{% block %}`` tags in a template,\nthat template's parent wouldn't know which one of the blocks' content to use.\n\nIf you want to print a block multiple times, you can, however, use the special\n`self` variable and call the block with that name::\n\n    <title>{% block title %}{% endblock %}</title>\n    <h1>{{ self.title() }}</h1>\n    {% block body %}{% endblock %}\n\n\nSuper Blocks\n~~~~~~~~~~~~\n\nIt's possible to render the contents of the parent block by calling ``super()``.\nThis gives back the results of the parent block::\n\n    {% block sidebar %}\n        <h3>Table Of Contents</h3>\n        ...\n        {{ super() }}\n    {% endblock %}\n\n\nNesting extends\n~~~~~~~~~~~~~~~\n\nIn the case of multiple levels of ``{% extends %}``,\n``super`` references may be chained (as in ``super.super()``)\nto skip levels in the inheritance tree.\n\nFor example::\n\n    # parent.tmpl\n    body: {% block body %}Hi from parent.{% endblock %}\n\n    # child.tmpl\n    {% extends \"parent.tmpl\" %}\n    {% block body %}Hi from child. {{ super() }}{% endblock %}\n\n    # grandchild1.tmpl\n    {% extends \"child.tmpl\" %}\n    {% block body %}Hi from grandchild1.{% endblock %}\n\n    # grandchild2.tmpl\n    {% extends \"child.tmpl\" %}\n    {% block body %}Hi from grandchild2. {{ super.super() }} {% endblock %}\n\n\nRendering ``child.tmpl`` will give\n``body: Hi from child. Hi from parent.``\n\nRendering ``grandchild1.tmpl`` will give\n``body: Hi from grandchild1.``\n\nRendering ``grandchild2.tmpl`` will give\n``body: Hi from grandchild2. Hi from parent.``\n\n\nNamed Block End-Tags\n~~~~~~~~~~~~~~~~~~~~\n\nJinja allows you to put the name of the block after the end tag for better\nreadability::\n\n    {% block sidebar %}\n        {% block inner_sidebar %}\n            ...\n        {% endblock inner_sidebar %}\n    {% endblock sidebar %}\n\nHowever, the name after the `endblock` word must match the block name.\n\n\nBlock Nesting and Scope\n~~~~~~~~~~~~~~~~~~~~~~~\n\nBlocks can be nested for more complex layouts. By default, a block may not\naccess variables from outside the block (outer scopes)::\n\n    {% for item in seq %}\n        <li>{% block loop_item %}{{ item }}{% endblock %}</li>\n    {% endfor %}\n\nThis example would output empty ``<li>`` items because `item` is unavailable\ninside the block.  The reason for this is that if the block is replaced by\na child template, a variable would appear that was not defined in the block or\npassed to the context.\n\nStarting with Jinja 2.2, you can explicitly specify that variables are\navailable in a block by setting the block to \"scoped\" by adding the `scoped`\nmodifier to a block declaration::\n\n    {% for item in seq %}\n        <li>{% block loop_item scoped %}{{ item }}{% endblock %}</li>\n    {% endfor %}\n\nWhen overriding a block, the `scoped` modifier does not have to be provided.\n\n\nRequired Blocks\n~~~~~~~~~~~~~~~\n\nBlocks can be marked as ``required``. They must be overridden at some\npoint, but not necessarily by the direct child template. Required blocks\nmay only contain space and comments, and they cannot be rendered\ndirectly.\n\n.. code-block:: jinja\n    :caption: ``page.txt``\n\n    {% block body required %}{% endblock %}\n\n.. code-block:: jinja\n    :caption: ``issue.txt``\n\n    {% extends \"page.txt\" %}\n\n.. code-block:: jinja\n    :caption: ``bug_report.txt``\n\n    {% extends \"issue.txt\" %}\n    {% block body %}Provide steps to demonstrate the bug.{% endblock %}\n\nRendering ``page.txt`` or ``issue.txt`` will raise\n``TemplateRuntimeError`` because they don't override the ``body`` block.\nRendering ``bug_report.txt`` will succeed because it does override the\nblock.\n\nWhen combined with ``scoped``, the ``required`` modifier must be placed\n*after* the scoped modifier. Here are some valid examples:\n\n.. code-block:: jinja\n\n    {% block body scoped %}{% endblock %}\n    {% block body required %}{% endblock %}\n    {% block body scoped required %}{% endblock %}\n\n\nTemplate Objects\n~~~~~~~~~~~~~~~~\n\n``extends``, ``include``, and ``import`` can take a template object\ninstead of the name of a template to load. This could be useful in some\nadvanced situations, since you can use Python code to load a template\nfirst and pass it in to ``render``.\n\n.. code-block:: python\n\n    if debug_mode:\n        layout = env.get_template(\"debug_layout.html\")\n    else:\n        layout = env.get_template(\"layout.html\")\n\n    user_detail = env.get_template(\"user/detail.html\")\n    return user_detail.render(layout=layout)\n\n.. code-block:: jinja\n\n    {% extends layout %}\n\nNote how ``extends`` is passed the variable with the template object\nthat was passed to ``render``, instead of a string.\n\n\nHTML Escaping\n-------------\n\nWhen generating HTML from templates, there's always a risk that a variable will\ninclude characters that affect the resulting HTML. There are two approaches:\n\na. manually escaping each variable; or\nb. automatically escaping everything by default.\n\nJinja supports both. What is used depends on the application configuration.\nThe default configuration is no automatic escaping; for various reasons:\n\n-   Escaping everything except for safe values will also mean that Jinja is\n    escaping variables known to not include HTML (e.g. numbers, booleans)\n    which can be a huge performance hit.\n\n-   The information about the safety of a variable is very fragile.  It could\n    happen that by coercing safe and unsafe values, the return value is\n    double-escaped HTML.\n\nWorking with Manual Escaping\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nIf manual escaping is enabled, it's **your** responsibility to escape\nvariables if needed.  What to escape?  If you have a variable that *may*\ninclude any of the following chars (``>``, ``<``, ``&``, or ``\"``) you\n**SHOULD** escape it unless the variable contains well-formed and trusted\nHTML.  Escaping works by piping the variable through the ``|e`` filter::\n\n    {{ user.username|e }}\n\nWorking with Automatic Escaping\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nWhen automatic escaping is enabled, everything is escaped by default except\nfor values explicitly marked as safe.  Variables and expressions\ncan be marked as safe either in:\n\na.  The context dictionary by the application with\n    :class:`markupsafe.Markup`\nb.  The template, with the ``|safe`` filter.\n\nIf a string that you marked safe is passed through other Python code\nthat doesn't understand that mark, it may get lost. Be aware of when\nyour data is marked safe and how it is processed before arriving at the\ntemplate.\n\nIf a value has been escaped but is not marked safe, auto-escaping will\nstill take place and result in double-escaped characters. If you know\nyou have data that is already safe but not marked, be sure to wrap it in\n``Markup`` or use the ``|safe`` filter.\n\nJinja functions (macros, `super`, `self.BLOCKNAME`) always return template\ndata that is marked as safe.\n\nString literals in templates with automatic escaping are considered\nunsafe because native Python strings are not safe.\n\n.. _list-of-control-structures:\n\nList of Control Structures\n--------------------------\n\nA control structure refers to all those things that control the flow of a\nprogram - conditionals (i.e. if/elif/else), for-loops, as well as things like\nmacros and blocks.  With the default syntax, control structures appear inside\n``{% ... %}`` blocks.\n\n.. _for-loop:\n\nFor\n~~~\n\nLoop over each item in a sequence.  For example, to display a list of users\nprovided in a variable called `users`::\n\n    <h1>Members</h1>\n    <ul>\n    {% for user in users %}\n      <li>{{ user.username|e }}</li>\n    {% endfor %}\n    </ul>\n\nAs variables in templates retain their object properties, it is possible to\niterate over containers like `dict`::\n\n    <dl>\n    {% for key, value in my_dict.items() %}\n        <dt>{{ key|e }}</dt>\n        <dd>{{ value|e }}</dd>\n    {% endfor %}\n    </dl>\n\nPython dicts may not be in the order you want to display them in. If\norder matters, use the ``|dictsort`` filter.\n\n.. code-block:: jinja\n\n    <dl>\n    {% for key, value in my_dict | dictsort %}\n        <dt>{{ key|e }}</dt>\n        <dd>{{ value|e }}</dd>\n    {% endfor %}\n    </dl>\n\nInside of a for-loop block, you can access some special variables:\n\n+-----------------------+---------------------------------------------------+\n| Variable              | Description                                       |\n+=======================+===================================================+\n| `loop.index`          | The current iteration of the loop. (1 indexed)    |\n+-----------------------+---------------------------------------------------+\n| `loop.index0`         | The current iteration of the loop. (0 indexed)    |\n+-----------------------+---------------------------------------------------+\n| `loop.revindex`       | The number of iterations from the end of the loop |\n|                       | (1 indexed)                                       |\n+-----------------------+---------------------------------------------------+\n| `loop.revindex0`      | The number of iterations from the end of the loop |\n|                       | (0 indexed)                                       |\n+-----------------------+---------------------------------------------------+\n| `loop.first`          | True if first iteration.                          |\n+-----------------------+---------------------------------------------------+\n| `loop.last`           | True if last iteration.                           |\n+-----------------------+---------------------------------------------------+\n| `loop.length`         | The number of items in the sequence.              |\n+-----------------------+---------------------------------------------------+\n| `loop.cycle`          | A helper function to cycle between a list of      |\n|                       | sequences.  See the explanation below.            |\n+-----------------------+---------------------------------------------------+\n| `loop.depth`          | Indicates how deep in a recursive loop            |\n|                       | the rendering currently is.  Starts at level 1    |\n+-----------------------+---------------------------------------------------+\n| `loop.depth0`         | Indicates how deep in a recursive loop            |\n|                       | the rendering currently is.  Starts at level 0    |\n+-----------------------+---------------------------------------------------+\n| `loop.previtem`       | The item from the previous iteration of the loop. |\n|                       | Undefined during the first iteration.             |\n+-----------------------+---------------------------------------------------+\n| `loop.nextitem`       | The item from the following iteration of the loop.|\n|                       | Undefined during the last iteration.              |\n+-----------------------+---------------------------------------------------+\n| `loop.changed(*val)`  | True if previously called with a different value  |\n|                       | (or not called at all).                           |\n+-----------------------+---------------------------------------------------+\n\nWithin a for-loop, it's possible to cycle among a list of strings/variables\neach time through the loop by using the special `loop.cycle` helper::\n\n    {% for row in rows %}\n        <li class=\"{{ loop.cycle('odd', 'even') }}\">{{ row }}</li>\n    {% endfor %}\n\nSince Jinja 2.1, an extra `cycle` helper exists that allows loop-unbound\ncycling.  For more information, have a look at the :ref:`builtin-globals`.\n\n.. _loop-filtering:\n\nUnlike in Python, it's not possible to `break` or `continue` in a loop.  You\ncan, however, filter the sequence during iteration, which allows you to skip\nitems.  The following example skips all the users which are hidden::\n\n    {% for user in users if not user.hidden %}\n        <li>{{ user.username|e }}</li>\n    {% endfor %}\n\nThe advantage is that the special `loop` variable will count correctly; thus\nnot counting the users not iterated over.\n\nIf no iteration took place because the sequence was empty or the filtering\nremoved all the items from the sequence, you can render a default block\nby using `else`::\n\n    <ul>\n    {% for user in users %}\n        <li>{{ user.username|e }}</li>\n    {% else %}\n        <li><em>no users found</em></li>\n    {% endfor %}\n    </ul>\n\nNote that, in Python, `else` blocks are executed whenever the corresponding\nloop **did not** `break`.  Since Jinja loops cannot `break` anyway,\na slightly different behavior of the `else` keyword was chosen.\n\nIt is also possible to use loops recursively.  This is useful if you are\ndealing with recursive data such as sitemaps or RDFa.\nTo use loops recursively, you basically have to add the `recursive` modifier\nto the loop definition and call the `loop` variable with the new iterable\nwhere you want to recurse.\n\nThe following example implements a sitemap with recursive loops::\n\n    <ul class=\"sitemap\">\n    {%- for item in sitemap recursive %}\n        <li><a href=\"{{ item.href|e }}\">{{ item.title }}</a>\n        {%- if item.children -%}\n            <ul class=\"submenu\">{{ loop(item.children) }}</ul>\n        {%- endif %}</li>\n    {%- endfor %}\n    </ul>\n\nThe `loop` variable always refers to the closest (innermost) loop. If we\nhave more than one level of loops, we can rebind the variable `loop` by\nwriting `{% set outer_loop = loop %}` after the loop that we want to\nuse recursively. Then, we can call it using `{{ outer_loop(...) }}`\n\nPlease note that assignments in loops will be cleared at the end of the\niteration and cannot outlive the loop scope.  Older versions of Jinja had\na bug where in some circumstances it appeared that assignments would work.\nThis is not supported.  See :ref:`assignments` for more information about\nhow to deal with this.\n\nIf all you want to do is check whether some value has changed since the\nlast iteration or will change in the next iteration, you can use `previtem`\nand `nextitem`::\n\n    {% for value in values %}\n        {% if loop.previtem is defined and value > loop.previtem %}\n            The value just increased!\n        {% endif %}\n        {{ value }}\n        {% if loop.nextitem is defined and loop.nextitem > value %}\n            The value will increase even more!\n        {% endif %}\n    {% endfor %}\n\nIf you only care whether the value changed at all, using `changed` is even\neasier::\n\n    {% for entry in entries %}\n        {% if loop.changed(entry.category) %}\n            <h2>{{ entry.category }}</h2>\n        {% endif %}\n        <p>{{ entry.message }}</p>\n    {% endfor %}\n\n.. _if:\n\nIf\n~~\n\nThe `if` statement in Jinja is comparable with the Python if statement.\nIn the simplest form, you can use it to test if a variable is defined, not\nempty and not false::\n\n    {% if users %}\n    <ul>\n    {% for user in users %}\n        <li>{{ user.username|e }}</li>\n    {% endfor %}\n    </ul>\n    {% endif %}\n\nFor multiple branches, `elif` and `else` can be used like in Python.  You can\nuse more complex :ref:`expressions` there, too::\n\n    {% if kenny.sick %}\n        Kenny is sick.\n    {% elif kenny.dead %}\n        You killed Kenny!  You bastard!!!\n    {% else %}\n        Kenny looks okay --- so far\n    {% endif %}\n\nIf can also be used as an :ref:`inline expression <if-expression>` and for\n:ref:`loop filtering <loop-filtering>`.\n\n.. _macros:\n\nMacros\n~~~~~~\n\nMacros are comparable with functions in regular programming languages.  They\nare useful to put often used idioms into reusable functions to not repeat\nyourself (\"DRY\").\n\nHere's a small example of a macro that renders a form element::\n\n    {% macro input(name, value='', type='text', size=20) -%}\n        <input type=\"{{ type }}\" name=\"{{ name }}\" value=\"{{\n            value|e }}\" size=\"{{ size }}\">\n    {%- endmacro %}\n\nThe macro can then be called like a function in the namespace::\n\n    <p>{{ input('username') }}</p>\n    <p>{{ input('password', type='password') }}</p>\n\nIf the macro was defined in a different template, you have to\n:ref:`import <import>` it first.\n\nInside macros, you have access to three special variables:\n\n`varargs`\n    If more positional arguments are passed to the macro than accepted by the\n    macro, they end up in the special `varargs` variable as a list of values.\n\n`kwargs`\n    Like `varargs` but for keyword arguments.  All unconsumed keyword\n    arguments are stored in this special variable.\n\n`caller`\n    If the macro was called from a :ref:`call<call>` tag, the caller is stored\n    in this variable as a callable macro.\n\nMacros also expose some of their internal details.  The following attributes\nare available on a macro object:\n\n`name`\n    The name of the macro.  ``{{ input.name }}`` will print ``input``.\n\n`arguments`\n    A tuple of the names of arguments the macro accepts.\n\n`catch_kwargs`\n    This is `true` if the macro accepts extra keyword arguments (i.e.: accesses\n    the special `kwargs` variable).\n\n`catch_varargs`\n    This is `true` if the macro accepts extra positional arguments (i.e.:\n    accesses the special `varargs` variable).\n\n`caller`\n    This is `true` if the macro accesses the special `caller` variable and may\n    be called from a :ref:`call<call>` tag.\n\nIf a macro name starts with an underscore, it's not exported and can't\nbe imported.\n\nDue to how scopes work in Jinja, a macro in a child template does not\noverride a macro in a parent template. The following will output\n\"LAYOUT\", not \"CHILD\".\n\n.. code-block:: jinja\n    :caption: ``layout.txt``\n\n    {% macro foo() %}LAYOUT{% endmacro %}\n    {% block body %}{% endblock %}\n\n.. code-block:: jinja\n    :caption: ``child.txt``\n\n    {% extends 'layout.txt' %}\n    {% macro foo() %}CHILD{% endmacro %}\n    {% block body %}{{ foo() }}{% endblock %}\n\n\n.. _call:\n\nCall\n~~~~\n\nIn some cases it can be useful to pass a macro to another macro.  For this\npurpose, you can use the special `call` block.  The following example shows\na macro that takes advantage of the call functionality and how it can be\nused::\n\n    {% macro render_dialog(title, class='dialog') -%}\n        <div class=\"{{ class }}\">\n            <h2>{{ title }}</h2>\n            <div class=\"contents\">\n                {{ caller() }}\n            </div>\n        </div>\n    {%- endmacro %}\n\n    {% call render_dialog('Hello World') %}\n        This is a simple dialog rendered by using a macro and\n        a call block.\n    {% endcall %}\n\nIt's also possible to pass arguments back to the call block.  This makes it\nuseful as a replacement for loops.  Generally speaking, a call block works\nexactly like a macro without a name.\n\nHere's an example of how a call block can be used with arguments::\n\n    {% macro dump_users(users) -%}\n        <ul>\n        {%- for user in users %}\n            <li><p>{{ user.username|e }}</p>{{ caller(user) }}</li>\n        {%- endfor %}\n        </ul>\n    {%- endmacro %}\n\n    {% call(user) dump_users(list_of_user) %}\n        <dl>\n            <dt>Realname</dt>\n            <dd>{{ user.realname|e }}</dd>\n            <dt>Description</dt>\n            <dd>{{ user.description }}</dd>\n        </dl>\n    {% endcall %}\n\n\nFilters\n~~~~~~~\n\nFilter sections allow you to apply regular Jinja filters on a block of\ntemplate data.  Just wrap the code in the special `filter` section::\n\n    {% filter upper %}\n        This text becomes uppercase\n    {% endfilter %}\n\nFilters that accept arguments can be called like this::\n\n    {% filter center(100) %}Center this{% endfilter %}\n\n.. _assignments:\n\nAssignments\n~~~~~~~~~~~\n\nInside code blocks, you can also assign values to variables.  Assignments at\ntop level (outside of blocks, macros or loops) are exported from the template\nlike top level macros and can be imported by other templates.\n\nAssignments use the `set` tag and can have multiple targets::\n\n    {% set navigation = [('index.html', 'Index'), ('about.html', 'About')] %}\n    {% set key, value = call_something() %}\n\n.. admonition:: Scoping Behavior\n\n    Please keep in mind that it is not possible to set variables inside a\n    block and have them show up outside of it.  This also applies to\n    loops.  The only exception to that rule are if statements which do not\n    introduce a scope.  As a result the following template is not going\n    to do what you might expect::\n\n        {% set iterated = false %}\n        {% for item in seq %}\n            {{ item }}\n            {% set iterated = true %}\n        {% endfor %}\n        {% if not iterated %} did not iterate {% endif %}\n\n    It is not possible with Jinja syntax to do this.  Instead use\n    alternative constructs like the loop else block or the special `loop`\n    variable::\n\n        {% for item in seq %}\n            {{ item }}\n        {% else %}\n            did not iterate\n        {% endfor %}\n\n    As of version 2.10 more complex use cases can be handled using namespace\n    objects which allow propagating of changes across scopes::\n\n        {% set ns = namespace(found=false) %}\n        {% for item in items %}\n            {% if item.check_something() %}\n                {% set ns.found = true %}\n            {% endif %}\n            * {{ item.title }}\n        {% endfor %}\n        Found item having something: {{ ns.found }}\n\n    Note that the ``obj.attr`` notation in the `set` tag is only allowed for\n    namespace objects; attempting to assign an attribute on any other object\n    will raise an exception.\n\n    .. versionadded:: 2.10 Added support for namespace objects\n\n\nBlock Assignments\n~~~~~~~~~~~~~~~~~\n\nIt's possible to use `set` as a block to assign the content of the block to a\nvariable. This can be used to create multi-line strings, since Jinja doesn't\nsupport Python's triple quotes (``\"\"\"``, ``'''``).\n\nInstead of using an equals sign and a value, you only write the variable name,\nand everything until ``{% endset %}`` is captured.\n\n.. code-block:: jinja\n\n    {% set navigation %}\n        <li><a href=\"/\">Index</a>\n        <li><a href=\"/downloads\">Downloads</a>\n    {% endset %}\n\nFilters applied to the variable name will be applied to the block's content.\n\n.. code-block:: jinja\n\n    {% set reply | wordwrap %}\n        You wrote:\n        {{ message }}\n    {% endset %}\n\n.. versionadded:: 2.8\n\n.. versionchanged:: 2.10\n\n    Block assignment supports filters.\n\n.. _extends:\n\nExtends\n~~~~~~~\n\nThe `extends` tag can be used to extend one template from another.  You can\nhave multiple `extends` tags in a file, but only one of them may be executed at\na time.\n\nSee the section about :ref:`template-inheritance` above.\n\n\n.. _blocks:\n\nBlocks\n~~~~~~\n\nBlocks are used for inheritance and act as both placeholders and replacements\nat the same time.  They are documented in detail in the\n:ref:`template-inheritance` section.\n\n\nInclude\n~~~~~~~\n\nThe ``include`` tag renders another template and outputs the result into\nthe current template.\n\n.. code-block:: jinja\n\n    {% include 'header.html' %}\n    Body goes here.\n    {% include 'footer.html' %}\n\nThe included template has access to context of the current template by\ndefault. Use ``without context`` to use a separate context instead.\n``with context`` is also valid, but is the default behavior. See\n:ref:`import-visibility`.\n\nThe included template can ``extend`` another template and override\nblocks in that template. However, the current template cannot override\nany blocks that the included template outputs.\n\nUse ``ignore missing`` to ignore the statement if the template does not\nexist. It must be placed *before* a context visibility statement.\n\n.. code-block:: jinja\n\n    {% include \"sidebar.html\" without context %}\n    {% include \"sidebar.html\" ignore missing %}\n    {% include \"sidebar.html\" ignore missing with context %}\n    {% include \"sidebar.html\" ignore missing without context %}\n\nIf a list of templates is given, each will be tried in order until one\nis not missing. This can be used with ``ignore missing`` to ignore if\nnone of the templates exist.\n\n.. code-block:: jinja\n\n    {% include ['page_detailed.html', 'page.html'] %}\n    {% include ['special_sidebar.html', 'sidebar.html'] ignore missing %}\n\nA variable, with either a template name or template object, can also be\npassed to the statement.\n\n.. _import:\n\nImport\n~~~~~~\n\nJinja supports putting often used code into macros.  These macros can go into\ndifferent templates and get imported from there.  This works similarly to the\nimport statements in Python.  It's important to know that imports are cached\nand imported templates don't have access to the current template variables,\njust the globals by default.  For more details about context behavior of\nimports and includes, see :ref:`import-visibility`.\n\nThere are two ways to import templates.  You can import a complete template\ninto a variable or request specific macros / exported variables from it.\n\nImagine we have a helper module that renders forms (called `forms.html`)::\n\n    {% macro input(name, value='', type='text') -%}\n        <input type=\"{{ type }}\" value=\"{{ value|e }}\" name=\"{{ name }}\">\n    {%- endmacro %}\n\n    {%- macro textarea(name, value='', rows=10, cols=40) -%}\n        <textarea name=\"{{ name }}\" rows=\"{{ rows }}\" cols=\"{{ cols\n            }}\">{{ value|e }}</textarea>\n    {%- endmacro %}\n\nThe easiest and most flexible way to access a template's variables\nand macros is to import the whole template module into a variable.\nThat way, you can access the attributes::\n\n    {% import 'forms.html' as forms %}\n    <dl>\n        <dt>Username</dt>\n        <dd>{{ forms.input('username') }}</dd>\n        <dt>Password</dt>\n        <dd>{{ forms.input('password', type='password') }}</dd>\n    </dl>\n    <p>{{ forms.textarea('comment') }}</p>\n\n\nAlternatively, you can import specific names from a template into the current\nnamespace::\n\n    {% from 'forms.html' import input as input_field, textarea %}\n    <dl>\n        <dt>Username</dt>\n        <dd>{{ input_field('username') }}</dd>\n        <dt>Password</dt>\n        <dd>{{ input_field('password', type='password') }}</dd>\n    </dl>\n    <p>{{ textarea('comment') }}</p>\n\nMacros and variables starting with one or more underscores are private and\ncannot be imported.\n\n.. versionchanged:: 2.4\n   If a template object was passed to the template context, you can\n   import from that object.\n\n\n.. _import-visibility:\n\nImport Context Behavior\n-----------------------\n\nBy default, included templates are passed the current context and imported\ntemplates are not.  The reason for this is that imports, unlike includes,\nare cached; as imports are often used just as a module that holds macros.\n\nThis behavior can be changed explicitly: by adding `with context`\nor `without context` to the import/include directive, the current context\ncan be passed to the template and caching is disabled automatically.\n\nHere are two examples::\n\n    {% from 'forms.html' import input with context %}\n    {% include 'header.html' without context %}\n\n.. admonition:: Note\n\n    In Jinja 2.0, the context that was passed to the included template\n    did not include variables defined in the template.  As a matter of\n    fact, this did not work::\n\n        {% for box in boxes %}\n            {% include \"render_box.html\" %}\n        {% endfor %}\n\n    The included template ``render_box.html`` is *not* able to access\n    `box` in Jinja 2.0. As of Jinja 2.1, ``render_box.html`` *is* able\n    to do so.\n\n\n.. _expressions:\n\nExpressions\n-----------\n\nJinja allows basic expressions everywhere.  These work very similarly to\nregular Python; even if you're not working with Python\nyou should feel comfortable with it.\n\nLiterals\n~~~~~~~~\n\nThe simplest form of expressions are literals.  Literals are representations\nfor Python objects such as strings and numbers.  The following literals exist:\n\n``\"Hello World\"``\n    Everything between two double or single quotes is a string.  They are\n    useful whenever you need a string in the template (e.g. as\n    arguments to function calls and filters, or just to extend or include a\n    template).\n\n``42`` / ``123_456``\n    Integers are whole numbers without a decimal part. The '_' character\n    can be used to separate groups for legibility.\n\n``42.23`` / ``42.1e2`` / ``123_456.789``\n    Floating point numbers can be written using a '.' as a decimal mark.\n    They can also be written in scientific notation with an upper or\n    lower case 'e' to indicate the exponent part. The '_' character can\n    be used to separate groups for legibility, but cannot be used in the\n    exponent part.\n\n``['list', 'of', 'objects']``\n    Everything between two brackets is a list.  Lists are useful for storing\n    sequential data to be iterated over.  For example, you can easily\n    create a list of links using lists and tuples for (and with) a for loop::\n\n        <ul>\n        {% for href, caption in [('index.html', 'Index'), ('about.html', 'About'),\n                                 ('downloads.html', 'Downloads')] %}\n            <li><a href=\"{{ href }}\">{{ caption }}</a></li>\n        {% endfor %}\n        </ul>\n\n``('tuple', 'of', 'values')``\n    Tuples are like lists that cannot be modified (\"immutable\").  If a tuple\n    only has one item, it must be followed by a comma (``('1-tuple',)``).\n    Tuples are usually used to represent items of two or more elements.\n    See the list example above for more details.\n\n``{'dict': 'of', 'key': 'and', 'value': 'pairs'}``\n    A dict in Python is a structure that combines keys and values.  Keys must\n    be unique and always have exactly one value.  Dicts are rarely used in\n    templates; they are useful in some rare cases such as the :func:`xmlattr`\n    filter.\n\n``true`` / ``false``\n    ``true`` is always true and ``false`` is always false.\n\n.. admonition:: Note\n\n    The special constants `true`, `false`, and `none` are indeed lowercase.\n    Because that caused confusion in the past, (`True` used to expand\n    to an undefined variable that was considered false),\n    all three can now also be written in title case\n    (`True`, `False`, and `None`).\n    However, for consistency, (all Jinja identifiers are lowercase)\n    you should use the lowercase versions.\n\nMath\n~~~~\n\nJinja allows you to calculate with values.  This is rarely useful in templates\nbut exists for completeness' sake.  The following operators are supported:\n\n``+``\n    Adds two objects together. Usually the objects are numbers, but if both are\n    strings or lists, you can concatenate them this way.  This, however, is not\n    the preferred way to concatenate strings!  For string concatenation, have\n    a look-see at the ``~`` operator.  ``{{ 1 + 1 }}`` is ``2``.\n\n``-``\n    Subtract the second number from the first one.  ``{{ 3 - 2 }}`` is ``1``.\n\n``/``\n    Divide two numbers.  The return value will be a floating point number.\n    ``{{ 1 / 2 }}`` is ``{{ 0.5 }}``.\n\n``//``\n    Divide two numbers and return the truncated integer result.\n    ``{{ 20 // 7 }}`` is ``2``.\n\n``%``\n    Calculate the remainder of an integer division.  ``{{ 11 % 7 }}`` is ``4``.\n\n``*``\n    Multiply the left operand with the right one.  ``{{ 2 * 2 }}`` would\n    return ``4``.  This can also be used to repeat a string multiple times.\n    ``{{ '=' * 80 }}`` would print a bar of 80 equal signs.\n\n``**``\n    Raise the left operand to the power of the right operand.\n    ``{{ 2**3 }}`` would return ``8``.\n\n    Unlike Python, chained pow is evaluated left to right.\n    ``{{ 3**3**3 }}`` is evaluated as ``(3**3)**3`` in Jinja, but would\n    be evaluated as ``3**(3**3)`` in Python. Use parentheses in Jinja\n    to be explicit about what order you want. It is usually preferable\n    to do extended math in Python and pass the results to ``render``\n    rather than doing it in the template.\n\n    This behavior may be changed in the future to match Python, if it's\n    possible to introduce an upgrade path.\n\n\nComparisons\n~~~~~~~~~~~\n\n``==``\n    Compares two objects for equality.\n\n``!=``\n    Compares two objects for inequality.\n\n``>``\n    ``true`` if the left hand side is greater than the right hand side.\n\n``>=``\n    ``true`` if the left hand side is greater or equal to the right hand side.\n\n``<``\n    ``true`` if the left hand side is lower than the right hand side.\n\n``<=``\n    ``true`` if the left hand side is lower or equal to the right hand side.\n\nLogic\n~~~~~\n\nFor ``if`` statements, ``for`` filtering, and ``if`` expressions, it can be\nuseful to combine multiple expressions.\n\n``and``\n    For ``x and y``, if ``x`` is false, then the value is ``x``, else ``y``. In\n    a boolean context, this will be treated as ``True`` if both operands are\n    truthy.\n\n``or``\n    For ``x or y``, if ``x`` is true, then the value is ``x``, else ``y``. In a\n    boolean context, this will be treated as ``True`` if at least one operand is\n    truthy.\n\n``not``\n    For ``not x``, if ``x`` is false, then the value is ``True``, else\n    ``False``.\n\n    Prefer negating ``is`` and ``in`` using their infix notation:\n    ``foo is not bar`` instead of ``not foo is bar``; ``foo not in bar`` instead\n    of ``not foo in bar``. All other expressions require prefix notation:\n    ``not (foo and bar).``\n\n``(expr)``\n    Parentheses group an expression. This is used to change evaluation order, or\n    to make a long expression easier to read or less ambiguous.\n\n\nOther Operators\n~~~~~~~~~~~~~~~\n\nThe following operators are very useful but don't fit into any of the other\ntwo categories:\n\n``in``\n    Perform a sequence / mapping containment test.  Returns true if the left\n    operand is contained in the right.  ``{{ 1 in [1, 2, 3] }}`` would, for\n    example, return true.\n\n``is``\n    Performs a :ref:`test <tests>`.\n\n``|`` (pipe, vertical bar)\n    Applies a :ref:`filter <filters>`.\n\n``~`` (tilde)\n    Converts all operands into strings and concatenates them.\n\n    ``{{ \"Hello \" ~ name ~ \"!\" }}`` would return (assuming `name` is set\n    to ``'John'``) ``Hello John!``.\n\n``()``\n    Call a callable: ``{{ post.render() }}``.  Inside of the parentheses you\n    can use positional arguments and keyword arguments like in Python:\n\n    ``{{ post.render(user, full=true) }}``.\n\n``.`` / ``[]``\n    Get an attribute of an object.  (See :ref:`variables`)\n\n\n.. _if-expression:\n\nIf Expression\n~~~~~~~~~~~~~\n\nIt is also possible to use inline `if` expressions.  These are useful in some\nsituations.  For example, you can use this to extend from one template if a\nvariable is defined, otherwise from the default layout template::\n\n    {% extends layout_template if layout_template is defined else 'default.html' %}\n\nThe general syntax is ``<do something> if <something is true> else <do\nsomething else>``.\n\nThe `else` part is optional.  If not provided, the else block implicitly\nevaluates into an :class:`Undefined` object (regardless of what ``undefined``\nin the environment is set to):\n\n.. code-block:: jinja\n\n    {{ \"[{}]\".format(page.title) if page.title }}\n\n\n.. _python-methods:\n\nPython Methods\n~~~~~~~~~~~~~~\n\nYou can also use any of the methods defined on a variable's type.\nThe value returned from the method invocation is used as the value of the expression.\nHere is an example that uses methods defined on strings (where ``page.title`` is a string):\n\n.. code-block:: text\n\n    {{ page.title.capitalize() }}\n\nThis works for methods on user-defined types. For example, if variable\n``f`` of type ``Foo`` has a method ``bar`` defined on it, you can do the\nfollowing:\n\n.. code-block:: text\n\n    {{ f.bar(value) }}\n\nOperator methods also work as expected. For example, ``%`` implements\nprintf-style for strings:\n\n.. code-block:: text\n\n    {{ \"Hello, %s!\" % name }}\n\nAlthough you should prefer the ``.format`` method for that case (which\nis a bit contrived in the context of rendering a template):\n\n.. code-block:: text\n\n    {{ \"Hello, {}!\".format(name) }}\n\n\n.. _builtin-filters:\n\nList of Builtin Filters\n-----------------------\n\n.. py:currentmodule:: jinja-filters\n\n.. jinja:filters:: jinja2.defaults.DEFAULT_FILTERS\n\n\n.. _builtin-tests:\n\nList of Builtin Tests\n---------------------\n\n.. py:currentmodule:: jinja-tests\n\n.. jinja:tests:: jinja2.defaults.DEFAULT_TESTS\n\n\n.. _builtin-globals:\n\nList of Global Functions\n------------------------\n\nThe following functions are available in the global scope by default:\n\n.. py:currentmodule:: jinja-globals\n\n.. function:: range([start,] stop[, step])\n\n    Return a list containing an arithmetic progression of integers.\n    ``range(i, j)`` returns ``[i, i+1, i+2, ..., j-1]``;\n    start (!) defaults to ``0``.\n    When step is given, it specifies the increment (or decrement).\n    For example, ``range(4)`` and ``range(0, 4, 1)`` return ``[0, 1, 2, 3]``.\n    The end point is omitted!\n    These are exactly the valid indices for a list of 4 elements.\n\n    This is useful to repeat a template block multiple times, e.g.\n    to fill a list.  Imagine you have 7 users in the list but you want to\n    render three empty items to enforce a height with CSS::\n\n        <ul>\n        {% for user in users %}\n            <li>{{ user.username }}</li>\n        {% endfor %}\n        {% for number in range(10 - users|count) %}\n            <li class=\"empty\"><span>...</span></li>\n        {% endfor %}\n        </ul>\n\n.. function:: lipsum(n=5, html=True, min=20, max=100)\n\n    Generates some lorem ipsum for the template.  By default, five paragraphs\n    of HTML are generated with each paragraph between 20 and 100 words.\n    If html is False, regular text is returned.  This is useful to generate simple\n    contents for layout testing.\n\n.. function:: dict(\\**items)\n\n    A convenient alternative to dict literals.  ``{'foo': 'bar'}`` is the same\n    as ``dict(foo='bar')``.\n\n.. class:: cycler(\\*items)\n\n    Cycle through values by yielding them one at a time, then restarting\n    once the end is reached.\n\n    Similar to ``loop.cycle``, but can be used outside loops or across\n    multiple loops. For example, render a list of folders and files in a\n    list, alternating giving them \"odd\" and \"even\" classes.\n\n    .. code-block:: html+jinja\n\n        {% set row_class = cycler(\"odd\", \"even\") %}\n        <ul class=\"browser\">\n        {% for folder in folders %}\n          <li class=\"folder {{ row_class.next() }}\">{{ folder }}\n        {% endfor %}\n        {% for file in files %}\n          <li class=\"file {{ row_class.next() }}\">{{ file }}\n        {% endfor %}\n        </ul>\n\n    :param items: Each positional argument will be yielded in the order\n        given for each cycle.\n\n    .. versionadded:: 2.1\n\n    .. property:: current\n\n        Return the current item. Equivalent to the item that will be\n        returned next time :meth:`next` is called.\n\n    .. method:: next()\n\n        Return the current item, then advance :attr:`current` to the\n        next item.\n\n    .. method:: reset()\n\n        Resets the current item to the first item.\n\n.. class:: joiner(sep=', ')\n\n    A tiny helper that can be used to \"join\" multiple sections.  A joiner is\n    passed a string and will return that string every time it's called, except\n    the first time (in which case it returns an empty string).  You can\n    use this to join things::\n\n        {% set pipe = joiner(\"|\") %}\n        {% if categories %} {{ pipe() }}\n            Categories: {{ categories|join(\", \") }}\n        {% endif %}\n        {% if author %} {{ pipe() }}\n            Author: {{ author() }}\n        {% endif %}\n        {% if can_edit %} {{ pipe() }}\n            <a href=\"?action=edit\">Edit</a>\n        {% endif %}\n\n    .. versionadded:: 2.1\n\n.. class:: namespace(...)\n\n    Creates a new container that allows attribute assignment using the\n    ``{% set %}`` tag::\n\n        {% set ns = namespace() %}\n        {% set ns.foo = 'bar' %}\n\n    The main purpose of this is to allow carrying a value from within a loop\n    body to an outer scope.  Initial values can be provided as a dict, as\n    keyword arguments, or both (same behavior as Python's `dict` constructor)::\n\n        {% set ns = namespace(found=false) %}\n        {% for item in items %}\n            {% if item.check_something() %}\n                {% set ns.found = true %}\n            {% endif %}\n            * {{ item.title }}\n        {% endfor %}\n        Found item having something: {{ ns.found }}\n\n    .. versionadded:: 2.10\n\n    .. versionchanged:: 3.2\n        Namespace attributes can be assigned to in multiple assignment.\n\n\nExtensions\n----------\n\n.. py:currentmodule:: jinja2\n\nThe following sections cover the built-in Jinja extensions that may be\nenabled by an application.  An application could also provide further\nextensions not covered by this documentation; in which case there should\nbe a separate document explaining said :ref:`extensions\n<jinja-extensions>`.\n\n\n.. _i18n-in-templates:\n\ni18n\n~~~~\n\nIf the :ref:`i18n-extension` is enabled, it's possible to mark text in\nthe template as translatable. To mark a section as translatable, use a\n``trans`` block:\n\n.. code-block:: jinja\n\n    {% trans %}Hello, {{ user }}!{% endtrans %}\n\nInside the block, no statements are allowed, only text and simple\nvariable tags.\n\nVariable tags can only be a name, not attribute access, filters, or\nother expressions. To use an expression, bind it to a name in the\n``trans`` tag for use in the block.\n\n.. code-block:: jinja\n\n    {% trans user=user.username %}Hello, {{ user }}!{% endtrans %}\n\nTo bind more than one expression, separate each with a comma (``,``).\n\n.. code-block:: jinja\n\n    {% trans book_title=book.title, author=author.name %}\n    This is {{ book_title }} by {{ author }}\n    {% endtrans %}\n\nTo pluralize, specify both the singular and plural forms separated by\nthe ``pluralize`` tag.\n\n.. code-block:: jinja\n\n    {% trans count=list|length %}\n    There is {{ count }} {{ name }} object.\n    {% pluralize %}\n    There are {{ count }} {{ name }} objects.\n    {% endtrans %}\n\nBy default, the first variable in a block is used to determine whether\nto use singular or plural form. If that isn't correct, specify the\nvariable used for pluralizing as a parameter to ``pluralize``.\n\n.. code-block:: jinja\n\n    {% trans ..., user_count=users|length %}...\n    {% pluralize user_count %}...{% endtrans %}\n\nWhen translating blocks of text, whitespace and linebreaks result in\nhard to read and error-prone translation strings. To avoid this, a trans\nblock can be marked as trimmed, which will replace all linebreaks and\nthe whitespace surrounding them with a single space and remove leading\nand trailing whitespace.\n\n.. code-block:: jinja\n\n    {% trans trimmed book_title=book.title %}\n        This is {{ book_title }}.\n        You should read it!\n    {% endtrans %}\n\nThis results in ``This is %(book_title)s. You should read it!`` in the\ntranslation file.\n\nIf trimming is enabled globally, the ``notrimmed`` modifier can be used\nto disable it for a block.\n\n.. versionadded:: 2.10\n   The ``trimmed`` and ``notrimmed`` modifiers have been added.\n\nIf the translation depends on the context that the message appears in,\nthe ``pgettext`` and ``npgettext`` functions take a ``context`` string\nas the first argument, which is used to select the appropriate\ntranslation. To specify a context with the ``{% trans %}`` tag, provide\na string as the first token after ``trans``.\n\n.. code-block:: jinja\n\n    {% trans \"fruit\" %}apple{% endtrans %}\n    {% trans \"fruit\" trimmed count -%}\n        1 apple\n    {%- pluralize -%}\n        {{ count }} apples\n    {%- endtrans %}\n\n.. versionadded:: 3.1\n    A context can be passed to the ``trans`` tag to use ``pgettext`` and\n    ``npgettext``.\n\nIt's possible to translate strings in expressions with these functions:\n\n-   ``_(message)``: Alias for ``gettext``.\n-   ``gettext(message)``: Translate a message.\n-   ``ngettext(singular, plural, n)``: Translate a singular or plural\n    message based on a count variable.\n-   ``pgettext(context, message)``: Like ``gettext()``, but picks the\n    translation based on the context string.\n-   ``npgettext(context, singular, plural, n)``: Like ``npgettext()``,\n    but picks the translation based on the context string.\n\nYou can print a translated string like this:\n\n.. code-block:: jinja\n\n    {{ _(\"Hello, World!\") }}\n\nTo use placeholders, use the ``format`` filter.\n\n.. code-block:: jinja\n\n    {{ _(\"Hello, %(user)s!\")|format(user=user.username) }}\n\nAlways use keyword arguments to ``format``, as other languages may not\nuse the words in the same order.\n\nIf :ref:`newstyle-gettext` calls are activated, using placeholders is\neasier. Formatting is part of the ``gettext`` call instead of using the\n``format`` filter.\n\n.. sourcecode:: jinja\n\n    {{ gettext('Hello World!') }}\n    {{ gettext('Hello %(name)s!', name='World') }}\n    {{ ngettext('%(num)d apple', '%(num)d apples', apples|count) }}\n\nThe ``ngettext`` function's format string automatically receives the\ncount as a ``num`` parameter in addition to the given parameters.\n\n\nExpression Statement\n~~~~~~~~~~~~~~~~~~~~\n\nIf the expression-statement extension is loaded, a tag called `do` is available\nthat works exactly like the regular variable expression (``{{ ... }}``); except\nit doesn't print anything.  This can be used to modify lists::\n\n    {% do navigation.append('a string') %}\n\n\nLoop Controls\n~~~~~~~~~~~~~\n\nIf the application enables the :ref:`loopcontrols-extension`, it's possible to\nuse `break` and `continue` in loops.  When `break` is reached, the loop is\nterminated;  if `continue` is reached, the processing is stopped and continues\nwith the next iteration.\n\nHere's a loop that skips every second item::\n\n    {% for user in users %}\n        {%- if loop.index is even %}{% continue %}{% endif %}\n        ...\n    {% endfor %}\n\nLikewise, a loop that stops processing after the 10th iteration::\n\n    {% for user in users %}\n        {%- if loop.index >= 10 %}{% break %}{% endif %}\n    {%- endfor %}\n\nNote that ``loop.index`` starts with 1, and ``loop.index0`` starts with 0\n(See: :ref:`for-loop`).\n\n\nDebug Statement\n~~~~~~~~~~~~~~~\n\nIf the :ref:`debug-extension` is enabled, a ``{% debug %}`` tag will be\navailable to dump the current context as well as the available filters\nand tests. This is useful to see what's available to use in the template\nwithout setting up a debugger.\n\n.. code-block:: html+jinja\n\n    <pre>{% debug %}</pre>\n\n.. code-block:: text\n\n    {'context': {'cycler': <class 'jinja2.utils.Cycler'>,\n                 ...,\n                 'namespace': <class 'jinja2.utils.Namespace'>},\n     'filters': ['abs', 'attr', 'batch', 'capitalize', 'center', 'count', 'd',\n                 ..., 'urlencode', 'urlize', 'wordcount', 'wordwrap', 'xmlattr'],\n     'tests': ['!=', '<', '<=', '==', '>', '>=', 'callable', 'defined',\n               ..., 'odd', 'sameas', 'sequence', 'string', 'undefined', 'upper']}\n\n\nWith Statement\n~~~~~~~~~~~~~~\n\n.. versionadded:: 2.3\n\nThe with statement makes it possible to create a new inner scope.\nVariables set within this scope are not visible outside of the scope.\n\nWith in a nutshell::\n\n    {% with %}\n        {% set foo = 42 %}\n        {{ foo }}           foo is 42 here\n    {% endwith %}\n    foo is not visible here any longer\n\nBecause it is common to set variables at the beginning of the scope,\nyou can do that within the `with` statement.  The following two examples\nare equivalent::\n\n    {% with foo = 42 %}\n        {{ foo }}\n    {% endwith %}\n\n    {% with %}\n        {% set foo = 42 %}\n        {{ foo }}\n    {% endwith %}\n\nAn important note on scoping here.  In Jinja versions before 2.9 the\nbehavior of referencing one variable to another had some unintended\nconsequences.  In particular one variable could refer to another defined\nin the same with block's opening statement.  This caused issues with the\ncleaned up scoping behavior and has since been improved.  In particular\nin newer Jinja versions the following code always refers to the variable\n`a` from outside the `with` block::\n\n    {% with a={}, b=a.attribute %}...{% endwith %}\n\nIn earlier Jinja versions the `b` attribute would refer to the results of\nthe first attribute.  If you depend on this behavior you can rewrite it to\nuse the ``set`` tag::\n\n    {% with a={} %}\n        {% set b = a.attribute %}\n    {% endwith %}\n\n.. admonition:: Extension\n\n   In older versions of Jinja (before 2.9) it was required to enable this\n   feature with an extension.  It's now enabled by default.\n\n.. _autoescape-overrides:\n\nAutoescape Overrides\n--------------------\n\n.. versionadded:: 2.4\n\nIf you want you can activate and deactivate the autoescaping from within\nthe templates.\n\nExample::\n\n    {% autoescape true %}\n        Autoescaping is active within this block\n    {% endautoescape %}\n\n    {% autoescape false %}\n        Autoescaping is inactive within this block\n    {% endautoescape %}\n\nAfter an `endautoescape` the behavior is reverted to what it was before.\n\n.. admonition:: Extension\n\n   In older versions of Jinja (before 2.9) it was required to enable this\n   feature with an extension.  It's now enabled by default.\n"
  },
  {
    "path": "docs/tricks.rst",
    "content": "Tips and Tricks\n===============\n\n.. highlight:: html+jinja\n\nThis part of the documentation shows some tips and tricks for Jinja\ntemplates.\n\n\n.. _null-default-fallback:\n\nNull-Default Fallback\n---------------------\n\nJinja supports dynamic inheritance and does not distinguish between parent\nand child template as long as no `extends` tag is visited.  While this leads\nto the surprising behavior that everything before the first `extends` tag\nincluding whitespace is printed out instead of being ignored, it can be used\nfor a neat trick.\n\nUsually child templates extend from one template that adds a basic HTML\nskeleton.  However it's possible to put the `extends` tag into an `if` tag to\nonly extend from the layout template if the `standalone` variable evaluates\nto false, which it does by default if it's not defined.  Additionally a very\nbasic skeleton is added to the file so that if it's indeed rendered with\n`standalone` set to `True` a very basic HTML skeleton is added::\n\n    {% if not standalone %}{% extends 'default.html' %}{% endif -%}\n    <!DOCTYPE html>\n    <title>{% block title %}The Page Title{% endblock %}</title>\n    <link rel=\"stylesheet\" href=\"style.css\" type=\"text/css\">\n    {% block body %}\n      <p>This is the page body.</p>\n    {% endblock %}\n\n\nAlternating Rows\n----------------\n\nIf you want to have different styles for each row of a table or\nlist you can use the `cycle` method on the `loop` object::\n\n    <ul>\n    {% for row in rows %}\n      <li class=\"{{ loop.cycle('odd', 'even') }}\">{{ row }}</li>\n    {% endfor %}\n    </ul>\n\n`cycle` can take an unlimited number of strings.  Each time this\ntag is encountered the next item from the list is rendered.\n\n\nHighlighting Active Menu Items\n------------------------------\n\nOften you want to have a navigation bar with an active navigation\nitem.  This is really simple to achieve.  Because assignments outside\nof `block`\\s in child templates are global and executed before the layout\ntemplate is evaluated it's possible to define the active menu item in the\nchild template::\n\n    {% extends \"layout.html\" %}\n    {% set active_page = \"index\" %}\n\nThe layout template can then access `active_page`.  Additionally it makes\nsense to define a default for that variable::\n\n    {% set navigation_bar = [\n        ('/', 'index', 'Index'),\n        ('/downloads/', 'downloads', 'Downloads'),\n        ('/about/', 'about', 'About')\n    ] -%}\n    {% set active_page = active_page|default('index') -%}\n    ...\n    <ul id=\"navigation\">\n    {% for href, id, caption in navigation_bar %}\n      <li{% if id == active_page %} class=\"active\"{% endif %}>\n      <a href=\"{{ href|e }}\">{{ caption|e }}</a></li>\n    {% endfor %}\n    </ul>\n    ...\n\n.. _accessing-the-parent-loop:\n\nAccessing the parent Loop\n-------------------------\n\nThe special `loop` variable always points to the innermost loop.  If it's\ndesired to have access to an outer loop it's possible to alias it::\n\n    <table>\n    {% for row in table %}\n      <tr>\n      {% set rowloop = loop %}\n      {% for cell in row %}\n        <td id=\"cell-{{ rowloop.index }}-{{ loop.index }}\">{{ cell }}</td>\n      {% endfor %}\n      </tr>\n    {% endfor %}\n    </table>\n"
  },
  {
    "path": "examples/basic/cycle.py",
    "content": "from jinja2 import Environment\n\nenv = Environment(\n    line_statement_prefix=\"#\", variable_start_string=\"${\", variable_end_string=\"}\"\n)\nprint(\n    env.from_string(\n        \"\"\"\\\n<ul>\n# for item in range(10)\n    <li class=\"${loop.cycle('odd', 'even')}\">${item}</li>\n# endfor\n</ul>\\\n\"\"\"\n    ).render()\n)\n"
  },
  {
    "path": "examples/basic/debugger.py",
    "content": "from jinja2 import Environment\nfrom jinja2.loaders import FileSystemLoader\n\nenv = Environment(loader=FileSystemLoader(\"templates\"))\ntmpl = env.get_template(\"broken.html\")\nprint(tmpl.render(seq=[3, 2, 4, 5, 3, 2, 0, 2, 1]))\n"
  },
  {
    "path": "examples/basic/inheritance.py",
    "content": "from jinja2 import Environment\nfrom jinja2.loaders import DictLoader\n\nenv = Environment(\n    loader=DictLoader(\n        {\n            \"a\": \"[A[{% block body %}{% endblock %}]]\",\n            \"b\": \"{% extends 'a' %}{% block body %}[B]{% endblock %}\",\n            \"c\": \"{% extends 'b' %}{% block body %}###{{ super() }}###{% endblock %}\",\n        }\n    )\n)\nprint(env.get_template(\"c\").render())\n"
  },
  {
    "path": "examples/basic/templates/broken.html",
    "content": "{% from 'subbroken.html' import may_break %}\n<ul>\n{% for item in seq %}\n  <li>{{ may_break(item) }}</li>\n{% endfor %}\n</ul>\n"
  },
  {
    "path": "examples/basic/templates/subbroken.html",
    "content": "{% macro may_break(item) -%}\n  [{{ item / 0 }}]\n{%- endmacro %}\n"
  },
  {
    "path": "examples/basic/test.py",
    "content": "from jinja2 import Environment\nfrom jinja2.loaders import DictLoader\n\nenv = Environment(\n    loader=DictLoader(\n        {\n            \"child.html\": \"\"\"\\\n{% extends default_layout or 'default.html' %}\n{% import 'helpers.html' as helpers %}\n{% macro get_the_answer() %}42{% endmacro %}\n{% set title = 'Hello World' %}\n{% block body %}\n    {{ get_the_answer() }}\n    {{ helpers.conspirate() }}\n{% endblock %}\n\"\"\",\n            \"default.html\": \"\"\"\\\n<!doctype html>\n<title>{{ title }}</title>\n{% block body %}{% endblock %}\n\"\"\",\n            \"helpers.html\": \"\"\"\\\n{% macro conspirate() %}23{% endmacro %}\n\"\"\",\n        }\n    )\n)\ntmpl = env.get_template(\"child.html\")\nprint(tmpl.render())\n"
  },
  {
    "path": "examples/basic/test_filter_and_linestatements.py",
    "content": "from jinja2 import Environment\n\nenv = Environment(\n    line_statement_prefix=\"%\", variable_start_string=\"${\", variable_end_string=\"}\"\n)\ntmpl = env.from_string(\n    \"\"\"\\\n% macro foo()\n    ${caller(42)}\n% endmacro\n<ul>\n% for item in seq\n    <li>${item}</li>\n% endfor\n</ul>\n% call(var) foo()\n    [${var}]\n% endcall\n% filter escape\n    <hello world>\n    % for item in [1, 2, 3]\n      -  ${item}\n    % endfor\n% endfilter\n\"\"\"\n)\nprint(tmpl.render(seq=range(10)))\n"
  },
  {
    "path": "examples/basic/test_loop_filter.py",
    "content": "from jinja2 import Environment\n\ntmpl = Environment().from_string(\n    \"\"\"\\\n<ul>\n{%- for item in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] if item % 2 == 0 %}\n    <li>{{ loop.index }} / {{ loop.length }}: {{ item }}</li>\n{%- endfor %}\n</ul>\nif condition: {{ 1 if foo else 0 }}\n\"\"\"\n)\nprint(tmpl.render(foo=True))\n"
  },
  {
    "path": "examples/basic/translate.py",
    "content": "from jinja2 import Environment\n\nenv = Environment(extensions=[\"jinja2.ext.i18n\"])\nenv.globals[\"gettext\"] = {\"Hello %(user)s!\": \"Hallo %(user)s!\"}.__getitem__\nenv.globals[\"ngettext\"] = lambda s, p, n: {\n    \"%(count)s user\": \"%(count)d Benutzer\",\n    \"%(count)s users\": \"%(count)d Benutzer\",\n}[s if n == 1 else p]\nprint(\n    env.from_string(\n        \"\"\"\\\n{% trans %}Hello {{ user }}!{% endtrans %}\n{% trans count=users|count -%}\n{{ count }} user{% pluralize %}{{ count }} users\n{% endtrans %}\n\"\"\"\n    ).render(user=\"someone\", users=[1, 2, 3])\n)\n"
  },
  {
    "path": "pyproject.toml",
    "content": "[project]\nname = \"Jinja2\"\nversion = \"3.2.0.dev\"\ndescription = \"A very fast and expressive template engine.\"\nreadme = \"README.md\"\nlicense = \"BSD-3-Clause\"\nlicense-files = [\"LICENSE.txt\"]\nmaintainers = [{name = \"Pallets\", email = \"contact@palletsprojects.com\"}]\nclassifiers = [\n    \"Development Status :: 5 - Production/Stable\",\n    \"Environment :: Web Environment\",\n    \"Intended Audience :: Developers\",\n    \"Operating System :: OS Independent\",\n    \"Programming Language :: Python\",\n    \"Topic :: Internet :: WWW/HTTP :: Dynamic Content\",\n    \"Topic :: Text Processing :: Markup :: HTML\",\n    \"Typing :: Typed\",\n]\nrequires-python = \">=3.10\"\ndependencies = [\"MarkupSafe>=3.0\"]\n\n[project.urls]\nDonate = \"https://palletsprojects.com/donate\"\nDocumentation = \"https://jinja.palletsprojects.com/\"\nChanges = \"https://jinja.palletsprojects.com/page/changes/\"\nSource = \"https://github.com/pallets/jinja/\"\nChat = \"https://discord.gg/pallets\"\n\n[project.optional-dependencies]\ni18n = [\"Babel>=2.17\"]\n\n[dependency-groups]\ndev = [\n    \"ruff\",\n    \"tox\",\n    \"tox-uv\",\n]\ndocs = [\n    \"pallets-sphinx-themes\",\n    \"sphinx\",\n    \"sphinxcontrib-log-cabinet\",\n]\ndocs-auto = [\n    \"sphinx-autobuild\",\n]\ngha-update = [\n    \"gha-update ; python_full_version >= '3.12'\",\n]\npre-commit = [\n    \"pre-commit\",\n    \"pre-commit-uv\",\n]\ntests = [\n    \"pytest\",\n    \"pytest-timeout\",\n    \"trio\"\n]\ntyping = [\n    \"mypy\",\n    \"pyright\",\n    \"pytest\",\n]\n\n[build-system]\nrequires = [\"flit_core<4\"]\nbuild-backend = \"flit_core.buildapi\"\n\n[tool.flit.module]\nname = \"jinja2\"\n\n[tool.flit.sdist]\ninclude = [\n    \"docs/\",\n    \"examples/\",\n    \"tests/\",\n    \"CHANGES.rst\",\n    \"uv.lock\"\n]\nexclude = [\n    \"docs/_build/\",\n]\n\n[tool.uv]\ndefault-groups = [\"dev\", \"pre-commit\", \"tests\", \"typing\"]\n\n[tool.pytest.ini_options]\ntestpaths = [\"tests\"]\nfilterwarnings = [\n    \"error\",\n]\n\n[tool.coverage.run]\nbranch = true\nsource = [\"jinja2\", \"tests\"]\n\n[tool.coverage.paths]\nsource = [\"src\", \"*/site-packages\"]\n\n[tool.coverage.report]\nexclude_also = [\n    \"if t.TYPE_CHECKING\",\n    \"raise NotImplementedError\",\n    \": \\\\.{3}\",\n]\n\n[tool.mypy]\npython_version = \"3.10\"\nfiles = [\"src\"]\nshow_error_codes = true\npretty = true\nstrict = true\n\n[tool.pyright]\npythonVersion = \"3.10\"\ninclude = [\"src\"]\ntypeCheckingMode = \"standard\"\n\n[tool.ruff]\nsrc = [\"src\"]\nfix = true\nshow-fixes = true\noutput-format = \"full\"\n\n[tool.ruff.lint]\nselect = [\n    \"B\",  # flake8-bugbear\n    \"E\",  # pycodestyle error\n    \"F\",  # pyflakes\n    \"I\",  # isort\n    \"UP\",  # pyupgrade\n    \"W\",  # pycodestyle warning\n]\nignore = [\n    \"UP038\",  # keep isinstance tuple\n]\n\n[tool.ruff.lint.isort]\nforce-single-line = true\norder-by-type = false\n\n[tool.gha-update]\ntag-only = [\n    \"slsa-framework/slsa-github-generator\",\n]\n\n[tool.tox]\nenv_list = [\n    \"py3.13\", \"py3.12\", \"py3.11\", \"py3.10\",\n    \"pypy3.11\",\n    \"style\",\n    \"typing\",\n    \"docs\",\n]\n\n[tool.tox.env_run_base]\ndescription = \"pytest on latest dependency versions\"\nrunner = \"uv-venv-lock-runner\"\npackage = \"wheel\"\nwheel_build_env = \".pkg\"\nconstrain_package_deps = true\nuse_frozen_constraints = true\ndependency_groups = [\"tests\"]\ncommands = [[\n    \"pytest\", \"-v\", \"--tb=short\", \"--basetemp={env_tmp_dir}\",\n    {replace = \"posargs\", default = [], extend = true},\n]]\n\n[tool.tox.env.style]\ndescription = \"run all pre-commit hooks on all files\"\ndependency_groups = [\"pre-commit\"]\nskip_install = true\ncommands = [[\"pre-commit\", \"run\", \"--all-files\"]]\n\n[tool.tox.env.typing]\ndescription = \"run static type checkers\"\ndependency_groups = [\"typing\"]\ncommands = [\n    [\"mypy\"],\n]\n\n[tool.tox.env.docs]\ndescription = \"build docs\"\ndependency_groups = [\"docs\"]\ncommands = [[\"sphinx-build\", \"-E\", \"-W\", \"-b\", \"dirhtml\", \"docs\", \"docs/_build/dirhtml\"]]\n\n[tool.tox.env.docs-auto]\ndescription = \"continuously rebuild docs and start a local server\"\ndependency_groups = [\"docs\", \"docs-auto\"]\ncommands = [[\"sphinx-autobuild\", \"-W\", \"-b\", \"dirhtml\", \"--watch\", \"src\", \"docs\", \"docs/_build/dirhtml\"]]\n\n[tool.tox.env.update-actions]\ndescription = \"update GitHub Actions pins\"\nlabels = [\"update\"]\ndependency_groups = [\"gha-update\"]\nskip_install = true\ncommands = [[\"gha-update\"]]\n\n[tool.tox.env.update-pre_commit]\ndescription = \"update pre-commit pins\"\nlabels = [\"update\"]\ndependency_groups = [\"pre-commit\"]\nskip_install = true\ncommands = [[\"pre-commit\", \"autoupdate\", \"--freeze\", \"-j4\"]]\n\n[tool.tox.env.update-requirements]\ndescription = \"update uv lock\"\nlabels = [\"update\"]\ndependency_groups = []\nno_default_groups = true\nskip_install = true\ncommands = [[\"uv\", \"lock\", {replace = \"posargs\", default = [\"-U\"], extend = true}]]\n"
  },
  {
    "path": "scripts/generate_identifier_pattern.py",
    "content": "import itertools\nimport os\nimport re\nimport sys\n\n\ndef get_characters():\n    \"\"\"Find every Unicode character that is valid in a Python `identifier`_ but\n    is not matched by the regex ``\\\\w`` group.\n\n    ``\\\\w`` matches some characters that aren't valid in identifiers, but\n    :meth:`str.isidentifier` will catch that later in lexing.\n\n    All start characters are valid continue characters, so we only test for\n    continue characters.\n\n    _identifier: https://docs.python.org/3/reference/lexical_analysis.html#identifiers\n    \"\"\"\n    for cp in range(sys.maxunicode + 1):\n        s = chr(cp)\n\n        if (\"a\" + s).isidentifier() and not re.match(r\"\\w\", s):\n            yield s\n\n\ndef collapse_ranges(data):\n    \"\"\"Given a sorted list of unique characters, generate ranges representing\n    sequential code points.\n\n    Source: https://stackoverflow.com/a/4629241/400617\n    \"\"\"\n    for _, g in itertools.groupby(enumerate(data), lambda x: ord(x[1]) - x[0]):\n        lb = list(g)\n        yield lb[0][1], lb[-1][1]\n\n\ndef build_pattern(ranges):\n    \"\"\"Output the regex pattern for ranges of characters.\n\n    One and two character ranges output the individual characters.\n    \"\"\"\n    out = []\n\n    for a, b in ranges:\n        if a == b:  # single char\n            out.append(a)\n        elif ord(b) - ord(a) == 1:  # two chars, range is redundant\n            out.append(a)\n            out.append(b)\n        else:\n            out.append(f\"{a}-{b}\")\n\n    return \"\".join(out)\n\n\ndef main():\n    \"\"\"Build the regex pattern and write it to ``jinja2/_identifier.py``.\"\"\"\n    pattern = build_pattern(collapse_ranges(get_characters()))\n    filename = os.path.abspath(\n        os.path.join(os.path.dirname(__file__), \"..\", \"src\", \"jinja2\", \"_identifier.py\")\n    )\n\n    with open(filename, \"w\", encoding=\"utf8\") as f:\n        f.write(\"# generated by scripts/generate_identifier_pattern.py\")\n        f.write(f\"# Python {sys.version_info[0]}.{sys.version_info[1]}\\n\")\n        f.write(\"import re\\n\\n\")\n        f.write(\"pattern = re.compile(\\n\")\n        f.write(f'    r\"[\\\\w{pattern}]+\"  # noqa: B950\\n')\n        f.write(\")\\n\")\n\n\nif __name__ == \"__main__\":\n    main()\n"
  },
  {
    "path": "src/jinja2/__init__.py",
    "content": "\"\"\"Jinja is a template engine written in pure Python. It provides a\nnon-XML syntax that supports inline expressions and an optional\nsandboxed environment.\n\"\"\"\n\nfrom __future__ import annotations\n\nimport typing as t\n\nfrom .bccache import BytecodeCache as BytecodeCache\nfrom .bccache import FileSystemBytecodeCache as FileSystemBytecodeCache\nfrom .bccache import MemcachedBytecodeCache as MemcachedBytecodeCache\nfrom .environment import Environment as Environment\nfrom .environment import Template as Template\nfrom .exceptions import TemplateAssertionError as TemplateAssertionError\nfrom .exceptions import TemplateError as TemplateError\nfrom .exceptions import TemplateNotFound as TemplateNotFound\nfrom .exceptions import TemplateRuntimeError as TemplateRuntimeError\nfrom .exceptions import TemplatesNotFound as TemplatesNotFound\nfrom .exceptions import TemplateSyntaxError as TemplateSyntaxError\nfrom .exceptions import UndefinedError as UndefinedError\nfrom .loaders import BaseLoader as BaseLoader\nfrom .loaders import ChoiceLoader as ChoiceLoader\nfrom .loaders import DictLoader as DictLoader\nfrom .loaders import FileSystemLoader as FileSystemLoader\nfrom .loaders import FunctionLoader as FunctionLoader\nfrom .loaders import ModuleLoader as ModuleLoader\nfrom .loaders import PackageLoader as PackageLoader\nfrom .loaders import PrefixLoader as PrefixLoader\nfrom .runtime import ChainableUndefined as ChainableUndefined\nfrom .runtime import DebugUndefined as DebugUndefined\nfrom .runtime import make_logging_undefined as make_logging_undefined\nfrom .runtime import StrictUndefined as StrictUndefined\nfrom .runtime import Undefined as Undefined\nfrom .utils import clear_caches as clear_caches\nfrom .utils import is_undefined as is_undefined\nfrom .utils import pass_context as pass_context\nfrom .utils import pass_environment as pass_environment\nfrom .utils import pass_eval_context as pass_eval_context\nfrom .utils import select_autoescape as select_autoescape\n\n\ndef __getattr__(name: str) -> t.Any:\n    if name == \"__version__\":\n        import importlib.metadata\n        import warnings\n\n        warnings.warn(\n            \"The `__version__` attribute is deprecated and will be removed in\"\n            \" Jinja 3.3. Use feature detection or\"\n            ' `importlib.metadata.version(\"jinja2\")` instead.',\n            DeprecationWarning,\n            stacklevel=2,\n        )\n        return importlib.metadata.version(\"jinja2\")\n\n    raise AttributeError(name)\n"
  },
  {
    "path": "src/jinja2/_identifier.py",
    "content": "# generated by scripts/generate_identifier_pattern.py for Python 3.10\nimport re\n\npattern = re.compile(\n    r\"[\\w·̀-ͯ·҃-֑҇-ׇֽֿׁׂׅׄؐ-ًؚ-ٰٟۖ-ۜ۟-۪ۤۧۨ-ܑۭܰ-݊ަ-ް߫-߽߳ࠖ-࠙ࠛ-ࠣࠥ-ࠧࠩ-࡙࠭-࡛࣓-ࣣ࣡-ःऺ-़ा-ॏ॑-ॗॢॣঁ-ঃ়া-ৄেৈো-্ৗৢৣ৾ਁ-ਃ਼ਾ-ੂੇੈੋ-੍ੑੰੱੵઁ-ઃ઼ા-ૅે-ૉો-્ૢૣૺ-૿ଁ-ଃ଼ା-ୄେୈୋ-୍୕-ୗୢୣஂா-ூெ-ைொ-்ௗఀ-ఄా-ౄె-ైొ-్ౕౖౢౣಁ-ಃ಼ಾ-ೄೆ-ೈೊ-್ೕೖೢೣഀ-ഃ഻഼ാ-ൄെ-ൈൊ-്ൗൢൣඁ-ඃ්ා-ුූෘ-ෟෲෳัิ-ฺ็-๎ັິ-ຼ່-ໍ༹༘༙༵༷༾༿ཱ-྄྆྇ྍ-ྗྙ-ྼ࿆ါ-ှၖ-ၙၞ-ၠၢ-ၤၧ-ၭၱ-ၴႂ-ႍႏႚ-ႝ፝-፟ᜒ-᜔ᜲ-᜴ᝒᝓᝲᝳ឴-៓៝᠋-᠍ᢅᢆᢩᤠ-ᤫᤰ-᤻ᨗ-ᨛᩕ-ᩞ᩠-᩿᩼᪰-᪽ᪿᫀᬀ-ᬄ᬴-᭄᭫-᭳ᮀ-ᮂᮡ-ᮭ᯦-᯳ᰤ-᰷᳐-᳔᳒-᳨᳭᳴᳷-᳹᷀-᷹᷻-᷿‿⁀⁔⃐-⃥⃜⃡-⃰℘℮⳯-⵿⳱ⷠ-〪ⷿ-゙゚〯꙯ꙴ-꙽ꚞꚟ꛰꛱ꠂ꠆ꠋꠣ-ꠧ꠬ꢀꢁꢴ-ꣅ꣠-꣱ꣿꤦ-꤭ꥇ-꥓ꦀ-ꦃ꦳-꧀ꧥꨩ-ꨶꩃꩌꩍꩻ-ꩽꪰꪲ-ꪴꪷꪸꪾ꪿꫁ꫫ-ꫯꫵ꫶ꯣ-ꯪ꯬꯭ﬞ︀-️︠-︯︳︴﹍-﹏＿𐇽𐋠𐍶-𐍺𐨁-𐨃𐨅𐨆𐨌-𐨏𐨸-𐨿𐨺𐫦𐫥𐴤-𐽆𐴧𐺫𐺬-𐽐𑀀-𑀂𑀸-𑁆𑁿-𑂂𑂰-𑂺𑄀-𑄂𑄧-𑄴𑅅𑅆𑅳𑆀-𑆂𑆳-𑇀𑇉-𑇌𑇎𑇏𑈬-𑈷𑈾𑋟-𑋪𑌀-𑌃𑌻𑌼𑌾-𑍄𑍇𑍈𑍋-𑍍𑍗𑍢𑍣𑍦-𑍬𑍰-𑍴𑐵-𑑆𑑞𑒰-𑓃𑖯-𑖵𑖸-𑗀𑗜𑗝𑘰-𑙀𑚫-𑚷𑜝-𑜫𑠬-𑠺𑤰-𑤵𑤷𑤸𑤻-𑤾𑥀𑥂𑥃𑧑-𑧗𑧚-𑧠𑧤𑨁-𑨊𑨳-𑨹𑨻-𑨾𑩇𑩑-𑩛𑪊-𑪙𑰯-𑰶𑰸-𑰿𑲒-𑲧𑲩-𑲶𑴱-𑴶𑴺𑴼𑴽𑴿-𑵅𑵇𑶊-𑶎𑶐𑶑𑶓-𑶗𑻳-𑻶𖫰-𖫴𖬰-𖬶𖽏𖽑-𖾇𖾏-𖾒𖿤𖿰𖿱𛲝𛲞𝅥-𝅩𝅭-𝅲𝅻-𝆂𝆅-𝆋𝆪-𝆭𝉂-𝉄𝨀-𝨶𝨻-𝩬𝩵𝪄𝪛-𝪟𝪡-𝪯𞀀-𞀆𞀈-𞀘𞀛-𞀡𞀣𞀤𞀦-𞀪𞄰-𞄶𞋬-𞣐𞋯-𞣖𞥄-𞥊󠄀-󠇯]+\"  # noqa: B950\n)\n"
  },
  {
    "path": "src/jinja2/async_utils.py",
    "content": "import inspect\nimport typing as t\nfrom functools import WRAPPER_ASSIGNMENTS\nfrom functools import wraps\n\nfrom .utils import _PassArg\nfrom .utils import pass_eval_context\n\nif t.TYPE_CHECKING:\n    import typing_extensions as te\n\nV = t.TypeVar(\"V\")\n\n\ndef async_variant(normal_func):  # type: ignore\n    def decorator(async_func):  # type: ignore\n        pass_arg = _PassArg.from_obj(normal_func)\n        need_eval_context = pass_arg is None\n\n        if pass_arg is _PassArg.environment:\n\n            def is_async(args: t.Any) -> bool:\n                return t.cast(bool, args[0].is_async)\n\n        else:\n\n            def is_async(args: t.Any) -> bool:\n                return t.cast(bool, args[0].environment.is_async)\n\n        # Take the doc and annotations from the sync function, but the\n        # name from the async function. Pallets-Sphinx-Themes\n        # build_function_directive expects __wrapped__ to point to the\n        # sync function.\n        async_func_attrs = (\"__module__\", \"__name__\", \"__qualname__\")\n        normal_func_attrs = tuple(set(WRAPPER_ASSIGNMENTS).difference(async_func_attrs))\n\n        @wraps(normal_func, assigned=normal_func_attrs)\n        @wraps(async_func, assigned=async_func_attrs, updated=())\n        def wrapper(*args, **kwargs):  # type: ignore\n            b = is_async(args)\n\n            if need_eval_context:\n                args = args[1:]\n\n            if b:\n                return async_func(*args, **kwargs)\n\n            return normal_func(*args, **kwargs)\n\n        if need_eval_context:\n            wrapper = pass_eval_context(wrapper)\n\n        wrapper.jinja_async_variant = True  # type: ignore[attr-defined]\n        return wrapper\n\n    return decorator\n\n\n_common_primitives = {int, float, bool, str, list, dict, tuple, type(None)}\n\n\nasync def auto_await(value: t.Union[t.Awaitable[\"V\"], \"V\"]) -> \"V\":\n    # Avoid a costly call to isawaitable\n    if type(value) in _common_primitives:\n        return t.cast(\"V\", value)\n\n    if inspect.isawaitable(value):\n        return await t.cast(\"t.Awaitable[V]\", value)\n\n    return value\n\n\nclass _IteratorToAsyncIterator(t.Generic[V]):\n    def __init__(self, iterator: \"t.Iterator[V]\"):\n        self._iterator = iterator\n\n    def __aiter__(self) -> \"te.Self\":\n        return self\n\n    async def __anext__(self) -> V:\n        try:\n            return next(self._iterator)\n        except StopIteration as e:\n            raise StopAsyncIteration(e.value) from e\n\n\ndef auto_aiter(\n    iterable: \"t.AsyncIterable[V] | t.Iterable[V]\",\n) -> \"t.AsyncIterator[V]\":\n    if hasattr(iterable, \"__aiter__\"):\n        return iterable.__aiter__()\n    else:\n        return _IteratorToAsyncIterator(iter(iterable))\n\n\nasync def auto_to_list(\n    value: \"t.AsyncIterable[V] | t.Iterable[V]\",\n) -> list[\"V\"]:\n    return [x async for x in auto_aiter(value)]\n"
  },
  {
    "path": "src/jinja2/bccache.py",
    "content": "\"\"\"The optional bytecode cache system. This is useful if you have very\ncomplex template situations and the compilation of all those templates\nslows down your application too much.\n\nSituations where this is useful are often forking web applications that\nare initialized on the first request.\n\"\"\"\n\nimport errno\nimport fnmatch\nimport marshal\nimport os\nimport pickle\nimport stat\nimport sys\nimport tempfile\nimport typing as t\nfrom hashlib import sha1\nfrom io import BytesIO\nfrom types import CodeType\n\nif t.TYPE_CHECKING:\n    import typing_extensions as te\n\n    from .environment import Environment\n\n    class _MemcachedClient(te.Protocol):\n        def get(self, key: str) -> bytes: ...\n\n        def set(self, key: str, value: bytes, timeout: int | None = None) -> None: ...\n\n\nbc_version = 5\n# Magic bytes to identify Jinja bytecode cache files. Contains the\n# Python major and minor version to avoid loading incompatible bytecode\n# if a project upgrades its Python version.\nbc_magic = (\n    b\"j2\"\n    + pickle.dumps(bc_version, 2)\n    + pickle.dumps((sys.version_info[0] << 24) | sys.version_info[1], 2)\n)\n\n\nclass Bucket:\n    \"\"\"Buckets are used to store the bytecode for one template.  It's created\n    and initialized by the bytecode cache and passed to the loading functions.\n\n    The buckets get an internal checksum from the cache assigned and use this\n    to automatically reject outdated cache material.  Individual bytecode\n    cache subclasses don't have to care about cache invalidation.\n    \"\"\"\n\n    def __init__(self, environment: \"Environment\", key: str, checksum: str) -> None:\n        self.environment = environment\n        self.key = key\n        self.checksum = checksum\n        self.reset()\n\n    def reset(self) -> None:\n        \"\"\"Resets the bucket (unloads the bytecode).\"\"\"\n        self.code: CodeType | None = None\n\n    def load_bytecode(self, f: t.BinaryIO) -> None:\n        \"\"\"Loads bytecode from a file or file like object.\"\"\"\n        # make sure the magic header is correct\n        magic = f.read(len(bc_magic))\n        if magic != bc_magic:\n            self.reset()\n            return\n        # the source code of the file changed, we need to reload\n        checksum = pickle.load(f)\n        if self.checksum != checksum:\n            self.reset()\n            return\n        # if marshal_load fails then we need to reload\n        try:\n            self.code = marshal.load(f)\n        except (EOFError, ValueError, TypeError):\n            self.reset()\n            return\n\n    def write_bytecode(self, f: t.IO[bytes]) -> None:\n        \"\"\"Dump the bytecode into the file or file like object passed.\"\"\"\n        if self.code is None:\n            raise TypeError(\"can't write empty bucket\")\n        f.write(bc_magic)\n        pickle.dump(self.checksum, f, 2)\n        marshal.dump(self.code, f)\n\n    def bytecode_from_string(self, string: bytes) -> None:\n        \"\"\"Load bytecode from bytes.\"\"\"\n        self.load_bytecode(BytesIO(string))\n\n    def bytecode_to_string(self) -> bytes:\n        \"\"\"Return the bytecode as bytes.\"\"\"\n        out = BytesIO()\n        self.write_bytecode(out)\n        return out.getvalue()\n\n\nclass BytecodeCache:\n    \"\"\"To implement your own bytecode cache you have to subclass this class\n    and override :meth:`load_bytecode` and :meth:`dump_bytecode`.  Both of\n    these methods are passed a :class:`~jinja2.bccache.Bucket`.\n\n    A very basic bytecode cache that saves the bytecode on the file system::\n\n        from os import path\n\n        class MyCache(BytecodeCache):\n\n            def __init__(self, directory):\n                self.directory = directory\n\n            def load_bytecode(self, bucket):\n                filename = path.join(self.directory, bucket.key)\n                if path.exists(filename):\n                    with open(filename, 'rb') as f:\n                        bucket.load_bytecode(f)\n\n            def dump_bytecode(self, bucket):\n                filename = path.join(self.directory, bucket.key)\n                with open(filename, 'wb') as f:\n                    bucket.write_bytecode(f)\n\n    A more advanced version of a filesystem based bytecode cache is part of\n    Jinja.\n    \"\"\"\n\n    def load_bytecode(self, bucket: Bucket) -> None:\n        \"\"\"Subclasses have to override this method to load bytecode into a\n        bucket.  If they are not able to find code in the cache for the\n        bucket, it must not do anything.\n        \"\"\"\n        raise NotImplementedError()\n\n    def dump_bytecode(self, bucket: Bucket) -> None:\n        \"\"\"Subclasses have to override this method to write the bytecode\n        from a bucket back to the cache.  If it unable to do so it must not\n        fail silently but raise an exception.\n        \"\"\"\n        raise NotImplementedError()\n\n    def clear(self) -> None:\n        \"\"\"Clears the cache.  This method is not used by Jinja but should be\n        implemented to allow applications to clear the bytecode cache used\n        by a particular environment.\n        \"\"\"\n\n    def get_cache_key(self, name: str, filename: str | None = None) -> str:\n        \"\"\"Returns the unique hash key for this template name.\"\"\"\n        hash = sha1(name.encode(\"utf-8\"))\n\n        if filename is not None:\n            hash.update(f\"|{filename}\".encode())\n\n        return hash.hexdigest()\n\n    def get_source_checksum(self, source: str) -> str:\n        \"\"\"Returns a checksum for the source.\"\"\"\n        return sha1(source.encode(\"utf-8\")).hexdigest()\n\n    def get_bucket(\n        self,\n        environment: \"Environment\",\n        name: str,\n        filename: str | None,\n        source: str,\n    ) -> Bucket:\n        \"\"\"Return a cache bucket for the given template.  All arguments are\n        mandatory but filename may be `None`.\n        \"\"\"\n        key = self.get_cache_key(name, filename)\n        checksum = self.get_source_checksum(source)\n        bucket = Bucket(environment, key, checksum)\n        self.load_bytecode(bucket)\n        return bucket\n\n    def set_bucket(self, bucket: Bucket) -> None:\n        \"\"\"Put the bucket into the cache.\"\"\"\n        self.dump_bytecode(bucket)\n\n\nclass FileSystemBytecodeCache(BytecodeCache):\n    \"\"\"A bytecode cache that stores bytecode on the filesystem.  It accepts\n    two arguments: The directory where the cache items are stored and a\n    pattern string that is used to build the filename.\n\n    If no directory is specified a default cache directory is selected.  On\n    Windows the user's temp directory is used, on UNIX systems a directory\n    is created for the user in the system temp directory.\n\n    The pattern can be used to have multiple separate caches operate on the\n    same directory.  The default pattern is ``'__jinja2_%s.cache'``.  ``%s``\n    is replaced with the cache key.\n\n    >>> bcc = FileSystemBytecodeCache('/tmp/jinja_cache', '%s.cache')\n\n    This bytecode cache supports clearing of the cache using the clear method.\n    \"\"\"\n\n    def __init__(\n        self, directory: str | None = None, pattern: str = \"__jinja2_%s.cache\"\n    ) -> None:\n        if directory is None:\n            directory = self._get_default_cache_dir()\n        self.directory = directory\n        self.pattern = pattern\n\n    def _get_default_cache_dir(self) -> str:\n        def _unsafe_dir() -> \"te.NoReturn\":\n            raise RuntimeError(\n                \"Cannot determine safe temp directory.  You \"\n                \"need to explicitly provide one.\"\n            )\n\n        tmpdir = tempfile.gettempdir()\n\n        # On windows the temporary directory is used specific unless\n        # explicitly forced otherwise.  We can just use that.\n        if os.name == \"nt\":\n            return tmpdir\n        if not hasattr(os, \"getuid\"):\n            _unsafe_dir()\n\n        dirname = f\"_jinja2-cache-{os.getuid()}\"\n        actual_dir = os.path.join(tmpdir, dirname)\n\n        try:\n            os.mkdir(actual_dir, stat.S_IRWXU)\n        except OSError as e:\n            if e.errno != errno.EEXIST:\n                raise\n        try:\n            os.chmod(actual_dir, stat.S_IRWXU)\n            actual_dir_stat = os.lstat(actual_dir)\n            if (\n                actual_dir_stat.st_uid != os.getuid()\n                or not stat.S_ISDIR(actual_dir_stat.st_mode)\n                or stat.S_IMODE(actual_dir_stat.st_mode) != stat.S_IRWXU\n            ):\n                _unsafe_dir()\n        except OSError as e:\n            if e.errno != errno.EEXIST:\n                raise\n\n        actual_dir_stat = os.lstat(actual_dir)\n        if (\n            actual_dir_stat.st_uid != os.getuid()\n            or not stat.S_ISDIR(actual_dir_stat.st_mode)\n            or stat.S_IMODE(actual_dir_stat.st_mode) != stat.S_IRWXU\n        ):\n            _unsafe_dir()\n\n        return actual_dir\n\n    def _get_cache_filename(self, bucket: Bucket) -> str:\n        return os.path.join(self.directory, self.pattern % (bucket.key,))\n\n    def load_bytecode(self, bucket: Bucket) -> None:\n        filename = self._get_cache_filename(bucket)\n\n        # Don't test for existence before opening the file, since the\n        # file could disappear after the test before the open.\n        try:\n            f = open(filename, \"rb\")\n        except (FileNotFoundError, IsADirectoryError, PermissionError):\n            # PermissionError can occur on Windows when an operation is\n            # in progress, such as calling clear().\n            return\n\n        with f:\n            bucket.load_bytecode(f)\n\n    def dump_bytecode(self, bucket: Bucket) -> None:\n        # Write to a temporary file, then rename to the real name after\n        # writing. This avoids another process reading the file before\n        # it is fully written.\n        name = self._get_cache_filename(bucket)\n        f = tempfile.NamedTemporaryFile(\n            mode=\"wb\",\n            dir=os.path.dirname(name),\n            prefix=os.path.basename(name),\n            suffix=\".tmp\",\n            delete=False,\n        )\n\n        def remove_silent() -> None:\n            try:\n                os.remove(f.name)\n            except OSError:\n                # Another process may have called clear(). On Windows,\n                # another program may be holding the file open.\n                pass\n\n        try:\n            with f:\n                bucket.write_bytecode(f)\n        except BaseException:\n            remove_silent()\n            raise\n\n        try:\n            os.replace(f.name, name)\n        except OSError:\n            # Another process may have called clear(). On Windows,\n            # another program may be holding the file open.\n            remove_silent()\n        except BaseException:\n            remove_silent()\n            raise\n\n    def clear(self) -> None:\n        # imported lazily here because google app-engine doesn't support\n        # write access on the file system and the function does not exist\n        # normally.\n        from os import remove\n\n        files = fnmatch.filter(os.listdir(self.directory), self.pattern % (\"*\",))\n        for filename in files:\n            try:\n                remove(os.path.join(self.directory, filename))\n            except OSError:\n                pass\n\n\nclass MemcachedBytecodeCache(BytecodeCache):\n    \"\"\"This class implements a bytecode cache that uses a memcache cache for\n    storing the information.  It does not enforce a specific memcache library\n    (tummy's memcache or cmemcache) but will accept any class that provides\n    the minimal interface required.\n\n    Libraries compatible with this class:\n\n    -   `cachelib <https://github.com/pallets/cachelib>`_\n    -   `python-memcached <https://pypi.org/project/python-memcached/>`_\n\n    (Unfortunately the django cache interface is not compatible because it\n    does not support storing binary data, only text. You can however pass\n    the underlying cache client to the bytecode cache which is available\n    as `django.core.cache.cache._client`.)\n\n    The minimal interface for the client passed to the constructor is this:\n\n    .. class:: MinimalClientInterface\n\n        .. method:: set(key, value[, timeout])\n\n            Stores the bytecode in the cache.  `value` is a string and\n            `timeout` the timeout of the key.  If timeout is not provided\n            a default timeout or no timeout should be assumed, if it's\n            provided it's an integer with the number of seconds the cache\n            item should exist.\n\n        .. method:: get(key)\n\n            Returns the value for the cache key.  If the item does not\n            exist in the cache the return value must be `None`.\n\n    The other arguments to the constructor are the prefix for all keys that\n    is added before the actual cache key and the timeout for the bytecode in\n    the cache system.  We recommend a high (or no) timeout.\n\n    This bytecode cache does not support clearing of used items in the cache.\n    The clear method is a no-operation function.\n\n    .. versionadded:: 2.7\n       Added support for ignoring memcache errors through the\n       `ignore_memcache_errors` parameter.\n    \"\"\"\n\n    def __init__(\n        self,\n        client: \"_MemcachedClient\",\n        prefix: str = \"jinja2/bytecode/\",\n        timeout: int | None = None,\n        ignore_memcache_errors: bool = True,\n    ):\n        self.client = client\n        self.prefix = prefix\n        self.timeout = timeout\n        self.ignore_memcache_errors = ignore_memcache_errors\n\n    def load_bytecode(self, bucket: Bucket) -> None:\n        try:\n            code = self.client.get(self.prefix + bucket.key)\n        except Exception:\n            if not self.ignore_memcache_errors:\n                raise\n        else:\n            bucket.bytecode_from_string(code)\n\n    def dump_bytecode(self, bucket: Bucket) -> None:\n        key = self.prefix + bucket.key\n        value = bucket.bytecode_to_string()\n\n        try:\n            if self.timeout is not None:\n                self.client.set(key, value, self.timeout)\n            else:\n                self.client.set(key, value)\n        except Exception:\n            if not self.ignore_memcache_errors:\n                raise\n"
  },
  {
    "path": "src/jinja2/compiler.py",
    "content": "\"\"\"Compiles nodes from the parser into Python code.\"\"\"\n\nimport typing as t\nfrom contextlib import contextmanager\nfrom functools import update_wrapper\nfrom io import StringIO\nfrom itertools import chain\nfrom keyword import iskeyword as is_python_keyword\n\nfrom markupsafe import escape\nfrom markupsafe import Markup\n\nfrom . import nodes\nfrom .exceptions import TemplateAssertionError\nfrom .idtracking import Symbols\nfrom .idtracking import VAR_LOAD_ALIAS\nfrom .idtracking import VAR_LOAD_PARAMETER\nfrom .idtracking import VAR_LOAD_RESOLVE\nfrom .idtracking import VAR_LOAD_UNDEFINED\nfrom .nodes import EvalContext\nfrom .optimizer import Optimizer\nfrom .utils import _PassArg\nfrom .utils import concat\nfrom .visitor import NodeVisitor\n\nif t.TYPE_CHECKING:\n    import typing_extensions as te\n\n    from .environment import Environment\n\nF = t.TypeVar(\"F\", bound=t.Callable[..., t.Any])\n\noperators = {\n    \"eq\": \"==\",\n    \"ne\": \"!=\",\n    \"gt\": \">\",\n    \"gteq\": \">=\",\n    \"lt\": \"<\",\n    \"lteq\": \"<=\",\n    \"in\": \"in\",\n    \"notin\": \"not in\",\n}\n\n\ndef optimizeconst(f: F) -> F:\n    def new_func(\n        self: \"CodeGenerator\", node: nodes.Expr, frame: \"Frame\", **kwargs: t.Any\n    ) -> t.Any:\n        # Only optimize if the frame is not volatile\n        if self.optimizer is not None and not frame.eval_ctx.volatile:\n            new_node = self.optimizer.visit(node, frame.eval_ctx)\n\n            if new_node != node:\n                return self.visit(new_node, frame)\n\n        return f(self, node, frame, **kwargs)\n\n    return update_wrapper(new_func, f)  # type: ignore[return-value]\n\n\ndef _make_binop(op: str) -> t.Callable[[\"CodeGenerator\", nodes.BinExpr, \"Frame\"], None]:\n    @optimizeconst\n    def visitor(self: \"CodeGenerator\", node: nodes.BinExpr, frame: Frame) -> None:\n        if (\n            self.environment.sandboxed and op in self.environment.intercepted_binops  # type: ignore\n        ):\n            self.write(f\"environment.call_binop(context, {op!r}, \")\n            self.visit(node.left, frame)\n            self.write(\", \")\n            self.visit(node.right, frame)\n        else:\n            self.write(\"(\")\n            self.visit(node.left, frame)\n            self.write(f\" {op} \")\n            self.visit(node.right, frame)\n\n        self.write(\")\")\n\n    return visitor\n\n\ndef _make_unop(\n    op: str,\n) -> t.Callable[[\"CodeGenerator\", nodes.UnaryExpr, \"Frame\"], None]:\n    @optimizeconst\n    def visitor(self: \"CodeGenerator\", node: nodes.UnaryExpr, frame: Frame) -> None:\n        if (\n            self.environment.sandboxed and op in self.environment.intercepted_unops  # type: ignore\n        ):\n            self.write(f\"environment.call_unop(context, {op!r}, \")\n            self.visit(node.node, frame)\n        else:\n            self.write(\"(\" + op)\n            self.visit(node.node, frame)\n\n        self.write(\")\")\n\n    return visitor\n\n\ndef generate(\n    node: nodes.Template,\n    environment: \"Environment\",\n    name: str | None,\n    filename: str | None,\n    stream: t.TextIO | None = None,\n    defer_init: bool = False,\n    optimized: bool = True,\n) -> str | None:\n    \"\"\"Generate the python source for a node tree.\"\"\"\n    if not isinstance(node, nodes.Template):\n        raise TypeError(\"Can't compile non template nodes\")\n\n    generator = environment.code_generator_class(\n        environment, name, filename, stream, defer_init, optimized\n    )\n    generator.visit(node)\n\n    if stream is None:\n        return generator.stream.getvalue()  # type: ignore\n\n    return None\n\n\ndef has_safe_repr(value: t.Any) -> bool:\n    \"\"\"Does the node have a safe representation?\"\"\"\n    if value is None or value is NotImplemented or value is Ellipsis:\n        return True\n\n    if type(value) in {bool, int, float, complex, range, str, Markup}:\n        return True\n\n    if type(value) in {tuple, list, set, frozenset}:\n        return all(has_safe_repr(v) for v in value)\n\n    if type(value) is dict:  # noqa E721\n        return all(has_safe_repr(k) and has_safe_repr(v) for k, v in value.items())\n\n    return False\n\n\ndef find_undeclared(nodes: t.Iterable[nodes.Node], names: t.Iterable[str]) -> set[str]:\n    \"\"\"Check if the names passed are accessed undeclared.  The return value\n    is a set of all the undeclared names from the sequence of names found.\n    \"\"\"\n    visitor = UndeclaredNameVisitor(names)\n    try:\n        for node in nodes:\n            visitor.visit(node)\n    except VisitorExit:\n        pass\n    return visitor.undeclared\n\n\nclass MacroRef:\n    def __init__(self, node: nodes.Macro | nodes.CallBlock) -> None:\n        self.node = node\n        self.accesses_caller = False\n        self.accesses_kwargs = False\n        self.accesses_varargs = False\n\n\nclass Frame:\n    \"\"\"Holds compile time information for us.\"\"\"\n\n    def __init__(\n        self,\n        eval_ctx: EvalContext,\n        parent: t.Optional[\"Frame\"] = None,\n        level: int | None = None,\n    ) -> None:\n        self.eval_ctx = eval_ctx\n\n        # the parent of this frame\n        self.parent = parent\n\n        if parent is None:\n            self.symbols = Symbols(level=level)\n\n            # in some dynamic inheritance situations the compiler needs to add\n            # write tests around output statements.\n            self.require_output_check = False\n\n            # inside some tags we are using a buffer rather than yield statements.\n            # this for example affects {% filter %} or {% macro %}.  If a frame\n            # is buffered this variable points to the name of the list used as\n            # buffer.\n            self.buffer: str | None = None\n\n            # the name of the block we're in, otherwise None.\n            self.block: str | None = None\n\n        else:\n            self.symbols = Symbols(parent.symbols, level=level)\n            self.require_output_check = parent.require_output_check\n            self.buffer = parent.buffer\n            self.block = parent.block\n\n        # a toplevel frame is the root + soft frames such as if conditions.\n        self.toplevel = False\n\n        # the root frame is basically just the outermost frame, so no if\n        # conditions.  This information is used to optimize inheritance\n        # situations.\n        self.rootlevel = False\n\n        # variables set inside of loops and blocks should not affect outer frames,\n        # but they still needs to be kept track of as part of the active context.\n        self.loop_frame = False\n        self.block_frame = False\n\n        # track whether the frame is being used in an if-statement or conditional\n        # expression as it determines which errors should be raised during runtime\n        # or compile time.\n        self.soft_frame = False\n\n    def copy(self) -> \"te.Self\":\n        \"\"\"Create a copy of the current one.\"\"\"\n        rv = object.__new__(self.__class__)\n        rv.__dict__.update(self.__dict__)\n        rv.symbols = self.symbols.copy()\n        return rv\n\n    def inner(self, isolated: bool = False) -> \"Frame\":\n        \"\"\"Return an inner frame.\"\"\"\n        if isolated:\n            return Frame(self.eval_ctx, level=self.symbols.level + 1)\n        return Frame(self.eval_ctx, self)\n\n    def soft(self) -> \"te.Self\":\n        \"\"\"Return a soft frame.  A soft frame may not be modified as\n        standalone thing as it shares the resources with the frame it\n        was created of, but it's not a rootlevel frame any longer.\n\n        This is only used to implement if-statements and conditional\n        expressions.\n        \"\"\"\n        rv = self.copy()\n        rv.rootlevel = False\n        rv.soft_frame = True\n        return rv\n\n    __copy__ = copy\n\n\nclass VisitorExit(RuntimeError):\n    \"\"\"Exception used by the `UndeclaredNameVisitor` to signal a stop.\"\"\"\n\n\nclass DependencyFinderVisitor(NodeVisitor):\n    \"\"\"A visitor that collects filter and test calls.\"\"\"\n\n    def __init__(self) -> None:\n        self.filters: set[str] = set()\n        self.tests: set[str] = set()\n\n    def visit_Filter(self, node: nodes.Filter) -> None:\n        self.generic_visit(node)\n        self.filters.add(node.name)\n\n    def visit_Test(self, node: nodes.Test) -> None:\n        self.generic_visit(node)\n        self.tests.add(node.name)\n\n    def visit_Block(self, node: nodes.Block) -> None:\n        \"\"\"Stop visiting at blocks.\"\"\"\n\n\nclass UndeclaredNameVisitor(NodeVisitor):\n    \"\"\"A visitor that checks if a name is accessed without being\n    declared.  This is different from the frame visitor as it will\n    not stop at closure frames.\n    \"\"\"\n\n    def __init__(self, names: t.Iterable[str]) -> None:\n        self.names = set(names)\n        self.undeclared: set[str] = set()\n\n    def visit_Name(self, node: nodes.Name) -> None:\n        if node.ctx == \"load\" and node.name in self.names:\n            self.undeclared.add(node.name)\n            if self.undeclared == self.names:\n                raise VisitorExit()\n        else:\n            self.names.discard(node.name)\n\n    def visit_Block(self, node: nodes.Block) -> None:\n        \"\"\"Stop visiting a blocks.\"\"\"\n\n\nclass CompilerExit(Exception):\n    \"\"\"Raised if the compiler encountered a situation where it just\n    doesn't make sense to further process the code.  Any block that\n    raises such an exception is not further processed.\n    \"\"\"\n\n\nclass CodeGenerator(NodeVisitor):\n    def __init__(\n        self,\n        environment: \"Environment\",\n        name: str | None,\n        filename: str | None,\n        stream: t.TextIO | None = None,\n        defer_init: bool = False,\n        optimized: bool = True,\n    ) -> None:\n        if stream is None:\n            stream = StringIO()\n        self.environment = environment\n        self.name = name\n        self.filename = filename\n        self.stream = stream\n        self.created_block_context = False\n        self.defer_init = defer_init\n        self.optimizer: Optimizer | None = None\n\n        if optimized:\n            self.optimizer = Optimizer(environment)\n\n        # aliases for imports\n        self.import_aliases: dict[str, str] = {}\n\n        # a registry for all blocks.  Because blocks are moved out\n        # into the global python scope they are registered here\n        self.blocks: dict[str, nodes.Block] = {}\n\n        # the number of extends statements so far\n        self.extends_so_far = 0\n\n        # some templates have a rootlevel extends.  In this case we\n        # can safely assume that we're a child template and do some\n        # more optimizations.\n        self.has_known_extends = False\n\n        # the current line number\n        self.code_lineno = 1\n\n        # registry of all filters and tests (global, not block local)\n        self.tests: dict[str, str] = {}\n        self.filters: dict[str, str] = {}\n\n        # the debug information\n        self.debug_info: list[tuple[int, int]] = []\n        self._write_debug_info: int | None = None\n\n        # the number of new lines before the next write()\n        self._new_lines = 0\n\n        # the line number of the last written statement\n        self._last_line = 0\n\n        # true if nothing was written so far.\n        self._first_write = True\n\n        # used by the `temporary_identifier` method to get new\n        # unique, temporary identifier\n        self._last_identifier = 0\n\n        # the current indentation\n        self._indentation = 0\n\n        # Tracks toplevel assignments\n        self._assign_stack: list[set[str]] = []\n\n        # Tracks parameter definition blocks\n        self._param_def_block: list[set[str]] = []\n\n        # Tracks the current context.\n        self._context_reference_stack = [\"context\"]\n\n    @property\n    def optimized(self) -> bool:\n        return self.optimizer is not None\n\n    # -- Various compilation helpers\n\n    def fail(self, msg: str, lineno: int) -> \"te.NoReturn\":\n        \"\"\"Fail with a :exc:`TemplateAssertionError`.\"\"\"\n        raise TemplateAssertionError(msg, lineno, self.name, self.filename)\n\n    def temporary_identifier(self) -> str:\n        \"\"\"Get a new unique identifier.\"\"\"\n        self._last_identifier += 1\n        return f\"t_{self._last_identifier}\"\n\n    def buffer(self, frame: Frame) -> None:\n        \"\"\"Enable buffering for the frame from that point onwards.\"\"\"\n        frame.buffer = self.temporary_identifier()\n        self.writeline(f\"{frame.buffer} = []\")\n\n    def return_buffer_contents(\n        self, frame: Frame, force_unescaped: bool = False\n    ) -> None:\n        \"\"\"Return the buffer contents of the frame.\"\"\"\n        if not force_unescaped:\n            if frame.eval_ctx.volatile:\n                self.writeline(\"if context.eval_ctx.autoescape:\")\n                self.indent()\n                self.writeline(f\"return Markup(concat({frame.buffer}))\")\n                self.outdent()\n                self.writeline(\"else:\")\n                self.indent()\n                self.writeline(f\"return concat({frame.buffer})\")\n                self.outdent()\n                return\n            elif frame.eval_ctx.autoescape:\n                self.writeline(f\"return Markup(concat({frame.buffer}))\")\n                return\n        self.writeline(f\"return concat({frame.buffer})\")\n\n    def indent(self) -> None:\n        \"\"\"Indent by one.\"\"\"\n        self._indentation += 1\n\n    def outdent(self, step: int = 1) -> None:\n        \"\"\"Outdent by step.\"\"\"\n        self._indentation -= step\n\n    def start_write(self, frame: Frame, node: nodes.Node | None = None) -> None:\n        \"\"\"Yield or write into the frame buffer.\"\"\"\n        if frame.buffer is None:\n            self.writeline(\"yield \", node)\n        else:\n            self.writeline(f\"{frame.buffer}.append(\", node)\n\n    def end_write(self, frame: Frame) -> None:\n        \"\"\"End the writing process started by `start_write`.\"\"\"\n        if frame.buffer is not None:\n            self.write(\")\")\n\n    def simple_write(\n        self, s: str, frame: Frame, node: nodes.Node | None = None\n    ) -> None:\n        \"\"\"Simple shortcut for start_write + write + end_write.\"\"\"\n        self.start_write(frame, node)\n        self.write(s)\n        self.end_write(frame)\n\n    def blockvisit(self, nodes: t.Iterable[nodes.Node], frame: Frame) -> None:\n        \"\"\"Visit a list of nodes as block in a frame.  If the current frame\n        is no buffer a dummy ``if 0: yield None`` is written automatically.\n        \"\"\"\n        try:\n            self.writeline(\"pass\")\n            for node in nodes:\n                self.visit(node, frame)\n        except CompilerExit:\n            pass\n\n    def write(self, x: str) -> None:\n        \"\"\"Write a string into the output stream.\"\"\"\n        if self._new_lines:\n            if not self._first_write:\n                self.stream.write(\"\\n\" * self._new_lines)\n                self.code_lineno += self._new_lines\n                if self._write_debug_info is not None:\n                    self.debug_info.append((self._write_debug_info, self.code_lineno))\n                    self._write_debug_info = None\n            self._first_write = False\n            self.stream.write(\"    \" * self._indentation)\n            self._new_lines = 0\n        self.stream.write(x)\n\n    def writeline(self, x: str, node: nodes.Node | None = None, extra: int = 0) -> None:\n        \"\"\"Combination of newline and write.\"\"\"\n        self.newline(node, extra)\n        self.write(x)\n\n    def newline(self, node: nodes.Node | None = None, extra: int = 0) -> None:\n        \"\"\"Add one or more newlines before the next write.\"\"\"\n        self._new_lines = max(self._new_lines, 1 + extra)\n        if node is not None and node.lineno != self._last_line:\n            self._write_debug_info = node.lineno\n            self._last_line = node.lineno\n\n    def signature(\n        self,\n        node: nodes.Call | nodes.Filter | nodes.Test,\n        frame: Frame,\n        extra_kwargs: t.Mapping[str, t.Any] | None = None,\n    ) -> None:\n        \"\"\"Writes a function call to the stream for the current node.\n        A leading comma is added automatically.  The extra keyword\n        arguments may not include python keywords otherwise a syntax\n        error could occur.  The extra keyword arguments should be given\n        as python dict.\n        \"\"\"\n        # if any of the given keyword arguments is a python keyword\n        # we have to make sure that no invalid call is created.\n        kwarg_workaround = any(\n            is_python_keyword(t.cast(str, k))\n            for k in chain((x.key for x in node.kwargs), extra_kwargs or ())\n        )\n\n        for arg in node.args:\n            self.write(\", \")\n            self.visit(arg, frame)\n\n        if not kwarg_workaround:\n            for kwarg in node.kwargs:\n                self.write(\", \")\n                self.visit(kwarg, frame)\n            if extra_kwargs is not None:\n                for key, value in extra_kwargs.items():\n                    self.write(f\", {key}={value}\")\n        if node.dyn_args:\n            self.write(\", *\")\n            self.visit(node.dyn_args, frame)\n\n        if kwarg_workaround:\n            if node.dyn_kwargs is not None:\n                self.write(\", **dict({\")\n            else:\n                self.write(\", **{\")\n            for kwarg in node.kwargs:\n                self.write(f\"{kwarg.key!r}: \")\n                self.visit(kwarg.value, frame)\n                self.write(\", \")\n            if extra_kwargs is not None:\n                for key, value in extra_kwargs.items():\n                    self.write(f\"{key!r}: {value}, \")\n            if node.dyn_kwargs is not None:\n                self.write(\"}, **\")\n                self.visit(node.dyn_kwargs, frame)\n                self.write(\")\")\n            else:\n                self.write(\"}\")\n\n        elif node.dyn_kwargs is not None:\n            self.write(\", **\")\n            self.visit(node.dyn_kwargs, frame)\n\n    def pull_dependencies(self, nodes: t.Iterable[nodes.Node]) -> None:\n        \"\"\"Find all filter and test names used in the template and\n        assign them to variables in the compiled namespace. Checking\n        that the names are registered with the environment is done when\n        compiling the Filter and Test nodes. If the node is in an If or\n        CondExpr node, the check is done at runtime instead.\n\n        .. versionchanged:: 3.0\n            Filters and tests in If and CondExpr nodes are checked at\n            runtime instead of compile time.\n        \"\"\"\n        visitor = DependencyFinderVisitor()\n\n        for node in nodes:\n            visitor.visit(node)\n\n        for id_map, names, dependency in (\n            (self.filters, visitor.filters, \"filters\"),\n            (\n                self.tests,\n                visitor.tests,\n                \"tests\",\n            ),\n        ):\n            for name in sorted(names):\n                if name not in id_map:\n                    id_map[name] = self.temporary_identifier()\n\n                # add check during runtime that dependencies used inside of executed\n                # blocks are defined, as this step may be skipped during compile time\n                self.writeline(\"try:\")\n                self.indent()\n                self.writeline(f\"{id_map[name]} = environment.{dependency}[{name!r}]\")\n                self.outdent()\n                self.writeline(\"except KeyError:\")\n                self.indent()\n                self.writeline(\"@internalcode\")\n                self.writeline(f\"def {id_map[name]}(*unused):\")\n                self.indent()\n                self.writeline(\n                    f'raise TemplateRuntimeError(\"No {dependency[:-1]}'\n                    f' named {name!r} found.\")'\n                )\n                self.outdent()\n                self.outdent()\n\n    def enter_frame(self, frame: Frame) -> None:\n        undefs = []\n        for target, (action, param) in frame.symbols.loads.items():\n            if action == VAR_LOAD_PARAMETER:\n                pass\n            elif action == VAR_LOAD_RESOLVE:\n                self.writeline(f\"{target} = {self.get_resolve_func()}({param!r})\")\n            elif action == VAR_LOAD_ALIAS:\n                self.writeline(f\"{target} = {param}\")\n            elif action == VAR_LOAD_UNDEFINED:\n                undefs.append(target)\n            else:\n                raise NotImplementedError(\"unknown load instruction\")\n        if undefs:\n            self.writeline(f\"{' = '.join(undefs)} = missing\")\n\n    def leave_frame(self, frame: Frame, with_python_scope: bool = False) -> None:\n        if not with_python_scope:\n            undefs = []\n            for target in frame.symbols.loads:\n                undefs.append(target)\n            if undefs:\n                self.writeline(f\"{' = '.join(undefs)} = missing\")\n\n    def choose_async(self, async_value: str = \"async \", sync_value: str = \"\") -> str:\n        return async_value if self.environment.is_async else sync_value\n\n    def func(self, name: str) -> str:\n        return f\"{self.choose_async()}def {name}\"\n\n    def macro_body(\n        self, node: nodes.Macro | nodes.CallBlock, frame: Frame\n    ) -> tuple[Frame, MacroRef]:\n        \"\"\"Dump the function def of a macro or call block.\"\"\"\n        frame = frame.inner()\n        frame.symbols.analyze_node(node)\n        macro_ref = MacroRef(node)\n\n        explicit_caller = None\n        skip_special_params = set()\n        args = []\n\n        for idx, arg in enumerate(node.args):\n            if arg.name == \"caller\":\n                explicit_caller = idx\n            if arg.name in (\"kwargs\", \"varargs\"):\n                skip_special_params.add(arg.name)\n            args.append(frame.symbols.ref(arg.name))\n\n        undeclared = find_undeclared(node.body, (\"caller\", \"kwargs\", \"varargs\"))\n\n        if \"caller\" in undeclared:\n            # In older Jinja versions there was a bug that allowed caller\n            # to retain the special behavior even if it was mentioned in\n            # the argument list.  However thankfully this was only really\n            # working if it was the last argument.  So we are explicitly\n            # checking this now and error out if it is anywhere else in\n            # the argument list.\n            if explicit_caller is not None:\n                try:\n                    node.defaults[explicit_caller - len(node.args)]\n                except IndexError:\n                    self.fail(\n                        \"When defining macros or call blocks the \"\n                        'special \"caller\" argument must be omitted '\n                        \"or be given a default.\",\n                        node.lineno,\n                    )\n            else:\n                args.append(frame.symbols.declare_parameter(\"caller\"))\n            macro_ref.accesses_caller = True\n        if \"kwargs\" in undeclared and \"kwargs\" not in skip_special_params:\n            args.append(frame.symbols.declare_parameter(\"kwargs\"))\n            macro_ref.accesses_kwargs = True\n        if \"varargs\" in undeclared and \"varargs\" not in skip_special_params:\n            args.append(frame.symbols.declare_parameter(\"varargs\"))\n            macro_ref.accesses_varargs = True\n\n        # macros are delayed, they never require output checks\n        frame.require_output_check = False\n        frame.symbols.analyze_node(node)\n        self.writeline(f\"{self.func('macro')}({', '.join(args)}):\", node)\n        self.indent()\n\n        self.buffer(frame)\n        self.enter_frame(frame)\n\n        self.push_parameter_definitions(frame)\n        for idx, arg in enumerate(node.args):\n            ref = frame.symbols.ref(arg.name)\n            self.writeline(f\"if {ref} is missing:\")\n            self.indent()\n            try:\n                default = node.defaults[idx - len(node.args)]\n            except IndexError:\n                self.writeline(\n                    f'{ref} = undefined(\"parameter {arg.name!r} was not provided\",'\n                    f\" name={arg.name!r})\"\n                )\n            else:\n                self.writeline(f\"{ref} = \")\n                self.visit(default, frame)\n            self.mark_parameter_stored(ref)\n            self.outdent()\n        self.pop_parameter_definitions()\n\n        self.blockvisit(node.body, frame)\n        self.return_buffer_contents(frame, force_unescaped=True)\n        self.leave_frame(frame, with_python_scope=True)\n        self.outdent()\n\n        return frame, macro_ref\n\n    def macro_def(self, macro_ref: MacroRef, frame: Frame) -> None:\n        \"\"\"Dump the macro definition for the def created by macro_body.\"\"\"\n        arg_tuple = \", \".join(repr(x.name) for x in macro_ref.node.args)\n        name = getattr(macro_ref.node, \"name\", None)\n        if len(macro_ref.node.args) == 1:\n            arg_tuple += \",\"\n        self.write(\n            f\"Macro(environment, macro, {name!r}, ({arg_tuple}),\"\n            f\" {macro_ref.accesses_kwargs!r}, {macro_ref.accesses_varargs!r},\"\n            f\" {macro_ref.accesses_caller!r}, context.eval_ctx.autoescape)\"\n        )\n\n    def position(self, node: nodes.Node) -> str:\n        \"\"\"Return a human readable position for the node.\"\"\"\n        rv = f\"line {node.lineno}\"\n        if self.name is not None:\n            rv = f\"{rv} in {self.name!r}\"\n        return rv\n\n    def dump_local_context(self, frame: Frame) -> str:\n        items_kv = \", \".join(\n            f\"{name!r}: {target}\"\n            for name, target in frame.symbols.dump_stores().items()\n        )\n        return f\"{{{items_kv}}}\"\n\n    def write_commons(self) -> None:\n        \"\"\"Writes a common preamble that is used by root and block functions.\n        Primarily this sets up common local helpers and enforces a generator\n        through a dead branch.\n        \"\"\"\n        self.writeline(\"resolve = context.resolve_or_missing\")\n        self.writeline(\"undefined = environment.undefined\")\n        self.writeline(\"concat = environment.concat\")\n        # always use the standard Undefined class for the implicit else of\n        # conditional expressions\n        self.writeline(\"cond_expr_undefined = Undefined\")\n        self.writeline(\"if 0: yield None\")\n\n    def push_parameter_definitions(self, frame: Frame) -> None:\n        \"\"\"Pushes all parameter targets from the given frame into a local\n        stack that permits tracking of yet to be assigned parameters.  In\n        particular this enables the optimization from `visit_Name` to skip\n        undefined expressions for parameters in macros as macros can reference\n        otherwise unbound parameters.\n        \"\"\"\n        self._param_def_block.append(frame.symbols.dump_param_targets())\n\n    def pop_parameter_definitions(self) -> None:\n        \"\"\"Pops the current parameter definitions set.\"\"\"\n        self._param_def_block.pop()\n\n    def mark_parameter_stored(self, target: str) -> None:\n        \"\"\"Marks a parameter in the current parameter definitions as stored.\n        This will skip the enforced undefined checks.\n        \"\"\"\n        if self._param_def_block:\n            self._param_def_block[-1].discard(target)\n\n    def push_context_reference(self, target: str) -> None:\n        self._context_reference_stack.append(target)\n\n    def pop_context_reference(self) -> None:\n        self._context_reference_stack.pop()\n\n    def get_context_ref(self) -> str:\n        return self._context_reference_stack[-1]\n\n    def get_resolve_func(self) -> str:\n        target = self._context_reference_stack[-1]\n        if target == \"context\":\n            return \"resolve\"\n        return f\"{target}.resolve\"\n\n    def derive_context(self, frame: Frame) -> str:\n        return f\"{self.get_context_ref()}.derived({self.dump_local_context(frame)})\"\n\n    def parameter_is_undeclared(self, target: str) -> bool:\n        \"\"\"Checks if a given target is an undeclared parameter.\"\"\"\n        if not self._param_def_block:\n            return False\n        return target in self._param_def_block[-1]\n\n    def push_assign_tracking(self) -> None:\n        \"\"\"Pushes a new layer for assignment tracking.\"\"\"\n        self._assign_stack.append(set())\n\n    def pop_assign_tracking(self, frame: Frame) -> None:\n        \"\"\"Pops the topmost level for assignment tracking and updates the\n        context variables if necessary.\n        \"\"\"\n        vars = self._assign_stack.pop()\n        if (\n            not frame.block_frame\n            and not frame.loop_frame\n            and not frame.toplevel\n            or not vars\n        ):\n            return\n        public_names = [x for x in vars if x[:1] != \"_\"]\n        if len(vars) == 1:\n            name = next(iter(vars))\n            ref = frame.symbols.ref(name)\n            if frame.loop_frame:\n                self.writeline(f\"_loop_vars[{name!r}] = {ref}\")\n                return\n            if frame.block_frame:\n                self.writeline(f\"_block_vars[{name!r}] = {ref}\")\n                return\n            self.writeline(f\"context.vars[{name!r}] = {ref}\")\n        else:\n            if frame.loop_frame:\n                self.writeline(\"_loop_vars.update({\")\n            elif frame.block_frame:\n                self.writeline(\"_block_vars.update({\")\n            else:\n                self.writeline(\"context.vars.update({\")\n            for idx, name in enumerate(sorted(vars)):\n                if idx:\n                    self.write(\", \")\n                ref = frame.symbols.ref(name)\n                self.write(f\"{name!r}: {ref}\")\n            self.write(\"})\")\n        if not frame.block_frame and not frame.loop_frame and public_names:\n            if len(public_names) == 1:\n                self.writeline(f\"context.exported_vars.add({public_names[0]!r})\")\n            else:\n                names_str = \", \".join(map(repr, sorted(public_names)))\n                self.writeline(f\"context.exported_vars.update(({names_str}))\")\n\n    # -- Statement Visitors\n\n    def visit_Template(self, node: nodes.Template, frame: Frame | None = None) -> None:\n        assert frame is None, \"no root frame allowed\"\n        eval_ctx = EvalContext(self.environment, self.name)\n\n        from .runtime import async_exported\n        from .runtime import exported\n\n        if self.environment.is_async:\n            exported_names = sorted(exported + async_exported)\n        else:\n            exported_names = sorted(exported)\n\n        self.writeline(\"from jinja2.runtime import \" + \", \".join(exported_names))\n\n        # if we want a deferred initialization we cannot move the\n        # environment into a local name\n        envenv = \"\" if self.defer_init else \", environment=environment\"\n\n        # do we have an extends tag at all?  If not, we can save some\n        # overhead by just not processing any inheritance code.\n        have_extends = node.find(nodes.Extends) is not None\n\n        # find all blocks\n        for block in node.find_all(nodes.Block):\n            if block.name in self.blocks:\n                self.fail(f\"block {block.name!r} defined twice\", block.lineno)\n            self.blocks[block.name] = block\n\n        # find all imports and import them\n        for import_ in node.find_all(nodes.ImportedName):\n            if import_.importname not in self.import_aliases:\n                imp = import_.importname\n                self.import_aliases[imp] = alias = self.temporary_identifier()\n                if \".\" in imp:\n                    module, obj = imp.rsplit(\".\", 1)\n                    self.writeline(f\"from {module} import {obj} as {alias}\")\n                else:\n                    self.writeline(f\"import {imp} as {alias}\")\n\n        # add the load name\n        self.writeline(f\"name = {self.name!r}\")\n\n        # generate the root render function.\n        self.writeline(\n            f\"{self.func('root')}(context, missing=missing{envenv}):\", extra=1\n        )\n        self.indent()\n        self.write_commons()\n\n        # process the root\n        frame = Frame(eval_ctx)\n        if \"self\" in find_undeclared(node.body, (\"self\",)):\n            ref = frame.symbols.declare_parameter(\"self\")\n            self.writeline(f\"{ref} = TemplateReference(context)\")\n        frame.symbols.analyze_node(node)\n        frame.toplevel = frame.rootlevel = True\n        frame.require_output_check = have_extends and not self.has_known_extends\n        if have_extends:\n            self.writeline(\"parent_template = None\")\n        self.enter_frame(frame)\n        self.pull_dependencies(node.body)\n        self.blockvisit(node.body, frame)\n        self.leave_frame(frame, with_python_scope=True)\n        self.outdent()\n\n        # make sure that the parent root is called.\n        if have_extends:\n            if not self.has_known_extends:\n                self.indent()\n                self.writeline(\"if parent_template is not None:\")\n            self.indent()\n            if not self.environment.is_async:\n                self.writeline(\"yield from parent_template.root_render_func(context)\")\n            else:\n                self.writeline(\"agen = parent_template.root_render_func(context)\")\n                self.writeline(\"try:\")\n                self.indent()\n                self.writeline(\"async for event in agen:\")\n                self.indent()\n                self.writeline(\"yield event\")\n                self.outdent()\n                self.outdent()\n                self.writeline(\"finally: await agen.aclose()\")\n            self.outdent(1 + (not self.has_known_extends))\n\n        # at this point we now have the blocks collected and can visit them too.\n        for name, block in self.blocks.items():\n            self.writeline(\n                f\"{self.func('block_' + name)}(context, missing=missing{envenv}):\",\n                block,\n                1,\n            )\n            self.indent()\n            self.write_commons()\n            # It's important that we do not make this frame a child of the\n            # toplevel template.  This would cause a variety of\n            # interesting issues with identifier tracking.\n            block_frame = Frame(eval_ctx)\n            block_frame.block_frame = True\n            undeclared = find_undeclared(block.body, (\"self\", \"super\"))\n            if \"self\" in undeclared:\n                ref = block_frame.symbols.declare_parameter(\"self\")\n                self.writeline(f\"{ref} = TemplateReference(context)\")\n            if \"super\" in undeclared:\n                ref = block_frame.symbols.declare_parameter(\"super\")\n                self.writeline(f\"{ref} = context.super({name!r}, block_{name})\")\n            block_frame.symbols.analyze_node(block)\n            block_frame.block = name\n            self.writeline(\"_block_vars = {}\")\n            self.enter_frame(block_frame)\n            self.pull_dependencies(block.body)\n            self.blockvisit(block.body, block_frame)\n            self.leave_frame(block_frame, with_python_scope=True)\n            self.outdent()\n\n        blocks_kv_str = \", \".join(f\"{x!r}: block_{x}\" for x in self.blocks)\n        self.writeline(f\"blocks = {{{blocks_kv_str}}}\", extra=1)\n        debug_kv_str = \"&\".join(f\"{k}={v}\" for k, v in self.debug_info)\n        self.writeline(f\"debug_info = {debug_kv_str!r}\")\n\n    def visit_Block(self, node: nodes.Block, frame: Frame) -> None:\n        \"\"\"Call a block and register it for the template.\"\"\"\n        level = 0\n        if frame.toplevel:\n            # if we know that we are a child template, there is no need to\n            # check if we are one\n            if self.has_known_extends:\n                return\n            if self.extends_so_far > 0:\n                self.writeline(\"if parent_template is None:\")\n                self.indent()\n                level += 1\n\n        if node.scoped:\n            context = self.derive_context(frame)\n        else:\n            context = self.get_context_ref()\n\n        if node.required:\n            self.writeline(f\"if len(context.blocks[{node.name!r}]) <= 1:\", node)\n            self.indent()\n            self.writeline(\n                f'raise TemplateRuntimeError(\"Required block {node.name!r} not found\")',\n                node,\n            )\n            self.outdent()\n\n        if not self.environment.is_async and frame.buffer is None:\n            self.writeline(\n                f\"yield from context.blocks[{node.name!r}][0]({context})\", node\n            )\n        else:\n            self.writeline(f\"gen = context.blocks[{node.name!r}][0]({context})\")\n            self.writeline(\"try:\")\n            self.indent()\n            self.writeline(\n                f\"{self.choose_async()}for event in gen:\",\n                node,\n            )\n            self.indent()\n            self.simple_write(\"event\", frame)\n            self.outdent()\n            self.outdent()\n            self.writeline(\n                f\"finally: {self.choose_async('await gen.aclose()', 'gen.close()')}\"\n            )\n\n        self.outdent(level)\n\n    def visit_Extends(self, node: nodes.Extends, frame: Frame) -> None:\n        \"\"\"Calls the extender.\"\"\"\n        if not frame.toplevel:\n            self.fail(\"cannot use extend from a non top-level scope\", node.lineno)\n\n        # if the number of extends statements in general is zero so\n        # far, we don't have to add a check if something extended\n        # the template before this one.\n        if self.extends_so_far > 0:\n            # if we have a known extends we just add a template runtime\n            # error into the generated code.  We could catch that at compile\n            # time too, but i welcome it not to confuse users by throwing the\n            # same error at different times just \"because we can\".\n            if not self.has_known_extends:\n                self.writeline(\"if parent_template is not None:\")\n                self.indent()\n            self.writeline('raise TemplateRuntimeError(\"extended multiple times\")')\n\n            # if we have a known extends already we don't need that code here\n            # as we know that the template execution will end here.\n            if self.has_known_extends:\n                raise CompilerExit()\n            else:\n                self.outdent()\n\n        self.writeline(\"parent_template = environment.get_template(\", node)\n        self.visit(node.template, frame)\n        self.write(f\", {self.name!r})\")\n        self.writeline(\"for name, parent_block in parent_template.blocks.items():\")\n        self.indent()\n        self.writeline(\"context.blocks.setdefault(name, []).append(parent_block)\")\n        self.outdent()\n\n        # if this extends statement was in the root level we can take\n        # advantage of that information and simplify the generated code\n        # in the top level from this point onwards\n        if frame.rootlevel:\n            self.has_known_extends = True\n\n        # and now we have one more\n        self.extends_so_far += 1\n\n    def visit_Include(self, node: nodes.Include, frame: Frame) -> None:\n        \"\"\"Handles includes.\"\"\"\n        if node.ignore_missing:\n            self.writeline(\"try:\")\n            self.indent()\n\n        func_name = \"get_or_select_template\"\n        if isinstance(node.template, nodes.Const):\n            if isinstance(node.template.value, str):\n                func_name = \"get_template\"\n            elif isinstance(node.template.value, (tuple, list)):\n                func_name = \"select_template\"\n        elif isinstance(node.template, (nodes.Tuple, nodes.List)):\n            func_name = \"select_template\"\n\n        self.writeline(f\"template = environment.{func_name}(\", node)\n        self.visit(node.template, frame)\n        self.write(f\", {self.name!r})\")\n        if node.ignore_missing:\n            self.outdent()\n            self.writeline(\"except TemplateNotFound:\")\n            self.indent()\n            self.writeline(\"pass\")\n            self.outdent()\n            self.writeline(\"else:\")\n            self.indent()\n\n        def loop_body() -> None:\n            self.indent()\n            self.simple_write(\"event\", frame)\n            self.outdent()\n\n        if node.with_context:\n            self.writeline(\n                f\"gen = template.root_render_func(\"\n                \"template.new_context(context.get_all(), True,\"\n                f\" {self.dump_local_context(frame)}))\"\n            )\n            self.writeline(\"try:\")\n            self.indent()\n            self.writeline(f\"{self.choose_async()}for event in gen:\")\n            loop_body()\n            self.outdent()\n            self.writeline(\n                f\"finally: {self.choose_async('await gen.aclose()', 'gen.close()')}\"\n            )\n        elif self.environment.is_async:\n            self.writeline(\n                \"for event in (await template._get_default_module_async())\"\n                \"._body_stream:\"\n            )\n            loop_body()\n        else:\n            self.writeline(\"yield from template._get_default_module()._body_stream\")\n\n        if node.ignore_missing:\n            self.outdent()\n\n    def _import_common(\n        self, node: nodes.Import | nodes.FromImport, frame: Frame\n    ) -> None:\n        self.write(f\"{self.choose_async('await ')}environment.get_template(\")\n        self.visit(node.template, frame)\n        self.write(f\", {self.name!r}).\")\n\n        if node.with_context:\n            f_name = f\"make_module{self.choose_async('_async')}\"\n            self.write(\n                f\"{f_name}(context.get_all(), True, {self.dump_local_context(frame)})\"\n            )\n        else:\n            self.write(f\"_get_default_module{self.choose_async('_async')}(context)\")\n\n    def visit_Import(self, node: nodes.Import, frame: Frame) -> None:\n        \"\"\"Visit regular imports.\"\"\"\n        self.writeline(f\"{frame.symbols.ref(node.target)} = \", node)\n        if frame.toplevel:\n            self.write(f\"context.vars[{node.target!r}] = \")\n\n        self._import_common(node, frame)\n\n        if frame.toplevel and not node.target.startswith(\"_\"):\n            self.writeline(f\"context.exported_vars.discard({node.target!r})\")\n\n    def visit_FromImport(self, node: nodes.FromImport, frame: Frame) -> None:\n        \"\"\"Visit named imports.\"\"\"\n        self.newline(node)\n        self.write(\"included_template = \")\n        self._import_common(node, frame)\n        var_names = []\n        discarded_names = []\n        for name in node.names:\n            if isinstance(name, tuple):\n                name, alias = name\n            else:\n                alias = name\n            self.writeline(\n                f\"{frame.symbols.ref(alias)} =\"\n                f\" getattr(included_template, {name!r}, missing)\"\n            )\n            self.writeline(f\"if {frame.symbols.ref(alias)} is missing:\")\n            self.indent()\n            # The position will contain the template name, and will be formatted\n            # into a string that will be compiled into an f-string. Curly braces\n            # in the name must be replaced with escapes so that they will not be\n            # executed as part of the f-string.\n            position = self.position(node).replace(\"{\", \"{{\").replace(\"}\", \"}}\")\n            message = (\n                \"the template {included_template.__name__!r}\"\n                f\" (imported on {position})\"\n                f\" does not export the requested name {name!r}\"\n            )\n            self.writeline(\n                f\"{frame.symbols.ref(alias)} = undefined(f{message!r}, name={name!r})\"\n            )\n            self.outdent()\n            if frame.toplevel:\n                var_names.append(alias)\n                if not alias.startswith(\"_\"):\n                    discarded_names.append(alias)\n\n        if var_names:\n            if len(var_names) == 1:\n                name = var_names[0]\n                self.writeline(f\"context.vars[{name!r}] = {frame.symbols.ref(name)}\")\n            else:\n                names_kv = \", \".join(\n                    f\"{name!r}: {frame.symbols.ref(name)}\" for name in var_names\n                )\n                self.writeline(f\"context.vars.update({{{names_kv}}})\")\n        if discarded_names:\n            if len(discarded_names) == 1:\n                self.writeline(f\"context.exported_vars.discard({discarded_names[0]!r})\")\n            else:\n                names_str = \", \".join(map(repr, discarded_names))\n                self.writeline(\n                    f\"context.exported_vars.difference_update(({names_str}))\"\n                )\n\n    def visit_For(self, node: nodes.For, frame: Frame) -> None:\n        loop_frame = frame.inner()\n        loop_frame.loop_frame = True\n        test_frame = frame.inner()\n        else_frame = frame.inner()\n\n        # try to figure out if we have an extended loop.  An extended loop\n        # is necessary if the loop is in recursive mode if the special loop\n        # variable is accessed in the body if the body is a scoped block.\n        extended_loop = (\n            node.recursive\n            or \"loop\"\n            in find_undeclared(node.iter_child_nodes(only=(\"body\",)), (\"loop\",))\n            or any(block.scoped for block in node.find_all(nodes.Block))\n        )\n\n        loop_ref = None\n        if extended_loop:\n            loop_ref = loop_frame.symbols.declare_parameter(\"loop\")\n\n        loop_frame.symbols.analyze_node(node, for_branch=\"body\")\n        if node.else_:\n            else_frame.symbols.analyze_node(node, for_branch=\"else\")\n\n        if node.test:\n            loop_filter_func = self.temporary_identifier()\n            test_frame.symbols.analyze_node(node, for_branch=\"test\")\n            self.writeline(f\"{self.func(loop_filter_func)}(fiter):\", node.test)\n            self.indent()\n            self.enter_frame(test_frame)\n            self.writeline(self.choose_async(\"async for \", \"for \"))\n            self.visit(node.target, loop_frame)\n            self.write(\" in \")\n            self.write(self.choose_async(\"auto_aiter(fiter)\", \"fiter\"))\n            self.write(\":\")\n            self.indent()\n            self.writeline(\"if \", node.test)\n            self.visit(node.test, test_frame)\n            self.write(\":\")\n            self.indent()\n            self.writeline(\"yield \")\n            self.visit(node.target, loop_frame)\n            self.outdent(3)\n            self.leave_frame(test_frame, with_python_scope=True)\n\n        # if we don't have an recursive loop we have to find the shadowed\n        # variables at that point.  Because loops can be nested but the loop\n        # variable is a special one we have to enforce aliasing for it.\n        if node.recursive:\n            self.writeline(\n                f\"{self.func('loop')}(reciter, loop_render_func, depth=0):\", node\n            )\n            self.indent()\n            self.buffer(loop_frame)\n\n            # Use the same buffer for the else frame\n            else_frame.buffer = loop_frame.buffer\n\n        # make sure the loop variable is a special one and raise a template\n        # assertion error if a loop tries to write to loop\n        if extended_loop:\n            self.writeline(f\"{loop_ref} = missing\")\n\n        for name in node.find_all(nodes.Name):\n            if name.ctx == \"store\" and name.name == \"loop\":\n                self.fail(\n                    \"Can't assign to special loop variable in for-loop target\",\n                    name.lineno,\n                )\n\n        if node.else_:\n            iteration_indicator = self.temporary_identifier()\n            self.writeline(f\"{iteration_indicator} = 1\")\n\n        self.writeline(self.choose_async(\"async for \", \"for \"), node)\n        self.visit(node.target, loop_frame)\n        if extended_loop:\n            self.write(f\", {loop_ref} in {self.choose_async('Async')}LoopContext(\")\n        else:\n            self.write(\" in \")\n\n        if node.test:\n            self.write(f\"{loop_filter_func}(\")\n        if node.recursive:\n            self.write(\"reciter\")\n        else:\n            if self.environment.is_async and not extended_loop:\n                self.write(\"auto_aiter(\")\n            self.visit(node.iter, frame)\n            if self.environment.is_async and not extended_loop:\n                self.write(\")\")\n        if node.test:\n            self.write(\")\")\n\n        if node.recursive:\n            self.write(\", undefined, loop_render_func, depth):\")\n        else:\n            self.write(\", undefined):\" if extended_loop else \":\")\n\n        self.indent()\n        self.enter_frame(loop_frame)\n\n        self.writeline(\"_loop_vars = {}\")\n        self.blockvisit(node.body, loop_frame)\n        if node.else_:\n            self.writeline(f\"{iteration_indicator} = 0\")\n        self.outdent()\n        self.leave_frame(\n            loop_frame, with_python_scope=node.recursive and not node.else_\n        )\n\n        if node.else_:\n            self.writeline(f\"if {iteration_indicator}:\")\n            self.indent()\n            self.enter_frame(else_frame)\n            self.blockvisit(node.else_, else_frame)\n            self.leave_frame(else_frame)\n            self.outdent()\n\n        # if the node was recursive we have to return the buffer contents\n        # and start the iteration code\n        if node.recursive:\n            self.return_buffer_contents(loop_frame)\n            self.outdent()\n            self.start_write(frame, node)\n            self.write(f\"{self.choose_async('await ')}loop(\")\n            if self.environment.is_async:\n                self.write(\"auto_aiter(\")\n            self.visit(node.iter, frame)\n            if self.environment.is_async:\n                self.write(\")\")\n            self.write(\", loop)\")\n            self.end_write(frame)\n\n        # at the end of the iteration, clear any assignments made in the\n        # loop from the top level\n        if self._assign_stack:\n            self._assign_stack[-1].difference_update(loop_frame.symbols.stores)\n\n    def visit_If(self, node: nodes.If, frame: Frame) -> None:\n        if_frame = frame.soft()\n        self.writeline(\"if \", node)\n        self.visit(node.test, if_frame)\n        self.write(\":\")\n        self.indent()\n        self.blockvisit(node.body, if_frame)\n        self.outdent()\n        for elif_ in node.elif_:\n            self.writeline(\"elif \", elif_)\n            self.visit(elif_.test, if_frame)\n            self.write(\":\")\n            self.indent()\n            self.blockvisit(elif_.body, if_frame)\n            self.outdent()\n        if node.else_:\n            self.writeline(\"else:\")\n            self.indent()\n            self.blockvisit(node.else_, if_frame)\n            self.outdent()\n\n    def visit_Macro(self, node: nodes.Macro, frame: Frame) -> None:\n        macro_frame, macro_ref = self.macro_body(node, frame)\n        self.newline()\n        if frame.toplevel:\n            if not node.name.startswith(\"_\"):\n                self.write(f\"context.exported_vars.add({node.name!r})\")\n            self.writeline(f\"context.vars[{node.name!r}] = \")\n        self.write(f\"{frame.symbols.ref(node.name)} = \")\n        self.macro_def(macro_ref, macro_frame)\n\n    def visit_CallBlock(self, node: nodes.CallBlock, frame: Frame) -> None:\n        call_frame, macro_ref = self.macro_body(node, frame)\n        self.writeline(\"caller = \")\n        self.macro_def(macro_ref, call_frame)\n        self.start_write(frame, node)\n        self.visit_Call(node.call, frame, forward_caller=True)\n        self.end_write(frame)\n\n    def visit_FilterBlock(self, node: nodes.FilterBlock, frame: Frame) -> None:\n        filter_frame = frame.inner()\n        filter_frame.symbols.analyze_node(node)\n        self.enter_frame(filter_frame)\n        self.buffer(filter_frame)\n        self.blockvisit(node.body, filter_frame)\n        self.start_write(frame, node)\n        self.visit_Filter(node.filter, filter_frame)\n        self.end_write(frame)\n        self.leave_frame(filter_frame)\n\n    def visit_With(self, node: nodes.With, frame: Frame) -> None:\n        with_frame = frame.inner()\n        with_frame.symbols.analyze_node(node)\n        self.enter_frame(with_frame)\n        for target, expr in zip(node.targets, node.values, strict=False):\n            self.newline()\n            self.visit(target, with_frame)\n            self.write(\" = \")\n            self.visit(expr, frame)\n        self.blockvisit(node.body, with_frame)\n        self.leave_frame(with_frame)\n\n    def visit_ExprStmt(self, node: nodes.ExprStmt, frame: Frame) -> None:\n        self.newline(node)\n        self.visit(node.node, frame)\n\n    class _FinalizeInfo(t.NamedTuple):\n        const: t.Callable[..., str] | None\n        src: str | None\n\n    @staticmethod\n    def _default_finalize(value: t.Any) -> t.Any:\n        \"\"\"The default finalize function if the environment isn't\n        configured with one. Or, if the environment has one, this is\n        called on that function's output for constants.\n        \"\"\"\n        return str(value)\n\n    _finalize: _FinalizeInfo | None = None\n\n    def _make_finalize(self) -> _FinalizeInfo:\n        \"\"\"Build the finalize function to be used on constants and at\n        runtime. Cached so it's only created once for all output nodes.\n\n        Returns a ``namedtuple`` with the following attributes:\n\n        ``const``\n            A function to finalize constant data at compile time.\n\n        ``src``\n            Source code to output around nodes to be evaluated at\n            runtime.\n        \"\"\"\n        if self._finalize is not None:\n            return self._finalize\n\n        finalize: t.Callable[..., t.Any] | None\n        finalize = default = self._default_finalize\n        src = None\n\n        if self.environment.finalize:\n            src = \"environment.finalize(\"\n            env_finalize = self.environment.finalize\n            pass_arg = {\n                _PassArg.context: \"context\",\n                _PassArg.eval_context: \"context.eval_ctx\",\n                _PassArg.environment: \"environment\",\n            }.get(\n                _PassArg.from_obj(env_finalize)  # type: ignore\n            )\n            finalize = None\n\n            if pass_arg is None:\n\n                def finalize(value: t.Any) -> t.Any:  # noqa: F811\n                    return default(env_finalize(value))\n\n            else:\n                src = f\"{src}{pass_arg}, \"\n\n                if pass_arg == \"environment\":\n\n                    def finalize(value: t.Any) -> t.Any:  # noqa: F811\n                        return default(env_finalize(self.environment, value))\n\n        self._finalize = self._FinalizeInfo(finalize, src)\n        return self._finalize\n\n    def _output_const_repr(self, group: t.Iterable[t.Any]) -> str:\n        \"\"\"Given a group of constant values converted from ``Output``\n        child nodes, produce a string to write to the template module\n        source.\n        \"\"\"\n        return repr(concat(group))\n\n    def _output_child_to_const(\n        self, node: nodes.Expr, frame: Frame, finalize: _FinalizeInfo\n    ) -> str:\n        \"\"\"Try to optimize a child of an ``Output`` node by trying to\n        convert it to constant, finalized data at compile time.\n\n        If :exc:`Impossible` is raised, the node is not constant and\n        will be evaluated at runtime. Any other exception will also be\n        evaluated at runtime for easier debugging.\n        \"\"\"\n        const = node.as_const(frame.eval_ctx)\n\n        if frame.eval_ctx.autoescape:\n            const = escape(const)\n\n        # Template data doesn't go through finalize.\n        if isinstance(node, nodes.TemplateData):\n            return str(const)\n\n        return finalize.const(const)  # type: ignore\n\n    def _output_child_pre(\n        self, node: nodes.Expr, frame: Frame, finalize: _FinalizeInfo\n    ) -> None:\n        \"\"\"Output extra source code before visiting a child of an\n        ``Output`` node.\n        \"\"\"\n        if frame.eval_ctx.volatile:\n            self.write(\"(escape if context.eval_ctx.autoescape else str)(\")\n        elif frame.eval_ctx.autoescape:\n            self.write(\"escape(\")\n        else:\n            self.write(\"str(\")\n\n        if finalize.src is not None:\n            self.write(finalize.src)\n\n    def _output_child_post(\n        self, node: nodes.Expr, frame: Frame, finalize: _FinalizeInfo\n    ) -> None:\n        \"\"\"Output extra source code after visiting a child of an\n        ``Output`` node.\n        \"\"\"\n        self.write(\")\")\n\n        if finalize.src is not None:\n            self.write(\")\")\n\n    def visit_Output(self, node: nodes.Output, frame: Frame) -> None:\n        # If an extends is active, don't render outside a block.\n        if frame.require_output_check:\n            # A top-level extends is known to exist at compile time.\n            if self.has_known_extends:\n                return\n\n            self.writeline(\"if parent_template is None:\")\n            self.indent()\n\n        finalize = self._make_finalize()\n        body: list[list[t.Any] | nodes.Expr] = []\n\n        # Evaluate constants at compile time if possible. Each item in\n        # body will be either a list of static data or a node to be\n        # evaluated at runtime.\n        for child in node.nodes:\n            try:\n                if not (\n                    # If the finalize function requires runtime context,\n                    # constants can't be evaluated at compile time.\n                    finalize.const\n                    # Unless it's basic template data that won't be\n                    # finalized anyway.\n                    or isinstance(child, nodes.TemplateData)\n                ):\n                    raise nodes.Impossible()\n\n                const = self._output_child_to_const(child, frame, finalize)\n            except (nodes.Impossible, Exception):\n                # The node was not constant and needs to be evaluated at\n                # runtime. Or another error was raised, which is easier\n                # to debug at runtime.\n                body.append(child)\n                continue\n\n            if body and isinstance(body[-1], list):\n                body[-1].append(const)\n            else:\n                body.append([const])\n\n        if frame.buffer is not None:\n            if len(body) == 1:\n                self.writeline(f\"{frame.buffer}.append(\")\n            else:\n                self.writeline(f\"{frame.buffer}.extend((\")\n\n            self.indent()\n\n        for item in body:\n            if isinstance(item, list):\n                # A group of constant data to join and output.\n                val = self._output_const_repr(item)\n\n                if frame.buffer is None:\n                    self.writeline(\"yield \" + val)\n                else:\n                    self.writeline(val + \",\")\n            else:\n                if frame.buffer is None:\n                    self.writeline(\"yield \", item)\n                else:\n                    self.newline(item)\n\n                # A node to be evaluated at runtime.\n                self._output_child_pre(item, frame, finalize)\n                self.visit(item, frame)\n                self._output_child_post(item, frame, finalize)\n\n                if frame.buffer is not None:\n                    self.write(\",\")\n\n        if frame.buffer is not None:\n            self.outdent()\n            self.writeline(\")\" if len(body) == 1 else \"))\")\n\n        if frame.require_output_check:\n            self.outdent()\n\n    def visit_Assign(self, node: nodes.Assign, frame: Frame) -> None:\n        self.push_assign_tracking()\n\n        # ``a.b`` is allowed for assignment, and is parsed as an NSRef. However,\n        # it is only valid if it references a Namespace object. Emit a check for\n        # that for each ref here, before assignment code is emitted. This can't\n        # be done in visit_NSRef as the ref could be in the middle of a tuple.\n        seen_refs: set[str] = set()\n\n        for nsref in node.find_all(nodes.NSRef):\n            if nsref.name in seen_refs:\n                # Only emit the check for each reference once, in case the same\n                # ref is used multiple times in a tuple, `ns.a, ns.b = c, d`.\n                continue\n\n            seen_refs.add(nsref.name)\n            ref = frame.symbols.ref(nsref.name)\n            self.writeline(f\"if not isinstance({ref}, Namespace):\")\n            self.indent()\n            self.writeline(\n                \"raise TemplateRuntimeError\"\n                '(\"cannot assign attribute on non-namespace object\")'\n            )\n            self.outdent()\n\n        self.newline(node)\n        self.visit(node.target, frame)\n        self.write(\" = \")\n        self.visit(node.node, frame)\n        self.pop_assign_tracking(frame)\n\n    def visit_AssignBlock(self, node: nodes.AssignBlock, frame: Frame) -> None:\n        self.push_assign_tracking()\n        block_frame = frame.inner()\n        # This is a special case.  Since a set block always captures we\n        # will disable output checks.  This way one can use set blocks\n        # toplevel even in extended templates.\n        block_frame.require_output_check = False\n        block_frame.symbols.analyze_node(node)\n        self.enter_frame(block_frame)\n        self.buffer(block_frame)\n        self.blockvisit(node.body, block_frame)\n        self.newline(node)\n        self.visit(node.target, frame)\n        self.write(\" = (Markup if context.eval_ctx.autoescape else identity)(\")\n        if node.filter is not None:\n            self.visit_Filter(node.filter, block_frame)\n        else:\n            self.write(f\"concat({block_frame.buffer})\")\n        self.write(\")\")\n        self.pop_assign_tracking(frame)\n        self.leave_frame(block_frame)\n\n    # -- Expression Visitors\n\n    def visit_Name(self, node: nodes.Name, frame: Frame) -> None:\n        if node.ctx == \"store\" and (\n            frame.toplevel or frame.loop_frame or frame.block_frame\n        ):\n            if self._assign_stack:\n                self._assign_stack[-1].add(node.name)\n        ref = frame.symbols.ref(node.name)\n\n        # If we are looking up a variable we might have to deal with the\n        # case where it's undefined.  We can skip that case if the load\n        # instruction indicates a parameter which are always defined.\n        if node.ctx == \"load\":\n            load = frame.symbols.find_load(ref)\n            if not (\n                load is not None\n                and load[0] == VAR_LOAD_PARAMETER\n                and not self.parameter_is_undeclared(ref)\n            ):\n                self.write(\n                    f\"(undefined(name={node.name!r}) if {ref} is missing else {ref})\"\n                )\n                return\n\n        self.write(ref)\n\n    def visit_NSRef(self, node: nodes.NSRef, frame: Frame) -> None:\n        # NSRef is a dotted assignment target a.b=c, but uses a[b]=c internally.\n        # visit_Assign emits code to validate that each ref is to a Namespace\n        # object only. That can't be emitted here as the ref could be in the\n        # middle of a tuple assignment.\n        ref = frame.symbols.ref(node.name)\n        self.writeline(f\"{ref}[{node.attr!r}]\")\n\n    def visit_Const(self, node: nodes.Const, frame: Frame) -> None:\n        val = node.as_const(frame.eval_ctx)\n        if isinstance(val, float):\n            self.write(str(val))\n        else:\n            self.write(repr(val))\n\n    def visit_TemplateData(self, node: nodes.TemplateData, frame: Frame) -> None:\n        try:\n            self.write(repr(node.as_const(frame.eval_ctx)))\n        except nodes.Impossible:\n            self.write(\n                f\"(Markup if context.eval_ctx.autoescape else identity)({node.data!r})\"\n            )\n\n    def visit_Tuple(self, node: nodes.Tuple, frame: Frame) -> None:\n        self.write(\"(\")\n        idx = -1\n        for idx, item in enumerate(node.items):\n            if idx:\n                self.write(\", \")\n            self.visit(item, frame)\n        self.write(\",)\" if idx == 0 else \")\")\n\n    def visit_List(self, node: nodes.List, frame: Frame) -> None:\n        self.write(\"[\")\n        for idx, item in enumerate(node.items):\n            if idx:\n                self.write(\", \")\n            self.visit(item, frame)\n        self.write(\"]\")\n\n    def visit_Dict(self, node: nodes.Dict, frame: Frame) -> None:\n        self.write(\"{\")\n        for idx, item in enumerate(node.items):\n            if idx:\n                self.write(\", \")\n            self.visit(item.key, frame)\n            self.write(\": \")\n            self.visit(item.value, frame)\n        self.write(\"}\")\n\n    visit_Add = _make_binop(\"+\")\n    visit_Sub = _make_binop(\"-\")\n    visit_Mul = _make_binop(\"*\")\n    visit_Div = _make_binop(\"/\")\n    visit_FloorDiv = _make_binop(\"//\")\n    visit_Pow = _make_binop(\"**\")\n    visit_Mod = _make_binop(\"%\")\n    visit_And = _make_binop(\"and\")\n    visit_Or = _make_binop(\"or\")\n    visit_Pos = _make_unop(\"+\")\n    visit_Neg = _make_unop(\"-\")\n    visit_Not = _make_unop(\"not \")\n\n    @optimizeconst\n    def visit_Concat(self, node: nodes.Concat, frame: Frame) -> None:\n        if frame.eval_ctx.volatile:\n            func_name = \"(markup_join if context.eval_ctx.volatile else str_join)\"\n        elif frame.eval_ctx.autoescape:\n            func_name = \"markup_join\"\n        else:\n            func_name = \"str_join\"\n        self.write(f\"{func_name}((\")\n        for arg in node.nodes:\n            self.visit(arg, frame)\n            self.write(\", \")\n        self.write(\"))\")\n\n    @optimizeconst\n    def visit_Compare(self, node: nodes.Compare, frame: Frame) -> None:\n        self.write(\"(\")\n        self.visit(node.expr, frame)\n        for op in node.ops:\n            self.visit(op, frame)\n        self.write(\")\")\n\n    def visit_Operand(self, node: nodes.Operand, frame: Frame) -> None:\n        self.write(f\" {operators[node.op]} \")\n        self.visit(node.expr, frame)\n\n    @optimizeconst\n    def visit_Getattr(self, node: nodes.Getattr, frame: Frame) -> None:\n        if self.environment.is_async:\n            self.write(\"(await auto_await(\")\n\n        self.write(\"environment.getattr(\")\n        self.visit(node.node, frame)\n        self.write(f\", {node.attr!r})\")\n\n        if self.environment.is_async:\n            self.write(\"))\")\n\n    @optimizeconst\n    def visit_Getitem(self, node: nodes.Getitem, frame: Frame) -> None:\n        # slices bypass the environment getitem method.\n        if isinstance(node.arg, nodes.Slice):\n            self.visit(node.node, frame)\n            self.write(\"[\")\n            self.visit(node.arg, frame)\n            self.write(\"]\")\n        else:\n            if self.environment.is_async:\n                self.write(\"(await auto_await(\")\n\n            self.write(\"environment.getitem(\")\n            self.visit(node.node, frame)\n            self.write(\", \")\n            self.visit(node.arg, frame)\n            self.write(\")\")\n\n            if self.environment.is_async:\n                self.write(\"))\")\n\n    def visit_Slice(self, node: nodes.Slice, frame: Frame) -> None:\n        if node.start is not None:\n            self.visit(node.start, frame)\n        self.write(\":\")\n        if node.stop is not None:\n            self.visit(node.stop, frame)\n        if node.step is not None:\n            self.write(\":\")\n            self.visit(node.step, frame)\n\n    @contextmanager\n    def _filter_test_common(\n        self, node: nodes.Filter | nodes.Test, frame: Frame, is_filter: bool\n    ) -> t.Iterator[None]:\n        if self.environment.is_async:\n            self.write(\"(await auto_await(\")\n\n        if is_filter:\n            self.write(f\"{self.filters[node.name]}(\")\n            func = self.environment.filters.get(node.name)\n        else:\n            self.write(f\"{self.tests[node.name]}(\")\n            func = self.environment.tests.get(node.name)\n\n        # When inside an If or CondExpr frame, allow the filter to be\n        # undefined at compile time and only raise an error if it's\n        # actually called at runtime. See pull_dependencies.\n        if func is None and not frame.soft_frame:\n            type_name = \"filter\" if is_filter else \"test\"\n            self.fail(f\"No {type_name} named {node.name!r}.\", node.lineno)\n\n        pass_arg = {\n            _PassArg.context: \"context\",\n            _PassArg.eval_context: \"context.eval_ctx\",\n            _PassArg.environment: \"environment\",\n        }.get(\n            _PassArg.from_obj(func)  # type: ignore\n        )\n\n        if pass_arg is not None:\n            self.write(f\"{pass_arg}, \")\n\n        # Back to the visitor function to handle visiting the target of\n        # the filter or test.\n        yield\n\n        self.signature(node, frame)\n        self.write(\")\")\n\n        if self.environment.is_async:\n            self.write(\"))\")\n\n    @optimizeconst\n    def visit_Filter(self, node: nodes.Filter, frame: Frame) -> None:\n        with self._filter_test_common(node, frame, True):\n            # if the filter node is None we are inside a filter block\n            # and want to write to the current buffer\n            if node.node is not None:\n                self.visit(node.node, frame)\n            elif frame.eval_ctx.volatile:\n                self.write(\n                    f\"(Markup(concat({frame.buffer}))\"\n                    f\" if context.eval_ctx.autoescape else concat({frame.buffer}))\"\n                )\n            elif frame.eval_ctx.autoescape:\n                self.write(f\"Markup(concat({frame.buffer}))\")\n            else:\n                self.write(f\"concat({frame.buffer})\")\n\n    @optimizeconst\n    def visit_Test(self, node: nodes.Test, frame: Frame) -> None:\n        with self._filter_test_common(node, frame, False):\n            self.visit(node.node, frame)\n\n    @optimizeconst\n    def visit_CondExpr(self, node: nodes.CondExpr, frame: Frame) -> None:\n        frame = frame.soft()\n\n        def write_expr2() -> None:\n            if node.expr2 is not None:\n                self.visit(node.expr2, frame)\n                return\n\n            self.write(\n                f'cond_expr_undefined(\"the inline if-expression on'\n                f\" {self.position(node)} evaluated to false and no else\"\n                f' section was defined.\")'\n            )\n\n        self.write(\"(\")\n        self.visit(node.expr1, frame)\n        self.write(\" if \")\n        self.visit(node.test, frame)\n        self.write(\" else \")\n        write_expr2()\n        self.write(\")\")\n\n    @optimizeconst\n    def visit_Call(\n        self, node: nodes.Call, frame: Frame, forward_caller: bool = False\n    ) -> None:\n        if self.environment.is_async:\n            self.write(\"(await auto_await(\")\n        if self.environment.sandboxed:\n            self.write(\"environment.call(context, \")\n        else:\n            self.write(\"context.call(\")\n        self.visit(node.node, frame)\n        extra_kwargs = {\"caller\": \"caller\"} if forward_caller else None\n        loop_kwargs = {\"_loop_vars\": \"_loop_vars\"} if frame.loop_frame else {}\n        block_kwargs = {\"_block_vars\": \"_block_vars\"} if frame.block_frame else {}\n        if extra_kwargs:\n            extra_kwargs.update(loop_kwargs, **block_kwargs)\n        elif loop_kwargs or block_kwargs:\n            extra_kwargs = dict(loop_kwargs, **block_kwargs)\n        self.signature(node, frame, extra_kwargs)\n        self.write(\")\")\n        if self.environment.is_async:\n            self.write(\"))\")\n\n    def visit_Keyword(self, node: nodes.Keyword, frame: Frame) -> None:\n        self.write(node.key + \"=\")\n        self.visit(node.value, frame)\n\n    # -- Unused nodes for extensions\n\n    def visit_MarkSafe(self, node: nodes.MarkSafe, frame: Frame) -> None:\n        self.write(\"Markup(\")\n        self.visit(node.expr, frame)\n        self.write(\")\")\n\n    def visit_MarkSafeIfAutoescape(\n        self, node: nodes.MarkSafeIfAutoescape, frame: Frame\n    ) -> None:\n        self.write(\"(Markup if context.eval_ctx.autoescape else identity)(\")\n        self.visit(node.expr, frame)\n        self.write(\")\")\n\n    def visit_EnvironmentAttribute(\n        self, node: nodes.EnvironmentAttribute, frame: Frame\n    ) -> None:\n        self.write(\"environment.\" + node.name)\n\n    def visit_ExtensionAttribute(\n        self, node: nodes.ExtensionAttribute, frame: Frame\n    ) -> None:\n        self.write(f\"environment.extensions[{node.identifier!r}].{node.name}\")\n\n    def visit_ImportedName(self, node: nodes.ImportedName, frame: Frame) -> None:\n        self.write(self.import_aliases[node.importname])\n\n    def visit_InternalName(self, node: nodes.InternalName, frame: Frame) -> None:\n        self.write(node.name)\n\n    def visit_ContextReference(\n        self, node: nodes.ContextReference, frame: Frame\n    ) -> None:\n        self.write(\"context\")\n\n    def visit_DerivedContextReference(\n        self, node: nodes.DerivedContextReference, frame: Frame\n    ) -> None:\n        self.write(self.derive_context(frame))\n\n    def visit_Continue(self, node: nodes.Continue, frame: Frame) -> None:\n        self.writeline(\"continue\", node)\n\n    def visit_Break(self, node: nodes.Break, frame: Frame) -> None:\n        self.writeline(\"break\", node)\n\n    def visit_Scope(self, node: nodes.Scope, frame: Frame) -> None:\n        scope_frame = frame.inner()\n        scope_frame.symbols.analyze_node(node)\n        self.enter_frame(scope_frame)\n        self.blockvisit(node.body, scope_frame)\n        self.leave_frame(scope_frame)\n\n    def visit_OverlayScope(self, node: nodes.OverlayScope, frame: Frame) -> None:\n        ctx = self.temporary_identifier()\n        self.writeline(f\"{ctx} = {self.derive_context(frame)}\")\n        self.writeline(f\"{ctx}.vars = \")\n        self.visit(node.context, frame)\n        self.push_context_reference(ctx)\n\n        scope_frame = frame.inner(isolated=True)\n        scope_frame.symbols.analyze_node(node)\n        self.enter_frame(scope_frame)\n        self.blockvisit(node.body, scope_frame)\n        self.leave_frame(scope_frame)\n        self.pop_context_reference()\n\n    def visit_EvalContextModifier(\n        self, node: nodes.EvalContextModifier, frame: Frame\n    ) -> None:\n        for keyword in node.options:\n            self.writeline(f\"context.eval_ctx.{keyword.key} = \")\n            self.visit(keyword.value, frame)\n            try:\n                val = keyword.value.as_const(frame.eval_ctx)\n            except nodes.Impossible:\n                frame.eval_ctx.volatile = True\n            else:\n                setattr(frame.eval_ctx, keyword.key, val)\n\n    def visit_ScopedEvalContextModifier(\n        self, node: nodes.ScopedEvalContextModifier, frame: Frame\n    ) -> None:\n        old_ctx_name = self.temporary_identifier()\n        saved_ctx = frame.eval_ctx.save()\n        self.writeline(f\"{old_ctx_name} = context.eval_ctx.save()\")\n        self.visit_EvalContextModifier(node, frame)\n        for child in node.body:\n            self.visit(child, frame)\n        frame.eval_ctx.revert(saved_ctx)\n        self.writeline(f\"context.eval_ctx.revert({old_ctx_name})\")\n"
  },
  {
    "path": "src/jinja2/constants.py",
    "content": "#: list of lorem ipsum words used by the lipsum() helper function\nLOREM_IPSUM_WORDS = \"\"\"\\\na ac accumsan ad adipiscing aenean aliquam aliquet amet ante aptent arcu at\nauctor augue bibendum blandit class commodo condimentum congue consectetuer\nconsequat conubia convallis cras cubilia cum curabitur curae cursus dapibus\ndiam dictum dictumst dignissim dis dolor donec dui duis egestas eget eleifend\nelementum elit enim erat eros est et etiam eu euismod facilisi facilisis fames\nfaucibus felis fermentum feugiat fringilla fusce gravida habitant habitasse hac\nhendrerit hymenaeos iaculis id imperdiet in inceptos integer interdum ipsum\njusto lacinia lacus laoreet lectus leo libero ligula litora lobortis lorem\nluctus maecenas magna magnis malesuada massa mattis mauris metus mi molestie\nmollis montes morbi mus nam nascetur natoque nec neque netus nibh nisi nisl non\nnonummy nostra nulla nullam nunc odio orci ornare parturient pede pellentesque\npenatibus per pharetra phasellus placerat platea porta porttitor posuere\npotenti praesent pretium primis proin pulvinar purus quam quis quisque rhoncus\nridiculus risus rutrum sagittis sapien scelerisque sed sem semper senectus sit\nsociis sociosqu sodales sollicitudin suscipit suspendisse taciti tellus tempor\ntempus tincidunt torquent tortor tristique turpis ullamcorper ultrices\nultricies urna ut varius vehicula vel velit venenatis vestibulum vitae vivamus\nviverra volutpat vulputate\"\"\"\n"
  },
  {
    "path": "src/jinja2/debug.py",
    "content": "import sys\nimport typing as t\nfrom types import CodeType\nfrom types import TracebackType\n\nfrom .exceptions import TemplateSyntaxError\nfrom .utils import internal_code\nfrom .utils import missing\n\nif t.TYPE_CHECKING:\n    from .runtime import Context\n\n\ndef rewrite_traceback_stack(source: str | None = None) -> BaseException:\n    \"\"\"Rewrite the current exception to replace any tracebacks from\n    within compiled template code with tracebacks that look like they\n    came from the template source.\n\n    This must be called within an ``except`` block.\n\n    :param source: For ``TemplateSyntaxError``, the original source if\n        known.\n    :return: The original exception with the rewritten traceback.\n    \"\"\"\n    _, exc_value, tb = sys.exc_info()\n    exc_value = t.cast(BaseException, exc_value)\n    tb = t.cast(TracebackType, tb)\n\n    if isinstance(exc_value, TemplateSyntaxError) and not exc_value.translated:\n        exc_value.translated = True\n        exc_value.source = source\n        # Remove the old traceback, otherwise the frames from the\n        # compiler still show up.\n        exc_value.with_traceback(None)\n        # Outside of runtime, so the frame isn't executing template\n        # code, but it still needs to point at the template.\n        tb = fake_traceback(\n            exc_value, None, exc_value.filename or \"<unknown>\", exc_value.lineno\n        )\n    else:\n        # Skip the frame for the render function.\n        tb = tb.tb_next\n\n    stack = []\n\n    # Build the stack of traceback object, replacing any in template\n    # code with the source file and line information.\n    while tb is not None:\n        # Skip frames decorated with @internalcode. These are internal\n        # calls that aren't useful in template debugging output.\n        if tb.tb_frame.f_code in internal_code:\n            tb = tb.tb_next\n            continue\n\n        template = tb.tb_frame.f_globals.get(\"__jinja_template__\")\n\n        if template is not None:\n            lineno = template.get_corresponding_lineno(tb.tb_lineno)\n            fake_tb = fake_traceback(exc_value, tb, template.filename, lineno)\n            stack.append(fake_tb)\n        else:\n            stack.append(tb)\n\n        tb = tb.tb_next\n\n    tb_next = None\n\n    # Assign tb_next in reverse to avoid circular references.\n    for tb in reversed(stack):\n        tb.tb_next = tb_next\n        tb_next = tb\n\n    return exc_value.with_traceback(tb_next)\n\n\ndef fake_traceback(  # type: ignore\n    exc_value: BaseException, tb: TracebackType | None, filename: str, lineno: int\n) -> TracebackType:\n    \"\"\"Produce a new traceback object that looks like it came from the\n    template source instead of the compiled code. The filename, line\n    number, and location name will point to the template, and the local\n    variables will be the current template context.\n\n    :param exc_value: The original exception to be re-raised to create\n        the new traceback.\n    :param tb: The original traceback to get the local variables and\n        code info from.\n    :param filename: The template filename.\n    :param lineno: The line number in the template source.\n    \"\"\"\n    if tb is not None:\n        # Replace the real locals with the context that would be\n        # available at that point in the template.\n        locals = get_template_locals(tb.tb_frame.f_locals)\n        locals.pop(\"__jinja_exception__\", None)\n    else:\n        locals = {}\n\n    globals = {\n        \"__name__\": filename,\n        \"__file__\": filename,\n        \"__jinja_exception__\": exc_value,\n    }\n    # Raise an exception at the correct line number.\n    code: CodeType = compile(\n        \"\\n\" * (lineno - 1) + \"raise __jinja_exception__\", filename, \"exec\"\n    )\n\n    # Build a new code object that points to the template file and\n    # replaces the location with a block name.\n    location = \"template\"\n\n    if tb is not None:\n        function = tb.tb_frame.f_code.co_name\n\n        if function == \"root\":\n            location = \"top-level template code\"\n        elif function.startswith(\"block_\"):\n            location = f\"block {function[6:]!r}\"\n\n    code = code.replace(co_name=location)\n\n    # Execute the new code, which is guaranteed to raise, and return\n    # the new traceback without this frame.\n    try:\n        exec(code, globals, locals)\n    except BaseException:\n        return sys.exc_info()[2].tb_next  # type: ignore\n\n\ndef get_template_locals(real_locals: t.Mapping[str, t.Any]) -> dict[str, t.Any]:\n    \"\"\"Based on the runtime locals, get the context that would be\n    available at that point in the template.\n    \"\"\"\n    # Start with the current template context.\n    ctx: Context | None = real_locals.get(\"context\")\n\n    if ctx is not None:\n        data: dict[str, t.Any] = ctx.get_all().copy()\n    else:\n        data = {}\n\n    # Might be in a derived context that only sets local variables\n    # rather than pushing a context. Local variables follow the scheme\n    # l_depth_name. Find the highest-depth local that has a value for\n    # each name.\n    local_overrides: dict[str, tuple[int, t.Any]] = {}\n\n    for name, value in real_locals.items():\n        if not name.startswith(\"l_\") or value is missing:\n            # Not a template variable, or no longer relevant.\n            continue\n\n        try:\n            _, depth_str, name = name.split(\"_\", 2)\n            depth = int(depth_str)\n        except ValueError:\n            continue\n\n        cur_depth = local_overrides.get(name, (-1,))[0]\n\n        if cur_depth < depth:\n            local_overrides[name] = (depth, value)\n\n    # Modify the context with any derived context.\n    for name, (_, value) in local_overrides.items():\n        if value is missing:\n            data.pop(name, None)\n        else:\n            data[name] = value\n\n    return data\n"
  },
  {
    "path": "src/jinja2/defaults.py",
    "content": "import typing as t\n\nfrom .filters import FILTERS as DEFAULT_FILTERS  # noqa: F401\nfrom .tests import TESTS as DEFAULT_TESTS  # noqa: F401\nfrom .utils import Cycler\nfrom .utils import generate_lorem_ipsum\nfrom .utils import Joiner\nfrom .utils import Namespace\n\nif t.TYPE_CHECKING:\n    import typing_extensions as te\n\n# defaults for the parser / lexer\nBLOCK_START_STRING = \"{%\"\nBLOCK_END_STRING = \"%}\"\nVARIABLE_START_STRING = \"{{\"\nVARIABLE_END_STRING = \"}}\"\nCOMMENT_START_STRING = \"{#\"\nCOMMENT_END_STRING = \"#}\"\nLINE_STATEMENT_PREFIX: str | None = None\nLINE_COMMENT_PREFIX: str | None = None\nTRIM_BLOCKS = False\nLSTRIP_BLOCKS = False\nNEWLINE_SEQUENCE: \"te.Literal['\\\\n', '\\\\r\\\\n', '\\\\r']\" = \"\\n\"\nKEEP_TRAILING_NEWLINE = False\n\n# default filters, tests and namespace\n\nDEFAULT_NAMESPACE = {\n    \"range\": range,\n    \"dict\": dict,\n    \"lipsum\": generate_lorem_ipsum,\n    \"cycler\": Cycler,\n    \"joiner\": Joiner,\n    \"namespace\": Namespace,\n}\n\n# default policies\nDEFAULT_POLICIES: dict[str, t.Any] = {\n    \"compiler.ascii_str\": True,\n    \"urlize.rel\": \"noopener\",\n    \"urlize.target\": None,\n    \"urlize.extra_schemes\": None,\n    \"truncate.leeway\": 5,\n    \"json.dumps_function\": None,\n    \"json.dumps_kwargs\": {\"sort_keys\": True},\n    \"ext.i18n.trimmed\": False,\n}\n"
  },
  {
    "path": "src/jinja2/environment.py",
    "content": "\"\"\"Classes for managing templates and their runtime and compile time\noptions.\n\"\"\"\n\nimport os\nimport typing\nimport typing as t\nimport weakref\nfrom collections import ChainMap\nfrom contextlib import aclosing\nfrom functools import lru_cache\nfrom functools import partial\nfrom functools import reduce\nfrom types import CodeType\n\nfrom markupsafe import Markup\n\nfrom . import nodes\nfrom .compiler import CodeGenerator\nfrom .compiler import generate\nfrom .defaults import BLOCK_END_STRING\nfrom .defaults import BLOCK_START_STRING\nfrom .defaults import COMMENT_END_STRING\nfrom .defaults import COMMENT_START_STRING\nfrom .defaults import DEFAULT_FILTERS  # type: ignore[attr-defined]\nfrom .defaults import DEFAULT_NAMESPACE\nfrom .defaults import DEFAULT_POLICIES\nfrom .defaults import DEFAULT_TESTS  # type: ignore[attr-defined]\nfrom .defaults import KEEP_TRAILING_NEWLINE\nfrom .defaults import LINE_COMMENT_PREFIX\nfrom .defaults import LINE_STATEMENT_PREFIX\nfrom .defaults import LSTRIP_BLOCKS\nfrom .defaults import NEWLINE_SEQUENCE\nfrom .defaults import TRIM_BLOCKS\nfrom .defaults import VARIABLE_END_STRING\nfrom .defaults import VARIABLE_START_STRING\nfrom .exceptions import TemplateNotFound\nfrom .exceptions import TemplateRuntimeError\nfrom .exceptions import TemplatesNotFound\nfrom .exceptions import TemplateSyntaxError\nfrom .exceptions import UndefinedError\nfrom .lexer import get_lexer\nfrom .lexer import Lexer\nfrom .lexer import TokenStream\nfrom .nodes import EvalContext\nfrom .parser import Parser\nfrom .runtime import Context\nfrom .runtime import new_context\nfrom .runtime import Undefined\nfrom .utils import _PassArg\nfrom .utils import concat\nfrom .utils import consume\nfrom .utils import import_string\nfrom .utils import internalcode\nfrom .utils import LRUCache\nfrom .utils import missing\n\nif t.TYPE_CHECKING:\n    import typing_extensions as te\n\n    from .bccache import BytecodeCache\n    from .ext import Extension\n    from .loaders import BaseLoader\n\n_env_bound = t.TypeVar(\"_env_bound\", bound=\"Environment\")\n\n\n# for direct template usage we have up to ten living environments\n@lru_cache(maxsize=10)\ndef get_spontaneous_environment(cls: type[_env_bound], *args: t.Any) -> _env_bound:\n    \"\"\"Return a new spontaneous environment. A spontaneous environment\n    is used for templates created directly rather than through an\n    existing environment.\n\n    :param cls: Environment class to create.\n    :param args: Positional arguments passed to environment.\n    \"\"\"\n    env = cls(*args)\n    env.shared = True\n    return env\n\n\ndef create_cache(\n    size: int,\n) -> t.MutableMapping[tuple[\"weakref.ref[BaseLoader]\", str], \"Template\"] | None:\n    \"\"\"Return the cache class for the given size.\"\"\"\n    if size == 0:\n        return None\n\n    if size < 0:\n        return {}\n\n    return LRUCache(size)  # type: ignore\n\n\ndef copy_cache(\n    cache: t.MutableMapping[tuple[\"weakref.ref[BaseLoader]\", str], \"Template\"] | None,\n) -> t.MutableMapping[tuple[\"weakref.ref[BaseLoader]\", str], \"Template\"] | None:\n    \"\"\"Create an empty copy of the given cache.\"\"\"\n    if cache is None:\n        return None\n\n    if type(cache) is dict:  # noqa E721\n        return {}\n\n    return LRUCache(cache.capacity)  # type: ignore\n\n\ndef load_extensions(\n    environment: \"Environment\",\n    extensions: t.Sequence[str | type[\"Extension\"]],\n) -> dict[str, \"Extension\"]:\n    \"\"\"Load the extensions from the list and bind it to the environment.\n    Returns a dict of instantiated extensions.\n    \"\"\"\n    result = {}\n\n    for extension in extensions:\n        if isinstance(extension, str):\n            extension = t.cast(type[\"Extension\"], import_string(extension))\n\n        result[extension.identifier] = extension(environment)\n\n    return result\n\n\ndef _environment_config_check(environment: _env_bound) -> _env_bound:\n    \"\"\"Perform a sanity check on the environment.\"\"\"\n    assert issubclass(environment.undefined, Undefined), (\n        \"'undefined' must be a subclass of 'jinja2.Undefined'.\"\n    )\n    assert (\n        environment.block_start_string\n        != environment.variable_start_string\n        != environment.comment_start_string\n    ), \"block, variable and comment start strings must be different.\"\n    assert environment.newline_sequence in {\n        \"\\r\",\n        \"\\r\\n\",\n        \"\\n\",\n    }, \"'newline_sequence' must be one of '\\\\n', '\\\\r\\\\n', or '\\\\r'.\"\n    return environment\n\n\nclass Environment:\n    r\"\"\"The core component of Jinja is the `Environment`.  It contains\n    important shared variables like configuration, filters, tests,\n    globals and others.  Instances of this class may be modified if\n    they are not shared and if no template was loaded so far.\n    Modifications on environments after the first template was loaded\n    will lead to surprising effects and undefined behavior.\n\n    Here are the possible initialization parameters:\n\n        `block_start_string`\n            The string marking the beginning of a block.  Defaults to ``'{%'``.\n\n        `block_end_string`\n            The string marking the end of a block.  Defaults to ``'%}'``.\n\n        `variable_start_string`\n            The string marking the beginning of a print statement.\n            Defaults to ``'{{'``.\n\n        `variable_end_string`\n            The string marking the end of a print statement.  Defaults to\n            ``'}}'``.\n\n        `comment_start_string`\n            The string marking the beginning of a comment.  Defaults to ``'{#'``.\n\n        `comment_end_string`\n            The string marking the end of a comment.  Defaults to ``'#}'``.\n\n        `line_statement_prefix`\n            If given and a string, this will be used as prefix for line based\n            statements.  See also :ref:`line-statements`.\n\n        `line_comment_prefix`\n            If given and a string, this will be used as prefix for line based\n            comments.  See also :ref:`line-statements`.\n\n            .. versionadded:: 2.2\n\n        `trim_blocks`\n            If this is set to ``True`` the first newline after a block is\n            removed (block, not variable tag!).  Defaults to `False`.\n\n        `lstrip_blocks`\n            If this is set to ``True`` leading spaces and tabs are stripped\n            from the start of a line to a block.  Defaults to `False`.\n\n        `newline_sequence`\n            The sequence that starts a newline.  Must be one of ``'\\r'``,\n            ``'\\n'`` or ``'\\r\\n'``.  The default is ``'\\n'`` which is a\n            useful default for Linux and OS X systems as well as web\n            applications.\n\n        `keep_trailing_newline`\n            Preserve the trailing newline when rendering templates.\n            The default is ``False``, which causes a single newline,\n            if present, to be stripped from the end of the template.\n\n            .. versionadded:: 2.7\n\n        `extensions`\n            List of Jinja extensions to use.  This can either be import paths\n            as strings or extension classes.  For more information have a\n            look at :ref:`the extensions documentation <jinja-extensions>`.\n\n        `optimized`\n            should the optimizer be enabled?  Default is ``True``.\n\n        `undefined`\n            :class:`Undefined` or a subclass of it that is used to represent\n            undefined values in the template.\n\n        `finalize`\n            A callable that can be used to process the result of a variable\n            expression before it is output.  For example one can convert\n            ``None`` implicitly into an empty string here.\n\n        `autoescape`\n            If set to ``True`` the XML/HTML autoescaping feature is enabled by\n            default.  For more details about autoescaping see\n            :class:`~markupsafe.Markup`.  As of Jinja 2.4 this can also\n            be a callable that is passed the template name and has to\n            return ``True`` or ``False`` depending on autoescape should be\n            enabled by default.\n\n            .. versionchanged:: 2.4\n               `autoescape` can now be a function\n\n        `loader`\n            The template loader for this environment.\n\n        `cache_size`\n            The size of the cache.  Per default this is ``400`` which means\n            that if more than 400 templates are loaded the loader will clean\n            out the least recently used template.  If the cache size is set to\n            ``0`` templates are recompiled all the time, if the cache size is\n            ``-1`` the cache will not be cleaned.\n\n            .. versionchanged:: 2.8\n               The cache size was increased to 400 from a low 50.\n\n        `auto_reload`\n            Some loaders load templates from locations where the template\n            sources may change (ie: file system or database).  If\n            ``auto_reload`` is set to ``True`` (default) every time a template is\n            requested the loader checks if the source changed and if yes, it\n            will reload the template.  For higher performance it's possible to\n            disable that.\n\n        `bytecode_cache`\n            If set to a bytecode cache object, this object will provide a\n            cache for the internal Jinja bytecode so that templates don't\n            have to be parsed if they were not changed.\n\n            See :ref:`bytecode-cache` for more information.\n\n        `enable_async`\n            If set to true this enables async template execution which\n            allows using async functions and generators.\n    \"\"\"\n\n    #: if this environment is sandboxed.  Modifying this variable won't make\n    #: the environment sandboxed though.  For a real sandboxed environment\n    #: have a look at jinja2.sandbox.  This flag alone controls the code\n    #: generation by the compiler.\n    sandboxed = False\n\n    #: True if the environment is just an overlay\n    overlayed = False\n\n    #: the environment this environment is linked to if it is an overlay\n    linked_to: t.Optional[\"Environment\"] = None\n\n    #: shared environments have this set to `True`.  A shared environment\n    #: must not be modified\n    shared = False\n\n    #: the class that is used for code generation.  See\n    #: :class:`~jinja2.compiler.CodeGenerator` for more information.\n    code_generator_class: type[\"CodeGenerator\"] = CodeGenerator\n\n    concat = \"\".join\n\n    #: the context class that is used for templates.  See\n    #: :class:`~jinja2.runtime.Context` for more information.\n    context_class: type[Context] = Context\n\n    template_class: type[\"Template\"]\n\n    def __init__(\n        self,\n        block_start_string: str = BLOCK_START_STRING,\n        block_end_string: str = BLOCK_END_STRING,\n        variable_start_string: str = VARIABLE_START_STRING,\n        variable_end_string: str = VARIABLE_END_STRING,\n        comment_start_string: str = COMMENT_START_STRING,\n        comment_end_string: str = COMMENT_END_STRING,\n        line_statement_prefix: str | None = LINE_STATEMENT_PREFIX,\n        line_comment_prefix: str | None = LINE_COMMENT_PREFIX,\n        trim_blocks: bool = TRIM_BLOCKS,\n        lstrip_blocks: bool = LSTRIP_BLOCKS,\n        newline_sequence: \"te.Literal['\\\\n', '\\\\r\\\\n', '\\\\r']\" = NEWLINE_SEQUENCE,\n        keep_trailing_newline: bool = KEEP_TRAILING_NEWLINE,\n        extensions: t.Sequence[str | type[\"Extension\"]] = (),\n        optimized: bool = True,\n        undefined: type[Undefined] = Undefined,\n        finalize: t.Callable[..., t.Any] | None = None,\n        autoescape: bool | t.Callable[[str | None], bool] = False,\n        loader: t.Optional[\"BaseLoader\"] = None,\n        cache_size: int = 400,\n        auto_reload: bool = True,\n        bytecode_cache: t.Optional[\"BytecodeCache\"] = None,\n        enable_async: bool = False,\n    ):\n        # !!Important notice!!\n        #   The constructor accepts quite a few arguments that should be\n        #   passed by keyword rather than position.  However it's important to\n        #   not change the order of arguments because it's used at least\n        #   internally in those cases:\n        #       -   spontaneous environments (i18n extension and Template)\n        #       -   unittests\n        #   If parameter changes are required only add parameters at the end\n        #   and don't change the arguments (or the defaults!) of the arguments\n        #   existing already.\n\n        # lexer / parser information\n        self.block_start_string = block_start_string\n        self.block_end_string = block_end_string\n        self.variable_start_string = variable_start_string\n        self.variable_end_string = variable_end_string\n        self.comment_start_string = comment_start_string\n        self.comment_end_string = comment_end_string\n        self.line_statement_prefix = line_statement_prefix\n        self.line_comment_prefix = line_comment_prefix\n        self.trim_blocks = trim_blocks\n        self.lstrip_blocks = lstrip_blocks\n        self.newline_sequence = newline_sequence\n        self.keep_trailing_newline = keep_trailing_newline\n\n        # runtime information\n        self.undefined: type[Undefined] = undefined\n        self.optimized = optimized\n        self.finalize = finalize\n        self.autoescape = autoescape\n\n        # defaults\n        self.filters = DEFAULT_FILTERS.copy()\n        self.tests = DEFAULT_TESTS.copy()\n        self.globals = DEFAULT_NAMESPACE.copy()\n\n        # set the loader provided\n        self.loader = loader\n        self.cache = create_cache(cache_size)\n        self.bytecode_cache = bytecode_cache\n        self.auto_reload = auto_reload\n\n        # configurable policies\n        self.policies = DEFAULT_POLICIES.copy()\n\n        # load extensions\n        self.extensions = load_extensions(self, extensions)\n\n        self.is_async = enable_async\n        _environment_config_check(self)\n\n    def add_extension(self, extension: str | type[\"Extension\"]) -> None:\n        \"\"\"Adds an extension after the environment was created.\n\n        .. versionadded:: 2.5\n        \"\"\"\n        self.extensions.update(load_extensions(self, [extension]))\n\n    def extend(self, **attributes: t.Any) -> None:\n        \"\"\"Add the items to the instance of the environment if they do not exist\n        yet.  This is used by :ref:`extensions <writing-extensions>` to register\n        callbacks and configuration values without breaking inheritance.\n        \"\"\"\n        for key, value in attributes.items():\n            if not hasattr(self, key):\n                setattr(self, key, value)\n\n    def overlay(\n        self,\n        block_start_string: str = missing,\n        block_end_string: str = missing,\n        variable_start_string: str = missing,\n        variable_end_string: str = missing,\n        comment_start_string: str = missing,\n        comment_end_string: str = missing,\n        line_statement_prefix: str | None = missing,\n        line_comment_prefix: str | None = missing,\n        trim_blocks: bool = missing,\n        lstrip_blocks: bool = missing,\n        newline_sequence: \"te.Literal['\\\\n', '\\\\r\\\\n', '\\\\r']\" = missing,\n        keep_trailing_newline: bool = missing,\n        extensions: t.Sequence[str | type[\"Extension\"]] = missing,\n        optimized: bool = missing,\n        undefined: type[Undefined] = missing,\n        finalize: t.Callable[..., t.Any] | None = missing,\n        autoescape: bool | t.Callable[[str | None], bool] = missing,\n        loader: t.Optional[\"BaseLoader\"] = missing,\n        cache_size: int = missing,\n        auto_reload: bool = missing,\n        bytecode_cache: t.Optional[\"BytecodeCache\"] = missing,\n        enable_async: bool = missing,\n    ) -> \"te.Self\":\n        \"\"\"Create a new overlay environment that shares all the data with the\n        current environment except for cache and the overridden attributes.\n        Extensions cannot be removed for an overlayed environment.  An overlayed\n        environment automatically gets all the extensions of the environment it\n        is linked to plus optional extra extensions.\n\n        Creating overlays should happen after the initial environment was set\n        up completely.  Not all attributes are truly linked, some are just\n        copied over so modifications on the original environment may not shine\n        through.\n\n        .. versionchanged:: 3.1.5\n            ``enable_async`` is applied correctly.\n\n        .. versionchanged:: 3.1.2\n            Added the ``newline_sequence``, ``keep_trailing_newline``,\n            and ``enable_async`` parameters to match ``__init__``.\n        \"\"\"\n        args = dict(locals())\n        del args[\"self\"], args[\"cache_size\"], args[\"extensions\"], args[\"enable_async\"]\n\n        rv = object.__new__(self.__class__)\n        rv.__dict__.update(self.__dict__)\n        rv.overlayed = True\n        rv.linked_to = self\n\n        for key, value in args.items():\n            if value is not missing:\n                setattr(rv, key, value)\n\n        if cache_size is not missing:\n            rv.cache = create_cache(cache_size)\n        else:\n            rv.cache = copy_cache(self.cache)\n\n        rv.extensions = {}\n        for key, value in self.extensions.items():\n            rv.extensions[key] = value.bind(rv)\n        if extensions is not missing:\n            rv.extensions.update(load_extensions(rv, extensions))\n\n        if enable_async is not missing:\n            rv.is_async = enable_async\n\n        return _environment_config_check(rv)\n\n    @property\n    def lexer(self) -> Lexer:\n        \"\"\"The lexer for this environment.\"\"\"\n        return get_lexer(self)\n\n    def iter_extensions(self) -> t.Iterator[\"Extension\"]:\n        \"\"\"Iterates over the extensions by priority.\"\"\"\n        return iter(sorted(self.extensions.values(), key=lambda x: x.priority))\n\n    def getitem(self, obj: t.Any, argument: str | t.Any) -> t.Any | Undefined:\n        \"\"\"Get an item or attribute of an object but prefer the item.\"\"\"\n        try:\n            return obj[argument]\n        except (AttributeError, TypeError, LookupError):\n            if isinstance(argument, str):\n                try:\n                    attr = str(argument)\n                except Exception:\n                    pass\n                else:\n                    try:\n                        return getattr(obj, attr)\n                    except AttributeError:\n                        pass\n            return self.undefined(obj=obj, name=argument)\n\n    def getattr(self, obj: t.Any, attribute: str) -> t.Any:\n        \"\"\"Get an item or attribute of an object but prefer the attribute.\n        Unlike :meth:`getitem` the attribute *must* be a string.\n        \"\"\"\n        try:\n            return getattr(obj, attribute)\n        except AttributeError:\n            pass\n        try:\n            return obj[attribute]\n        except (TypeError, LookupError, AttributeError):\n            return self.undefined(obj=obj, name=attribute)\n\n    def _filter_test_common(\n        self,\n        name: str | Undefined,\n        value: t.Any,\n        args: t.Sequence[t.Any] | None,\n        kwargs: t.Mapping[str, t.Any] | None,\n        context: Context | None,\n        eval_ctx: EvalContext | None,\n        is_filter: bool,\n    ) -> t.Any:\n        if is_filter:\n            env_map = self.filters\n            type_name = \"filter\"\n        else:\n            env_map = self.tests\n            type_name = \"test\"\n\n        func = env_map.get(name)  # type: ignore\n\n        if func is None:\n            msg = f\"No {type_name} named {name!r}.\"\n\n            if isinstance(name, Undefined):\n                try:\n                    name._fail_with_undefined_error()\n                except Exception as e:\n                    msg = f\"{msg} ({e}; did you forget to quote the callable name?)\"\n\n            raise TemplateRuntimeError(msg)\n\n        args = [value, *(args if args is not None else ())]\n        kwargs = kwargs if kwargs is not None else {}\n        pass_arg = _PassArg.from_obj(func)\n\n        if pass_arg is _PassArg.context:\n            if context is None:\n                raise TemplateRuntimeError(\n                    f\"Attempted to invoke a context {type_name} without context.\"\n                )\n\n            args.insert(0, context)\n        elif pass_arg is _PassArg.eval_context:\n            if eval_ctx is None:\n                if context is not None:\n                    eval_ctx = context.eval_ctx\n                else:\n                    eval_ctx = EvalContext(self)\n\n            args.insert(0, eval_ctx)\n        elif pass_arg is _PassArg.environment:\n            args.insert(0, self)\n\n        return func(*args, **kwargs)\n\n    def call_filter(\n        self,\n        name: str,\n        value: t.Any,\n        args: t.Sequence[t.Any] | None = None,\n        kwargs: t.Mapping[str, t.Any] | None = None,\n        context: Context | None = None,\n        eval_ctx: EvalContext | None = None,\n    ) -> t.Any:\n        \"\"\"Invoke a filter on a value the same way the compiler does.\n\n        This might return a coroutine if the filter is running from an\n        environment in async mode and the filter supports async\n        execution. It's your responsibility to await this if needed.\n\n        .. versionadded:: 2.7\n        \"\"\"\n        return self._filter_test_common(\n            name, value, args, kwargs, context, eval_ctx, True\n        )\n\n    def call_test(\n        self,\n        name: str,\n        value: t.Any,\n        args: t.Sequence[t.Any] | None = None,\n        kwargs: t.Mapping[str, t.Any] | None = None,\n        context: Context | None = None,\n        eval_ctx: EvalContext | None = None,\n    ) -> t.Any:\n        \"\"\"Invoke a test on a value the same way the compiler does.\n\n        This might return a coroutine if the test is running from an\n        environment in async mode and the test supports async execution.\n        It's your responsibility to await this if needed.\n\n        .. versionchanged:: 3.0\n            Tests support ``@pass_context``, etc. decorators. Added\n            the ``context`` and ``eval_ctx`` parameters.\n\n        .. versionadded:: 2.7\n        \"\"\"\n        return self._filter_test_common(\n            name, value, args, kwargs, context, eval_ctx, False\n        )\n\n    @internalcode\n    def parse(\n        self,\n        source: str,\n        name: str | None = None,\n        filename: str | None = None,\n    ) -> nodes.Template:\n        \"\"\"Parse the sourcecode and return the abstract syntax tree.  This\n        tree of nodes is used by the compiler to convert the template into\n        executable source- or bytecode.  This is useful for debugging or to\n        extract information from templates.\n\n        If you are :ref:`developing Jinja extensions <writing-extensions>`\n        this gives you a good overview of the node tree generated.\n        \"\"\"\n        try:\n            return self._parse(source, name, filename)\n        except TemplateSyntaxError:\n            self.handle_exception(source=source)\n\n    def _parse(\n        self, source: str, name: str | None, filename: str | None\n    ) -> nodes.Template:\n        \"\"\"Internal parsing function used by `parse` and `compile`.\"\"\"\n        return Parser(self, source, name, filename).parse()\n\n    def lex(\n        self,\n        source: str,\n        name: str | None = None,\n        filename: str | None = None,\n    ) -> t.Iterator[tuple[int, str, str]]:\n        \"\"\"Lex the given sourcecode and return a generator that yields\n        tokens as tuples in the form ``(lineno, token_type, value)``.\n        This can be useful for :ref:`extension development <writing-extensions>`\n        and debugging templates.\n\n        This does not perform preprocessing.  If you want the preprocessing\n        of the extensions to be applied you have to filter source through\n        the :meth:`preprocess` method.\n        \"\"\"\n        source = str(source)\n        try:\n            return self.lexer.tokeniter(source, name, filename)\n        except TemplateSyntaxError:\n            self.handle_exception(source=source)\n\n    def preprocess(\n        self,\n        source: str,\n        name: str | None = None,\n        filename: str | None = None,\n    ) -> str:\n        \"\"\"Preprocesses the source with all extensions.  This is automatically\n        called for all parsing and compiling methods but *not* for :meth:`lex`\n        because there you usually only want the actual source tokenized.\n        \"\"\"\n        return reduce(\n            lambda s, e: e.preprocess(s, name, filename),\n            self.iter_extensions(),\n            str(source),\n        )\n\n    def _tokenize(\n        self,\n        source: str,\n        name: str | None,\n        filename: str | None = None,\n        state: str | None = None,\n    ) -> TokenStream:\n        \"\"\"Called by the parser to do the preprocessing and filtering\n        for all the extensions.  Returns a :class:`~jinja2.lexer.TokenStream`.\n        \"\"\"\n        source = self.preprocess(source, name, filename)\n        stream = self.lexer.tokenize(source, name, filename, state)\n\n        for ext in self.iter_extensions():\n            stream = ext.filter_stream(stream)  # type: ignore\n\n            if not isinstance(stream, TokenStream):\n                stream = TokenStream(stream, name, filename)\n\n        return stream\n\n    def _generate(\n        self,\n        source: nodes.Template,\n        name: str | None,\n        filename: str | None,\n        defer_init: bool = False,\n    ) -> str:\n        \"\"\"Internal hook that can be overridden to hook a different generate\n        method in.\n\n        .. versionadded:: 2.5\n        \"\"\"\n        return generate(  # type: ignore\n            source,\n            self,\n            name,\n            filename,\n            defer_init=defer_init,\n            optimized=self.optimized,\n        )\n\n    def _compile(self, source: str, filename: str) -> CodeType:\n        \"\"\"Internal hook that can be overridden to hook a different compile\n        method in.\n\n        .. versionadded:: 2.5\n        \"\"\"\n        return compile(source, filename, \"exec\")\n\n    @typing.overload\n    def compile(\n        self,\n        source: str | nodes.Template,\n        name: str | None = None,\n        filename: str | None = None,\n        raw: \"te.Literal[False]\" = False,\n        defer_init: bool = False,\n    ) -> CodeType: ...\n\n    @typing.overload\n    def compile(\n        self,\n        source: str | nodes.Template,\n        name: str | None = None,\n        filename: str | None = None,\n        raw: \"te.Literal[True]\" = ...,\n        defer_init: bool = False,\n    ) -> str: ...\n\n    @internalcode\n    def compile(\n        self,\n        source: str | nodes.Template,\n        name: str | None = None,\n        filename: str | None = None,\n        raw: bool = False,\n        defer_init: bool = False,\n    ) -> str | CodeType:\n        \"\"\"Compile a node or template source code.  The `name` parameter is\n        the load name of the template after it was joined using\n        :meth:`join_path` if necessary, not the filename on the file system.\n        the `filename` parameter is the estimated filename of the template on\n        the file system.  If the template came from a database or memory this\n        can be omitted.\n\n        The return value of this method is a python code object.  If the `raw`\n        parameter is `True` the return value will be a string with python\n        code equivalent to the bytecode returned otherwise.  This method is\n        mainly used internally.\n\n        `defer_init` is use internally to aid the module code generator.  This\n        causes the generated code to be able to import without the global\n        environment variable to be set.\n\n        .. versionadded:: 2.4\n           `defer_init` parameter added.\n        \"\"\"\n        source_hint = None\n        try:\n            if isinstance(source, str):\n                source_hint = source\n                source = self._parse(source, name, filename)\n            source = self._generate(source, name, filename, defer_init=defer_init)\n            if raw:\n                return source\n            if filename is None:\n                filename = \"<template>\"\n            return self._compile(source, filename)\n        except TemplateSyntaxError:\n            self.handle_exception(source=source_hint)\n\n    def compile_expression(\n        self, source: str, undefined_to_none: bool = True\n    ) -> \"TemplateExpression\":\n        \"\"\"A handy helper method that returns a callable that accepts keyword\n        arguments that appear as variables in the expression.  If called it\n        returns the result of the expression.\n\n        This is useful if applications want to use the same rules as Jinja\n        in template \"configuration files\" or similar situations.\n\n        Example usage:\n\n        >>> env = Environment()\n        >>> expr = env.compile_expression('foo == 42')\n        >>> expr(foo=23)\n        False\n        >>> expr(foo=42)\n        True\n\n        Per default the return value is converted to `None` if the\n        expression returns an undefined value.  This can be changed\n        by setting `undefined_to_none` to `False`.\n\n        >>> env.compile_expression('var')() is None\n        True\n        >>> env.compile_expression('var', undefined_to_none=False)()\n        Undefined\n\n        .. versionadded:: 2.1\n        \"\"\"\n        parser = Parser(self, source, state=\"variable\")\n        try:\n            expr = parser.parse_expression()\n            if not parser.stream.eos:\n                raise TemplateSyntaxError(\n                    \"chunk after expression\", parser.stream.current.lineno, None, None\n                )\n            expr.set_environment(self)\n        except TemplateSyntaxError:\n            self.handle_exception(source=source)\n\n        body = [nodes.Assign(nodes.Name(\"result\", \"store\"), expr, lineno=1)]\n        template = self.from_string(nodes.Template(body, lineno=1))\n        return TemplateExpression(template, undefined_to_none)\n\n    def compile_templates(\n        self,\n        target: t.Union[str, \"os.PathLike[str]\"],\n        extensions: t.Collection[str] | None = None,\n        filter_func: t.Callable[[str], bool] | None = None,\n        zip: str | None = \"deflated\",\n        log_function: t.Callable[[str], None] | None = None,\n        ignore_errors: bool = True,\n    ) -> None:\n        \"\"\"Finds all the templates the loader can find, compiles them\n        and stores them in `target`.  If `zip` is `None`, instead of in a\n        zipfile, the templates will be stored in a directory.\n        By default a deflate zip algorithm is used. To switch to\n        the stored algorithm, `zip` can be set to ``'stored'``.\n\n        `extensions` and `filter_func` are passed to :meth:`list_templates`.\n        Each template returned will be compiled to the target folder or\n        zipfile.\n\n        By default template compilation errors are ignored.  In case a\n        log function is provided, errors are logged.  If you want template\n        syntax errors to abort the compilation you can set `ignore_errors`\n        to `False` and you will get an exception on syntax errors.\n\n        .. versionadded:: 2.4\n        \"\"\"\n        from .loaders import ModuleLoader\n\n        if log_function is None:\n\n            def log_function(x: str) -> None:\n                pass\n\n        assert log_function is not None\n        assert self.loader is not None, \"No loader configured.\"\n\n        def write_file(filename: str, data: str) -> None:\n            if zip:\n                info = ZipInfo(filename)\n                info.external_attr = 0o755 << 16\n                zip_file.writestr(info, data)\n            else:\n                with open(os.path.join(target, filename), \"wb\") as f:\n                    f.write(data.encode(\"utf8\"))\n\n        if zip is not None:\n            from zipfile import ZIP_DEFLATED\n            from zipfile import ZIP_STORED\n            from zipfile import ZipFile\n            from zipfile import ZipInfo\n\n            zip_file = ZipFile(\n                target, \"w\", dict(deflated=ZIP_DEFLATED, stored=ZIP_STORED)[zip]\n            )\n            log_function(f\"Compiling into Zip archive {target!r}\")\n        else:\n            if not os.path.isdir(target):\n                os.makedirs(target)\n            log_function(f\"Compiling into folder {target!r}\")\n\n        try:\n            for name in self.list_templates(extensions, filter_func):\n                source, filename, _ = self.loader.get_source(self, name)\n                try:\n                    code = self.compile(source, name, filename, True, True)\n                except TemplateSyntaxError as e:\n                    if not ignore_errors:\n                        raise\n                    log_function(f'Could not compile \"{name}\": {e}')\n                    continue\n\n                filename = ModuleLoader.get_module_filename(name)\n\n                write_file(filename, code)\n                log_function(f'Compiled \"{name}\" as {filename}')\n        finally:\n            if zip:\n                zip_file.close()\n\n        log_function(\"Finished compiling templates\")\n\n    def list_templates(\n        self,\n        extensions: t.Collection[str] | None = None,\n        filter_func: t.Callable[[str], bool] | None = None,\n    ) -> list[str]:\n        \"\"\"Returns a list of templates for this environment.  This requires\n        that the loader supports the loader's\n        :meth:`~BaseLoader.list_templates` method.\n\n        If there are other files in the template folder besides the\n        actual templates, the returned list can be filtered.  There are two\n        ways: either `extensions` is set to a list of file extensions for\n        templates, or a `filter_func` can be provided which is a callable that\n        is passed a template name and should return `True` if it should end up\n        in the result list.\n\n        If the loader does not support that, a :exc:`TypeError` is raised.\n\n        .. versionadded:: 2.4\n        \"\"\"\n        assert self.loader is not None, \"No loader configured.\"\n        names = self.loader.list_templates()\n\n        if extensions is not None:\n            if filter_func is not None:\n                raise TypeError(\n                    \"either extensions or filter_func can be passed, but not both\"\n                )\n\n            def filter_func(x: str) -> bool:\n                return \".\" in x and x.rsplit(\".\", 1)[1] in extensions\n\n        if filter_func is not None:\n            names = [name for name in names if filter_func(name)]\n\n        return names\n\n    def handle_exception(self, source: str | None = None) -> \"te.NoReturn\":\n        \"\"\"Exception handling helper.  This is used internally to either raise\n        rewritten exceptions or return a rendered traceback for the template.\n        \"\"\"\n        from .debug import rewrite_traceback_stack\n\n        raise rewrite_traceback_stack(source=source)\n\n    def join_path(self, template: str, parent: str) -> str:\n        \"\"\"Join a template with the parent.  By default all the lookups are\n        relative to the loader root so this method returns the `template`\n        parameter unchanged, but if the paths should be relative to the\n        parent template, this function can be used to calculate the real\n        template name.\n\n        Subclasses may override this method and implement template path\n        joining here.\n        \"\"\"\n        return template\n\n    @internalcode\n    def _load_template(\n        self, name: str, globals: t.MutableMapping[str, t.Any] | None\n    ) -> \"Template\":\n        if self.loader is None:\n            raise TypeError(\"no loader for this environment specified\")\n        cache_key = (weakref.ref(self.loader), name)\n        if self.cache is not None:\n            template = self.cache.get(cache_key)\n            if template is not None and (\n                not self.auto_reload or template.is_up_to_date\n            ):\n                # template.globals is a ChainMap, modifying it will only\n                # affect the template, not the environment globals.\n                if globals:\n                    template.globals.update(globals)\n\n                return template\n\n        template = self.loader.load(self, name, self.make_globals(globals))\n\n        if self.cache is not None:\n            self.cache[cache_key] = template\n        return template\n\n    @internalcode\n    def get_template(\n        self,\n        name: t.Union[str, \"Template\"],\n        parent: str | None = None,\n        globals: t.MutableMapping[str, t.Any] | None = None,\n    ) -> \"Template\":\n        \"\"\"Load a template by name with :attr:`loader` and return a\n        :class:`Template`. If the template does not exist a\n        :exc:`TemplateNotFound` exception is raised.\n\n        :param name: Name of the template to load. When loading\n            templates from the filesystem, \"/\" is used as the path\n            separator, even on Windows.\n        :param parent: The name of the parent template importing this\n            template. :meth:`join_path` can be used to implement name\n            transformations with this.\n        :param globals: Extend the environment :attr:`globals` with\n            these extra variables available for all renders of this\n            template. If the template has already been loaded and\n            cached, its globals are updated with any new items.\n\n        .. versionchanged:: 3.0\n            If a template is loaded from cache, ``globals`` will update\n            the template's globals instead of ignoring the new values.\n\n        .. versionchanged:: 2.4\n            If ``name`` is a :class:`Template` object it is returned\n            unchanged.\n        \"\"\"\n        if isinstance(name, Template):\n            return name\n        if parent is not None:\n            name = self.join_path(name, parent)\n\n        return self._load_template(name, globals)\n\n    @internalcode\n    def select_template(\n        self,\n        names: t.Iterable[t.Union[str, \"Template\"]],\n        parent: str | None = None,\n        globals: t.MutableMapping[str, t.Any] | None = None,\n    ) -> \"Template\":\n        \"\"\"Like :meth:`get_template`, but tries loading multiple names.\n        If none of the names can be loaded a :exc:`TemplatesNotFound`\n        exception is raised.\n\n        :param names: List of template names to try loading in order.\n        :param parent: The name of the parent template importing this\n            template. :meth:`join_path` can be used to implement name\n            transformations with this.\n        :param globals: Extend the environment :attr:`globals` with\n            these extra variables available for all renders of this\n            template. If the template has already been loaded and\n            cached, its globals are updated with any new items.\n\n        .. versionchanged:: 3.0\n            If a template is loaded from cache, ``globals`` will update\n            the template's globals instead of ignoring the new values.\n\n        .. versionchanged:: 2.11\n            If ``names`` is :class:`Undefined`, an :exc:`UndefinedError`\n            is raised instead. If no templates were found and ``names``\n            contains :class:`Undefined`, the message is more helpful.\n\n        .. versionchanged:: 2.4\n            If ``names`` contains a :class:`Template` object it is\n            returned unchanged.\n\n        .. versionadded:: 2.3\n        \"\"\"\n        if isinstance(names, Undefined):\n            names._fail_with_undefined_error()\n\n        if not names:\n            raise TemplatesNotFound(\n                message=\"Tried to select from an empty list of templates.\"\n            )\n\n        for name in names:\n            if isinstance(name, Template):\n                return name\n            if parent is not None:\n                name = self.join_path(name, parent)\n            try:\n                return self._load_template(name, globals)\n            except (TemplateNotFound, UndefinedError):\n                pass\n        raise TemplatesNotFound(names)  # type: ignore\n\n    @internalcode\n    def get_or_select_template(\n        self,\n        template_name_or_list: t.Union[str, \"Template\", list[t.Union[str, \"Template\"]]],\n        parent: str | None = None,\n        globals: t.MutableMapping[str, t.Any] | None = None,\n    ) -> \"Template\":\n        \"\"\"Use :meth:`select_template` if an iterable of template names\n        is given, or :meth:`get_template` if one name is given.\n\n        .. versionadded:: 2.3\n        \"\"\"\n        if isinstance(template_name_or_list, (str, Undefined)):\n            return self.get_template(template_name_or_list, parent, globals)\n        elif isinstance(template_name_or_list, Template):\n            return template_name_or_list\n        return self.select_template(template_name_or_list, parent, globals)\n\n    def from_string(\n        self,\n        source: str | nodes.Template,\n        globals: t.MutableMapping[str, t.Any] | None = None,\n        template_class: type[\"Template\"] | None = None,\n    ) -> \"Template\":\n        \"\"\"Load a template from a source string without using\n        :attr:`loader`.\n\n        :param source: Jinja source to compile into a template.\n        :param globals: Extend the environment :attr:`globals` with\n            these extra variables available for all renders of this\n            template. If the template has already been loaded and\n            cached, its globals are updated with any new items.\n        :param template_class: Return an instance of this\n            :class:`Template` class.\n        \"\"\"\n        gs = self.make_globals(globals)\n        cls = template_class or self.template_class\n        return cls.from_code(self, self.compile(source), gs, None)\n\n    def make_globals(\n        self, d: t.MutableMapping[str, t.Any] | None\n    ) -> t.MutableMapping[str, t.Any]:\n        \"\"\"Make the globals map for a template. Any given template\n        globals overlay the environment :attr:`globals`.\n\n        Returns a :class:`collections.ChainMap`. This allows any changes\n        to a template's globals to only affect that template, while\n        changes to the environment's globals are still reflected.\n        However, avoid modifying any globals after a template is loaded.\n\n        :param d: Dict of template-specific globals.\n\n        .. versionchanged:: 3.0\n            Use :class:`collections.ChainMap` to always prevent mutating\n            environment globals.\n        \"\"\"\n        if d is None:\n            d = {}\n\n        return ChainMap(d, self.globals)\n\n\nclass Template:\n    \"\"\"A compiled template that can be rendered.\n\n    Use the methods on :class:`Environment` to create or load templates.\n    The environment is used to configure how templates are compiled and\n    behave.\n\n    It is also possible to create a template object directly. This is\n    not usually recommended. The constructor takes most of the same\n    arguments as :class:`Environment`. All templates created with the\n    same environment arguments share the same ephemeral ``Environment``\n    instance behind the scenes.\n\n    A template object should be considered immutable. Modifications on\n    the object are not supported.\n    \"\"\"\n\n    #: Type of environment to create when creating a template directly\n    #: rather than through an existing environment.\n    environment_class: type[Environment] = Environment\n\n    environment: Environment\n    globals: t.MutableMapping[str, t.Any]\n    name: str | None\n    filename: str | None\n    blocks: dict[str, t.Callable[[Context], t.Iterator[str]]]\n    root_render_func: t.Callable[[Context], t.Iterator[str]]\n    _module: t.Optional[\"TemplateModule\"]\n    _debug_info: str\n    _uptodate: t.Callable[[], bool] | None\n\n    def __new__(\n        cls,\n        source: str | nodes.Template,\n        block_start_string: str = BLOCK_START_STRING,\n        block_end_string: str = BLOCK_END_STRING,\n        variable_start_string: str = VARIABLE_START_STRING,\n        variable_end_string: str = VARIABLE_END_STRING,\n        comment_start_string: str = COMMENT_START_STRING,\n        comment_end_string: str = COMMENT_END_STRING,\n        line_statement_prefix: str | None = LINE_STATEMENT_PREFIX,\n        line_comment_prefix: str | None = LINE_COMMENT_PREFIX,\n        trim_blocks: bool = TRIM_BLOCKS,\n        lstrip_blocks: bool = LSTRIP_BLOCKS,\n        newline_sequence: \"te.Literal['\\\\n', '\\\\r\\\\n', '\\\\r']\" = NEWLINE_SEQUENCE,\n        keep_trailing_newline: bool = KEEP_TRAILING_NEWLINE,\n        extensions: t.Sequence[str | type[\"Extension\"]] = (),\n        optimized: bool = True,\n        undefined: type[Undefined] = Undefined,\n        finalize: t.Callable[..., t.Any] | None = None,\n        autoescape: bool | t.Callable[[str | None], bool] = False,\n        enable_async: bool = False,\n    ) -> t.Any:  # it returns a `Template`, but this breaks the sphinx build...\n        env = get_spontaneous_environment(\n            cls.environment_class,  # type: ignore\n            block_start_string,\n            block_end_string,\n            variable_start_string,\n            variable_end_string,\n            comment_start_string,\n            comment_end_string,\n            line_statement_prefix,\n            line_comment_prefix,\n            trim_blocks,\n            lstrip_blocks,\n            newline_sequence,\n            keep_trailing_newline,\n            frozenset(extensions),\n            optimized,\n            undefined,  # type: ignore\n            finalize,\n            autoescape,\n            None,\n            0,\n            False,\n            None,\n            enable_async,\n        )\n        return env.from_string(source, template_class=cls)\n\n    @classmethod\n    def from_code(\n        cls,\n        environment: Environment,\n        code: CodeType,\n        globals: t.MutableMapping[str, t.Any],\n        uptodate: t.Callable[[], bool] | None = None,\n    ) -> \"Template\":\n        \"\"\"Creates a template object from compiled code and the globals.  This\n        is used by the loaders and environment to create a template object.\n        \"\"\"\n        namespace = {\"environment\": environment, \"__file__\": code.co_filename}\n        exec(code, namespace)\n        rv = cls._from_namespace(environment, namespace, globals)\n        rv._uptodate = uptodate\n        return rv\n\n    @classmethod\n    def from_module_dict(\n        cls,\n        environment: Environment,\n        module_dict: t.MutableMapping[str, t.Any],\n        globals: t.MutableMapping[str, t.Any],\n    ) -> \"Template\":\n        \"\"\"Creates a template object from a module.  This is used by the\n        module loader to create a template object.\n\n        .. versionadded:: 2.4\n        \"\"\"\n        return cls._from_namespace(environment, module_dict, globals)\n\n    @classmethod\n    def _from_namespace(\n        cls,\n        environment: Environment,\n        namespace: t.MutableMapping[str, t.Any],\n        globals: t.MutableMapping[str, t.Any],\n    ) -> \"Template\":\n        t: Template = object.__new__(cls)\n        t.environment = environment\n        t.globals = globals\n        t.name = namespace[\"name\"]\n        t.filename = namespace[\"__file__\"]\n        t.blocks = namespace[\"blocks\"]\n\n        # render function and module\n        t.root_render_func = namespace[\"root\"]\n        t._module = None\n\n        # debug and loader helpers\n        t._debug_info = namespace[\"debug_info\"]\n        t._uptodate = None\n\n        # store the reference\n        namespace[\"environment\"] = environment\n        namespace[\"__jinja_template__\"] = t\n\n        return t\n\n    def render(self, *args: t.Any, **kwargs: t.Any) -> str:\n        \"\"\"This method accepts the same arguments as the `dict` constructor:\n        A dict, a dict subclass or some keyword arguments.  If no arguments\n        are given the context will be empty.  These two calls do the same::\n\n            template.render(knights='that say nih')\n            template.render({'knights': 'that say nih'})\n\n        This will return the rendered template as a string.\n        \"\"\"\n        if self.environment.is_async:\n            import asyncio\n\n            return asyncio.run(self.render_async(*args, **kwargs))\n\n        ctx = self.new_context(dict(*args, **kwargs))\n\n        try:\n            return self.environment.concat(self.root_render_func(ctx))  # type: ignore\n        except Exception:\n            self.environment.handle_exception()\n\n    async def render_async(self, *args: t.Any, **kwargs: t.Any) -> str:\n        \"\"\"This works similar to :meth:`render` but returns a coroutine\n        that when awaited returns the entire rendered template string.  This\n        requires the async feature to be enabled.\n\n        Example usage::\n\n            await template.render_async(knights='that say nih; asynchronously')\n        \"\"\"\n        if not self.environment.is_async:\n            raise RuntimeError(\n                \"The environment was not created with async mode enabled.\"\n            )\n\n        ctx = self.new_context(dict(*args, **kwargs))\n\n        try:\n            return self.environment.concat(  # type: ignore\n                [n async for n in self.root_render_func(ctx)]  # type: ignore\n            )\n        except Exception:\n            return self.environment.handle_exception()\n\n    def stream(self, *args: t.Any, **kwargs: t.Any) -> \"TemplateStream\":\n        \"\"\"Works exactly like :meth:`generate` but returns a\n        :class:`TemplateStream`.\n        \"\"\"\n        return TemplateStream(self.generate(*args, **kwargs))\n\n    def generate(self, *args: t.Any, **kwargs: t.Any) -> t.Iterator[str]:\n        \"\"\"For very large templates it can be useful to not render the whole\n        template at once but evaluate each statement after another and yield\n        piece for piece.  This method basically does exactly that and returns\n        a generator that yields one item after another as strings.\n\n        It accepts the same arguments as :meth:`render`.\n        \"\"\"\n        if self.environment.is_async:\n            import asyncio\n\n            async def to_list() -> list[str]:\n                return [x async for x in self.generate_async(*args, **kwargs)]\n\n            yield from asyncio.run(to_list())\n            return\n\n        ctx = self.new_context(dict(*args, **kwargs))\n\n        try:\n            yield from self.root_render_func(ctx)\n        except Exception:\n            yield self.environment.handle_exception()\n\n    async def generate_async(\n        self, *args: t.Any, **kwargs: t.Any\n    ) -> t.AsyncGenerator[str, object]:\n        \"\"\"An async version of :meth:`generate`.  Works very similarly but\n        returns an async iterator instead.\n        \"\"\"\n        if not self.environment.is_async:\n            raise RuntimeError(\n                \"The environment was not created with async mode enabled.\"\n            )\n\n        ctx = self.new_context(dict(*args, **kwargs))\n\n        try:\n            agen: t.AsyncGenerator[str, None] = self.root_render_func(ctx)  # type: ignore[assignment]\n\n            async with aclosing(agen):\n                async for event in agen:\n                    yield event\n        except Exception:\n            yield self.environment.handle_exception()\n\n    def new_context(\n        self,\n        vars: dict[str, t.Any] | None = None,\n        shared: bool = False,\n        locals: t.Mapping[str, t.Any] | None = None,\n    ) -> Context:\n        \"\"\"Create a new :class:`Context` for this template.  The vars\n        provided will be passed to the template.  Per default the globals\n        are added to the context.  If shared is set to `True` the data\n        is passed as is to the context without adding the globals.\n\n        `locals` can be a dict of local variables for internal usage.\n        \"\"\"\n        return new_context(\n            self.environment, self.name, self.blocks, vars, shared, self.globals, locals\n        )\n\n    def make_module(\n        self,\n        vars: dict[str, t.Any] | None = None,\n        shared: bool = False,\n        locals: t.Mapping[str, t.Any] | None = None,\n    ) -> \"TemplateModule\":\n        \"\"\"This method works like the :attr:`module` attribute when called\n        without arguments but it will evaluate the template on every call\n        rather than caching it.  It's also possible to provide\n        a dict which is then used as context.  The arguments are the same\n        as for the :meth:`new_context` method.\n        \"\"\"\n        ctx = self.new_context(vars, shared, locals)\n        return TemplateModule(self, ctx)\n\n    async def make_module_async(\n        self,\n        vars: dict[str, t.Any] | None = None,\n        shared: bool = False,\n        locals: t.Mapping[str, t.Any] | None = None,\n    ) -> \"TemplateModule\":\n        \"\"\"As template module creation can invoke template code for\n        asynchronous executions this method must be used instead of the\n        normal :meth:`make_module` one.  Likewise the module attribute\n        becomes unavailable in async mode.\n        \"\"\"\n        ctx = self.new_context(vars, shared, locals)\n        return TemplateModule(\n            self,\n            ctx,\n            [x async for x in self.root_render_func(ctx)],  # type: ignore\n        )\n\n    @internalcode\n    def _get_default_module(self, ctx: Context | None = None) -> \"TemplateModule\":\n        \"\"\"If a context is passed in, this means that the template was\n        imported. Imported templates have access to the current\n        template's globals by default, but they can only be accessed via\n        the context during runtime.\n\n        If there are new globals, we need to create a new module because\n        the cached module is already rendered and will not have access\n        to globals from the current context. This new module is not\n        cached because the template can be imported elsewhere, and it\n        should have access to only the current template's globals.\n        \"\"\"\n        if self.environment.is_async:\n            raise RuntimeError(\"Module is not available in async mode.\")\n\n        if ctx is not None:\n            keys = ctx.globals_keys - self.globals.keys()\n\n            if keys:\n                return self.make_module({k: ctx.parent[k] for k in keys})\n\n        if self._module is None:\n            self._module = self.make_module()\n\n        return self._module\n\n    async def _get_default_module_async(\n        self, ctx: Context | None = None\n    ) -> \"TemplateModule\":\n        if ctx is not None:\n            keys = ctx.globals_keys - self.globals.keys()\n\n            if keys:\n                return await self.make_module_async({k: ctx.parent[k] for k in keys})\n\n        if self._module is None:\n            self._module = await self.make_module_async()\n\n        return self._module\n\n    @property\n    def module(self) -> \"TemplateModule\":\n        \"\"\"The template as module.  This is used for imports in the\n        template runtime but is also useful if one wants to access\n        exported template variables from the Python layer:\n\n        >>> t = Template('{% macro foo() %}42{% endmacro %}23')\n        >>> str(t.module)\n        '23'\n        >>> t.module.foo() == u'42'\n        True\n\n        This attribute is not available if async mode is enabled.\n        \"\"\"\n        return self._get_default_module()\n\n    def get_corresponding_lineno(self, lineno: int) -> int:\n        \"\"\"Return the source line number of a line number in the\n        generated bytecode as they are not in sync.\n        \"\"\"\n        for template_line, code_line in reversed(self.debug_info):\n            if code_line <= lineno:\n                return template_line\n        return 1\n\n    @property\n    def is_up_to_date(self) -> bool:\n        \"\"\"If this variable is `False` there is a newer version available.\"\"\"\n        if self._uptodate is None:\n            return True\n        return self._uptodate()\n\n    @property\n    def debug_info(self) -> list[tuple[int, int]]:\n        \"\"\"The debug info mapping.\"\"\"\n        if self._debug_info:\n            return [\n                tuple(map(int, x.split(\"=\")))  # type: ignore\n                for x in self._debug_info.split(\"&\")\n            ]\n\n        return []\n\n    def __repr__(self) -> str:\n        if self.name is None:\n            name = f\"memory:{id(self):x}\"\n        else:\n            name = repr(self.name)\n        return f\"<{type(self).__name__} {name}>\"\n\n\nclass TemplateModule:\n    \"\"\"Represents an imported template.  All the exported names of the\n    template are available as attributes on this object.  Additionally\n    converting it into a string renders the contents.\n    \"\"\"\n\n    def __init__(\n        self,\n        template: Template,\n        context: Context,\n        body_stream: t.Iterable[str] | None = None,\n    ) -> None:\n        if body_stream is None:\n            if context.environment.is_async:\n                raise RuntimeError(\n                    \"Async mode requires a body stream to be passed to\"\n                    \" a template module. Use the async methods of the\"\n                    \" API you are using.\"\n                )\n\n            body_stream = list(template.root_render_func(context))\n\n        self._body_stream = body_stream\n        self.__dict__.update(context.get_exported())\n        self.__name__ = template.name\n\n    def __html__(self) -> Markup:\n        return Markup(concat(self._body_stream))\n\n    def __str__(self) -> str:\n        return concat(self._body_stream)\n\n    def __repr__(self) -> str:\n        if self.__name__ is None:\n            name = f\"memory:{id(self):x}\"\n        else:\n            name = repr(self.__name__)\n        return f\"<{type(self).__name__} {name}>\"\n\n\nclass TemplateExpression:\n    \"\"\"The :meth:`jinja2.Environment.compile_expression` method returns an\n    instance of this object.  It encapsulates the expression-like access\n    to the template with an expression it wraps.\n    \"\"\"\n\n    def __init__(self, template: Template, undefined_to_none: bool) -> None:\n        self._template = template\n        self._undefined_to_none = undefined_to_none\n\n    def __call__(self, *args: t.Any, **kwargs: t.Any) -> t.Any | None:\n        context = self._template.new_context(dict(*args, **kwargs))\n        consume(self._template.root_render_func(context))\n        rv = context.vars[\"result\"]\n        if self._undefined_to_none and isinstance(rv, Undefined):\n            rv = None\n        return rv\n\n\nclass TemplateStream:\n    \"\"\"A template stream works pretty much like an ordinary python generator\n    but it can buffer multiple items to reduce the number of total iterations.\n    Per default the output is unbuffered which means that for every unbuffered\n    instruction in the template one string is yielded.\n\n    If buffering is enabled with a buffer size of 5, five items are combined\n    into a new string.  This is mainly useful if you are streaming\n    big templates to a client via WSGI which flushes after each iteration.\n    \"\"\"\n\n    def __init__(self, gen: t.Iterator[str]) -> None:\n        self._gen = gen\n        self.disable_buffering()\n\n    def dump(\n        self,\n        fp: str | t.IO[bytes],\n        encoding: str | None = None,\n        errors: str | None = \"strict\",\n    ) -> None:\n        \"\"\"Dump the complete stream into a file or file-like object.\n        Per default strings are written, if you want to encode\n        before writing specify an `encoding`.\n\n        Example usage::\n\n            Template('Hello {{ name }}!').stream(name='foo').dump('hello.html')\n        \"\"\"\n        close = False\n\n        if isinstance(fp, str):\n            if encoding is None:\n                encoding = \"utf-8\"\n\n            real_fp: t.IO[bytes] = open(fp, \"wb\")\n            close = True\n        else:\n            real_fp = fp\n\n        try:\n            if encoding is not None:\n                iterable = (x.encode(encoding, errors) for x in self)  # type: ignore\n            else:\n                iterable = self  # type: ignore\n\n            if hasattr(real_fp, \"writelines\"):\n                real_fp.writelines(iterable)\n            else:\n                for item in iterable:\n                    real_fp.write(item)\n        finally:\n            if close:\n                real_fp.close()\n\n    def disable_buffering(self) -> None:\n        \"\"\"Disable the output buffering.\"\"\"\n        self._next = partial(next, self._gen)\n        self.buffered = False\n\n    def _buffered_generator(self, size: int) -> t.Iterator[str]:\n        buf: list[str] = []\n        c_size = 0\n        push = buf.append\n\n        while True:\n            try:\n                while c_size < size:\n                    c = next(self._gen)\n                    push(c)\n                    if c:\n                        c_size += 1\n            except StopIteration:\n                if not c_size:\n                    return\n            yield concat(buf)\n            del buf[:]\n            c_size = 0\n\n    def enable_buffering(self, size: int = 5) -> None:\n        \"\"\"Enable buffering.  Buffer `size` items before yielding them.\"\"\"\n        if size <= 1:\n            raise ValueError(\"buffer size too small\")\n\n        self.buffered = True\n        self._next = partial(next, self._buffered_generator(size))\n\n    def __iter__(self) -> \"TemplateStream\":\n        return self\n\n    def __next__(self) -> str:\n        return self._next()  # type: ignore\n\n\n# hook in default template class.  if anyone reads this comment: ignore that\n# it's possible to use custom templates ;-)\nEnvironment.template_class = Template\n"
  },
  {
    "path": "src/jinja2/exceptions.py",
    "content": "import typing as t\n\nif t.TYPE_CHECKING:\n    from .runtime import Undefined\n\n\nclass TemplateError(Exception):\n    \"\"\"Baseclass for all template errors.\"\"\"\n\n    def __init__(self, message: str | None = None) -> None:\n        super().__init__(message)\n\n    @property\n    def message(self) -> str | None:\n        return self.args[0] if self.args else None\n\n\nclass TemplateNotFound(IOError, LookupError, TemplateError):\n    \"\"\"Raised if a template does not exist.\n\n    .. versionchanged:: 2.11\n        If the given name is :class:`Undefined` and no message was\n        provided, an :exc:`UndefinedError` is raised.\n    \"\"\"\n\n    # Silence the Python warning about message being deprecated since\n    # it's not valid here.\n    message: str | None = None\n\n    def __init__(\n        self,\n        name: t.Union[str, \"Undefined\"] | None,\n        message: str | None = None,\n    ) -> None:\n        IOError.__init__(self, name)\n\n        if message is None:\n            from .runtime import Undefined\n\n            if isinstance(name, Undefined):\n                name._fail_with_undefined_error()\n\n            message = name\n\n        self.message = message\n        self.name = name\n        self.templates = [name]\n\n    def __str__(self) -> str:\n        return str(self.message)\n\n\nclass TemplatesNotFound(TemplateNotFound):\n    \"\"\"Like :class:`TemplateNotFound` but raised if multiple templates\n    are selected.  This is a subclass of :class:`TemplateNotFound`\n    exception, so just catching the base exception will catch both.\n\n    .. versionchanged:: 2.11\n        If a name in the list of names is :class:`Undefined`, a message\n        about it being undefined is shown rather than the empty string.\n\n    .. versionadded:: 2.2\n    \"\"\"\n\n    def __init__(\n        self,\n        names: t.Sequence[t.Union[str, \"Undefined\"]] = (),\n        message: str | None = None,\n    ) -> None:\n        if message is None:\n            from .runtime import Undefined\n\n            parts = []\n\n            for name in names:\n                if isinstance(name, Undefined):\n                    parts.append(name._undefined_message)\n                else:\n                    parts.append(name)\n\n            parts_str = \", \".join(map(str, parts))\n            message = f\"none of the templates given were found: {parts_str}\"\n\n        super().__init__(names[-1] if names else None, message)\n        self.templates = list(names)\n\n\nclass TemplateSyntaxError(TemplateError):\n    \"\"\"Raised to tell the user that there is a problem with the template.\"\"\"\n\n    def __init__(\n        self,\n        message: str,\n        lineno: int,\n        name: str | None = None,\n        filename: str | None = None,\n    ) -> None:\n        super().__init__(message)\n        self.lineno = lineno\n        self.name = name\n        self.filename = filename\n        self.source: str | None = None\n\n        # this is set to True if the debug.translate_syntax_error\n        # function translated the syntax error into a new traceback\n        self.translated = False\n\n    def __str__(self) -> str:\n        # for translated errors we only return the message\n        if self.translated:\n            return t.cast(str, self.message)\n\n        # otherwise attach some stuff\n        location = f\"line {self.lineno}\"\n        name = self.filename or self.name\n        if name:\n            location = f'File \"{name}\", {location}'\n        lines = [t.cast(str, self.message), \"  \" + location]\n\n        # if the source is set, add the line to the output\n        if self.source is not None:\n            try:\n                line = self.source.splitlines()[self.lineno - 1]\n            except IndexError:\n                pass\n            else:\n                lines.append(\"    \" + line.strip())\n\n        return \"\\n\".join(lines)\n\n    def __reduce__(self):  # type: ignore\n        # https://bugs.python.org/issue1692335 Exceptions that take\n        # multiple required arguments have problems with pickling.\n        # Without this, raises TypeError: __init__() missing 1 required\n        # positional argument: 'lineno'\n        return self.__class__, (self.message, self.lineno, self.name, self.filename)\n\n\nclass TemplateAssertionError(TemplateSyntaxError):\n    \"\"\"Like a template syntax error, but covers cases where something in the\n    template caused an error at compile time that wasn't necessarily caused\n    by a syntax error.  However it's a direct subclass of\n    :exc:`TemplateSyntaxError` and has the same attributes.\n    \"\"\"\n\n\nclass TemplateRuntimeError(TemplateError):\n    \"\"\"A generic runtime error in the template engine.  Under some situations\n    Jinja may raise this exception.\n    \"\"\"\n\n\nclass UndefinedError(TemplateRuntimeError):\n    \"\"\"Raised if a template tries to operate on :class:`Undefined`.\"\"\"\n\n\nclass SecurityError(TemplateRuntimeError):\n    \"\"\"Raised if a template tries to do something insecure if the\n    sandbox is enabled.\n    \"\"\"\n\n\nclass FilterArgumentError(TemplateRuntimeError):\n    \"\"\"This error is raised if a filter was called with inappropriate\n    arguments\n    \"\"\"\n"
  },
  {
    "path": "src/jinja2/ext.py",
    "content": "\"\"\"Extension API for adding custom tags and behavior.\"\"\"\n\nimport pprint\nimport re\nimport typing as t\n\nfrom markupsafe import Markup\n\nfrom . import defaults\nfrom . import nodes\nfrom .environment import Environment\nfrom .exceptions import TemplateAssertionError\nfrom .exceptions import TemplateSyntaxError\nfrom .runtime import concat  # type: ignore\nfrom .runtime import Context\nfrom .runtime import Undefined\nfrom .utils import import_string\nfrom .utils import pass_context\n\nif t.TYPE_CHECKING:\n    import typing_extensions as te\n\n    from .lexer import Token\n    from .lexer import TokenStream\n    from .parser import Parser\n\n    class _TranslationsBasic(te.Protocol):\n        def gettext(self, message: str) -> str: ...\n\n        def ngettext(self, singular: str, plural: str, n: int) -> str:\n            pass\n\n    class _TranslationsContext(_TranslationsBasic):\n        def pgettext(self, context: str, message: str) -> str: ...\n\n        def npgettext(\n            self, context: str, singular: str, plural: str, n: int\n        ) -> str: ...\n\n    _SupportedTranslations = _TranslationsBasic | _TranslationsContext\n\n\n# I18N functions available in Jinja templates. If the I18N library\n# provides ugettext, it will be assigned to gettext.\nGETTEXT_FUNCTIONS: tuple[str, ...] = (\n    \"_\",\n    \"gettext\",\n    \"ngettext\",\n    \"pgettext\",\n    \"npgettext\",\n)\n_ws_re = re.compile(r\"\\s*\\n\\s*\")\n\n\nclass Extension:\n    \"\"\"Extensions can be used to add extra functionality to the Jinja template\n    system at the parser level.  Custom extensions are bound to an environment\n    but may not store environment specific data on `self`.  The reason for\n    this is that an extension can be bound to another environment (for\n    overlays) by creating a copy and reassigning the `environment` attribute.\n\n    As extensions are created by the environment they cannot accept any\n    arguments for configuration.  One may want to work around that by using\n    a factory function, but that is not possible as extensions are identified\n    by their import name.  The correct way to configure the extension is\n    storing the configuration values on the environment.  Because this way the\n    environment ends up acting as central configuration storage the\n    attributes may clash which is why extensions have to ensure that the names\n    they choose for configuration are not too generic.  ``prefix`` for example\n    is a terrible name, ``fragment_cache_prefix`` on the other hand is a good\n    name as includes the name of the extension (fragment cache).\n    \"\"\"\n\n    identifier: t.ClassVar[str]\n\n    def __init_subclass__(cls) -> None:\n        cls.identifier = f\"{cls.__module__}.{cls.__name__}\"\n\n    #: if this extension parses this is the list of tags it's listening to.\n    tags: set[str] = set()\n\n    #: the priority of that extension.  This is especially useful for\n    #: extensions that preprocess values.  A lower value means higher\n    #: priority.\n    #:\n    #: .. versionadded:: 2.4\n    priority = 100\n\n    def __init__(self, environment: Environment) -> None:\n        self.environment = environment\n\n    def bind(self, environment: Environment) -> \"te.Self\":\n        \"\"\"Create a copy of this extension bound to another environment.\"\"\"\n        rv = object.__new__(self.__class__)\n        rv.__dict__.update(self.__dict__)\n        rv.environment = environment\n        return rv\n\n    def preprocess(\n        self, source: str, name: str | None, filename: str | None = None\n    ) -> str:\n        \"\"\"This method is called before the actual lexing and can be used to\n        preprocess the source.  The `filename` is optional.  The return value\n        must be the preprocessed source.\n        \"\"\"\n        return source\n\n    def filter_stream(\n        self, stream: \"TokenStream\"\n    ) -> t.Union[\"TokenStream\", t.Iterable[\"Token\"]]:\n        \"\"\"It's passed a :class:`~jinja2.lexer.TokenStream` that can be used\n        to filter tokens returned.  This method has to return an iterable of\n        :class:`~jinja2.lexer.Token`\\\\s, but it doesn't have to return a\n        :class:`~jinja2.lexer.TokenStream`.\n        \"\"\"\n        return stream\n\n    def parse(self, parser: \"Parser\") -> nodes.Node | list[nodes.Node]:\n        \"\"\"If any of the :attr:`tags` matched this method is called with the\n        parser as first argument.  The token the parser stream is pointing at\n        is the name token that matched.  This method has to return one or a\n        list of multiple nodes.\n        \"\"\"\n        raise NotImplementedError()\n\n    def attr(self, name: str, lineno: int | None = None) -> nodes.ExtensionAttribute:\n        \"\"\"Return an attribute node for the current extension.  This is useful\n        to pass constants on extensions to generated template code.\n\n        ::\n\n            self.attr('_my_attribute', lineno=lineno)\n        \"\"\"\n        return nodes.ExtensionAttribute(self.identifier, name, lineno=lineno)\n\n    def call_method(\n        self,\n        name: str,\n        args: list[nodes.Expr] | None = None,\n        kwargs: list[nodes.Keyword] | None = None,\n        dyn_args: nodes.Expr | None = None,\n        dyn_kwargs: nodes.Expr | None = None,\n        lineno: int | None = None,\n    ) -> nodes.Call:\n        \"\"\"Call a method of the extension.  This is a shortcut for\n        :meth:`attr` + :class:`jinja2.nodes.Call`.\n        \"\"\"\n        if args is None:\n            args = []\n        if kwargs is None:\n            kwargs = []\n        return nodes.Call(\n            self.attr(name, lineno=lineno),\n            args,\n            kwargs,\n            dyn_args,\n            dyn_kwargs,\n            lineno=lineno,\n        )\n\n\n@pass_context\ndef _gettext_alias(\n    __context: Context, *args: t.Any, **kwargs: t.Any\n) -> t.Any | Undefined:\n    return __context.call(__context.resolve(\"gettext\"), *args, **kwargs)\n\n\ndef _make_new_gettext(func: t.Callable[[str], str]) -> t.Callable[..., str]:\n    @pass_context\n    def gettext(__context: Context, __string: str, **variables: t.Any) -> str:\n        rv = __context.call(func, __string)\n        if __context.eval_ctx.autoescape:\n            rv = Markup(rv)\n        # Always treat as a format string, even if there are no\n        # variables. This makes translation strings more consistent\n        # and predictable. This requires escaping\n        return rv % variables  # type: ignore\n\n    return gettext\n\n\ndef _make_new_ngettext(func: t.Callable[[str, str, int], str]) -> t.Callable[..., str]:\n    @pass_context\n    def ngettext(\n        __context: Context,\n        __singular: str,\n        __plural: str,\n        __num: int,\n        **variables: t.Any,\n    ) -> str:\n        variables.setdefault(\"num\", __num)\n        rv = __context.call(func, __singular, __plural, __num)\n        if __context.eval_ctx.autoescape:\n            rv = Markup(rv)\n        # Always treat as a format string, see gettext comment above.\n        return rv % variables  # type: ignore\n\n    return ngettext\n\n\ndef _make_new_pgettext(func: t.Callable[[str, str], str]) -> t.Callable[..., str]:\n    @pass_context\n    def pgettext(\n        __context: Context, __string_ctx: str, __string: str, **variables: t.Any\n    ) -> str:\n        variables.setdefault(\"context\", __string_ctx)\n        rv = __context.call(func, __string_ctx, __string)\n\n        if __context.eval_ctx.autoescape:\n            rv = Markup(rv)\n\n        # Always treat as a format string, see gettext comment above.\n        return rv % variables  # type: ignore\n\n    return pgettext\n\n\ndef _make_new_npgettext(\n    func: t.Callable[[str, str, str, int], str],\n) -> t.Callable[..., str]:\n    @pass_context\n    def npgettext(\n        __context: Context,\n        __string_ctx: str,\n        __singular: str,\n        __plural: str,\n        __num: int,\n        **variables: t.Any,\n    ) -> str:\n        variables.setdefault(\"context\", __string_ctx)\n        variables.setdefault(\"num\", __num)\n        rv = __context.call(func, __string_ctx, __singular, __plural, __num)\n\n        if __context.eval_ctx.autoescape:\n            rv = Markup(rv)\n\n        # Always treat as a format string, see gettext comment above.\n        return rv % variables  # type: ignore\n\n    return npgettext\n\n\nclass InternationalizationExtension(Extension):\n    \"\"\"This extension adds gettext support to Jinja.\"\"\"\n\n    tags = {\"trans\"}\n\n    # TODO: the i18n extension is currently reevaluating values in a few\n    # situations.  Take this example:\n    #   {% trans count=something() %}{{ count }} foo{% pluralize\n    #     %}{{ count }} fooss{% endtrans %}\n    # something is called twice here.  One time for the gettext value and\n    # the other time for the n-parameter of the ngettext function.\n\n    def __init__(self, environment: Environment) -> None:\n        super().__init__(environment)\n        environment.globals[\"_\"] = _gettext_alias\n        environment.extend(\n            install_gettext_translations=self._install,\n            install_null_translations=self._install_null,\n            install_gettext_callables=self._install_callables,\n            uninstall_gettext_translations=self._uninstall,\n            extract_translations=self._extract,\n            newstyle_gettext=False,\n        )\n\n    def _install(\n        self, translations: \"_SupportedTranslations\", newstyle: bool | None = None\n    ) -> None:\n        # ugettext and ungettext are preferred in case the I18N library\n        # is providing compatibility with older Python versions.\n        gettext = getattr(translations, \"ugettext\", None)\n        if gettext is None:\n            gettext = translations.gettext\n        ngettext = getattr(translations, \"ungettext\", None)\n        if ngettext is None:\n            ngettext = translations.ngettext\n\n        pgettext = getattr(translations, \"pgettext\", None)\n        npgettext = getattr(translations, \"npgettext\", None)\n        self._install_callables(\n            gettext, ngettext, newstyle=newstyle, pgettext=pgettext, npgettext=npgettext\n        )\n\n    def _install_null(self, newstyle: bool | None = None) -> None:\n        import gettext\n\n        translations = gettext.NullTranslations()\n        self._install_callables(\n            gettext=translations.gettext,\n            ngettext=translations.ngettext,\n            newstyle=newstyle,\n            pgettext=translations.pgettext,\n            npgettext=translations.npgettext,\n        )\n\n    def _install_callables(\n        self,\n        gettext: t.Callable[[str], str],\n        ngettext: t.Callable[[str, str, int], str],\n        newstyle: bool | None = None,\n        pgettext: t.Callable[[str, str], str] | None = None,\n        npgettext: t.Callable[[str, str, str, int], str] | None = None,\n    ) -> None:\n        if newstyle is not None:\n            self.environment.newstyle_gettext = newstyle  # type: ignore\n        if self.environment.newstyle_gettext:  # type: ignore\n            gettext = _make_new_gettext(gettext)\n            ngettext = _make_new_ngettext(ngettext)\n\n            if pgettext is not None:\n                pgettext = _make_new_pgettext(pgettext)\n\n            if npgettext is not None:\n                npgettext = _make_new_npgettext(npgettext)\n\n        self.environment.globals.update(\n            gettext=gettext, ngettext=ngettext, pgettext=pgettext, npgettext=npgettext\n        )\n\n    def _uninstall(self, translations: \"_SupportedTranslations\") -> None:\n        for key in (\"gettext\", \"ngettext\", \"pgettext\", \"npgettext\"):\n            self.environment.globals.pop(key, None)\n\n    def _extract(\n        self,\n        source: str | nodes.Template,\n        gettext_functions: t.Sequence[str] = GETTEXT_FUNCTIONS,\n    ) -> t.Iterator[tuple[int, str, str | None | tuple[str | None, ...]]]:\n        if isinstance(source, str):\n            source = self.environment.parse(source)\n        return extract_from_ast(source, gettext_functions)\n\n    def parse(self, parser: \"Parser\") -> nodes.Node | list[nodes.Node]:\n        \"\"\"Parse a translatable tag.\"\"\"\n        lineno = next(parser.stream).lineno\n\n        context = None\n        context_token = parser.stream.next_if(\"string\")\n\n        if context_token is not None:\n            context = context_token.value\n\n        # find all the variables referenced.  Additionally a variable can be\n        # defined in the body of the trans block too, but this is checked at\n        # a later state.\n        plural_expr: nodes.Expr | None = None\n        plural_expr_assignment: nodes.Assign | None = None\n        num_called_num = False\n        variables: dict[str, nodes.Expr] = {}\n        trimmed = None\n        while parser.stream.current.type != \"block_end\":\n            if variables:\n                parser.stream.expect(\"comma\")\n\n            # skip colon for python compatibility\n            if parser.stream.skip_if(\"colon\"):\n                break\n\n            token = parser.stream.expect(\"name\")\n            if token.value in variables:\n                parser.fail(\n                    f\"translatable variable {token.value!r} defined twice.\",\n                    token.lineno,\n                    exc=TemplateAssertionError,\n                )\n\n            # expressions\n            if parser.stream.current.type == \"assign\":\n                next(parser.stream)\n                variables[token.value] = var = parser.parse_expression()\n            elif trimmed is None and token.value in (\"trimmed\", \"notrimmed\"):\n                trimmed = token.value == \"trimmed\"\n                continue\n            else:\n                variables[token.value] = var = nodes.Name(token.value, \"load\")\n\n            if plural_expr is None:\n                if isinstance(var, nodes.Call):\n                    plural_expr = nodes.Name(\"_trans\", \"load\")\n                    variables[token.value] = plural_expr\n                    plural_expr_assignment = nodes.Assign(\n                        nodes.Name(\"_trans\", \"store\"), var\n                    )\n                else:\n                    plural_expr = var\n                num_called_num = token.value == \"num\"\n\n        parser.stream.expect(\"block_end\")\n\n        plural = None\n        have_plural = False\n        referenced = set()\n\n        # now parse until endtrans or pluralize\n        singular_names, singular = self._parse_block(parser, True)\n        if singular_names:\n            referenced.update(singular_names)\n            if plural_expr is None:\n                plural_expr = nodes.Name(singular_names[0], \"load\")\n                num_called_num = singular_names[0] == \"num\"\n\n        # if we have a pluralize block, we parse that too\n        if parser.stream.current.test(\"name:pluralize\"):\n            have_plural = True\n            next(parser.stream)\n            if parser.stream.current.type != \"block_end\":\n                token = parser.stream.expect(\"name\")\n                if token.value not in variables:\n                    parser.fail(\n                        f\"unknown variable {token.value!r} for pluralization\",\n                        token.lineno,\n                        exc=TemplateAssertionError,\n                    )\n                plural_expr = variables[token.value]\n                num_called_num = token.value == \"num\"\n            parser.stream.expect(\"block_end\")\n            plural_names, plural = self._parse_block(parser, False)\n            next(parser.stream)\n            referenced.update(plural_names)\n        else:\n            next(parser.stream)\n\n        # register free names as simple name expressions\n        for name in referenced:\n            if name not in variables:\n                variables[name] = nodes.Name(name, \"load\")\n\n        if not have_plural:\n            plural_expr = None\n        elif plural_expr is None:\n            parser.fail(\"pluralize without variables\", lineno)\n\n        if trimmed is None:\n            trimmed = self.environment.policies[\"ext.i18n.trimmed\"]\n        if trimmed:\n            singular = self._trim_whitespace(singular)\n            if plural:\n                plural = self._trim_whitespace(plural)\n\n        node = self._make_node(\n            singular,\n            plural,\n            context,\n            variables,\n            plural_expr,\n            bool(referenced),\n            num_called_num and have_plural,\n        )\n        node.set_lineno(lineno)\n        if plural_expr_assignment is not None:\n            return [plural_expr_assignment, node]\n        else:\n            return node\n\n    def _trim_whitespace(self, string: str, _ws_re: t.Pattern[str] = _ws_re) -> str:\n        return _ws_re.sub(\" \", string.strip())\n\n    def _parse_block(\n        self, parser: \"Parser\", allow_pluralize: bool\n    ) -> tuple[list[str], str]:\n        \"\"\"Parse until the next block tag with a given name.\"\"\"\n        referenced = []\n        buf = []\n\n        while True:\n            if parser.stream.current.type == \"data\":\n                buf.append(parser.stream.current.value.replace(\"%\", \"%%\"))\n                next(parser.stream)\n            elif parser.stream.current.type == \"variable_begin\":\n                next(parser.stream)\n                name = parser.stream.expect(\"name\").value\n                referenced.append(name)\n                buf.append(f\"%({name})s\")\n                parser.stream.expect(\"variable_end\")\n            elif parser.stream.current.type == \"block_begin\":\n                next(parser.stream)\n                block_name = (\n                    parser.stream.current.value\n                    if parser.stream.current.type == \"name\"\n                    else None\n                )\n                if block_name == \"endtrans\":\n                    break\n                elif block_name == \"pluralize\":\n                    if allow_pluralize:\n                        break\n                    parser.fail(\n                        \"a translatable section can have only one pluralize section\"\n                    )\n                elif block_name == \"trans\":\n                    parser.fail(\n                        \"trans blocks can't be nested; did you mean `endtrans`?\"\n                    )\n                parser.fail(\n                    f\"control structures in translatable sections are not allowed; \"\n                    f\"saw `{block_name}`\"\n                )\n            elif parser.stream.eos:\n                parser.fail(\"unclosed translation block\")\n            else:\n                raise RuntimeError(\"internal parser error\")\n\n        return referenced, concat(buf)\n\n    def _make_node(\n        self,\n        singular: str,\n        plural: str | None,\n        context: str | None,\n        variables: dict[str, nodes.Expr],\n        plural_expr: nodes.Expr | None,\n        vars_referenced: bool,\n        num_called_num: bool,\n    ) -> nodes.Output:\n        \"\"\"Generates a useful node from the data provided.\"\"\"\n        newstyle = self.environment.newstyle_gettext  # type: ignore\n        node: nodes.Expr\n\n        # no variables referenced?  no need to escape for old style\n        # gettext invocations only if there are vars.\n        if not vars_referenced and not newstyle:\n            singular = singular.replace(\"%%\", \"%\")\n            if plural:\n                plural = plural.replace(\"%%\", \"%\")\n\n        func_name = \"gettext\"\n        func_args: list[nodes.Expr] = [nodes.Const(singular)]\n\n        if context is not None:\n            func_args.insert(0, nodes.Const(context))\n            func_name = f\"p{func_name}\"\n\n        if plural_expr is not None:\n            func_name = f\"n{func_name}\"\n            func_args.extend((nodes.Const(plural), plural_expr))\n\n        node = nodes.Call(nodes.Name(func_name, \"load\"), func_args, [], None, None)\n\n        # in case newstyle gettext is used, the method is powerful\n        # enough to handle the variable expansion and autoescape\n        # handling itself\n        if newstyle:\n            for key, value in variables.items():\n                # the function adds that later anyways in case num was\n                # called num, so just skip it.\n                if num_called_num and key == \"num\":\n                    continue\n                node.kwargs.append(nodes.Keyword(key, value))\n\n        # otherwise do that here\n        else:\n            # mark the return value as safe if we are in an\n            # environment with autoescaping turned on\n            node = nodes.MarkSafeIfAutoescape(node)\n            if variables:\n                node = nodes.Mod(\n                    node,\n                    nodes.Dict(\n                        [\n                            nodes.Pair(nodes.Const(key), value)\n                            for key, value in variables.items()\n                        ]\n                    ),\n                )\n        return nodes.Output([node])\n\n\nclass ExprStmtExtension(Extension):\n    \"\"\"Adds a `do` tag to Jinja that works like the print statement just\n    that it doesn't print the return value.\n    \"\"\"\n\n    tags = {\"do\"}\n\n    def parse(self, parser: \"Parser\") -> nodes.ExprStmt:\n        node = nodes.ExprStmt(lineno=next(parser.stream).lineno)\n        node.node = parser.parse_tuple()\n        return node\n\n\nclass LoopControlExtension(Extension):\n    \"\"\"Adds break and continue to the template engine.\"\"\"\n\n    tags = {\"break\", \"continue\"}\n\n    def parse(self, parser: \"Parser\") -> nodes.Break | nodes.Continue:\n        token = next(parser.stream)\n        if token.value == \"break\":\n            return nodes.Break(lineno=token.lineno)\n        return nodes.Continue(lineno=token.lineno)\n\n\nclass DebugExtension(Extension):\n    \"\"\"A ``{% debug %}`` tag that dumps the available variables,\n    filters, and tests.\n\n    .. code-block:: html+jinja\n\n        <pre>{% debug %}</pre>\n\n    .. code-block:: text\n\n        {'context': {'cycler': <class 'jinja2.utils.Cycler'>,\n                     ...,\n                     'namespace': <class 'jinja2.utils.Namespace'>},\n         'filters': ['abs', 'attr', 'batch', 'capitalize', 'center', 'count', 'd',\n                     ..., 'urlencode', 'urlize', 'wordcount', 'wordwrap', 'xmlattr'],\n         'tests': ['!=', '<', '<=', '==', '>', '>=', 'callable', 'defined',\n                   ..., 'odd', 'sameas', 'sequence', 'string', 'undefined', 'upper']}\n\n    .. versionadded:: 2.11.0\n    \"\"\"\n\n    tags = {\"debug\"}\n\n    def parse(self, parser: \"Parser\") -> nodes.Output:\n        lineno = parser.stream.expect(\"name:debug\").lineno\n        context = nodes.ContextReference()\n        result = self.call_method(\"_render\", [context], lineno=lineno)\n        return nodes.Output([result], lineno=lineno)\n\n    def _render(self, context: Context) -> str:\n        result = {\n            \"context\": context.get_all(),\n            \"filters\": sorted(self.environment.filters.keys()),\n            \"tests\": sorted(self.environment.tests.keys()),\n        }\n\n        # Set the depth since the intent is to show the top few names.\n        return pprint.pformat(result, depth=3, compact=True)\n\n\ndef extract_from_ast(\n    ast: nodes.Template,\n    gettext_functions: t.Sequence[str] = GETTEXT_FUNCTIONS,\n    babel_style: bool = True,\n) -> t.Iterator[tuple[int, str, str | None | tuple[str | None, ...]]]:\n    \"\"\"Extract localizable strings from the given template node.  Per\n    default this function returns matches in babel style that means non string\n    parameters as well as keyword arguments are returned as `None`.  This\n    allows Babel to figure out what you really meant if you are using\n    gettext functions that allow keyword arguments for placeholder expansion.\n    If you don't want that behavior set the `babel_style` parameter to `False`\n    which causes only strings to be returned and parameters are always stored\n    in tuples.  As a consequence invalid gettext calls (calls without a single\n    string parameter or string parameters after non-string parameters) are\n    skipped.\n\n    This example explains the behavior:\n\n    >>> from jinja2 import Environment\n    >>> env = Environment()\n    >>> node = env.parse('{{ (_(\"foo\"), _(), ngettext(\"foo\", \"bar\", 42)) }}')\n    >>> list(extract_from_ast(node))\n    [(1, '_', 'foo'), (1, '_', ()), (1, 'ngettext', ('foo', 'bar', None))]\n    >>> list(extract_from_ast(node, babel_style=False))\n    [(1, '_', ('foo',)), (1, 'ngettext', ('foo', 'bar'))]\n\n    For every string found this function yields a ``(lineno, function,\n    message)`` tuple, where:\n\n    * ``lineno`` is the number of the line on which the string was found,\n    * ``function`` is the name of the ``gettext`` function used (if the\n      string was extracted from embedded Python code), and\n    *   ``message`` is the string, or a tuple of strings for functions\n         with multiple string arguments.\n\n    This extraction function operates on the AST and is because of that unable\n    to extract any comments.  For comment support you have to use the babel\n    extraction interface or extract comments yourself.\n    \"\"\"\n    out: str | None | tuple[str | None, ...]\n\n    for node in ast.find_all(nodes.Call):\n        if (\n            not isinstance(node.node, nodes.Name)\n            or node.node.name not in gettext_functions\n        ):\n            continue\n\n        strings: list[str | None] = []\n\n        for arg in node.args:\n            if isinstance(arg, nodes.Const) and isinstance(arg.value, str):\n                strings.append(arg.value)\n            else:\n                strings.append(None)\n\n        for _ in node.kwargs:\n            strings.append(None)\n        if node.dyn_args is not None:\n            strings.append(None)\n        if node.dyn_kwargs is not None:\n            strings.append(None)\n\n        if not babel_style:\n            out = tuple(x for x in strings if x is not None)\n\n            if not out:\n                continue\n        else:\n            if len(strings) == 1:\n                out = strings[0]\n            else:\n                out = tuple(strings)\n\n        yield node.lineno, node.node.name, out\n\n\nclass _CommentFinder:\n    \"\"\"Helper class to find comments in a token stream.  Can only\n    find comments for gettext calls forwards.  Once the comment\n    from line 4 is found, a comment for line 1 will not return a\n    usable value.\n    \"\"\"\n\n    def __init__(\n        self, tokens: t.Sequence[tuple[int, str, str]], comment_tags: t.Sequence[str]\n    ) -> None:\n        self.tokens = tokens\n        self.comment_tags = comment_tags\n        self.offset = 0\n        self.last_lineno = 0\n\n    def find_backwards(self, offset: int) -> list[str]:\n        try:\n            for _, token_type, token_value in reversed(\n                self.tokens[self.offset : offset]\n            ):\n                if token_type in (\"comment\", \"linecomment\"):\n                    try:\n                        prefix, comment = token_value.split(None, 1)\n                    except ValueError:\n                        continue\n                    if prefix in self.comment_tags:\n                        return [comment.rstrip()]\n            return []\n        finally:\n            self.offset = offset\n\n    def find_comments(self, lineno: int) -> list[str]:\n        if not self.comment_tags or self.last_lineno > lineno:\n            return []\n        for idx, (token_lineno, _, _) in enumerate(self.tokens[self.offset :]):\n            if token_lineno > lineno:\n                return self.find_backwards(self.offset + idx)\n        return self.find_backwards(len(self.tokens))\n\n\ndef babel_extract(\n    fileobj: t.BinaryIO,\n    keywords: t.Sequence[str],\n    comment_tags: t.Sequence[str],\n    options: dict[str, t.Any],\n) -> t.Iterator[tuple[int, str, str | None | tuple[str | None, ...], list[str]]]:\n    \"\"\"Babel extraction method for Jinja templates.\n\n    .. versionchanged:: 2.3\n       Basic support for translation comments was added.  If `comment_tags`\n       is now set to a list of keywords for extraction, the extractor will\n       try to find the best preceding comment that begins with one of the\n       keywords.  For best results, make sure to not have more than one\n       gettext call in one line of code and the matching comment in the\n       same line or the line before.\n\n    .. versionchanged:: 2.5.1\n       The `newstyle_gettext` flag can be set to `True` to enable newstyle\n       gettext calls.\n\n    .. versionchanged:: 2.7\n       A `silent` option can now be provided.  If set to `False` template\n       syntax errors are propagated instead of being ignored.\n\n    :param fileobj: the file-like object the messages should be extracted from\n    :param keywords: a list of keywords (i.e. function names) that should be\n                     recognized as translation functions\n    :param comment_tags: a list of translator tags to search for and include\n                         in the results.\n    :param options: a dictionary of additional options (optional)\n    :return: an iterator over ``(lineno, funcname, message, comments)`` tuples.\n             (comments will be empty currently)\n    \"\"\"\n    extensions: dict[type[Extension], None] = {}\n\n    for extension_name in options.get(\"extensions\", \"\").split(\",\"):\n        extension_name = extension_name.strip()\n\n        if not extension_name:\n            continue\n\n        extensions[import_string(extension_name)] = None\n\n    if InternationalizationExtension not in extensions:\n        extensions[InternationalizationExtension] = None\n\n    def getbool(options: t.Mapping[str, str], key: str, default: bool = False) -> bool:\n        return options.get(key, str(default)).lower() in {\"1\", \"on\", \"yes\", \"true\"}\n\n    silent = getbool(options, \"silent\", True)\n    environment = Environment(\n        options.get(\"block_start_string\", defaults.BLOCK_START_STRING),\n        options.get(\"block_end_string\", defaults.BLOCK_END_STRING),\n        options.get(\"variable_start_string\", defaults.VARIABLE_START_STRING),\n        options.get(\"variable_end_string\", defaults.VARIABLE_END_STRING),\n        options.get(\"comment_start_string\", defaults.COMMENT_START_STRING),\n        options.get(\"comment_end_string\", defaults.COMMENT_END_STRING),\n        options.get(\"line_statement_prefix\") or defaults.LINE_STATEMENT_PREFIX,\n        options.get(\"line_comment_prefix\") or defaults.LINE_COMMENT_PREFIX,\n        getbool(options, \"trim_blocks\", defaults.TRIM_BLOCKS),\n        getbool(options, \"lstrip_blocks\", defaults.LSTRIP_BLOCKS),\n        defaults.NEWLINE_SEQUENCE,\n        getbool(options, \"keep_trailing_newline\", defaults.KEEP_TRAILING_NEWLINE),\n        tuple(extensions),\n        cache_size=0,\n        auto_reload=False,\n    )\n\n    if getbool(options, \"trimmed\"):\n        environment.policies[\"ext.i18n.trimmed\"] = True\n    if getbool(options, \"newstyle_gettext\"):\n        environment.newstyle_gettext = True  # type: ignore\n\n    source = fileobj.read().decode(options.get(\"encoding\", \"utf-8\"))\n    try:\n        node = environment.parse(source)\n        tokens = list(environment.lex(environment.preprocess(source)))\n    except TemplateSyntaxError:\n        if not silent:\n            raise\n        # skip templates with syntax errors\n        return\n\n    finder = _CommentFinder(tokens, comment_tags)\n    for lineno, func, message in extract_from_ast(node, keywords):\n        yield lineno, func, message, finder.find_comments(lineno)\n\n\n#: nicer import names\ni18n = InternationalizationExtension\ndo = ExprStmtExtension\nloopcontrols = LoopControlExtension\ndebug = DebugExtension\n"
  },
  {
    "path": "src/jinja2/filters.py",
    "content": "\"\"\"Built-in template filters used with the ``|`` operator.\"\"\"\n\nimport math\nimport random\nimport re\nimport typing\nimport typing as t\nfrom collections import abc\nfrom inspect import getattr_static\nfrom itertools import chain\nfrom itertools import groupby\n\nfrom markupsafe import escape\nfrom markupsafe import Markup\nfrom markupsafe import soft_str\n\nfrom .async_utils import async_variant\nfrom .async_utils import auto_aiter\nfrom .async_utils import auto_await\nfrom .async_utils import auto_to_list\nfrom .exceptions import FilterArgumentError\nfrom .runtime import Undefined\nfrom .utils import htmlsafe_json_dumps\nfrom .utils import pass_context\nfrom .utils import pass_environment\nfrom .utils import pass_eval_context\nfrom .utils import pformat\nfrom .utils import url_quote\nfrom .utils import urlize\n\nif t.TYPE_CHECKING:\n    import typing_extensions as te\n\n    from .environment import Environment\n    from .nodes import EvalContext\n    from .runtime import Context\n    from .sandbox import SandboxedEnvironment  # noqa: F401\n\n    class HasHTML(te.Protocol):\n        def __html__(self) -> str:\n            pass\n\n\nF = t.TypeVar(\"F\", bound=t.Callable[..., t.Any])\nK = t.TypeVar(\"K\")\nV = t.TypeVar(\"V\")\n\n\ndef ignore_case(value: V) -> V:\n    \"\"\"For use as a postprocessor for :func:`make_attrgetter`. Converts strings\n    to lowercase and returns other types as-is.\"\"\"\n    if isinstance(value, str):\n        return t.cast(V, value.lower())\n\n    return value\n\n\ndef make_attrgetter(\n    environment: \"Environment\",\n    attribute: str | int | None,\n    postprocess: t.Callable[[t.Any], t.Any] | None = None,\n    default: t.Any | None = None,\n) -> t.Callable[[t.Any], t.Any]:\n    \"\"\"Returns a callable that looks up the given attribute from a\n    passed object with the rules of the environment.  Dots are allowed\n    to access attributes of attributes.  Integer parts in paths are\n    looked up as integers.\n    \"\"\"\n    parts = _prepare_attribute_parts(attribute)\n\n    def attrgetter(item: t.Any) -> t.Any:\n        for part in parts:\n            item = environment.getitem(item, part)\n\n            if default is not None and isinstance(item, Undefined):\n                item = default\n\n        if postprocess is not None:\n            item = postprocess(item)\n\n        return item\n\n    return attrgetter\n\n\ndef make_multi_attrgetter(\n    environment: \"Environment\",\n    attribute: str | int | None,\n    postprocess: t.Callable[[t.Any], t.Any] | None = None,\n) -> t.Callable[[t.Any], list[t.Any]]:\n    \"\"\"Returns a callable that looks up the given comma separated\n    attributes from a passed object with the rules of the environment.\n    Dots are allowed to access attributes of each attribute.  Integer\n    parts in paths are looked up as integers.\n\n    The value returned by the returned callable is a list of extracted\n    attribute values.\n\n    Examples of attribute: \"attr1,attr2\", \"attr1.inner1.0,attr2.inner2.0\", etc.\n    \"\"\"\n    if isinstance(attribute, str):\n        split: t.Sequence[str | int | None] = attribute.split(\",\")\n    else:\n        split = [attribute]\n\n    parts = [_prepare_attribute_parts(item) for item in split]\n\n    def attrgetter(item: t.Any) -> list[t.Any]:\n        items = [None] * len(parts)\n\n        for i, attribute_part in enumerate(parts):\n            item_i = item\n\n            for part in attribute_part:\n                item_i = environment.getitem(item_i, part)\n\n            if postprocess is not None:\n                item_i = postprocess(item_i)\n\n            items[i] = item_i\n\n        return items\n\n    return attrgetter\n\n\ndef _prepare_attribute_parts(\n    attr: str | int | None,\n) -> list[str | int]:\n    if attr is None:\n        return []\n\n    if isinstance(attr, str):\n        return [int(x) if x.isdigit() else x for x in attr.split(\".\")]\n\n    return [attr]\n\n\ndef do_forceescape(value: \"str | HasHTML\") -> Markup:\n    \"\"\"Enforce HTML escaping.  This will probably double escape variables.\"\"\"\n    if hasattr(value, \"__html__\"):\n        value = t.cast(\"HasHTML\", value).__html__()\n\n    return escape(str(value))\n\n\ndef do_urlencode(\n    value: str | t.Mapping[str, t.Any] | t.Iterable[tuple[str, t.Any]],\n) -> str:\n    \"\"\"Quote data for use in a URL path or query using UTF-8.\n\n    Basic wrapper around :func:`urllib.parse.quote` when given a\n    string, or :func:`urllib.parse.urlencode` for a dict or iterable.\n\n    :param value: Data to quote. A string will be quoted directly. A\n        dict or iterable of ``(key, value)`` pairs will be joined as a\n        query string.\n\n    When given a string, \"/\" is not quoted. HTTP servers treat \"/\" and\n    \"%2F\" equivalently in paths. If you need quoted slashes, use the\n    ``|replace(\"/\", \"%2F\")`` filter.\n\n    .. versionadded:: 2.7\n    \"\"\"\n    if isinstance(value, str) or not isinstance(value, abc.Iterable):\n        return url_quote(value)\n\n    if isinstance(value, dict):\n        items: t.Iterable[tuple[str, t.Any]] = value.items()\n    else:\n        items = value  # type: ignore\n\n    return \"&\".join(\n        f\"{url_quote(k, for_qs=True)}={url_quote(v, for_qs=True)}\" for k, v in items\n    )\n\n\n@pass_eval_context\ndef do_replace(\n    eval_ctx: \"EvalContext\", s: str, old: str, new: str, count: int | None = None\n) -> str:\n    \"\"\"Return a copy of the value with all occurrences of a substring\n    replaced with a new one. The first argument is the substring\n    that should be replaced, the second is the replacement string.\n    If the optional third argument ``count`` is given, only the first\n    ``count`` occurrences are replaced:\n\n    .. sourcecode:: jinja\n\n        {{ \"Hello World\"|replace(\"Hello\", \"Goodbye\") }}\n            -> Goodbye World\n\n        {{ \"aaaaargh\"|replace(\"a\", \"d'oh, \", 2) }}\n            -> d'oh, d'oh, aaargh\n    \"\"\"\n    if count is None:\n        count = -1\n\n    if not eval_ctx.autoescape:\n        return str(s).replace(str(old), str(new), count)\n\n    if (\n        hasattr(old, \"__html__\")\n        or hasattr(new, \"__html__\")\n        and not hasattr(s, \"__html__\")\n    ):\n        s = escape(s)\n    else:\n        s = soft_str(s)\n\n    return s.replace(soft_str(old), soft_str(new), count)\n\n\ndef do_upper(s: str) -> str:\n    \"\"\"Convert a value to uppercase.\"\"\"\n    return soft_str(s).upper()\n\n\ndef do_lower(s: str) -> str:\n    \"\"\"Convert a value to lowercase.\"\"\"\n    return soft_str(s).lower()\n\n\ndef do_items(value: t.Mapping[K, V] | Undefined) -> t.Iterator[tuple[K, V]]:\n    \"\"\"Return an iterator over the ``(key, value)`` items of a mapping.\n\n    ``x|items`` is the same as ``x.items()``, except if ``x`` is\n    undefined an empty iterator is returned.\n\n    This filter is useful if you expect the template to be rendered with\n    an implementation of Jinja in another programming language that does\n    not have a ``.items()`` method on its mapping type.\n\n    .. code-block:: html+jinja\n\n        <dl>\n        {% for key, value in my_dict|items %}\n            <dt>{{ key }}\n            <dd>{{ value }}\n        {% endfor %}\n        </dl>\n\n    .. versionadded:: 3.1\n    \"\"\"\n    if isinstance(value, Undefined):\n        return\n\n    if not isinstance(value, abc.Mapping):\n        raise TypeError(\"Can only get item pairs from a mapping.\")\n\n    yield from value.items()\n\n\n# Check for characters that would move the parser state from key to value.\n# https://html.spec.whatwg.org/#attribute-name-state\n_attr_key_re = re.compile(r\"[\\s/>=]\", flags=re.ASCII)\n\n\n@pass_eval_context\ndef do_xmlattr(\n    eval_ctx: \"EvalContext\", d: t.Mapping[str, t.Any], autospace: bool = True\n) -> str:\n    \"\"\"Create an SGML/XML attribute string based on the items in a dict.\n\n    **Values** that are neither ``none`` nor ``undefined`` are automatically\n    escaped, safely allowing untrusted user input.\n\n    User input should not be used as **keys** to this filter. If any key\n    contains a space, ``/`` solidus, ``>`` greater-than sign, or ``=`` equals\n    sign, this fails with a ``ValueError``. Regardless of this, user input\n    should never be used as keys to this filter, or must be separately validated\n    first.\n\n    .. sourcecode:: html+jinja\n\n        <ul{{ {'class': 'my_list', 'missing': none,\n                'id': 'list-%d'|format(variable)}|xmlattr }}>\n        ...\n        </ul>\n\n    Results in something like this:\n\n    .. sourcecode:: html\n\n        <ul class=\"my_list\" id=\"list-42\">\n        ...\n        </ul>\n\n    As you can see it automatically prepends a space in front of the item\n    if the filter returned something unless the second parameter is false.\n\n    .. versionchanged:: 3.1.4\n        Keys with ``/`` solidus, ``>`` greater-than sign, or ``=`` equals sign\n        are not allowed.\n\n    .. versionchanged:: 3.1.3\n        Keys with spaces are not allowed.\n    \"\"\"\n    items = []\n\n    for key, value in d.items():\n        if value is None or isinstance(value, Undefined):\n            continue\n\n        if _attr_key_re.search(key) is not None:\n            raise ValueError(f\"Invalid character in attribute name: {key!r}\")\n\n        items.append(f'{escape(key)}=\"{escape(value)}\"')\n\n    rv = \" \".join(items)\n\n    if autospace and rv:\n        rv = \" \" + rv\n\n    if eval_ctx.autoescape:\n        rv = Markup(rv)\n\n    return rv\n\n\ndef do_capitalize(s: str) -> str:\n    \"\"\"Capitalize a value. The first character will be uppercase, all others\n    lowercase.\n    \"\"\"\n    return soft_str(s).capitalize()\n\n\n_word_beginning_split_re = re.compile(r\"([-\\s({\\[<]+)\")\n\n\ndef do_title(s: str) -> str:\n    \"\"\"Return a titlecased version of the value. I.e. words will start with\n    uppercase letters, all remaining characters are lowercase.\n    \"\"\"\n    return \"\".join(\n        [\n            item[0].upper() + item[1:].lower()\n            for item in _word_beginning_split_re.split(soft_str(s))\n            if item\n        ]\n    )\n\n\ndef do_dictsort(\n    value: t.Mapping[K, V],\n    case_sensitive: bool = False,\n    by: 'te.Literal[\"key\", \"value\"]' = \"key\",\n    reverse: bool = False,\n) -> list[tuple[K, V]]:\n    \"\"\"Sort a dict and yield (key, value) pairs. Python dicts may not\n    be in the order you want to display them in, so sort them first.\n\n    .. sourcecode:: jinja\n\n        {% for key, value in mydict|dictsort %}\n            sort the dict by key, case insensitive\n\n        {% for key, value in mydict|dictsort(reverse=true) %}\n            sort the dict by key, case insensitive, reverse order\n\n        {% for key, value in mydict|dictsort(true) %}\n            sort the dict by key, case sensitive\n\n        {% for key, value in mydict|dictsort(false, 'value') %}\n            sort the dict by value, case insensitive\n    \"\"\"\n    if by == \"key\":\n        pos = 0\n    elif by == \"value\":\n        pos = 1\n    else:\n        raise FilterArgumentError('You can only sort by either \"key\" or \"value\"')\n\n    def sort_func(item: tuple[t.Any, t.Any]) -> t.Any:\n        value = item[pos]\n\n        if not case_sensitive:\n            value = ignore_case(value)\n\n        return value\n\n    return sorted(value.items(), key=sort_func, reverse=reverse)\n\n\n@pass_environment\ndef do_sort(\n    environment: \"Environment\",\n    value: \"t.Iterable[V]\",\n    reverse: bool = False,\n    case_sensitive: bool = False,\n    attribute: str | int | None = None,\n) -> \"list[V]\":\n    \"\"\"Sort an iterable using Python's :func:`sorted`.\n\n    .. sourcecode:: jinja\n\n        {% for city in cities|sort %}\n            ...\n        {% endfor %}\n\n    :param reverse: Sort descending instead of ascending.\n    :param case_sensitive: When sorting strings, sort upper and lower\n        case separately.\n    :param attribute: When sorting objects or dicts, an attribute or\n        key to sort by. Can use dot notation like ``\"address.city\"``.\n        Can be a list of attributes like ``\"age,name\"``.\n\n    The sort is stable, it does not change the relative order of\n    elements that compare equal. This makes it is possible to chain\n    sorts on different attributes and ordering.\n\n    .. sourcecode:: jinja\n\n        {% for user in users|sort(attribute=\"name\")\n            |sort(reverse=true, attribute=\"age\") %}\n            ...\n        {% endfor %}\n\n    As a shortcut to chaining when the direction is the same for all\n    attributes, pass a comma separate list of attributes.\n\n    .. sourcecode:: jinja\n\n        {% for user in users|sort(attribute=\"age,name\") %}\n            ...\n        {% endfor %}\n\n    .. versionchanged:: 2.11.0\n        The ``attribute`` parameter can be a comma separated list of\n        attributes, e.g. ``\"age,name\"``.\n\n    .. versionchanged:: 2.6\n       The ``attribute`` parameter was added.\n    \"\"\"\n    key_func = make_multi_attrgetter(\n        environment, attribute, postprocess=ignore_case if not case_sensitive else None\n    )\n    return sorted(value, key=key_func, reverse=reverse)\n\n\n@pass_environment\ndef sync_do_unique(\n    environment: \"Environment\",\n    value: \"t.Iterable[V]\",\n    case_sensitive: bool = False,\n    attribute: str | int | None = None,\n) -> \"t.Iterator[V]\":\n    \"\"\"Returns a list of unique items from the given iterable.\n\n    .. sourcecode:: jinja\n\n        {{ ['foo', 'bar', 'foobar', 'FooBar']|unique|list }}\n            -> ['foo', 'bar', 'foobar']\n\n    The unique items are yielded in the same order as their first occurrence in\n    the iterable passed to the filter.\n\n    :param case_sensitive: Treat upper and lower case strings as distinct.\n    :param attribute: Filter objects with unique values for this attribute.\n    \"\"\"\n    getter = make_attrgetter(\n        environment, attribute, postprocess=ignore_case if not case_sensitive else None\n    )\n    seen = set()\n\n    for item in value:\n        key = getter(item)\n\n        if key not in seen:\n            seen.add(key)\n            yield item\n\n\n@async_variant(sync_do_unique)  # type: ignore\nasync def do_unique(\n    environment: \"Environment\",\n    value: \"t.AsyncIterable[V] | t.Iterable[V]\",\n    case_sensitive: bool = False,\n    attribute: str | int | None = None,\n) -> \"t.Iterator[V]\":\n    return sync_do_unique(\n        environment, await auto_to_list(value), case_sensitive, attribute\n    )\n\n\ndef _min_or_max(\n    environment: \"Environment\",\n    value: \"t.Iterable[V]\",\n    func: \"t.Callable[..., V]\",\n    case_sensitive: bool,\n    attribute: str | int | None,\n) -> \"V | Undefined\":\n    it = iter(value)\n\n    try:\n        first = next(it)\n    except StopIteration:\n        return environment.undefined(\"No aggregated item, sequence was empty.\")\n\n    key_func = make_attrgetter(\n        environment, attribute, postprocess=ignore_case if not case_sensitive else None\n    )\n    return func(chain([first], it), key=key_func)\n\n\n@pass_environment\ndef do_min(\n    environment: \"Environment\",\n    value: \"t.Iterable[V]\",\n    case_sensitive: bool = False,\n    attribute: str | int | None = None,\n) -> \"V | Undefined\":\n    \"\"\"Return the smallest item from the sequence.\n\n    .. sourcecode:: jinja\n\n        {{ [1, 2, 3]|min }}\n            -> 1\n\n    :param case_sensitive: Treat upper and lower case strings as distinct.\n    :param attribute: Get the object with the min value of this attribute.\n    \"\"\"\n    return _min_or_max(environment, value, min, case_sensitive, attribute)\n\n\n@pass_environment\ndef do_max(\n    environment: \"Environment\",\n    value: \"t.Iterable[V]\",\n    case_sensitive: bool = False,\n    attribute: str | int | None = None,\n) -> \"V | Undefined\":\n    \"\"\"Return the largest item from the sequence.\n\n    .. sourcecode:: jinja\n\n        {{ [1, 2, 3]|max }}\n            -> 3\n\n    :param case_sensitive: Treat upper and lower case strings as distinct.\n    :param attribute: Get the object with the max value of this attribute.\n    \"\"\"\n    return _min_or_max(environment, value, max, case_sensitive, attribute)\n\n\ndef do_default(\n    value: V,\n    default_value: V = \"\",  # type: ignore\n    boolean: bool = False,\n) -> V:\n    \"\"\"If the value is undefined it will return the passed default value,\n    otherwise the value of the variable:\n\n    .. sourcecode:: jinja\n\n        {{ my_variable|default('my_variable is not defined') }}\n\n    This will output the value of ``my_variable`` if the variable was\n    defined, otherwise ``'my_variable is not defined'``. If you want\n    to use default with variables that evaluate to false you have to\n    set the second parameter to `true`:\n\n    .. sourcecode:: jinja\n\n        {{ ''|default('the string was empty', true) }}\n\n    .. versionchanged:: 2.11\n       It's now possible to configure the :class:`~jinja2.Environment` with\n       :class:`~jinja2.ChainableUndefined` to make the `default` filter work\n       on nested elements and attributes that may contain undefined values\n       in the chain without getting an :exc:`~jinja2.UndefinedError`.\n    \"\"\"\n    if isinstance(value, Undefined) or (boolean and not value):\n        return default_value\n\n    return value\n\n\n@pass_eval_context\ndef sync_do_join(\n    eval_ctx: \"EvalContext\",\n    value: t.Iterable[t.Any],\n    d: str = \"\",\n    attribute: str | int | None = None,\n) -> str:\n    \"\"\"Return a string which is the concatenation of the strings in the\n    sequence. The separator between elements is an empty string per\n    default, you can define it with the optional parameter:\n\n    .. sourcecode:: jinja\n\n        {{ [1, 2, 3]|join('|') }}\n            -> 1|2|3\n\n        {{ [1, 2, 3]|join }}\n            -> 123\n\n    It is also possible to join certain attributes of an object:\n\n    .. sourcecode:: jinja\n\n        {{ users|join(', ', attribute='username') }}\n\n    .. versionadded:: 2.6\n       The `attribute` parameter was added.\n    \"\"\"\n    if attribute is not None:\n        value = map(make_attrgetter(eval_ctx.environment, attribute), value)\n\n    # no automatic escaping?  joining is a lot easier then\n    if not eval_ctx.autoescape:\n        return str(d).join(map(str, value))\n\n    # if the delimiter doesn't have an html representation we check\n    # if any of the items has.  If yes we do a coercion to Markup\n    if not hasattr(d, \"__html__\"):\n        value = list(value)\n        do_escape = False\n\n        for idx, item in enumerate(value):\n            if hasattr(item, \"__html__\"):\n                do_escape = True\n            else:\n                value[idx] = str(item)\n\n        if do_escape:\n            d = escape(d)\n        else:\n            d = str(d)\n\n        return d.join(value)\n\n    # no html involved, to normal joining\n    return soft_str(d).join(map(soft_str, value))\n\n\n@async_variant(sync_do_join)  # type: ignore\nasync def do_join(\n    eval_ctx: \"EvalContext\",\n    value: t.AsyncIterable[t.Any] | t.Iterable[t.Any],\n    d: str = \"\",\n    attribute: str | int | None = None,\n) -> str:\n    return sync_do_join(eval_ctx, await auto_to_list(value), d, attribute)\n\n\ndef do_center(value: str, width: int = 80) -> str:\n    \"\"\"Centers the value in a field of a given width.\"\"\"\n    return soft_str(value).center(width)\n\n\n@pass_environment\ndef sync_do_first(environment: \"Environment\", seq: \"t.Iterable[V]\") -> \"V | Undefined\":\n    \"\"\"Return the first item of a sequence.\"\"\"\n    try:\n        return next(iter(seq))\n    except StopIteration:\n        return environment.undefined(\"No first item, sequence was empty.\")\n\n\n@async_variant(sync_do_first)  # type: ignore\nasync def do_first(\n    environment: \"Environment\", seq: \"t.AsyncIterable[V] | t.Iterable[V]\"\n) -> \"V | Undefined\":\n    try:\n        return await auto_aiter(seq).__anext__()\n    except StopAsyncIteration:\n        return environment.undefined(\"No first item, sequence was empty.\")\n\n\n@pass_environment\ndef do_last(environment: \"Environment\", seq: \"t.Reversible[V]\") -> \"V | Undefined\":\n    \"\"\"Return the last item of a sequence.\n\n    Note: Does not work with generators. You may want to explicitly\n    convert it to a list:\n\n    .. sourcecode:: jinja\n\n        {{ data | selectattr('name', '==', 'Jinja') | list | last }}\n    \"\"\"\n    try:\n        return next(iter(reversed(seq)))\n    except StopIteration:\n        return environment.undefined(\"No last item, sequence was empty.\")\n\n\n# No async do_last, it may not be safe in async mode.\n\n\n@pass_context\ndef do_random(context: \"Context\", seq: \"t.Sequence[V]\") -> \"V | Undefined\":\n    \"\"\"Return a random item from the sequence.\"\"\"\n    try:\n        return random.choice(seq)\n    except IndexError:\n        return context.environment.undefined(\"No random item, sequence was empty.\")\n\n\ndef do_filesizeformat(value: str | float | int, binary: bool = False) -> str:\n    \"\"\"Format the value like a 'human-readable' file size (i.e. 13 kB,\n    4.1 MB, 102 Bytes, etc).  Per default decimal prefixes are used (Mega,\n    Giga, etc.), if the second parameter is set to `True` the binary\n    prefixes are used (Mebi, Gibi).\n    \"\"\"\n    bytes = float(value)\n    base = 1024 if binary else 1000\n    prefixes = [\n        (\"KiB\" if binary else \"kB\"),\n        (\"MiB\" if binary else \"MB\"),\n        (\"GiB\" if binary else \"GB\"),\n        (\"TiB\" if binary else \"TB\"),\n        (\"PiB\" if binary else \"PB\"),\n        (\"EiB\" if binary else \"EB\"),\n        (\"ZiB\" if binary else \"ZB\"),\n        (\"YiB\" if binary else \"YB\"),\n    ]\n\n    if bytes == 1:\n        return \"1 Byte\"\n    elif bytes < base:\n        return f\"{int(bytes)} Bytes\"\n    else:\n        for i, prefix in enumerate(prefixes):\n            unit = base ** (i + 2)\n\n            if bytes < unit:\n                return f\"{base * bytes / unit:.1f} {prefix}\"\n\n        return f\"{base * bytes / unit:.1f} {prefix}\"\n\n\ndef do_pprint(value: t.Any) -> str:\n    \"\"\"Pretty print a variable. Useful for debugging.\"\"\"\n    return pformat(value)\n\n\n_uri_scheme_re = re.compile(r\"^([\\w.+-]{2,}:(/){0,2})$\")\n\n\n@pass_eval_context\ndef do_urlize(\n    eval_ctx: \"EvalContext\",\n    value: str,\n    trim_url_limit: int | None = None,\n    nofollow: bool = False,\n    target: str | None = None,\n    rel: str | None = None,\n    extra_schemes: t.Iterable[str] | None = None,\n) -> str:\n    \"\"\"Convert URLs in text into clickable links.\n\n    This may not recognize links in some situations. Usually, a more\n    comprehensive formatter, such as a Markdown library, is a better\n    choice.\n\n    Works on ``http://``, ``https://``, ``www.``, ``mailto:``, and email\n    addresses. Links with trailing punctuation (periods, commas, closing\n    parentheses) and leading punctuation (opening parentheses) are\n    recognized excluding the punctuation. Email addresses that include\n    header fields are not recognized (for example,\n    ``mailto:address@example.com?cc=copy@example.com``).\n\n    :param value: Original text containing URLs to link.\n    :param trim_url_limit: Shorten displayed URL values to this length.\n    :param nofollow: Add the ``rel=nofollow`` attribute to links.\n    :param target: Add the ``target`` attribute to links.\n    :param rel: Add the ``rel`` attribute to links.\n    :param extra_schemes: Recognize URLs that start with these schemes\n        in addition to the default behavior. Defaults to\n        ``env.policies[\"urlize.extra_schemes\"]``, which defaults to no\n        extra schemes.\n\n    .. versionchanged:: 3.0\n        The ``extra_schemes`` parameter was added.\n\n    .. versionchanged:: 3.0\n        Generate ``https://`` links for URLs without a scheme.\n\n    .. versionchanged:: 3.0\n        The parsing rules were updated. Recognize email addresses with\n        or without the ``mailto:`` scheme. Validate IP addresses. Ignore\n        parentheses and brackets in more cases.\n\n    .. versionchanged:: 2.8\n       The ``target`` parameter was added.\n    \"\"\"\n    policies = eval_ctx.environment.policies\n    rel_parts = set((rel or \"\").split())\n\n    if nofollow:\n        rel_parts.add(\"nofollow\")\n\n    rel_parts.update((policies[\"urlize.rel\"] or \"\").split())\n    rel = \" \".join(sorted(rel_parts)) or None\n\n    if target is None:\n        target = policies[\"urlize.target\"]\n\n    if extra_schemes is None:\n        extra_schemes = policies[\"urlize.extra_schemes\"] or ()\n\n    for scheme in extra_schemes:\n        if _uri_scheme_re.fullmatch(scheme) is None:\n            raise FilterArgumentError(f\"{scheme!r} is not a valid URI scheme prefix.\")\n\n    rv = urlize(\n        value,\n        trim_url_limit=trim_url_limit,\n        rel=rel,\n        target=target,\n        extra_schemes=extra_schemes,\n    )\n\n    if eval_ctx.autoescape:\n        rv = Markup(rv)\n\n    return rv\n\n\ndef do_indent(\n    s: str, width: int | str = 4, first: bool = False, blank: bool = False\n) -> str:\n    \"\"\"Return a copy of the string with each line indented by 4 spaces. The\n    first line and blank lines are not indented by default.\n\n    :param width: Number of spaces, or a string, to indent by.\n    :param first: Don't skip indenting the first line.\n    :param blank: Don't skip indenting empty lines.\n\n    .. versionchanged:: 3.0\n        ``width`` can be a string.\n\n    .. versionchanged:: 2.10\n        Blank lines are not indented by default.\n\n        Rename the ``indentfirst`` argument to ``first``.\n    \"\"\"\n    if isinstance(width, str):\n        indention = width\n    else:\n        indention = \" \" * width\n\n    newline = \"\\n\"\n\n    if isinstance(s, Markup):\n        indention = Markup(indention)\n        newline = Markup(newline)\n\n    s += newline  # this quirk is necessary for splitlines method\n\n    if blank:\n        rv = (newline + indention).join(s.splitlines())\n    else:\n        lines = s.splitlines()\n        rv = lines.pop(0)\n\n        if lines:\n            rv += newline + newline.join(\n                indention + line if line else line for line in lines\n            )\n\n    if first:\n        rv = indention + rv\n\n    return rv\n\n\n@pass_environment\ndef do_truncate(\n    env: \"Environment\",\n    s: str,\n    length: int = 255,\n    killwords: bool = False,\n    end: str = \"...\",\n    leeway: int | None = None,\n) -> str:\n    \"\"\"Return a truncated copy of the string. The length is specified\n    with the first parameter which defaults to ``255``. If the second\n    parameter is ``true`` the filter will cut the text at length. Otherwise\n    it will discard the last word. If the text was in fact\n    truncated it will append an ellipsis sign (``\"...\"``). If you want a\n    different ellipsis sign than ``\"...\"`` you can specify it using the\n    third parameter. Strings that only exceed the length by the tolerance\n    margin given in the fourth parameter will not be truncated.\n\n    .. sourcecode:: jinja\n\n        {{ \"foo bar baz qux\"|truncate(9) }}\n            -> \"foo...\"\n        {{ \"foo bar baz qux\"|truncate(9, True) }}\n            -> \"foo ba...\"\n        {{ \"foo bar baz qux\"|truncate(11) }}\n            -> \"foo bar baz qux\"\n        {{ \"foo bar baz qux\"|truncate(11, False, '...', 0) }}\n            -> \"foo bar...\"\n\n    The default leeway on newer Jinja versions is 5 and was 0 before but\n    can be reconfigured globally.\n    \"\"\"\n    if leeway is None:\n        leeway = env.policies[\"truncate.leeway\"]\n\n    assert length >= len(end), f\"expected length >= {len(end)}, got {length}\"\n    assert leeway >= 0, f\"expected leeway >= 0, got {leeway}\"\n\n    if len(s) <= length + leeway:\n        return s\n\n    if killwords:\n        return s[: length - len(end)] + end\n\n    result = s[: length - len(end)].rsplit(\" \", 1)[0]\n    return result + end\n\n\n@pass_environment\ndef do_wordwrap(\n    environment: \"Environment\",\n    s: str,\n    width: int = 79,\n    break_long_words: bool = True,\n    wrapstring: str | None = None,\n    break_on_hyphens: bool = True,\n) -> str:\n    \"\"\"Wrap a string to the given width. Existing newlines are treated\n    as paragraphs to be wrapped separately.\n\n    :param s: Original text to wrap.\n    :param width: Maximum length of wrapped lines.\n    :param break_long_words: If a word is longer than ``width``, break\n        it across lines.\n    :param break_on_hyphens: If a word contains hyphens, it may be split\n        across lines.\n    :param wrapstring: String to join each wrapped line. Defaults to\n        :attr:`Environment.newline_sequence`.\n\n    .. versionchanged:: 2.11\n        Existing newlines are treated as paragraphs wrapped separately.\n\n    .. versionchanged:: 2.11\n        Added the ``break_on_hyphens`` parameter.\n\n    .. versionchanged:: 2.7\n        Added the ``wrapstring`` parameter.\n    \"\"\"\n    import textwrap\n\n    if wrapstring is None:\n        wrapstring = environment.newline_sequence\n\n    # textwrap.wrap doesn't consider existing newlines when wrapping.\n    # If the string has a newline before width, wrap will still insert\n    # a newline at width, resulting in a short line. Instead, split and\n    # wrap each paragraph individually.\n    return wrapstring.join(\n        [\n            wrapstring.join(\n                textwrap.wrap(\n                    line,\n                    width=width,\n                    expand_tabs=False,\n                    replace_whitespace=False,\n                    break_long_words=break_long_words,\n                    break_on_hyphens=break_on_hyphens,\n                )\n            )\n            for line in s.splitlines()\n        ]\n    )\n\n\n_word_re = re.compile(r\"\\w+\")\n\n\ndef do_wordcount(s: str) -> int:\n    \"\"\"Count the words in that string.\"\"\"\n    return len(_word_re.findall(soft_str(s)))\n\n\ndef do_int(value: t.Any, default: int = 0, base: int = 10) -> int:\n    \"\"\"Convert the value into an integer. If the\n    conversion doesn't work it will return ``0``. You can\n    override this default using the first parameter. You\n    can also override the default base (10) in the second\n    parameter, which handles input with prefixes such as\n    0b, 0o and 0x for bases 2, 8 and 16 respectively.\n    The base is ignored for decimal numbers and non-string values.\n    \"\"\"\n    try:\n        if isinstance(value, str):\n            return int(value, base)\n\n        return int(value)\n    except (TypeError, ValueError):\n        # this quirk is necessary so that \"42.23\"|int gives 42.\n        try:\n            return int(float(value))\n        except (TypeError, ValueError, OverflowError):\n            return default\n\n\ndef do_float(value: t.Any, default: float = 0.0) -> float:\n    \"\"\"Convert the value into a floating point number. If the\n    conversion doesn't work it will return ``0.0``. You can\n    override this default using the first parameter.\n    \"\"\"\n    try:\n        return float(value)\n    except (TypeError, ValueError):\n        return default\n\n\ndef do_format(value: str, *args: t.Any, **kwargs: t.Any) -> str:\n    \"\"\"Apply the given values to a `printf-style`_ format string, like\n    ``string % values``.\n\n    .. sourcecode:: jinja\n\n        {{ \"%s, %s!\"|format(greeting, name) }}\n        Hello, World!\n\n    In most cases it should be more convenient and efficient to use the\n    ``%`` operator or :meth:`str.format`.\n\n    .. code-block:: text\n\n        {{ \"%s, %s!\" % (greeting, name) }}\n        {{ \"{}, {}!\".format(greeting, name) }}\n\n    .. _printf-style: https://docs.python.org/library/stdtypes.html\n        #printf-style-string-formatting\n    \"\"\"\n    if args and kwargs:\n        raise FilterArgumentError(\n            \"can't handle positional and keyword arguments at the same time\"\n        )\n\n    return soft_str(value) % (kwargs or args)\n\n\ndef do_trim(value: str, chars: str | None = None) -> str:\n    \"\"\"Strip leading and trailing characters, by default whitespace.\"\"\"\n    return soft_str(value).strip(chars)\n\n\ndef do_striptags(value: \"str | HasHTML\") -> str:\n    \"\"\"Strip SGML/XML tags and replace adjacent whitespace by one space.\"\"\"\n    if hasattr(value, \"__html__\"):\n        value = t.cast(\"HasHTML\", value).__html__()\n\n    return Markup(str(value)).striptags()\n\n\ndef sync_do_slice(\n    value: \"t.Collection[V]\", slices: int, fill_with: \"V | None\" = None\n) -> \"t.Iterator[list[V]]\":\n    \"\"\"Slice an iterator and return a list of lists containing\n    those items. Useful if you want to create a div containing\n    three ul tags that represent columns:\n\n    .. sourcecode:: html+jinja\n\n        <div class=\"columnwrapper\">\n          {%- for column in items|slice(3) %}\n            <ul class=\"column-{{ loop.index }}\">\n            {%- for item in column %}\n              <li>{{ item }}</li>\n            {%- endfor %}\n            </ul>\n          {%- endfor %}\n        </div>\n\n    If you pass it a second argument it's used to fill missing\n    values on the last iteration.\n    \"\"\"\n    seq = list(value)\n    length = len(seq)\n    items_per_slice = length // slices\n    slices_with_extra = length % slices\n    offset = 0\n\n    for slice_number in range(slices):\n        start = offset + slice_number * items_per_slice\n\n        if slice_number < slices_with_extra:\n            offset += 1\n\n        end = offset + (slice_number + 1) * items_per_slice\n        tmp = seq[start:end]\n\n        if fill_with is not None and slice_number >= slices_with_extra:\n            tmp.append(fill_with)\n\n        yield tmp\n\n\n@async_variant(sync_do_slice)  # type: ignore\nasync def do_slice(\n    value: \"t.AsyncIterable[V] | t.Iterable[V]\",\n    slices: int,\n    fill_with: t.Any | None = None,\n) -> \"t.Iterator[list[V]]\":\n    return sync_do_slice(await auto_to_list(value), slices, fill_with)\n\n\ndef do_batch(\n    value: \"t.Iterable[V]\", linecount: int, fill_with: \"V | None\" = None\n) -> \"t.Iterator[list[V]]\":\n    \"\"\"\n    A filter that batches items. It works pretty much like `slice`\n    just the other way round. It returns a list of lists with the\n    given number of items. If you provide a second parameter this\n    is used to fill up missing items. See this example:\n\n    .. sourcecode:: html+jinja\n\n        <table>\n        {%- for row in items|batch(3, '&nbsp;') %}\n          <tr>\n          {%- for column in row %}\n            <td>{{ column }}</td>\n          {%- endfor %}\n          </tr>\n        {%- endfor %}\n        </table>\n    \"\"\"\n    tmp: list[V] = []\n\n    for item in value:\n        if len(tmp) == linecount:\n            yield tmp\n            tmp = []\n\n        tmp.append(item)\n\n    if tmp:\n        if fill_with is not None and len(tmp) < linecount:\n            tmp += [fill_with] * (linecount - len(tmp))\n\n        yield tmp\n\n\ndef do_round(\n    value: float,\n    precision: int = 0,\n    method: 'te.Literal[\"common\", \"ceil\", \"floor\"]' = \"common\",\n) -> float:\n    \"\"\"Round the number to a given precision. The first\n    parameter specifies the precision (default is ``0``), the\n    second the rounding method:\n\n    - ``'common'`` rounds either up or down\n    - ``'ceil'`` always rounds up\n    - ``'floor'`` always rounds down\n\n    If you don't specify a method ``'common'`` is used.\n\n    .. sourcecode:: jinja\n\n        {{ 42.55|round }}\n            -> 43.0\n        {{ 42.55|round(1, 'floor') }}\n            -> 42.5\n\n    Note that even if rounded to 0 precision, a float is returned.  If\n    you need a real integer, pipe it through `int`:\n\n    .. sourcecode:: jinja\n\n        {{ 42.55|round|int }}\n            -> 43\n    \"\"\"\n    if method not in {\"common\", \"ceil\", \"floor\"}:\n        raise FilterArgumentError(\"method must be common, ceil or floor\")\n\n    if method == \"common\":\n        return round(value, precision)\n\n    func = getattr(math, method)\n    return t.cast(float, func(value * (10**precision)) / (10**precision))\n\n\nclass _GroupTuple(t.NamedTuple):\n    grouper: t.Any\n    list: list[t.Any]\n\n    # Use the regular tuple repr to hide this subclass if users print\n    # out the value during debugging.\n    def __repr__(self) -> str:\n        return tuple.__repr__(self)\n\n    def __str__(self) -> str:\n        return tuple.__str__(self)\n\n\n@pass_environment\ndef sync_do_groupby(\n    environment: \"Environment\",\n    value: \"t.Iterable[V]\",\n    attribute: str | int,\n    default: t.Any | None = None,\n    case_sensitive: bool = False,\n) -> \"list[_GroupTuple]\":\n    \"\"\"Group a sequence of objects by an attribute using Python's\n    :func:`itertools.groupby`. The attribute can use dot notation for\n    nested access, like ``\"address.city\"``. Unlike Python's ``groupby``,\n    the values are sorted first so only one group is returned for each\n    unique value.\n\n    For example, a list of ``User`` objects with a ``city`` attribute\n    can be rendered in groups. In this example, ``grouper`` refers to\n    the ``city`` value of the group.\n\n    .. sourcecode:: html+jinja\n\n        <ul>{% for city, items in users|groupby(\"city\") %}\n          <li>{{ city }}\n            <ul>{% for user in items %}\n              <li>{{ user.name }}\n            {% endfor %}</ul>\n          </li>\n        {% endfor %}</ul>\n\n    ``groupby`` yields namedtuples of ``(grouper, list)``, which\n    can be used instead of the tuple unpacking above. ``grouper`` is the\n    value of the attribute, and ``list`` is the items with that value.\n\n    .. sourcecode:: html+jinja\n\n        <ul>{% for group in users|groupby(\"city\") %}\n          <li>{{ group.grouper }}: {{ group.list|join(\", \") }}\n        {% endfor %}</ul>\n\n    You can specify a ``default`` value to use if an object in the list\n    does not have the given attribute.\n\n    .. sourcecode:: jinja\n\n        <ul>{% for city, items in users|groupby(\"city\", default=\"NY\") %}\n          <li>{{ city }}: {{ items|map(attribute=\"name\")|join(\", \") }}</li>\n        {% endfor %}</ul>\n\n    Like the :func:`~jinja-filters.sort` filter, sorting and grouping is\n    case-insensitive by default. The ``key`` for each group will have\n    the case of the first item in that group of values. For example, if\n    a list of users has cities ``[\"CA\", \"NY\", \"ca\"]``, the \"CA\" group\n    will have two values. This can be disabled by passing\n    ``case_sensitive=True``.\n\n    .. versionchanged:: 3.1\n        Added the ``case_sensitive`` parameter. Sorting and grouping is\n        case-insensitive by default, matching other filters that do\n        comparisons.\n\n    .. versionchanged:: 3.0\n        Added the ``default`` parameter.\n\n    .. versionchanged:: 2.6\n        The attribute supports dot notation for nested access.\n    \"\"\"\n    expr = make_attrgetter(\n        environment,\n        attribute,\n        postprocess=ignore_case if not case_sensitive else None,\n        default=default,\n    )\n    out = [\n        _GroupTuple(key, list(values))\n        for key, values in groupby(sorted(value, key=expr), expr)\n    ]\n\n    if not case_sensitive:\n        # Return the real key from the first value instead of the lowercase key.\n        output_expr = make_attrgetter(environment, attribute, default=default)\n        out = [_GroupTuple(output_expr(values[0]), values) for _, values in out]\n\n    return out\n\n\n@async_variant(sync_do_groupby)  # type: ignore\nasync def do_groupby(\n    environment: \"Environment\",\n    value: \"t.AsyncIterable[V] | t.Iterable[V]\",\n    attribute: str | int,\n    default: t.Any | None = None,\n    case_sensitive: bool = False,\n) -> \"list[_GroupTuple]\":\n    expr = make_attrgetter(\n        environment,\n        attribute,\n        postprocess=ignore_case if not case_sensitive else None,\n        default=default,\n    )\n    out = [\n        _GroupTuple(key, await auto_to_list(values))\n        for key, values in groupby(sorted(await auto_to_list(value), key=expr), expr)\n    ]\n\n    if not case_sensitive:\n        # Return the real key from the first value instead of the lowercase key.\n        output_expr = make_attrgetter(environment, attribute, default=default)\n        out = [_GroupTuple(output_expr(values[0]), values) for _, values in out]\n\n    return out\n\n\n@pass_environment\ndef sync_do_sum(\n    environment: \"Environment\",\n    iterable: \"t.Iterable[V]\",\n    attribute: str | int | None = None,\n    start: V = 0,  # type: ignore\n) -> V:\n    \"\"\"Returns the sum of a sequence of numbers plus the value of parameter\n    'start' (which defaults to 0).  When the sequence is empty it returns\n    start.\n\n    It is also possible to sum up only certain attributes:\n\n    .. sourcecode:: jinja\n\n        Total: {{ items|sum(attribute='price') }}\n\n    .. versionchanged:: 2.6\n       The ``attribute`` parameter was added to allow summing up over\n       attributes.  Also the ``start`` parameter was moved on to the right.\n    \"\"\"\n    if attribute is not None:\n        iterable = map(make_attrgetter(environment, attribute), iterable)\n\n    return sum(iterable, start)  # type: ignore[no-any-return, call-overload]\n\n\n@async_variant(sync_do_sum)  # type: ignore\nasync def do_sum(\n    environment: \"Environment\",\n    iterable: \"t.AsyncIterable[V] | t.Iterable[V]\",\n    attribute: str | int | None = None,\n    start: V = 0,  # type: ignore\n) -> V:\n    rv = start\n\n    if attribute is not None:\n        func = make_attrgetter(environment, attribute)\n    else:\n\n        def func(x: V) -> V:\n            return x\n\n    async for item in auto_aiter(iterable):\n        rv += func(item)\n\n    return rv\n\n\ndef sync_do_list(value: \"t.Iterable[V]\") -> \"list[V]\":\n    \"\"\"Convert the value into a list.  If it was a string the returned list\n    will be a list of characters.\n    \"\"\"\n    return list(value)\n\n\n@async_variant(sync_do_list)  # type: ignore\nasync def do_list(value: \"t.AsyncIterable[V] | t.Iterable[V]\") -> \"list[V]\":\n    return await auto_to_list(value)\n\n\ndef do_mark_safe(value: str) -> Markup:\n    \"\"\"Mark the value as safe which means that in an environment with automatic\n    escaping enabled this variable will not be escaped.\n    \"\"\"\n    return Markup(value)\n\n\ndef do_mark_unsafe(value: str) -> str:\n    \"\"\"Mark a value as unsafe.  This is the reverse operation for :func:`safe`.\"\"\"\n    return str(value)\n\n\n@typing.overload\ndef do_reverse(value: str) -> str: ...\n\n\n@typing.overload\ndef do_reverse(value: \"t.Iterable[V]\") -> \"t.Iterable[V]\": ...\n\n\ndef do_reverse(value: str | t.Iterable[V]) -> str | t.Iterable[V]:\n    \"\"\"Reverse the object or return an iterator that iterates over it the other\n    way round.\n    \"\"\"\n    if isinstance(value, str):\n        return value[::-1]\n\n    try:\n        return reversed(value)  # type: ignore\n    except TypeError:\n        try:\n            rv = list(value)\n            rv.reverse()\n            return rv\n        except TypeError as e:\n            raise FilterArgumentError(\"argument must be iterable\") from e\n\n\n@pass_environment\ndef do_attr(environment: \"Environment\", obj: t.Any, name: str) -> Undefined | t.Any:\n    \"\"\"Get an attribute of an object. ``foo|attr(\"bar\")`` works like\n    ``foo.bar``, but returns undefined instead of falling back to ``foo[\"bar\"]``\n    if the attribute doesn't exist.\n\n    See :ref:`Notes on subscriptions <notes-on-subscriptions>` for more details.\n    \"\"\"\n    # Environment.getattr will fall back to obj[name] if obj.name doesn't exist.\n    # But we want to call env.getattr to get behavior such as sandboxing.\n    # Determine if the attr exists first, so we know the fallback won't trigger.\n    try:\n        # This avoids executing properties/descriptors, but misses __getattr__\n        # and __getattribute__ dynamic attrs.\n        getattr_static(obj, name)\n    except AttributeError:\n        # This finds dynamic attrs, and we know it's not a descriptor at this point.\n        if not hasattr(obj, name):\n            return environment.undefined(obj=obj, name=name)\n\n    return environment.getattr(obj, name)\n\n\n@typing.overload\ndef sync_do_map(\n    context: \"Context\",\n    value: t.Iterable[t.Any],\n    name: str,\n    *args: t.Any,\n    **kwargs: t.Any,\n) -> t.Iterable[t.Any]: ...\n\n\n@typing.overload\ndef sync_do_map(\n    context: \"Context\",\n    value: t.Iterable[t.Any],\n    *,\n    attribute: str = ...,\n    default: t.Any | None = None,\n) -> t.Iterable[t.Any]: ...\n\n\n@pass_context\ndef sync_do_map(\n    context: \"Context\", value: t.Iterable[t.Any], *args: t.Any, **kwargs: t.Any\n) -> t.Iterable[t.Any]:\n    \"\"\"Applies a filter on a sequence of objects or looks up an attribute.\n    This is useful when dealing with lists of objects but you are really\n    only interested in a certain value of it.\n\n    The basic usage is mapping on an attribute.  Imagine you have a list\n    of users but you are only interested in a list of usernames:\n\n    .. sourcecode:: jinja\n\n        Users on this page: {{ users|map(attribute='username')|join(', ') }}\n\n    You can specify a ``default`` value to use if an object in the list\n    does not have the given attribute.\n\n    .. sourcecode:: jinja\n\n        {{ users|map(attribute=\"username\", default=\"Anonymous\")|join(\", \") }}\n\n    Alternatively you can let it invoke a filter by passing the name of the\n    filter and the arguments afterwards.  A good example would be applying a\n    text conversion filter on a sequence:\n\n    .. sourcecode:: jinja\n\n        Users on this page: {{ titles|map('lower')|join(', ') }}\n\n    Similar to a generator comprehension such as:\n\n    .. code-block:: python\n\n        (u.username for u in users)\n        (getattr(u, \"username\", \"Anonymous\") for u in users)\n        (do_lower(x) for x in titles)\n\n    .. versionchanged:: 2.11.0\n        Added the ``default`` parameter.\n\n    .. versionadded:: 2.7\n    \"\"\"\n    if value:\n        func = prepare_map(context, args, kwargs)\n\n        for item in value:\n            yield func(item)\n\n\n@typing.overload\ndef do_map(\n    context: \"Context\",\n    value: t.AsyncIterable[t.Any] | t.Iterable[t.Any],\n    name: str,\n    *args: t.Any,\n    **kwargs: t.Any,\n) -> t.Iterable[t.Any]: ...\n\n\n@typing.overload\ndef do_map(\n    context: \"Context\",\n    value: t.AsyncIterable[t.Any] | t.Iterable[t.Any],\n    *,\n    attribute: str = ...,\n    default: t.Any | None = None,\n) -> t.Iterable[t.Any]: ...\n\n\n@async_variant(sync_do_map)  # type: ignore\nasync def do_map(\n    context: \"Context\",\n    value: t.AsyncIterable[t.Any] | t.Iterable[t.Any],\n    *args: t.Any,\n    **kwargs: t.Any,\n) -> t.AsyncIterable[t.Any]:\n    if value:\n        func = prepare_map(context, args, kwargs)\n\n        async for item in auto_aiter(value):\n            yield await auto_await(func(item))\n\n\n@pass_context\ndef sync_do_select(\n    context: \"Context\", value: \"t.Iterable[V]\", *args: t.Any, **kwargs: t.Any\n) -> \"t.Iterator[V]\":\n    \"\"\"Filters a sequence of objects by applying a test to each object,\n    and only selecting the objects with the test succeeding.\n\n    If no test is specified, each object will be evaluated as a boolean.\n\n    Example usage:\n\n    .. sourcecode:: jinja\n\n        {{ numbers|select(\"odd\") }}\n        {{ numbers|select(\"odd\") }}\n        {{ numbers|select(\"divisibleby\", 3) }}\n        {{ numbers|select(\"lessthan\", 42) }}\n        {{ strings|select(\"equalto\", \"mystring\") }}\n\n    Similar to a generator comprehension such as:\n\n    .. code-block:: python\n\n        (n for n in numbers if test_odd(n))\n        (n for n in numbers if test_divisibleby(n, 3))\n\n    .. versionadded:: 2.7\n    \"\"\"\n    return select_or_reject(context, value, args, kwargs, lambda x: x, False)\n\n\n@async_variant(sync_do_select)  # type: ignore\nasync def do_select(\n    context: \"Context\",\n    value: \"t.AsyncIterable[V] | t.Iterable[V]\",\n    *args: t.Any,\n    **kwargs: t.Any,\n) -> \"t.AsyncIterator[V]\":\n    return async_select_or_reject(context, value, args, kwargs, lambda x: x, False)\n\n\n@pass_context\ndef sync_do_reject(\n    context: \"Context\", value: \"t.Iterable[V]\", *args: t.Any, **kwargs: t.Any\n) -> \"t.Iterator[V]\":\n    \"\"\"Filters a sequence of objects by applying a test to each object,\n    and rejecting the objects with the test succeeding.\n\n    If no test is specified, each object will be evaluated as a boolean.\n\n    Example usage:\n\n    .. sourcecode:: jinja\n\n        {{ numbers|reject(\"odd\") }}\n\n    Similar to a generator comprehension such as:\n\n    .. code-block:: python\n\n        (n for n in numbers if not test_odd(n))\n\n    .. versionadded:: 2.7\n    \"\"\"\n    return select_or_reject(context, value, args, kwargs, lambda x: not x, False)\n\n\n@async_variant(sync_do_reject)  # type: ignore\nasync def do_reject(\n    context: \"Context\",\n    value: \"t.AsyncIterable[V] | t.Iterable[V]\",\n    *args: t.Any,\n    **kwargs: t.Any,\n) -> \"t.AsyncIterator[V]\":\n    return async_select_or_reject(context, value, args, kwargs, lambda x: not x, False)\n\n\n@pass_context\ndef sync_do_selectattr(\n    context: \"Context\", value: \"t.Iterable[V]\", *args: t.Any, **kwargs: t.Any\n) -> \"t.Iterator[V]\":\n    \"\"\"Filters a sequence of objects by applying a test to the specified\n    attribute of each object, and only selecting the objects with the\n    test succeeding.\n\n    If no test is specified, the attribute's value will be evaluated as\n    a boolean.\n\n    Example usage:\n\n    .. sourcecode:: jinja\n\n        {{ users|selectattr(\"is_active\") }}\n        {{ users|selectattr(\"email\", \"none\") }}\n\n    Similar to a generator comprehension such as:\n\n    .. code-block:: python\n\n        (user for user in users if user.is_active)\n        (user for user in users if test_none(user.email))\n\n    .. versionadded:: 2.7\n    \"\"\"\n    return select_or_reject(context, value, args, kwargs, lambda x: x, True)\n\n\n@async_variant(sync_do_selectattr)  # type: ignore\nasync def do_selectattr(\n    context: \"Context\",\n    value: \"t.AsyncIterable[V] | t.Iterable[V]\",\n    *args: t.Any,\n    **kwargs: t.Any,\n) -> \"t.AsyncIterator[V]\":\n    return async_select_or_reject(context, value, args, kwargs, lambda x: x, True)\n\n\n@pass_context\ndef sync_do_rejectattr(\n    context: \"Context\", value: \"t.Iterable[V]\", *args: t.Any, **kwargs: t.Any\n) -> \"t.Iterator[V]\":\n    \"\"\"Filters a sequence of objects by applying a test to the specified\n    attribute of each object, and rejecting the objects with the test\n    succeeding.\n\n    If no test is specified, the attribute's value will be evaluated as\n    a boolean.\n\n    .. sourcecode:: jinja\n\n        {{ users|rejectattr(\"is_active\") }}\n        {{ users|rejectattr(\"email\", \"none\") }}\n\n    Similar to a generator comprehension such as:\n\n    .. code-block:: python\n\n        (user for user in users if not user.is_active)\n        (user for user in users if not test_none(user.email))\n\n    .. versionadded:: 2.7\n    \"\"\"\n    return select_or_reject(context, value, args, kwargs, lambda x: not x, True)\n\n\n@async_variant(sync_do_rejectattr)  # type: ignore\nasync def do_rejectattr(\n    context: \"Context\",\n    value: \"t.AsyncIterable[V] | t.Iterable[V]\",\n    *args: t.Any,\n    **kwargs: t.Any,\n) -> \"t.AsyncIterator[V]\":\n    return async_select_or_reject(context, value, args, kwargs, lambda x: not x, True)\n\n\n@pass_eval_context\ndef do_tojson(\n    eval_ctx: \"EvalContext\", value: t.Any, indent: int | None = None\n) -> Markup:\n    \"\"\"Serialize an object to a string of JSON, and mark it safe to\n    render in HTML. This filter is only for use in HTML documents.\n\n    The returned string is safe to render in HTML documents and\n    ``<script>`` tags. The exception is in HTML attributes that are\n    double quoted; either use single quotes or the ``|forceescape``\n    filter.\n\n    :param value: The object to serialize to JSON.\n    :param indent: The ``indent`` parameter passed to ``dumps``, for\n        pretty-printing the value.\n\n    .. versionadded:: 2.9\n    \"\"\"\n    policies = eval_ctx.environment.policies\n    dumps = policies[\"json.dumps_function\"]\n    kwargs = policies[\"json.dumps_kwargs\"]\n\n    if indent is not None:\n        kwargs = kwargs.copy()\n        kwargs[\"indent\"] = indent\n\n    return htmlsafe_json_dumps(value, dumps=dumps, **kwargs)\n\n\ndef prepare_map(\n    context: \"Context\", args: tuple[t.Any, ...], kwargs: dict[str, t.Any]\n) -> t.Callable[[t.Any], t.Any]:\n    if not args and \"attribute\" in kwargs:\n        attribute = kwargs.pop(\"attribute\")\n        default = kwargs.pop(\"default\", None)\n\n        if kwargs:\n            raise FilterArgumentError(\n                f\"Unexpected keyword argument {next(iter(kwargs))!r}\"\n            )\n\n        func = make_attrgetter(context.environment, attribute, default=default)\n    else:\n        try:\n            name = args[0]\n            args = args[1:]\n        except LookupError:\n            raise FilterArgumentError(\"map requires a filter argument\") from None\n\n        def func(item: t.Any) -> t.Any:\n            return context.environment.call_filter(\n                name, item, args, kwargs, context=context\n            )\n\n    return func\n\n\ndef prepare_select_or_reject(\n    context: \"Context\",\n    args: tuple[t.Any, ...],\n    kwargs: dict[str, t.Any],\n    modfunc: t.Callable[[t.Any], t.Any],\n    lookup_attr: bool,\n) -> t.Callable[[t.Any], t.Any]:\n    if lookup_attr:\n        try:\n            attr = args[0]\n        except LookupError:\n            raise FilterArgumentError(\"Missing parameter for attribute name\") from None\n\n        transfunc = make_attrgetter(context.environment, attr)\n        off = 1\n    else:\n        off = 0\n\n        def transfunc(x: V) -> V:\n            return x\n\n    try:\n        name = args[off]\n        args = args[1 + off :]\n\n        def func(item: t.Any) -> t.Any:\n            return context.environment.call_test(name, item, args, kwargs, context)\n\n    except LookupError:\n        func = bool  # type: ignore\n\n    return lambda item: modfunc(func(transfunc(item)))\n\n\ndef select_or_reject(\n    context: \"Context\",\n    value: \"t.Iterable[V]\",\n    args: tuple[t.Any, ...],\n    kwargs: dict[str, t.Any],\n    modfunc: t.Callable[[t.Any], t.Any],\n    lookup_attr: bool,\n) -> \"t.Iterator[V]\":\n    if value:\n        func = prepare_select_or_reject(context, args, kwargs, modfunc, lookup_attr)\n\n        for item in value:\n            if func(item):\n                yield item\n\n\nasync def async_select_or_reject(\n    context: \"Context\",\n    value: \"t.AsyncIterable[V] | t.Iterable[V]\",\n    args: tuple[t.Any, ...],\n    kwargs: dict[str, t.Any],\n    modfunc: t.Callable[[t.Any], t.Any],\n    lookup_attr: bool,\n) -> \"t.AsyncIterator[V]\":\n    if value:\n        func = prepare_select_or_reject(context, args, kwargs, modfunc, lookup_attr)\n\n        async for item in auto_aiter(value):\n            if func(item):\n                yield item\n\n\nFILTERS = {\n    \"abs\": abs,\n    \"attr\": do_attr,\n    \"batch\": do_batch,\n    \"capitalize\": do_capitalize,\n    \"center\": do_center,\n    \"count\": len,\n    \"d\": do_default,\n    \"default\": do_default,\n    \"dictsort\": do_dictsort,\n    \"e\": escape,\n    \"escape\": escape,\n    \"filesizeformat\": do_filesizeformat,\n    \"first\": do_first,\n    \"float\": do_float,\n    \"forceescape\": do_forceescape,\n    \"format\": do_format,\n    \"groupby\": do_groupby,\n    \"indent\": do_indent,\n    \"int\": do_int,\n    \"join\": do_join,\n    \"last\": do_last,\n    \"length\": len,\n    \"list\": do_list,\n    \"lower\": do_lower,\n    \"items\": do_items,\n    \"map\": do_map,\n    \"min\": do_min,\n    \"max\": do_max,\n    \"pprint\": do_pprint,\n    \"random\": do_random,\n    \"reject\": do_reject,\n    \"rejectattr\": do_rejectattr,\n    \"replace\": do_replace,\n    \"reverse\": do_reverse,\n    \"round\": do_round,\n    \"safe\": do_mark_safe,\n    \"select\": do_select,\n    \"selectattr\": do_selectattr,\n    \"slice\": do_slice,\n    \"sort\": do_sort,\n    \"string\": soft_str,\n    \"striptags\": do_striptags,\n    \"sum\": do_sum,\n    \"title\": do_title,\n    \"trim\": do_trim,\n    \"truncate\": do_truncate,\n    \"unique\": do_unique,\n    \"upper\": do_upper,\n    \"urlencode\": do_urlencode,\n    \"urlize\": do_urlize,\n    \"wordcount\": do_wordcount,\n    \"wordwrap\": do_wordwrap,\n    \"xmlattr\": do_xmlattr,\n    \"tojson\": do_tojson,\n}\n"
  },
  {
    "path": "src/jinja2/idtracking.py",
    "content": "import typing as t\n\nfrom . import nodes\nfrom .visitor import NodeVisitor\n\nif t.TYPE_CHECKING:\n    import typing_extensions as te\n\nVAR_LOAD_PARAMETER = \"param\"\nVAR_LOAD_RESOLVE = \"resolve\"\nVAR_LOAD_ALIAS = \"alias\"\nVAR_LOAD_UNDEFINED = \"undefined\"\n\n\ndef find_symbols(\n    nodes: t.Iterable[nodes.Node], parent_symbols: t.Optional[\"Symbols\"] = None\n) -> \"Symbols\":\n    sym = Symbols(parent=parent_symbols)\n    visitor = FrameSymbolVisitor(sym)\n    for node in nodes:\n        visitor.visit(node)\n    return sym\n\n\ndef symbols_for_node(\n    node: nodes.Node, parent_symbols: t.Optional[\"Symbols\"] = None\n) -> \"Symbols\":\n    sym = Symbols(parent=parent_symbols)\n    sym.analyze_node(node)\n    return sym\n\n\nclass Symbols:\n    def __init__(\n        self, parent: t.Optional[\"Symbols\"] = None, level: int | None = None\n    ) -> None:\n        if level is None:\n            if parent is None:\n                level = 0\n            else:\n                level = parent.level + 1\n\n        self.level: int = level\n        self.parent = parent\n        self.refs: dict[str, str] = {}\n        self.loads: dict[str, t.Any] = {}\n        self.stores: set[str] = set()\n\n    def analyze_node(self, node: nodes.Node, **kwargs: t.Any) -> None:\n        visitor = RootVisitor(self)\n        visitor.visit(node, **kwargs)\n\n    def _define_ref(self, name: str, load: tuple[str, str | None] | None = None) -> str:\n        ident = f\"l_{self.level}_{name}\"\n        self.refs[name] = ident\n        if load is not None:\n            self.loads[ident] = load\n        return ident\n\n    def find_load(self, target: str) -> t.Any | None:\n        if target in self.loads:\n            return self.loads[target]\n\n        if self.parent is not None:\n            return self.parent.find_load(target)\n\n        return None\n\n    def find_ref(self, name: str) -> str | None:\n        if name in self.refs:\n            return self.refs[name]\n\n        if self.parent is not None:\n            return self.parent.find_ref(name)\n\n        return None\n\n    def ref(self, name: str) -> str:\n        rv = self.find_ref(name)\n        if rv is None:\n            raise AssertionError(\n                \"Tried to resolve a name to a reference that was\"\n                f\" unknown to the frame ({name!r})\"\n            )\n        return rv\n\n    def copy(self) -> \"te.Self\":\n        rv = object.__new__(self.__class__)\n        rv.__dict__.update(self.__dict__)\n        rv.refs = self.refs.copy()\n        rv.loads = self.loads.copy()\n        rv.stores = self.stores.copy()\n        return rv\n\n    def store(self, name: str) -> None:\n        self.stores.add(name)\n\n        # If we have not see the name referenced yet, we need to figure\n        # out what to set it to.\n        if name not in self.refs:\n            # If there is a parent scope we check if the name has a\n            # reference there.  If it does it means we might have to alias\n            # to a variable there.\n            if self.parent is not None:\n                outer_ref = self.parent.find_ref(name)\n                if outer_ref is not None:\n                    self._define_ref(name, load=(VAR_LOAD_ALIAS, outer_ref))\n                    return\n\n            # Otherwise we can just set it to undefined.\n            self._define_ref(name, load=(VAR_LOAD_UNDEFINED, None))\n\n    def declare_parameter(self, name: str) -> str:\n        self.stores.add(name)\n        return self._define_ref(name, load=(VAR_LOAD_PARAMETER, None))\n\n    def load(self, name: str) -> None:\n        if self.find_ref(name) is None:\n            self._define_ref(name, load=(VAR_LOAD_RESOLVE, name))\n\n    def branch_update(self, branch_symbols: t.Sequence[\"Symbols\"]) -> None:\n        stores: set[str] = set()\n\n        for branch in branch_symbols:\n            stores.update(branch.stores)\n\n        stores.difference_update(self.stores)\n\n        for sym in branch_symbols:\n            self.refs.update(sym.refs)\n            self.loads.update(sym.loads)\n            self.stores.update(sym.stores)\n\n        for name in stores:\n            target = self.find_ref(name)\n            assert target is not None, \"should not happen\"\n\n            if self.parent is not None:\n                outer_target = self.parent.find_ref(name)\n                if outer_target is not None:\n                    self.loads[target] = (VAR_LOAD_ALIAS, outer_target)\n                    continue\n            self.loads[target] = (VAR_LOAD_RESOLVE, name)\n\n    def dump_stores(self) -> dict[str, str]:\n        rv: dict[str, str] = {}\n        node: Symbols | None = self\n\n        while node is not None:\n            for name in sorted(node.stores):\n                if name not in rv:\n                    rv[name] = self.find_ref(name)  # type: ignore\n\n            node = node.parent\n\n        return rv\n\n    def dump_param_targets(self) -> set[str]:\n        rv = set()\n        node: Symbols | None = self\n\n        while node is not None:\n            for target, (instr, _) in self.loads.items():\n                if instr == VAR_LOAD_PARAMETER:\n                    rv.add(target)\n\n            node = node.parent\n\n        return rv\n\n\nclass RootVisitor(NodeVisitor):\n    def __init__(self, symbols: \"Symbols\") -> None:\n        self.sym_visitor = FrameSymbolVisitor(symbols)\n\n    def _simple_visit(self, node: nodes.Node, **kwargs: t.Any) -> None:\n        for child in node.iter_child_nodes():\n            self.sym_visitor.visit(child)\n\n    visit_Template = _simple_visit\n    visit_Block = _simple_visit\n    visit_Macro = _simple_visit\n    visit_FilterBlock = _simple_visit\n    visit_Scope = _simple_visit\n    visit_If = _simple_visit\n    visit_ScopedEvalContextModifier = _simple_visit\n\n    def visit_AssignBlock(self, node: nodes.AssignBlock, **kwargs: t.Any) -> None:\n        for child in node.body:\n            self.sym_visitor.visit(child)\n\n    def visit_CallBlock(self, node: nodes.CallBlock, **kwargs: t.Any) -> None:\n        for child in node.iter_child_nodes(exclude=(\"call\",)):\n            self.sym_visitor.visit(child)\n\n    def visit_OverlayScope(self, node: nodes.OverlayScope, **kwargs: t.Any) -> None:\n        for child in node.body:\n            self.sym_visitor.visit(child)\n\n    def visit_For(\n        self, node: nodes.For, for_branch: str = \"body\", **kwargs: t.Any\n    ) -> None:\n        if for_branch == \"body\":\n            self.sym_visitor.visit(node.target, store_as_param=True)\n            branch = node.body\n        elif for_branch == \"else\":\n            branch = node.else_\n        elif for_branch == \"test\":\n            self.sym_visitor.visit(node.target, store_as_param=True)\n            if node.test is not None:\n                self.sym_visitor.visit(node.test)\n            return\n        else:\n            raise RuntimeError(\"Unknown for branch\")\n\n        if branch:\n            for item in branch:\n                self.sym_visitor.visit(item)\n\n    def visit_With(self, node: nodes.With, **kwargs: t.Any) -> None:\n        for target in node.targets:\n            self.sym_visitor.visit(target)\n        for child in node.body:\n            self.sym_visitor.visit(child)\n\n    def generic_visit(self, node: nodes.Node, *args: t.Any, **kwargs: t.Any) -> None:\n        raise NotImplementedError(f\"Cannot find symbols for {type(node).__name__!r}\")\n\n\nclass FrameSymbolVisitor(NodeVisitor):\n    \"\"\"A visitor for `Frame.inspect`.\"\"\"\n\n    def __init__(self, symbols: \"Symbols\") -> None:\n        self.symbols = symbols\n\n    def visit_Name(\n        self, node: nodes.Name, store_as_param: bool = False, **kwargs: t.Any\n    ) -> None:\n        \"\"\"All assignments to names go through this function.\"\"\"\n        if store_as_param or node.ctx == \"param\":\n            self.symbols.declare_parameter(node.name)\n        elif node.ctx == \"store\":\n            self.symbols.store(node.name)\n        elif node.ctx == \"load\":\n            self.symbols.load(node.name)\n\n    def visit_NSRef(self, node: nodes.NSRef, **kwargs: t.Any) -> None:\n        self.symbols.load(node.name)\n\n    def visit_If(self, node: nodes.If, **kwargs: t.Any) -> None:\n        self.visit(node.test, **kwargs)\n        original_symbols = self.symbols\n\n        def inner_visit(nodes: t.Iterable[nodes.Node]) -> \"Symbols\":\n            self.symbols = rv = original_symbols.copy()\n\n            for subnode in nodes:\n                self.visit(subnode, **kwargs)\n\n            self.symbols = original_symbols\n            return rv\n\n        body_symbols = inner_visit(node.body)\n        elif_symbols = inner_visit(node.elif_)\n        else_symbols = inner_visit(node.else_ or ())\n        self.symbols.branch_update([body_symbols, elif_symbols, else_symbols])\n\n    def visit_Macro(self, node: nodes.Macro, **kwargs: t.Any) -> None:\n        self.symbols.store(node.name)\n\n    def visit_Import(self, node: nodes.Import, **kwargs: t.Any) -> None:\n        self.generic_visit(node, **kwargs)\n        self.symbols.store(node.target)\n\n    def visit_FromImport(self, node: nodes.FromImport, **kwargs: t.Any) -> None:\n        self.generic_visit(node, **kwargs)\n\n        for name in node.names:\n            if isinstance(name, tuple):\n                self.symbols.store(name[1])\n            else:\n                self.symbols.store(name)\n\n    def visit_Assign(self, node: nodes.Assign, **kwargs: t.Any) -> None:\n        \"\"\"Visit assignments in the correct order.\"\"\"\n        self.visit(node.node, **kwargs)\n        self.visit(node.target, **kwargs)\n\n    def visit_For(self, node: nodes.For, **kwargs: t.Any) -> None:\n        \"\"\"Visiting stops at for blocks.  However the block sequence\n        is visited as part of the outer scope.\n        \"\"\"\n        self.visit(node.iter, **kwargs)\n\n    def visit_CallBlock(self, node: nodes.CallBlock, **kwargs: t.Any) -> None:\n        self.visit(node.call, **kwargs)\n\n    def visit_FilterBlock(self, node: nodes.FilterBlock, **kwargs: t.Any) -> None:\n        self.visit(node.filter, **kwargs)\n\n    def visit_With(self, node: nodes.With, **kwargs: t.Any) -> None:\n        for target in node.values:\n            self.visit(target)\n\n    def visit_AssignBlock(self, node: nodes.AssignBlock, **kwargs: t.Any) -> None:\n        \"\"\"Stop visiting at block assigns.\"\"\"\n        self.visit(node.target, **kwargs)\n\n    def visit_Scope(self, node: nodes.Scope, **kwargs: t.Any) -> None:\n        \"\"\"Stop visiting at scopes.\"\"\"\n\n    def visit_Block(self, node: nodes.Block, **kwargs: t.Any) -> None:\n        \"\"\"Stop visiting at blocks.\"\"\"\n\n    def visit_OverlayScope(self, node: nodes.OverlayScope, **kwargs: t.Any) -> None:\n        \"\"\"Do not visit into overlay scopes.\"\"\"\n"
  },
  {
    "path": "src/jinja2/lexer.py",
    "content": "\"\"\"Implements a Jinja / Python combination lexer. The ``Lexer`` class\nis used to do some preprocessing. It filters out invalid operators like\nthe bitshift operators we don't allow in templates. It separates\ntemplate code and python code in expressions.\n\"\"\"\n\nimport re\nimport typing as t\nfrom ast import literal_eval\nfrom collections import deque\nfrom sys import intern\n\nfrom ._identifier import pattern as name_re\nfrom .exceptions import TemplateSyntaxError\nfrom .utils import LRUCache\n\nif t.TYPE_CHECKING:\n    import typing_extensions as te\n\n    from .environment import Environment\n\n# cache for the lexers. Exists in order to be able to have multiple\n# environments with the same lexer\n_lexer_cache: t.MutableMapping[tuple, \"Lexer\"] = LRUCache(50)  # type: ignore\n\n# static regular expressions\nwhitespace_re = re.compile(r\"\\s+\")\nnewline_re = re.compile(r\"(\\r\\n|\\r|\\n)\")\nstring_re = re.compile(\n    r\"('([^'\\\\]*(?:\\\\.[^'\\\\]*)*)'\" r'|\"([^\"\\\\]*(?:\\\\.[^\"\\\\]*)*)\")', re.S\n)\ninteger_re = re.compile(\n    r\"\"\"\n    (\n        0b(_?[0-1])+ # binary\n    |\n        0o(_?[0-7])+ # octal\n    |\n        0x(_?[\\da-f])+ # hex\n    |\n        [1-9](_?\\d)* # decimal\n    |\n        0(_?0)* # decimal zero\n    )\n    \"\"\",\n    re.IGNORECASE | re.VERBOSE,\n)\nfloat_re = re.compile(\n    r\"\"\"\n    (?<!\\.)  # doesn't start with a .\n    (\\d+_)*\\d+  # digits, possibly _ separated\n    (\n        (\\.(\\d+_)*\\d+)?  # optional fractional part\n        e[+\\-]?(\\d+_)*\\d+  # exponent part\n    |\n        \\.(\\d+_)*\\d+  # required fractional part\n    )\n    \"\"\",\n    re.IGNORECASE | re.VERBOSE,\n)\n\n# internal the tokens and keep references to them\nTOKEN_ADD = intern(\"add\")\nTOKEN_ASSIGN = intern(\"assign\")\nTOKEN_COLON = intern(\"colon\")\nTOKEN_COMMA = intern(\"comma\")\nTOKEN_DIV = intern(\"div\")\nTOKEN_DOT = intern(\"dot\")\nTOKEN_EQ = intern(\"eq\")\nTOKEN_FLOORDIV = intern(\"floordiv\")\nTOKEN_GT = intern(\"gt\")\nTOKEN_GTEQ = intern(\"gteq\")\nTOKEN_LBRACE = intern(\"lbrace\")\nTOKEN_LBRACKET = intern(\"lbracket\")\nTOKEN_LPAREN = intern(\"lparen\")\nTOKEN_LT = intern(\"lt\")\nTOKEN_LTEQ = intern(\"lteq\")\nTOKEN_MOD = intern(\"mod\")\nTOKEN_MUL = intern(\"mul\")\nTOKEN_NE = intern(\"ne\")\nTOKEN_PIPE = intern(\"pipe\")\nTOKEN_POW = intern(\"pow\")\nTOKEN_RBRACE = intern(\"rbrace\")\nTOKEN_RBRACKET = intern(\"rbracket\")\nTOKEN_RPAREN = intern(\"rparen\")\nTOKEN_SEMICOLON = intern(\"semicolon\")\nTOKEN_SUB = intern(\"sub\")\nTOKEN_TILDE = intern(\"tilde\")\nTOKEN_WHITESPACE = intern(\"whitespace\")\nTOKEN_FLOAT = intern(\"float\")\nTOKEN_INTEGER = intern(\"integer\")\nTOKEN_NAME = intern(\"name\")\nTOKEN_STRING = intern(\"string\")\nTOKEN_OPERATOR = intern(\"operator\")\nTOKEN_BLOCK_BEGIN = intern(\"block_begin\")\nTOKEN_BLOCK_END = intern(\"block_end\")\nTOKEN_VARIABLE_BEGIN = intern(\"variable_begin\")\nTOKEN_VARIABLE_END = intern(\"variable_end\")\nTOKEN_RAW_BEGIN = intern(\"raw_begin\")\nTOKEN_RAW_END = intern(\"raw_end\")\nTOKEN_COMMENT_BEGIN = intern(\"comment_begin\")\nTOKEN_COMMENT_END = intern(\"comment_end\")\nTOKEN_COMMENT = intern(\"comment\")\nTOKEN_LINESTATEMENT_BEGIN = intern(\"linestatement_begin\")\nTOKEN_LINESTATEMENT_END = intern(\"linestatement_end\")\nTOKEN_LINECOMMENT_BEGIN = intern(\"linecomment_begin\")\nTOKEN_LINECOMMENT_END = intern(\"linecomment_end\")\nTOKEN_LINECOMMENT = intern(\"linecomment\")\nTOKEN_DATA = intern(\"data\")\nTOKEN_INITIAL = intern(\"initial\")\nTOKEN_EOF = intern(\"eof\")\n\n# bind operators to token types\noperators = {\n    \"+\": TOKEN_ADD,\n    \"-\": TOKEN_SUB,\n    \"/\": TOKEN_DIV,\n    \"//\": TOKEN_FLOORDIV,\n    \"*\": TOKEN_MUL,\n    \"%\": TOKEN_MOD,\n    \"**\": TOKEN_POW,\n    \"~\": TOKEN_TILDE,\n    \"[\": TOKEN_LBRACKET,\n    \"]\": TOKEN_RBRACKET,\n    \"(\": TOKEN_LPAREN,\n    \")\": TOKEN_RPAREN,\n    \"{\": TOKEN_LBRACE,\n    \"}\": TOKEN_RBRACE,\n    \"==\": TOKEN_EQ,\n    \"!=\": TOKEN_NE,\n    \">\": TOKEN_GT,\n    \">=\": TOKEN_GTEQ,\n    \"<\": TOKEN_LT,\n    \"<=\": TOKEN_LTEQ,\n    \"=\": TOKEN_ASSIGN,\n    \".\": TOKEN_DOT,\n    \":\": TOKEN_COLON,\n    \"|\": TOKEN_PIPE,\n    \",\": TOKEN_COMMA,\n    \";\": TOKEN_SEMICOLON,\n}\n\nreverse_operators = {v: k for k, v in operators.items()}\nassert len(operators) == len(reverse_operators), \"operators dropped\"\noperator_re = re.compile(\n    f\"({'|'.join(re.escape(x) for x in sorted(operators, key=lambda x: -len(x)))})\"\n)\n\nignored_tokens = frozenset(\n    [\n        TOKEN_COMMENT_BEGIN,\n        TOKEN_COMMENT,\n        TOKEN_COMMENT_END,\n        TOKEN_WHITESPACE,\n        TOKEN_LINECOMMENT_BEGIN,\n        TOKEN_LINECOMMENT_END,\n        TOKEN_LINECOMMENT,\n    ]\n)\nignore_if_empty = frozenset(\n    [TOKEN_WHITESPACE, TOKEN_DATA, TOKEN_COMMENT, TOKEN_LINECOMMENT]\n)\n\n\ndef _describe_token_type(token_type: str) -> str:\n    if token_type in reverse_operators:\n        return reverse_operators[token_type]\n\n    return {\n        TOKEN_COMMENT_BEGIN: \"begin of comment\",\n        TOKEN_COMMENT_END: \"end of comment\",\n        TOKEN_COMMENT: \"comment\",\n        TOKEN_LINECOMMENT: \"comment\",\n        TOKEN_BLOCK_BEGIN: \"begin of statement block\",\n        TOKEN_BLOCK_END: \"end of statement block\",\n        TOKEN_VARIABLE_BEGIN: \"begin of print statement\",\n        TOKEN_VARIABLE_END: \"end of print statement\",\n        TOKEN_LINESTATEMENT_BEGIN: \"begin of line statement\",\n        TOKEN_LINESTATEMENT_END: \"end of line statement\",\n        TOKEN_DATA: \"template data / text\",\n        TOKEN_EOF: \"end of template\",\n    }.get(token_type, token_type)\n\n\ndef describe_token(token: \"Token\") -> str:\n    \"\"\"Returns a description of the token.\"\"\"\n    if token.type == TOKEN_NAME:\n        return token.value\n\n    return _describe_token_type(token.type)\n\n\ndef describe_token_expr(expr: str) -> str:\n    \"\"\"Like `describe_token` but for token expressions.\"\"\"\n    if \":\" in expr:\n        type, value = expr.split(\":\", 1)\n\n        if type == TOKEN_NAME:\n            return value\n    else:\n        type = expr\n\n    return _describe_token_type(type)\n\n\ndef count_newlines(value: str) -> int:\n    \"\"\"Count the number of newline characters in the string.  This is\n    useful for extensions that filter a stream.\n    \"\"\"\n    return len(newline_re.findall(value))\n\n\ndef compile_rules(environment: \"Environment\") -> list[tuple[str, str]]:\n    \"\"\"Compiles all the rules from the environment into a list of rules.\"\"\"\n    e = re.escape\n    rules = [\n        (\n            len(environment.comment_start_string),\n            TOKEN_COMMENT_BEGIN,\n            e(environment.comment_start_string),\n        ),\n        (\n            len(environment.block_start_string),\n            TOKEN_BLOCK_BEGIN,\n            e(environment.block_start_string),\n        ),\n        (\n            len(environment.variable_start_string),\n            TOKEN_VARIABLE_BEGIN,\n            e(environment.variable_start_string),\n        ),\n    ]\n\n    if environment.line_statement_prefix is not None:\n        rules.append(\n            (\n                len(environment.line_statement_prefix),\n                TOKEN_LINESTATEMENT_BEGIN,\n                r\"^[ \\t\\v]*\" + e(environment.line_statement_prefix),\n            )\n        )\n    if environment.line_comment_prefix is not None:\n        rules.append(\n            (\n                len(environment.line_comment_prefix),\n                TOKEN_LINECOMMENT_BEGIN,\n                r\"(?:^|(?<=\\S))[^\\S\\r\\n]*\" + e(environment.line_comment_prefix),\n            )\n        )\n\n    return [x[1:] for x in sorted(rules, reverse=True)]\n\n\nclass Failure:\n    \"\"\"Class that raises a `TemplateSyntaxError` if called.\n    Used by the `Lexer` to specify known errors.\n    \"\"\"\n\n    def __init__(\n        self, message: str, cls: type[TemplateSyntaxError] = TemplateSyntaxError\n    ) -> None:\n        self.message = message\n        self.error_class = cls\n\n    def __call__(self, lineno: int, filename: str | None) -> \"te.NoReturn\":\n        raise self.error_class(self.message, lineno, filename)\n\n\nclass Token(t.NamedTuple):\n    lineno: int\n    type: str\n    value: str\n\n    def __str__(self) -> str:\n        return describe_token(self)\n\n    def test(self, expr: str) -> bool:\n        \"\"\"Test a token against a token expression.  This can either be a\n        token type or ``'token_type:token_value'``.  This can only test\n        against string values and types.\n        \"\"\"\n        # here we do a regular string equality check as test_any is usually\n        # passed an iterable of not interned strings.\n        if self.type == expr:\n            return True\n\n        if \":\" in expr:\n            return expr.split(\":\", 1) == [self.type, self.value]\n\n        return False\n\n    def test_any(self, *iterable: str) -> bool:\n        \"\"\"Test against multiple token expressions.\"\"\"\n        return any(self.test(expr) for expr in iterable)\n\n\nclass TokenStreamIterator:\n    \"\"\"The iterator for tokenstreams.  Iterate over the stream\n    until the eof token is reached.\n    \"\"\"\n\n    def __init__(self, stream: \"TokenStream\") -> None:\n        self.stream = stream\n\n    def __iter__(self) -> \"TokenStreamIterator\":\n        return self\n\n    def __next__(self) -> Token:\n        token = self.stream.current\n\n        if token.type is TOKEN_EOF:\n            self.stream.close()\n            raise StopIteration\n\n        next(self.stream)\n        return token\n\n\nclass TokenStream:\n    \"\"\"A token stream is an iterable that yields :class:`Token`\\\\s.  The\n    parser however does not iterate over it but calls :meth:`next` to go\n    one token ahead.  The current active token is stored as :attr:`current`.\n    \"\"\"\n\n    def __init__(\n        self,\n        generator: t.Iterable[Token],\n        name: str | None,\n        filename: str | None,\n    ):\n        self._iter = iter(generator)\n        self._pushed: deque[Token] = deque()\n        self.name = name\n        self.filename = filename\n        self.closed = False\n        self.current = Token(1, TOKEN_INITIAL, \"\")\n        next(self)\n\n    def __iter__(self) -> TokenStreamIterator:\n        return TokenStreamIterator(self)\n\n    def __bool__(self) -> bool:\n        return bool(self._pushed) or self.current.type is not TOKEN_EOF\n\n    @property\n    def eos(self) -> bool:\n        \"\"\"Are we at the end of the stream?\"\"\"\n        return not self\n\n    def push(self, token: Token) -> None:\n        \"\"\"Push a token back to the stream.\"\"\"\n        self._pushed.append(token)\n\n    def look(self) -> Token:\n        \"\"\"Look at the next token.\"\"\"\n        old_token = next(self)\n        result = self.current\n        self.push(result)\n        self.current = old_token\n        return result\n\n    def skip(self, n: int = 1) -> None:\n        \"\"\"Got n tokens ahead.\"\"\"\n        for _ in range(n):\n            next(self)\n\n    def next_if(self, expr: str) -> Token | None:\n        \"\"\"Perform the token test and return the token if it matched.\n        Otherwise the return value is `None`.\n        \"\"\"\n        if self.current.test(expr):\n            return next(self)\n\n        return None\n\n    def skip_if(self, expr: str) -> bool:\n        \"\"\"Like :meth:`next_if` but only returns `True` or `False`.\"\"\"\n        return self.next_if(expr) is not None\n\n    def __next__(self) -> Token:\n        \"\"\"Go one token ahead and return the old one.\n\n        Use the built-in :func:`next` instead of calling this directly.\n        \"\"\"\n        rv = self.current\n\n        if self._pushed:\n            self.current = self._pushed.popleft()\n        elif self.current.type is not TOKEN_EOF:\n            try:\n                self.current = next(self._iter)\n            except StopIteration:\n                self.close()\n\n        return rv\n\n    def close(self) -> None:\n        \"\"\"Close the stream.\"\"\"\n        self.current = Token(self.current.lineno, TOKEN_EOF, \"\")\n        self._iter = iter(())\n        self.closed = True\n\n    def expect(self, expr: str) -> Token:\n        \"\"\"Expect a given token type and return it.  This accepts the same\n        argument as :meth:`jinja2.lexer.Token.test`.\n        \"\"\"\n        if not self.current.test(expr):\n            expr = describe_token_expr(expr)\n\n            if self.current.type is TOKEN_EOF:\n                raise TemplateSyntaxError(\n                    f\"unexpected end of template, expected {expr!r}.\",\n                    self.current.lineno,\n                    self.name,\n                    self.filename,\n                )\n\n            raise TemplateSyntaxError(\n                f\"expected token {expr!r}, got {describe_token(self.current)!r}\",\n                self.current.lineno,\n                self.name,\n                self.filename,\n            )\n\n        return next(self)\n\n\ndef get_lexer(environment: \"Environment\") -> \"Lexer\":\n    \"\"\"Return a lexer which is probably cached.\"\"\"\n    key = (\n        environment.block_start_string,\n        environment.block_end_string,\n        environment.variable_start_string,\n        environment.variable_end_string,\n        environment.comment_start_string,\n        environment.comment_end_string,\n        environment.line_statement_prefix,\n        environment.line_comment_prefix,\n        environment.trim_blocks,\n        environment.lstrip_blocks,\n        environment.newline_sequence,\n        environment.keep_trailing_newline,\n    )\n    lexer = _lexer_cache.get(key)\n\n    if lexer is None:\n        _lexer_cache[key] = lexer = Lexer(environment)\n\n    return lexer\n\n\nclass OptionalLStrip(tuple):  # type: ignore[type-arg]\n    \"\"\"A special tuple for marking a point in the state that can have\n    lstrip applied.\n    \"\"\"\n\n    __slots__ = ()\n\n    # Even though it looks like a no-op, creating instances fails\n    # without this.\n    def __new__(cls, *members, **kwargs):  # type: ignore\n        return super().__new__(cls, members)\n\n\nclass _Rule(t.NamedTuple):\n    pattern: t.Pattern[str]\n    tokens: str | tuple[str, ...] | tuple[Failure]\n    command: str | None\n\n\nclass Lexer:\n    \"\"\"Class that implements a lexer for a given environment. Automatically\n    created by the environment class, usually you don't have to do that.\n\n    Note that the lexer is not automatically bound to an environment.\n    Multiple environments can share the same lexer.\n    \"\"\"\n\n    def __init__(self, environment: \"Environment\") -> None:\n        # shortcuts\n        e = re.escape\n\n        def c(x: str) -> t.Pattern[str]:\n            return re.compile(x, re.M | re.S)\n\n        # lexing rules for tags\n        tag_rules: list[_Rule] = [\n            _Rule(whitespace_re, TOKEN_WHITESPACE, None),\n            _Rule(float_re, TOKEN_FLOAT, None),\n            _Rule(integer_re, TOKEN_INTEGER, None),\n            _Rule(name_re, TOKEN_NAME, None),\n            _Rule(string_re, TOKEN_STRING, None),\n            _Rule(operator_re, TOKEN_OPERATOR, None),\n        ]\n\n        # assemble the root lexing rule. because \"|\" is ungreedy\n        # we have to sort by length so that the lexer continues working\n        # as expected when we have parsing rules like <% for block and\n        # <%= for variables. (if someone wants asp like syntax)\n        # variables are just part of the rules if variable processing\n        # is required.\n        root_tag_rules = compile_rules(environment)\n\n        block_start_re = e(environment.block_start_string)\n        block_end_re = e(environment.block_end_string)\n        comment_end_re = e(environment.comment_end_string)\n        variable_end_re = e(environment.variable_end_string)\n\n        # block suffix if trimming is enabled\n        block_suffix_re = \"\\\\n?\" if environment.trim_blocks else \"\"\n\n        self.lstrip_blocks = environment.lstrip_blocks\n\n        self.newline_sequence = environment.newline_sequence\n        self.keep_trailing_newline = environment.keep_trailing_newline\n\n        root_raw_re = (\n            rf\"(?P<raw_begin>{block_start_re}(\\-|\\+|)\\s*raw\\s*\"\n            rf\"(?:\\-{block_end_re}\\s*|{block_end_re}))\"\n        )\n        root_parts_re = \"|\".join(\n            [root_raw_re] + [rf\"(?P<{n}>{r}(\\-|\\+|))\" for n, r in root_tag_rules]\n        )\n\n        # global lexing rules\n        self.rules: dict[str, list[_Rule]] = {\n            \"root\": [\n                # directives\n                _Rule(\n                    c(rf\"(.*?)(?:{root_parts_re})\"),\n                    OptionalLStrip(TOKEN_DATA, \"#bygroup\"),  # type: ignore\n                    \"#bygroup\",\n                ),\n                # data\n                _Rule(c(\".+\"), TOKEN_DATA, None),\n            ],\n            # comments\n            TOKEN_COMMENT_BEGIN: [\n                _Rule(\n                    c(\n                        rf\"(.*?)((?:\\+{comment_end_re}|\\-{comment_end_re}\\s*\"\n                        rf\"|{comment_end_re}{block_suffix_re}))\"\n                    ),\n                    (TOKEN_COMMENT, TOKEN_COMMENT_END),\n                    \"#pop\",\n                ),\n                _Rule(c(r\"(.)\"), (Failure(\"Missing end of comment tag\"),), None),\n            ],\n            # blocks\n            TOKEN_BLOCK_BEGIN: [\n                _Rule(\n                    c(\n                        rf\"(?:\\+{block_end_re}|\\-{block_end_re}\\s*\"\n                        rf\"|{block_end_re}{block_suffix_re})\"\n                    ),\n                    TOKEN_BLOCK_END,\n                    \"#pop\",\n                ),\n            ]\n            + tag_rules,\n            # variables\n            TOKEN_VARIABLE_BEGIN: [\n                _Rule(\n                    c(rf\"\\-{variable_end_re}\\s*|{variable_end_re}\"),\n                    TOKEN_VARIABLE_END,\n                    \"#pop\",\n                )\n            ]\n            + tag_rules,\n            # raw block\n            TOKEN_RAW_BEGIN: [\n                _Rule(\n                    c(\n                        rf\"(.*?)((?:{block_start_re}(\\-|\\+|))\\s*endraw\\s*\"\n                        rf\"(?:\\+{block_end_re}|\\-{block_end_re}\\s*\"\n                        rf\"|{block_end_re}{block_suffix_re}))\"\n                    ),\n                    OptionalLStrip(TOKEN_DATA, TOKEN_RAW_END),  # type: ignore\n                    \"#pop\",\n                ),\n                _Rule(c(r\"(.)\"), (Failure(\"Missing end of raw directive\"),), None),\n            ],\n            # line statements\n            TOKEN_LINESTATEMENT_BEGIN: [\n                _Rule(c(r\"\\s*(\\n|$)\"), TOKEN_LINESTATEMENT_END, \"#pop\")\n            ]\n            + tag_rules,\n            # line comments\n            TOKEN_LINECOMMENT_BEGIN: [\n                _Rule(\n                    c(r\"(.*?)()(?=\\n|$)\"),\n                    (TOKEN_LINECOMMENT, TOKEN_LINECOMMENT_END),\n                    \"#pop\",\n                )\n            ],\n        }\n\n    def _normalize_newlines(self, value: str) -> str:\n        \"\"\"Replace all newlines with the configured sequence in strings\n        and template data.\n        \"\"\"\n        return newline_re.sub(self.newline_sequence, value)\n\n    def tokenize(\n        self,\n        source: str,\n        name: str | None = None,\n        filename: str | None = None,\n        state: str | None = None,\n    ) -> TokenStream:\n        \"\"\"Calls tokeniter + tokenize and wraps it in a token stream.\"\"\"\n        stream = self.tokeniter(source, name, filename, state)\n        return TokenStream(self.wrap(stream, name, filename), name, filename)\n\n    def wrap(\n        self,\n        stream: t.Iterable[tuple[int, str, str]],\n        name: str | None = None,\n        filename: str | None = None,\n    ) -> t.Iterator[Token]:\n        \"\"\"This is called with the stream as returned by `tokenize` and wraps\n        every token in a :class:`Token` and converts the value.\n        \"\"\"\n        for lineno, token, value_str in stream:\n            if token in ignored_tokens:\n                continue\n\n            value: t.Any = value_str\n\n            if token == TOKEN_LINESTATEMENT_BEGIN:\n                token = TOKEN_BLOCK_BEGIN\n            elif token == TOKEN_LINESTATEMENT_END:\n                token = TOKEN_BLOCK_END\n            # we are not interested in those tokens in the parser\n            elif token in (TOKEN_RAW_BEGIN, TOKEN_RAW_END):\n                continue\n            elif token == TOKEN_DATA:\n                value = self._normalize_newlines(value_str)\n            elif token == \"keyword\":\n                token = value_str\n            elif token == TOKEN_NAME:\n                value = value_str\n\n                if not value.isidentifier():\n                    raise TemplateSyntaxError(\n                        \"Invalid character in identifier\", lineno, name, filename\n                    )\n            elif token == TOKEN_STRING:\n                # try to unescape string\n                try:\n                    value = (\n                        self._normalize_newlines(value_str[1:-1])\n                        .encode(\"ascii\", \"backslashreplace\")\n                        .decode(\"unicode-escape\")\n                    )\n                except Exception as e:\n                    msg = str(e).split(\":\")[-1].strip()\n                    raise TemplateSyntaxError(msg, lineno, name, filename) from e\n            elif token == TOKEN_INTEGER:\n                value = int(value_str.replace(\"_\", \"\"), 0)\n            elif token == TOKEN_FLOAT:\n                # remove all \"_\" first to support more Python versions\n                value = literal_eval(value_str.replace(\"_\", \"\"))\n            elif token == TOKEN_OPERATOR:\n                token = operators[value_str]\n\n            yield Token(lineno, token, value)\n\n    def tokeniter(\n        self,\n        source: str,\n        name: str | None,\n        filename: str | None = None,\n        state: str | None = None,\n    ) -> t.Iterator[tuple[int, str, str]]:\n        \"\"\"This method tokenizes the text and returns the tokens in a\n        generator. Use this method if you just want to tokenize a template.\n\n        .. versionchanged:: 3.0\n            Only ``\\\\n``, ``\\\\r\\\\n`` and ``\\\\r`` are treated as line\n            breaks.\n        \"\"\"\n        lines = newline_re.split(source)[::2]\n\n        if not self.keep_trailing_newline and lines[-1] == \"\":\n            del lines[-1]\n\n        source = \"\\n\".join(lines)\n        pos = 0\n        lineno = 1\n        stack = [\"root\"]\n\n        if state is not None and state != \"root\":\n            assert state in (\"variable\", \"block\"), \"invalid state\"\n            stack.append(state + \"_begin\")\n\n        statetokens = self.rules[stack[-1]]\n        source_length = len(source)\n        balancing_stack: list[str] = []\n        newlines_stripped = 0\n        line_starting = True\n\n        while True:\n            # tokenizer loop\n            for regex, tokens, new_state in statetokens:\n                m = regex.match(source, pos)\n\n                # if no match we try again with the next rule\n                if m is None:\n                    continue\n\n                # we only match blocks and variables if braces / parentheses\n                # are balanced. continue parsing with the lower rule which\n                # is the operator rule. do this only if the end tags look\n                # like operators\n                if balancing_stack and tokens in (\n                    TOKEN_VARIABLE_END,\n                    TOKEN_BLOCK_END,\n                    TOKEN_LINESTATEMENT_END,\n                ):\n                    continue\n\n                # tuples support more options\n                if isinstance(tokens, tuple):\n                    groups: t.Sequence[str] = m.groups()\n\n                    if isinstance(tokens, OptionalLStrip):\n                        # Rule supports lstrip. Match will look like\n                        # text, block type, whitespace control, type, control, ...\n                        text = groups[0]\n                        # Skipping the text and first type, every other group is the\n                        # whitespace control for each type. One of the groups will be\n                        # -, +, or empty string instead of None.\n                        strip_sign = next(g for g in groups[2::2] if g is not None)\n\n                        if strip_sign == \"-\":\n                            # Strip all whitespace between the text and the tag.\n                            stripped = text.rstrip()\n                            newlines_stripped = text[len(stripped) :].count(\"\\n\")\n                            groups = [stripped, *groups[1:]]\n                        elif (\n                            # Not marked for preserving whitespace.\n                            strip_sign != \"+\"\n                            # lstrip is enabled.\n                            and self.lstrip_blocks\n                            # Not a variable expression.\n                            and not m.groupdict().get(TOKEN_VARIABLE_BEGIN)\n                        ):\n                            # The start of text between the last newline and the tag.\n                            l_pos = text.rfind(\"\\n\") + 1\n\n                            if l_pos > 0 or line_starting:\n                                # If there's only whitespace between the newline and the\n                                # tag, strip it.\n                                if whitespace_re.fullmatch(text, l_pos):\n                                    groups = [text[:l_pos], *groups[1:]]\n\n                    for idx, token in enumerate(tokens):\n                        # failure group\n                        if isinstance(token, Failure):\n                            raise token(lineno, filename)\n                        # bygroup is a bit more complex, in that case we\n                        # yield for the current token the first named\n                        # group that matched\n                        elif token == \"#bygroup\":\n                            for key, value in m.groupdict().items():\n                                if value is not None:\n                                    yield lineno, key, value\n                                    lineno += value.count(\"\\n\")\n                                    break\n                            else:\n                                raise RuntimeError(\n                                    f\"{regex!r} wanted to resolve the token dynamically\"\n                                    \" but no group matched\"\n                                )\n                        # normal group\n                        else:\n                            data = groups[idx]\n\n                            if data or token not in ignore_if_empty:\n                                yield lineno, token, data  # type: ignore[misc]\n\n                            lineno += data.count(\"\\n\") + newlines_stripped\n                            newlines_stripped = 0\n\n                # strings as token just are yielded as it.\n                else:\n                    data = m.group()\n\n                    # update brace/parentheses balance\n                    if tokens == TOKEN_OPERATOR:\n                        if data == \"{\":\n                            balancing_stack.append(\"}\")\n                        elif data == \"(\":\n                            balancing_stack.append(\")\")\n                        elif data == \"[\":\n                            balancing_stack.append(\"]\")\n                        elif data in (\"}\", \")\", \"]\"):\n                            if not balancing_stack:\n                                raise TemplateSyntaxError(\n                                    f\"unexpected '{data}'\", lineno, name, filename\n                                )\n\n                            expected_op = balancing_stack.pop()\n\n                            if expected_op != data:\n                                raise TemplateSyntaxError(\n                                    f\"unexpected '{data}', expected '{expected_op}'\",\n                                    lineno,\n                                    name,\n                                    filename,\n                                )\n\n                    # yield items\n                    if data or tokens not in ignore_if_empty:\n                        yield lineno, tokens, data\n\n                    lineno += data.count(\"\\n\")\n\n                line_starting = m.group()[-1:] == \"\\n\"\n                # fetch new position into new variable so that we can check\n                # if there is a internal parsing error which would result\n                # in an infinite loop\n                pos2 = m.end()\n\n                # handle state changes\n                if new_state is not None:\n                    # remove the uppermost state\n                    if new_state == \"#pop\":\n                        stack.pop()\n                    # resolve the new state by group checking\n                    elif new_state == \"#bygroup\":\n                        for key, value in m.groupdict().items():\n                            if value is not None:\n                                stack.append(key)\n                                break\n                        else:\n                            raise RuntimeError(\n                                f\"{regex!r} wanted to resolve the new state dynamically\"\n                                f\" but no group matched\"\n                            )\n                    # direct state name given\n                    else:\n                        stack.append(new_state)\n\n                    statetokens = self.rules[stack[-1]]\n                # we are still at the same position and no stack change.\n                # this means a loop without break condition, avoid that and\n                # raise error\n                elif pos2 == pos:\n                    raise RuntimeError(\n                        f\"{regex!r} yielded empty string without stack change\"\n                    )\n\n                # publish new function and start again\n                pos = pos2\n                break\n            # if loop terminated without break we haven't found a single match\n            # either we are at the end of the file or we have a problem\n            else:\n                # end of text\n                if pos >= source_length:\n                    return\n\n                # something went wrong\n                raise TemplateSyntaxError(\n                    f\"unexpected char {source[pos]!r} at {pos}\", lineno, name, filename\n                )\n"
  },
  {
    "path": "src/jinja2/loaders.py",
    "content": "\"\"\"API and implementations for loading templates from different data\nsources.\n\"\"\"\n\nimport importlib.util\nimport os\nimport posixpath\nimport sys\nimport typing as t\nimport weakref\nimport zipimport\nfrom collections import abc\nfrom hashlib import sha1\nfrom importlib import import_module\nfrom types import ModuleType\n\nfrom .exceptions import TemplateNotFound\nfrom .utils import internalcode\n\nif t.TYPE_CHECKING:\n    from .environment import Environment\n    from .environment import Template\n\n\ndef split_template_path(template: str) -> list[str]:\n    \"\"\"Split a path into segments and perform a sanity check.  If it detects\n    '..' in the path it will raise a `TemplateNotFound` error.\n    \"\"\"\n    pieces = []\n    for piece in template.split(\"/\"):\n        if (\n            os.sep in piece\n            or (os.path.altsep and os.path.altsep in piece)\n            or piece == os.path.pardir\n        ):\n            raise TemplateNotFound(template)\n        elif piece and piece != \".\":\n            pieces.append(piece)\n    return pieces\n\n\nclass BaseLoader:\n    \"\"\"Baseclass for all loaders.  Subclass this and override `get_source` to\n    implement a custom loading mechanism.  The environment provides a\n    `get_template` method that calls the loader's `load` method to get the\n    :class:`Template` object.\n\n    A very basic example for a loader that looks up templates on the file\n    system could look like this::\n\n        from jinja2 import BaseLoader, TemplateNotFound\n        from os.path import join, exists, getmtime\n\n        class MyLoader(BaseLoader):\n\n            def __init__(self, path):\n                self.path = path\n\n            def get_source(self, environment, template):\n                path = join(self.path, template)\n                if not exists(path):\n                    raise TemplateNotFound(template)\n                mtime = getmtime(path)\n                with open(path) as f:\n                    source = f.read()\n                return source, path, lambda: mtime == getmtime(path)\n    \"\"\"\n\n    #: if set to `False` it indicates that the loader cannot provide access\n    #: to the source of templates.\n    #:\n    #: .. versionadded:: 2.4\n    has_source_access = True\n\n    def get_source(\n        self, environment: \"Environment\", template: str\n    ) -> tuple[str, str | None, t.Callable[[], bool] | None]:\n        \"\"\"Get the template source, filename and reload helper for a template.\n        It's passed the environment and template name and has to return a\n        tuple in the form ``(source, filename, uptodate)`` or raise a\n        `TemplateNotFound` error if it can't locate the template.\n\n        The source part of the returned tuple must be the source of the\n        template as a string. The filename should be the name of the\n        file on the filesystem if it was loaded from there, otherwise\n        ``None``. The filename is used by Python for the tracebacks\n        if no loader extension is used.\n\n        The last item in the tuple is the `uptodate` function.  If auto\n        reloading is enabled it's always called to check if the template\n        changed.  No arguments are passed so the function must store the\n        old state somewhere (for example in a closure).  If it returns `False`\n        the template will be reloaded.\n        \"\"\"\n        if not self.has_source_access:\n            raise RuntimeError(\n                f\"{type(self).__name__} cannot provide access to the source\"\n            )\n        raise TemplateNotFound(template)\n\n    def list_templates(self) -> list[str]:\n        \"\"\"Iterates over all templates.  If the loader does not support that\n        it should raise a :exc:`TypeError` which is the default behavior.\n        \"\"\"\n        raise TypeError(\"this loader cannot iterate over all templates\")\n\n    @internalcode\n    def load(\n        self,\n        environment: \"Environment\",\n        name: str,\n        globals: t.MutableMapping[str, t.Any] | None = None,\n    ) -> \"Template\":\n        \"\"\"Loads a template.  This method looks up the template in the cache\n        or loads one by calling :meth:`get_source`.  Subclasses should not\n        override this method as loaders working on collections of other\n        loaders (such as :class:`PrefixLoader` or :class:`ChoiceLoader`)\n        will not call this method but `get_source` directly.\n        \"\"\"\n        code = None\n        if globals is None:\n            globals = {}\n\n        # first we try to get the source for this template together\n        # with the filename and the uptodate function.\n        source, filename, uptodate = self.get_source(environment, name)\n\n        # try to load the code from the bytecode cache if there is a\n        # bytecode cache configured.\n        bcc = environment.bytecode_cache\n        if bcc is not None:\n            bucket = bcc.get_bucket(environment, name, filename, source)\n            code = bucket.code\n\n        # if we don't have code so far (not cached, no longer up to\n        # date) etc. we compile the template\n        if code is None:\n            code = environment.compile(source, name, filename)\n\n        # if the bytecode cache is available and the bucket doesn't\n        # have a code so far, we give the bucket the new code and put\n        # it back to the bytecode cache.\n        if bcc is not None and bucket.code is None:\n            bucket.code = code\n            bcc.set_bucket(bucket)\n\n        return environment.template_class.from_code(\n            environment, code, globals, uptodate\n        )\n\n\nclass FileSystemLoader(BaseLoader):\n    \"\"\"Load templates from a directory in the file system.\n\n    The path can be relative or absolute. Relative paths are relative to\n    the current working directory.\n\n    .. code-block:: python\n\n        loader = FileSystemLoader(\"templates\")\n\n    A list of paths can be given. The directories will be searched in\n    order, stopping at the first matching template.\n\n    .. code-block:: python\n\n        loader = FileSystemLoader([\"/override/templates\", \"/default/templates\"])\n\n    :param searchpath: A path, or list of paths, to the directory that\n        contains the templates.\n    :param encoding: Use this encoding to read the text from template\n        files.\n    :param followlinks: Follow symbolic links in the path.\n\n    .. versionchanged:: 2.8\n        Added the ``followlinks`` parameter.\n    \"\"\"\n\n    def __init__(\n        self,\n        searchpath: t.Union[\n            str, \"os.PathLike[str]\", t.Sequence[t.Union[str, \"os.PathLike[str]\"]]\n        ],\n        encoding: str = \"utf-8\",\n        followlinks: bool = False,\n    ) -> None:\n        if not isinstance(searchpath, abc.Iterable) or isinstance(searchpath, str):\n            searchpath = [searchpath]\n\n        self.searchpath = [os.fspath(p) for p in searchpath]\n        self.encoding = encoding\n        self.followlinks = followlinks\n\n    def get_source(\n        self, environment: \"Environment\", template: str\n    ) -> tuple[str, str, t.Callable[[], bool]]:\n        pieces = split_template_path(template)\n\n        for searchpath in self.searchpath:\n            # Use posixpath even on Windows to avoid \"drive:\" or UNC\n            # segments breaking out of the search directory.\n            filename = posixpath.join(searchpath, *pieces)\n\n            if os.path.isfile(filename):\n                break\n        else:\n            plural = \"path\" if len(self.searchpath) == 1 else \"paths\"\n            paths_str = \", \".join(repr(p) for p in self.searchpath)\n            raise TemplateNotFound(\n                template,\n                f\"{template!r} not found in search {plural}: {paths_str}\",\n            )\n\n        with open(filename, encoding=self.encoding) as f:\n            contents = f.read()\n\n        mtime = os.path.getmtime(filename)\n\n        def uptodate() -> bool:\n            try:\n                return os.path.getmtime(filename) == mtime\n            except OSError:\n                return False\n\n        # Use normpath to convert Windows altsep to sep.\n        return contents, os.path.normpath(filename), uptodate\n\n    def list_templates(self) -> list[str]:\n        found = set()\n        for searchpath in self.searchpath:\n            walk_dir = os.walk(searchpath, followlinks=self.followlinks)\n            for dirpath, _, filenames in walk_dir:\n                for filename in filenames:\n                    template = (\n                        os.path.join(dirpath, filename)[len(searchpath) :]\n                        .strip(os.sep)\n                        .replace(os.sep, \"/\")\n                    )\n                    if template[:2] == \"./\":\n                        template = template[2:]\n                    if template not in found:\n                        found.add(template)\n        return sorted(found)\n\n\nif sys.version_info >= (3, 13):\n\n    def _get_zipimporter_files(z: t.Any) -> dict[str, object]:\n        try:\n            get_files = z._get_files\n        except AttributeError as e:\n            raise TypeError(\n                \"This zip import does not have the required metadata to list templates.\"\n            ) from e\n        return get_files()\n\nelse:\n\n    def _get_zipimporter_files(z: t.Any) -> dict[str, object]:\n        try:\n            files = z._files\n        except AttributeError as e:\n            raise TypeError(\n                \"This zip import does not have the required metadata to list templates.\"\n            ) from e\n        return files  # type: ignore[no-any-return]\n\n\nclass PackageLoader(BaseLoader):\n    \"\"\"Load templates from a directory in a Python package.\n\n    :param package_name: Import name of the package that contains the\n        template directory.\n    :param package_path: Directory within the imported package that\n        contains the templates.\n    :param encoding: Encoding of template files.\n\n    The following example looks up templates in the ``pages`` directory\n    within the ``project.ui`` package.\n\n    .. code-block:: python\n\n        loader = PackageLoader(\"project.ui\", \"pages\")\n\n    Only packages installed as directories (standard pip behavior) or\n    zip/egg files (less common) are supported. The Python API for\n    introspecting data in packages is too limited to support other\n    installation methods the way this loader requires.\n\n    There is limited support for :pep:`420` namespace packages. The\n    template directory is assumed to only be in one namespace\n    contributor. Zip files contributing to a namespace are not\n    supported.\n\n    .. versionchanged:: 3.0\n        No longer uses ``setuptools`` as a dependency.\n\n    .. versionchanged:: 3.0\n        Limited PEP 420 namespace package support.\n    \"\"\"\n\n    def __init__(\n        self,\n        package_name: str,\n        package_path: \"str\" = \"templates\",\n        encoding: str = \"utf-8\",\n    ) -> None:\n        package_path = os.path.normpath(package_path).rstrip(os.sep)\n\n        # normpath preserves \".\", which isn't valid in zip paths.\n        if package_path == os.path.curdir:\n            package_path = \"\"\n        elif package_path[:2] == os.path.curdir + os.sep:\n            package_path = package_path[2:]\n\n        self.package_path = package_path\n        self.package_name = package_name\n        self.encoding = encoding\n\n        # Make sure the package exists. This also makes namespace\n        # packages work, otherwise get_loader returns None.\n        import_module(package_name)\n        spec = importlib.util.find_spec(package_name)\n        assert spec is not None, \"An import spec was not found for the package.\"\n        loader = spec.loader\n        assert loader is not None, \"A loader was not found for the package.\"\n        self._loader = loader\n        self._archive = None\n\n        if isinstance(loader, zipimport.zipimporter):\n            self._archive = loader.archive\n            pkgdir = next(iter(spec.submodule_search_locations))  # type: ignore\n            template_root = os.path.join(pkgdir, package_path).rstrip(os.sep)\n        else:\n            roots: list[str] = []\n\n            # One element for regular packages, multiple for namespace\n            # packages, or None for single module file.\n            if spec.submodule_search_locations:\n                roots.extend(spec.submodule_search_locations)\n            # A single module file, use the parent directory instead.\n            elif spec.origin is not None:\n                roots.append(os.path.dirname(spec.origin))\n\n            if not roots:\n                raise ValueError(\n                    f\"The {package_name!r} package was not installed in a\"\n                    \" way that PackageLoader understands.\"\n                )\n\n            for root in roots:\n                root = os.path.join(root, package_path)\n\n                if os.path.isdir(root):\n                    template_root = root\n                    break\n            else:\n                raise ValueError(\n                    f\"PackageLoader could not find a {package_path!r} directory\"\n                    f\" in the {package_name!r} package.\"\n                )\n\n        self._template_root = template_root\n\n    def get_source(\n        self, environment: \"Environment\", template: str\n    ) -> tuple[str, str, t.Callable[[], bool] | None]:\n        # Use posixpath even on Windows to avoid \"drive:\" or UNC\n        # segments breaking out of the search directory. Use normpath to\n        # convert Windows altsep to sep.\n        p = os.path.normpath(\n            posixpath.join(self._template_root, *split_template_path(template))\n        )\n        up_to_date: t.Callable[[], bool] | None\n\n        if self._archive is None:\n            # Package is a directory.\n            if not os.path.isfile(p):\n                raise TemplateNotFound(template)\n\n            with open(p, \"rb\") as f:\n                source = f.read()\n\n            mtime = os.path.getmtime(p)\n\n            def up_to_date() -> bool:\n                return os.path.isfile(p) and os.path.getmtime(p) == mtime\n\n        else:\n            # Package is a zip file.\n            try:\n                source = self._loader.get_data(p)  # type: ignore\n            except OSError as e:\n                raise TemplateNotFound(template) from e\n\n            # Could use the zip's mtime for all template mtimes, but\n            # would need to safely reload the module if it's out of\n            # date, so just report it as always current.\n            up_to_date = None\n\n        return source.decode(self.encoding), p, up_to_date\n\n    def list_templates(self) -> list[str]:\n        results: list[str] = []\n\n        if self._archive is None:\n            # Package is a directory.\n            offset = len(self._template_root)\n\n            for dirpath, _, filenames in os.walk(self._template_root):\n                dirpath = dirpath[offset:].lstrip(os.sep)\n                results.extend(\n                    os.path.join(dirpath, name).replace(os.sep, \"/\")\n                    for name in filenames\n                )\n        else:\n            files = _get_zipimporter_files(self._loader)\n\n            # Package is a zip file.\n            prefix = self._template_root[len(self._archive) :].lstrip(os.sep) + os.sep\n            offset = len(prefix)\n\n            for name in files:\n                # Find names under the templates directory that aren't directories.\n                if name.startswith(prefix) and name[-1] != os.sep:\n                    results.append(name[offset:].replace(os.sep, \"/\"))\n\n        results.sort()\n        return results\n\n\nclass DictLoader(BaseLoader):\n    \"\"\"Loads a template from a Python dict mapping template names to\n    template source.  This loader is useful for unittesting:\n\n    >>> loader = DictLoader({'index.html': 'source here'})\n\n    Because auto reloading is rarely useful this is disabled by default.\n    \"\"\"\n\n    def __init__(self, mapping: t.Mapping[str, str]) -> None:\n        self.mapping = mapping\n\n    def get_source(\n        self, environment: \"Environment\", template: str\n    ) -> tuple[str, None, t.Callable[[], bool]]:\n        if template in self.mapping:\n            source = self.mapping[template]\n            return source, None, lambda: source == self.mapping.get(template)\n        raise TemplateNotFound(template)\n\n    def list_templates(self) -> list[str]:\n        return sorted(self.mapping)\n\n\nclass FunctionLoader(BaseLoader):\n    \"\"\"A loader that is passed a function which does the loading.  The\n    function receives the name of the template and has to return either\n    a string with the template source, a tuple in the form ``(source,\n    filename, uptodatefunc)`` or `None` if the template does not exist.\n\n    >>> def load_template(name):\n    ...     if name == 'index.html':\n    ...         return '...'\n    ...\n    >>> loader = FunctionLoader(load_template)\n\n    The `uptodatefunc` is a function that is called if autoreload is enabled\n    and has to return `True` if the template is still up to date.  For more\n    details have a look at :meth:`BaseLoader.get_source` which has the same\n    return value.\n    \"\"\"\n\n    def __init__(\n        self,\n        load_func: t.Callable[\n            [str],\n            str | tuple[str, str | None, t.Callable[[], bool] | None] | None,\n        ],\n    ) -> None:\n        self.load_func = load_func\n\n    def get_source(\n        self, environment: \"Environment\", template: str\n    ) -> tuple[str, str | None, t.Callable[[], bool] | None]:\n        rv = self.load_func(template)\n\n        if rv is None:\n            raise TemplateNotFound(template)\n\n        if isinstance(rv, str):\n            return rv, None, None\n\n        return rv\n\n\nclass PrefixLoader(BaseLoader):\n    \"\"\"A loader that is passed a dict of loaders where each loader is bound\n    to a prefix.  The prefix is delimited from the template by a slash per\n    default, which can be changed by setting the `delimiter` argument to\n    something else::\n\n        loader = PrefixLoader({\n            'app1':     PackageLoader('mypackage.app1'),\n            'app2':     PackageLoader('mypackage.app2')\n        })\n\n    By loading ``'app1/index.html'`` the file from the app1 package is loaded,\n    by loading ``'app2/index.html'`` the file from the second.\n    \"\"\"\n\n    def __init__(\n        self, mapping: t.Mapping[str, BaseLoader], delimiter: str = \"/\"\n    ) -> None:\n        self.mapping = mapping\n        self.delimiter = delimiter\n\n    def get_loader(self, template: str) -> tuple[BaseLoader, str]:\n        try:\n            prefix, name = template.split(self.delimiter, 1)\n            loader = self.mapping[prefix]\n        except (ValueError, KeyError) as e:\n            raise TemplateNotFound(template) from e\n        return loader, name\n\n    def get_source(\n        self, environment: \"Environment\", template: str\n    ) -> tuple[str, str | None, t.Callable[[], bool] | None]:\n        loader, name = self.get_loader(template)\n        try:\n            return loader.get_source(environment, name)\n        except TemplateNotFound as e:\n            # re-raise the exception with the correct filename here.\n            # (the one that includes the prefix)\n            raise TemplateNotFound(template) from e\n\n    @internalcode\n    def load(\n        self,\n        environment: \"Environment\",\n        name: str,\n        globals: t.MutableMapping[str, t.Any] | None = None,\n    ) -> \"Template\":\n        loader, local_name = self.get_loader(name)\n        try:\n            return loader.load(environment, local_name, globals)\n        except TemplateNotFound as e:\n            # re-raise the exception with the correct filename here.\n            # (the one that includes the prefix)\n            raise TemplateNotFound(name) from e\n\n    def list_templates(self) -> list[str]:\n        result = []\n        for prefix, loader in self.mapping.items():\n            for template in loader.list_templates():\n                result.append(prefix + self.delimiter + template)\n        return result\n\n\nclass ChoiceLoader(BaseLoader):\n    \"\"\"This loader works like the `PrefixLoader` just that no prefix is\n    specified.  If a template could not be found by one loader the next one\n    is tried.\n\n    >>> loader = ChoiceLoader([\n    ...     FileSystemLoader('/path/to/user/templates'),\n    ...     FileSystemLoader('/path/to/system/templates')\n    ... ])\n\n    This is useful if you want to allow users to override builtin templates\n    from a different location.\n    \"\"\"\n\n    def __init__(self, loaders: t.Sequence[BaseLoader]) -> None:\n        self.loaders = loaders\n\n    def get_source(\n        self, environment: \"Environment\", template: str\n    ) -> tuple[str, str | None, t.Callable[[], bool] | None]:\n        for loader in self.loaders:\n            try:\n                return loader.get_source(environment, template)\n            except TemplateNotFound:\n                pass\n        raise TemplateNotFound(template)\n\n    @internalcode\n    def load(\n        self,\n        environment: \"Environment\",\n        name: str,\n        globals: t.MutableMapping[str, t.Any] | None = None,\n    ) -> \"Template\":\n        for loader in self.loaders:\n            try:\n                return loader.load(environment, name, globals)\n            except TemplateNotFound:\n                pass\n        raise TemplateNotFound(name)\n\n    def list_templates(self) -> list[str]:\n        found = set()\n        for loader in self.loaders:\n            found.update(loader.list_templates())\n        return sorted(found)\n\n\nclass _TemplateModule(ModuleType):\n    \"\"\"Like a normal module but with support for weak references\"\"\"\n\n\nclass ModuleLoader(BaseLoader):\n    \"\"\"This loader loads templates from precompiled templates.\n\n    Example usage:\n\n    >>> loader = ModuleLoader('/path/to/compiled/templates')\n\n    Templates can be precompiled with :meth:`Environment.compile_templates`.\n    \"\"\"\n\n    has_source_access = False\n\n    def __init__(\n        self,\n        path: t.Union[\n            str, \"os.PathLike[str]\", t.Sequence[t.Union[str, \"os.PathLike[str]\"]]\n        ],\n    ) -> None:\n        package_name = f\"_jinja2_module_templates_{id(self):x}\"\n\n        # create a fake module that looks for the templates in the\n        # path given.\n        mod = _TemplateModule(package_name)\n\n        if not isinstance(path, abc.Iterable) or isinstance(path, str):\n            path = [path]\n\n        mod.__path__ = [os.fspath(p) for p in path]\n\n        sys.modules[package_name] = weakref.proxy(\n            mod, lambda x: sys.modules.pop(package_name, None)\n        )\n\n        # the only strong reference, the sys.modules entry is weak\n        # so that the garbage collector can remove it once the\n        # loader that created it goes out of business.\n        self.module = mod\n        self.package_name = package_name\n\n    @staticmethod\n    def get_template_key(name: str) -> str:\n        return \"tmpl_\" + sha1(name.encode(\"utf-8\")).hexdigest()\n\n    @staticmethod\n    def get_module_filename(name: str) -> str:\n        return ModuleLoader.get_template_key(name) + \".py\"\n\n    @internalcode\n    def load(\n        self,\n        environment: \"Environment\",\n        name: str,\n        globals: t.MutableMapping[str, t.Any] | None = None,\n    ) -> \"Template\":\n        key = self.get_template_key(name)\n        module = f\"{self.package_name}.{key}\"\n        mod = getattr(self.module, module, None)\n\n        if mod is None:\n            try:\n                mod = __import__(module, None, None, [\"root\"])\n            except ImportError as e:\n                raise TemplateNotFound(name) from e\n\n            # remove the entry from sys.modules, we only want the attribute\n            # on the module object we have stored on the loader.\n            sys.modules.pop(module, None)\n\n        if globals is None:\n            globals = {}\n\n        return environment.template_class.from_module_dict(\n            environment, mod.__dict__, globals\n        )\n"
  },
  {
    "path": "src/jinja2/meta.py",
    "content": "\"\"\"Functions that expose information about templates that might be\ninteresting for introspection.\n\"\"\"\n\nimport typing as t\n\nfrom . import nodes\nfrom .compiler import CodeGenerator\nfrom .compiler import Frame\n\nif t.TYPE_CHECKING:\n    from .environment import Environment\n\n\nclass TrackingCodeGenerator(CodeGenerator):\n    \"\"\"We abuse the code generator for introspection.\"\"\"\n\n    def __init__(self, environment: \"Environment\") -> None:\n        super().__init__(environment, \"<introspection>\", \"<introspection>\")\n        self.undeclared_identifiers: set[str] = set()\n\n    def write(self, x: str) -> None:\n        \"\"\"Don't write.\"\"\"\n\n    def enter_frame(self, frame: Frame) -> None:\n        \"\"\"Remember all undeclared identifiers.\"\"\"\n        super().enter_frame(frame)\n\n        for _, (action, param) in frame.symbols.loads.items():\n            if action == \"resolve\" and param not in self.environment.globals:\n                self.undeclared_identifiers.add(param)\n\n\ndef find_undeclared_variables(ast: nodes.Template) -> set[str]:\n    \"\"\"Returns a set of all variables in the AST that will be looked up from\n    the context at runtime.  Because at compile time it's not known which\n    variables will be used depending on the path the execution takes at\n    runtime, all variables are returned.\n\n    >>> from jinja2 import Environment, meta\n    >>> env = Environment()\n    >>> ast = env.parse('{% set foo = 42 %}{{ bar + foo }}')\n    >>> meta.find_undeclared_variables(ast) == {'bar'}\n    True\n\n    .. admonition:: Implementation\n\n       Internally the code generator is used for finding undeclared variables.\n       This is good to know because the code generator might raise a\n       :exc:`TemplateAssertionError` during compilation and as a matter of\n       fact this function can currently raise that exception as well.\n    \"\"\"\n    codegen = TrackingCodeGenerator(ast.environment)  # type: ignore\n    codegen.visit(ast)\n    return codegen.undeclared_identifiers\n\n\n_ref_types = (nodes.Extends, nodes.FromImport, nodes.Import, nodes.Include)\n_RefType = nodes.Extends | nodes.FromImport | nodes.Import | nodes.Include\n\n\ndef find_referenced_templates(ast: nodes.Template) -> t.Iterator[str | None]:\n    \"\"\"Finds all the referenced templates from the AST.  This will return an\n    iterator over all the hardcoded template extensions, inclusions and\n    imports.  If dynamic inheritance or inclusion is used, `None` will be\n    yielded.\n\n    >>> from jinja2 import Environment, meta\n    >>> env = Environment()\n    >>> ast = env.parse('{% extends \"layout.html\" %}{% include helper %}')\n    >>> list(meta.find_referenced_templates(ast))\n    ['layout.html', None]\n\n    This function is useful for dependency tracking.  For example if you want\n    to rebuild parts of the website after a layout template has changed.\n    \"\"\"\n    template_name: t.Any\n\n    for node in ast.find_all(_ref_types):\n        template: nodes.Expr = node.template  # type: ignore\n\n        if not isinstance(template, nodes.Const):\n            # a tuple with some non consts in there\n            if isinstance(template, (nodes.Tuple, nodes.List)):\n                for template_name in template.items:\n                    # something const, only yield the strings and ignore\n                    # non-string consts that really just make no sense\n                    if isinstance(template_name, nodes.Const):\n                        if isinstance(template_name.value, str):\n                            yield template_name.value\n                    # something dynamic in there\n                    else:\n                        yield None\n            # something dynamic we don't know about here\n            else:\n                yield None\n            continue\n        # constant is a basestring, direct template name\n        if isinstance(template.value, str):\n            yield template.value\n        # a tuple or list (latter *should* not happen) made of consts,\n        # yield the consts that are strings.  We could warn here for\n        # non string values\n        elif isinstance(node, nodes.Include) and isinstance(\n            template.value, (tuple, list)\n        ):\n            for template_name in template.value:\n                if isinstance(template_name, str):\n                    yield template_name\n        # something else we don't care about, we could warn here\n        else:\n            yield None\n"
  },
  {
    "path": "src/jinja2/nativetypes.py",
    "content": "import typing as t\nfrom ast import literal_eval\nfrom ast import parse\nfrom itertools import chain\nfrom itertools import islice\nfrom types import GeneratorType\n\nfrom . import nodes\nfrom .compiler import CodeGenerator\nfrom .compiler import Frame\nfrom .compiler import has_safe_repr\nfrom .environment import Environment\nfrom .environment import Template\n\n\ndef native_concat(values: t.Iterable[t.Any]) -> t.Any | None:\n    \"\"\"Return a native Python type from the list of compiled nodes. If\n    the result is a single node, its value is returned. Otherwise, the\n    nodes are concatenated as strings. If the result can be parsed with\n    :func:`ast.literal_eval`, the parsed value is returned. Otherwise,\n    the string is returned.\n\n    :param values: Iterable of outputs to concatenate.\n    \"\"\"\n    head = list(islice(values, 2))\n\n    if not head:\n        return None\n\n    if len(head) == 1:\n        raw = head[0]\n        if not isinstance(raw, str):\n            return raw\n    else:\n        if isinstance(values, GeneratorType):\n            values = chain(head, values)\n        raw = \"\".join([str(v) for v in values])\n\n    try:\n        return literal_eval(\n            # In Python 3.10+ ast.literal_eval removes leading spaces/tabs\n            # from the given string. For backwards compatibility we need to\n            # parse the string ourselves without removing leading spaces/tabs.\n            parse(raw, mode=\"eval\")\n        )\n    except (ValueError, SyntaxError, MemoryError):\n        return raw\n\n\nclass NativeCodeGenerator(CodeGenerator):\n    \"\"\"A code generator which renders Python types by not adding\n    ``str()`` around output nodes.\n    \"\"\"\n\n    @staticmethod\n    def _default_finalize(value: t.Any) -> t.Any:\n        return value\n\n    def _output_const_repr(self, group: t.Iterable[t.Any]) -> str:\n        return repr(\"\".join([str(v) for v in group]))\n\n    def _output_child_to_const(\n        self, node: nodes.Expr, frame: Frame, finalize: CodeGenerator._FinalizeInfo\n    ) -> t.Any:\n        const = node.as_const(frame.eval_ctx)\n\n        if not has_safe_repr(const):\n            raise nodes.Impossible()\n\n        if isinstance(node, nodes.TemplateData):\n            return const\n\n        return finalize.const(const)  # type: ignore\n\n    def _output_child_pre(\n        self, node: nodes.Expr, frame: Frame, finalize: CodeGenerator._FinalizeInfo\n    ) -> None:\n        if finalize.src is not None:\n            self.write(finalize.src)\n\n    def _output_child_post(\n        self, node: nodes.Expr, frame: Frame, finalize: CodeGenerator._FinalizeInfo\n    ) -> None:\n        if finalize.src is not None:\n            self.write(\")\")\n\n\nclass NativeEnvironment(Environment):\n    \"\"\"An environment that renders templates to native Python types.\"\"\"\n\n    code_generator_class = NativeCodeGenerator\n    concat = staticmethod(native_concat)  # type: ignore\n\n\nclass NativeTemplate(Template):\n    environment_class = NativeEnvironment\n\n    def render(self, *args: t.Any, **kwargs: t.Any) -> t.Any:\n        \"\"\"Render the template to produce a native Python type. If the\n        result is a single node, its value is returned. Otherwise, the\n        nodes are concatenated as strings. If the result can be parsed\n        with :func:`ast.literal_eval`, the parsed value is returned.\n        Otherwise, the string is returned.\n        \"\"\"\n        ctx = self.new_context(dict(*args, **kwargs))\n\n        try:\n            return self.environment_class.concat(  # type: ignore\n                self.root_render_func(ctx)\n            )\n        except Exception:\n            return self.environment.handle_exception()\n\n    async def render_async(self, *args: t.Any, **kwargs: t.Any) -> t.Any:\n        if not self.environment.is_async:\n            raise RuntimeError(\n                \"The environment was not created with async mode enabled.\"\n            )\n\n        ctx = self.new_context(dict(*args, **kwargs))\n\n        try:\n            return self.environment_class.concat(  # type: ignore\n                [n async for n in self.root_render_func(ctx)]  # type: ignore\n            )\n        except Exception:\n            return self.environment.handle_exception()\n\n\nNativeEnvironment.template_class = NativeTemplate\n"
  },
  {
    "path": "src/jinja2/nodes.py",
    "content": "\"\"\"AST nodes generated by the parser for the compiler. Also provides\nsome node tree helper functions used by the parser and compiler in order\nto normalize nodes.\n\"\"\"\n\nimport inspect\nimport operator\nimport typing as t\nfrom collections import deque\n\nfrom markupsafe import Markup\n\nfrom .utils import _PassArg\n\nif t.TYPE_CHECKING:\n    import typing_extensions as te\n\n    from .environment import Environment\n\n_NodeBound = t.TypeVar(\"_NodeBound\", bound=\"Node\")\n\n_binop_to_func: dict[str, t.Callable[[t.Any, t.Any], t.Any]] = {\n    \"*\": operator.mul,\n    \"/\": operator.truediv,\n    \"//\": operator.floordiv,\n    \"**\": operator.pow,\n    \"%\": operator.mod,\n    \"+\": operator.add,\n    \"-\": operator.sub,\n}\n\n_uaop_to_func: dict[str, t.Callable[[t.Any], t.Any]] = {\n    \"not\": operator.not_,\n    \"+\": operator.pos,\n    \"-\": operator.neg,\n}\n\n_cmpop_to_func: dict[str, t.Callable[[t.Any, t.Any], t.Any]] = {\n    \"eq\": operator.eq,\n    \"ne\": operator.ne,\n    \"gt\": operator.gt,\n    \"gteq\": operator.ge,\n    \"lt\": operator.lt,\n    \"lteq\": operator.le,\n    \"in\": lambda a, b: a in b,\n    \"notin\": lambda a, b: a not in b,\n}\n\n\nclass Impossible(Exception):\n    \"\"\"Raised if the node could not perform a requested action.\"\"\"\n\n\nclass NodeType(type):\n    \"\"\"A metaclass for nodes that handles the field and attribute\n    inheritance.  fields and attributes from the parent class are\n    automatically forwarded to the child.\"\"\"\n\n    def __new__(mcs, name, bases, d):  # type: ignore\n        for attr in \"fields\", \"attributes\":\n            storage: list[tuple[str, ...]] = []\n            storage.extend(getattr(bases[0] if bases else object, attr, ()))\n            storage.extend(d.get(attr, ()))\n            assert len(bases) <= 1, \"multiple inheritance not allowed\"\n            assert len(storage) == len(set(storage)), \"layout conflict\"\n            d[attr] = tuple(storage)\n        d.setdefault(\"abstract\", False)\n        return type.__new__(mcs, name, bases, d)\n\n\nclass EvalContext:\n    \"\"\"Holds evaluation time information.  Custom attributes can be attached\n    to it in extensions.\n    \"\"\"\n\n    def __init__(\n        self, environment: \"Environment\", template_name: str | None = None\n    ) -> None:\n        self.environment = environment\n        if callable(environment.autoescape):\n            self.autoescape = environment.autoescape(template_name)\n        else:\n            self.autoescape = environment.autoescape\n        self.volatile = False\n\n    def save(self) -> t.Mapping[str, t.Any]:\n        return self.__dict__.copy()\n\n    def revert(self, old: t.Mapping[str, t.Any]) -> None:\n        self.__dict__.clear()\n        self.__dict__.update(old)\n\n\ndef get_eval_context(node: \"Node\", ctx: EvalContext | None) -> EvalContext:\n    if ctx is None:\n        if node.environment is None:\n            raise RuntimeError(\n                \"if no eval context is passed, the node must have an\"\n                \" attached environment.\"\n            )\n        return EvalContext(node.environment)\n    return ctx\n\n\nclass Node(metaclass=NodeType):\n    \"\"\"Baseclass for all Jinja nodes.  There are a number of nodes available\n    of different types.  There are four major types:\n\n    -   :class:`Stmt`: statements\n    -   :class:`Expr`: expressions\n    -   :class:`Helper`: helper nodes\n    -   :class:`Template`: the outermost wrapper node\n\n    All nodes have fields and attributes.  Fields may be other nodes, lists,\n    or arbitrary values.  Fields are passed to the constructor as regular\n    positional arguments, attributes as keyword arguments.  Each node has\n    two attributes: `lineno` (the line number of the node) and `environment`.\n    The `environment` attribute is set at the end of the parsing process for\n    all nodes automatically.\n    \"\"\"\n\n    fields: tuple[str, ...] = ()\n    attributes: tuple[str, ...] = (\"lineno\", \"environment\")\n    abstract = True\n\n    lineno: int\n    environment: t.Optional[\"Environment\"]\n\n    def __init__(self, *fields: t.Any, **attributes: t.Any) -> None:\n        if self.abstract:\n            raise TypeError(\"abstract nodes are not instantiable\")\n        if fields:\n            if len(fields) != len(self.fields):\n                if not self.fields:\n                    raise TypeError(f\"{type(self).__name__!r} takes 0 arguments\")\n                raise TypeError(\n                    f\"{type(self).__name__!r} takes 0 or {len(self.fields)}\"\n                    f\" argument{'s' if len(self.fields) != 1 else ''}\"\n                )\n            for name, arg in zip(self.fields, fields, strict=False):\n                setattr(self, name, arg)\n        for attr in self.attributes:\n            setattr(self, attr, attributes.pop(attr, None))\n        if attributes:\n            raise TypeError(f\"unknown attribute {next(iter(attributes))!r}\")\n\n    def iter_fields(\n        self,\n        exclude: t.Container[str] | None = None,\n        only: t.Container[str] | None = None,\n    ) -> t.Iterator[tuple[str, t.Any]]:\n        \"\"\"This method iterates over all fields that are defined and yields\n        ``(key, value)`` tuples.  Per default all fields are returned, but\n        it's possible to limit that to some fields by providing the `only`\n        parameter or to exclude some using the `exclude` parameter.  Both\n        should be sets or tuples of field names.\n        \"\"\"\n        for name in self.fields:\n            if (\n                (exclude is None and only is None)\n                or (exclude is not None and name not in exclude)\n                or (only is not None and name in only)\n            ):\n                try:\n                    yield name, getattr(self, name)\n                except AttributeError:\n                    pass\n\n    def iter_child_nodes(\n        self,\n        exclude: t.Container[str] | None = None,\n        only: t.Container[str] | None = None,\n    ) -> t.Iterator[\"Node\"]:\n        \"\"\"Iterates over all direct child nodes of the node.  This iterates\n        over all fields and yields the values of they are nodes.  If the value\n        of a field is a list all the nodes in that list are returned.\n        \"\"\"\n        for _, item in self.iter_fields(exclude, only):\n            if isinstance(item, list):\n                for n in item:\n                    if isinstance(n, Node):\n                        yield n\n            elif isinstance(item, Node):\n                yield item\n\n    def find(self, node_type: type[_NodeBound]) -> _NodeBound | None:\n        \"\"\"Find the first node of a given type.  If no such node exists the\n        return value is `None`.\n        \"\"\"\n        for result in self.find_all(node_type):\n            return result\n\n        return None\n\n    def find_all(\n        self, node_type: type[_NodeBound] | tuple[type[_NodeBound], ...]\n    ) -> t.Iterator[_NodeBound]:\n        \"\"\"Find all the nodes of a given type.  If the type is a tuple,\n        the check is performed for any of the tuple items.\n        \"\"\"\n        for child in self.iter_child_nodes():\n            if isinstance(child, node_type):\n                yield child  # type: ignore\n            yield from child.find_all(node_type)\n\n    def set_ctx(self, ctx: str) -> \"Node\":\n        \"\"\"Reset the context of a node and all child nodes.  Per default the\n        parser will all generate nodes that have a 'load' context as it's the\n        most common one.  This method is used in the parser to set assignment\n        targets and other nodes to a store context.\n        \"\"\"\n        todo = deque([self])\n        while todo:\n            node = todo.popleft()\n            if \"ctx\" in node.fields:\n                node.ctx = ctx  # type: ignore\n            todo.extend(node.iter_child_nodes())\n        return self\n\n    def set_lineno(self, lineno: int, override: bool = False) -> \"Node\":\n        \"\"\"Set the line numbers of the node and children.\"\"\"\n        todo = deque([self])\n        while todo:\n            node = todo.popleft()\n            if \"lineno\" in node.attributes:\n                if node.lineno is None or override:\n                    node.lineno = lineno\n            todo.extend(node.iter_child_nodes())\n        return self\n\n    def set_environment(self, environment: \"Environment\") -> \"Node\":\n        \"\"\"Set the environment for all nodes.\"\"\"\n        todo = deque([self])\n        while todo:\n            node = todo.popleft()\n            node.environment = environment\n            todo.extend(node.iter_child_nodes())\n        return self\n\n    def __eq__(self, other: t.Any) -> bool:\n        if type(self) is not type(other):\n            return NotImplemented\n\n        return tuple(self.iter_fields()) == tuple(other.iter_fields())\n\n    __hash__ = object.__hash__\n\n    def __repr__(self) -> str:\n        args_str = \", \".join(f\"{a}={getattr(self, a, None)!r}\" for a in self.fields)\n        return f\"{type(self).__name__}({args_str})\"\n\n    def dump(self) -> str:\n        def _dump(node: Node | t.Any) -> None:\n            if not isinstance(node, Node):\n                buf.append(repr(node))\n                return\n\n            buf.append(f\"nodes.{type(node).__name__}(\")\n            if not node.fields:\n                buf.append(\")\")\n                return\n            for idx, field in enumerate(node.fields):\n                if idx:\n                    buf.append(\", \")\n                value = getattr(node, field)\n                if isinstance(value, list):\n                    buf.append(\"[\")\n                    for idx, item in enumerate(value):\n                        if idx:\n                            buf.append(\", \")\n                        _dump(item)\n                    buf.append(\"]\")\n                else:\n                    _dump(value)\n            buf.append(\")\")\n\n        buf: list[str] = []\n        _dump(self)\n        return \"\".join(buf)\n\n\nclass Stmt(Node):\n    \"\"\"Base node for all statements.\"\"\"\n\n    abstract = True\n\n\nclass Helper(Node):\n    \"\"\"Nodes that exist in a specific context only.\"\"\"\n\n    abstract = True\n\n\nclass Template(Node):\n    \"\"\"Node that represents a template.  This must be the outermost node that\n    is passed to the compiler.\n    \"\"\"\n\n    fields = (\"body\",)\n    body: list[Node]\n\n\nclass Output(Stmt):\n    \"\"\"A node that holds multiple expressions which are then printed out.\n    This is used both for the `print` statement and the regular template data.\n    \"\"\"\n\n    fields = (\"nodes\",)\n    nodes: list[\"Expr\"]\n\n\nclass Extends(Stmt):\n    \"\"\"Represents an extends statement.\"\"\"\n\n    fields = (\"template\",)\n    template: \"Expr\"\n\n\nclass For(Stmt):\n    \"\"\"The for loop.  `target` is the target for the iteration (usually a\n    :class:`Name` or :class:`Tuple`), `iter` the iterable.  `body` is a list\n    of nodes that are used as loop-body, and `else_` a list of nodes for the\n    `else` block.  If no else node exists it has to be an empty list.\n\n    For filtered nodes an expression can be stored as `test`, otherwise `None`.\n    \"\"\"\n\n    fields = (\"target\", \"iter\", \"body\", \"else_\", \"test\", \"recursive\")\n    target: Node\n    iter: Node\n    body: list[Node]\n    else_: list[Node]\n    test: Node | None\n    recursive: bool\n\n\nclass If(Stmt):\n    \"\"\"If `test` is true, `body` is rendered, else `else_`.\"\"\"\n\n    fields = (\"test\", \"body\", \"elif_\", \"else_\")\n    test: Node\n    body: list[Node]\n    elif_: list[\"If\"]\n    else_: list[Node]\n\n\nclass Macro(Stmt):\n    \"\"\"A macro definition.  `name` is the name of the macro, `args` a list of\n    arguments and `defaults` a list of defaults if there are any.  `body` is\n    a list of nodes for the macro body.\n    \"\"\"\n\n    fields = (\"name\", \"args\", \"defaults\", \"body\")\n    name: str\n    args: list[\"Name\"]\n    defaults: list[\"Expr\"]\n    body: list[Node]\n\n\nclass CallBlock(Stmt):\n    \"\"\"Like a macro without a name but a call instead.  `call` is called with\n    the unnamed macro as `caller` argument this node holds.\n    \"\"\"\n\n    fields = (\"call\", \"args\", \"defaults\", \"body\")\n    call: \"Call\"\n    args: list[\"Name\"]\n    defaults: list[\"Expr\"]\n    body: list[Node]\n\n\nclass FilterBlock(Stmt):\n    \"\"\"Node for filter sections.\"\"\"\n\n    fields = (\"body\", \"filter\")\n    body: list[Node]\n    filter: \"Filter\"\n\n\nclass With(Stmt):\n    \"\"\"Specific node for with statements.  In older versions of Jinja the\n    with statement was implemented on the base of the `Scope` node instead.\n\n    .. versionadded:: 2.9.3\n    \"\"\"\n\n    fields = (\"targets\", \"values\", \"body\")\n    targets: list[\"Expr\"]\n    values: list[\"Expr\"]\n    body: list[Node]\n\n\nclass Block(Stmt):\n    \"\"\"A node that represents a block.\n\n    .. versionchanged:: 3.0.0\n        the `required` field was added.\n    \"\"\"\n\n    fields = (\"name\", \"body\", \"scoped\", \"required\")\n    name: str\n    body: list[Node]\n    scoped: bool\n    required: bool\n\n\nclass Include(Stmt):\n    \"\"\"A node that represents the include tag.\"\"\"\n\n    fields = (\"template\", \"with_context\", \"ignore_missing\")\n    template: \"Expr\"\n    with_context: bool\n    ignore_missing: bool\n\n\nclass Import(Stmt):\n    \"\"\"A node that represents the import tag.\"\"\"\n\n    fields = (\"template\", \"target\", \"with_context\")\n    template: \"Expr\"\n    target: str\n    with_context: bool\n\n\nclass FromImport(Stmt):\n    \"\"\"A node that represents the from import tag.  It's important to not\n    pass unsafe names to the name attribute.  The compiler translates the\n    attribute lookups directly into getattr calls and does *not* use the\n    subscript callback of the interface.  As exported variables may not\n    start with double underscores (which the parser asserts) this is not a\n    problem for regular Jinja code, but if this node is used in an extension\n    extra care must be taken.\n\n    The list of names may contain tuples if aliases are wanted.\n    \"\"\"\n\n    fields = (\"template\", \"names\", \"with_context\")\n    template: \"Expr\"\n    names: list[str | tuple[str, str]]\n    with_context: bool\n\n\nclass ExprStmt(Stmt):\n    \"\"\"A statement that evaluates an expression and discards the result.\"\"\"\n\n    fields = (\"node\",)\n    node: Node\n\n\nclass Assign(Stmt):\n    \"\"\"Assigns an expression to a target.\"\"\"\n\n    fields = (\"target\", \"node\")\n    target: \"Expr\"\n    node: Node\n\n\nclass AssignBlock(Stmt):\n    \"\"\"Assigns a block to a target.\"\"\"\n\n    fields = (\"target\", \"filter\", \"body\")\n    target: \"Expr\"\n    filter: t.Optional[\"Filter\"]\n    body: list[Node]\n\n\nclass Expr(Node):\n    \"\"\"Baseclass for all expressions.\"\"\"\n\n    abstract = True\n\n    def as_const(self, eval_ctx: EvalContext | None = None) -> t.Any:\n        \"\"\"Return the value of the expression as constant or raise\n        :exc:`Impossible` if this was not possible.\n\n        An :class:`EvalContext` can be provided, if none is given\n        a default context is created which requires the nodes to have\n        an attached environment.\n\n        .. versionchanged:: 2.4\n           the `eval_ctx` parameter was added.\n        \"\"\"\n        raise Impossible()\n\n    def can_assign(self) -> bool:\n        \"\"\"Check if it's possible to assign something to this node.\"\"\"\n        return False\n\n\nclass BinExpr(Expr):\n    \"\"\"Baseclass for all binary expressions.\"\"\"\n\n    fields = (\"left\", \"right\")\n    left: Expr\n    right: Expr\n    operator: str\n    abstract = True\n\n    def as_const(self, eval_ctx: EvalContext | None = None) -> t.Any:\n        eval_ctx = get_eval_context(self, eval_ctx)\n\n        # intercepted operators cannot be folded at compile time\n        if (\n            eval_ctx.environment.sandboxed\n            and self.operator in eval_ctx.environment.intercepted_binops  # type: ignore\n        ):\n            raise Impossible()\n        f = _binop_to_func[self.operator]\n        try:\n            return f(self.left.as_const(eval_ctx), self.right.as_const(eval_ctx))\n        except Exception as e:\n            raise Impossible() from e\n\n\nclass UnaryExpr(Expr):\n    \"\"\"Baseclass for all unary expressions.\"\"\"\n\n    fields = (\"node\",)\n    node: Expr\n    operator: str\n    abstract = True\n\n    def as_const(self, eval_ctx: EvalContext | None = None) -> t.Any:\n        eval_ctx = get_eval_context(self, eval_ctx)\n\n        # intercepted operators cannot be folded at compile time\n        if (\n            eval_ctx.environment.sandboxed\n            and self.operator in eval_ctx.environment.intercepted_unops  # type: ignore\n        ):\n            raise Impossible()\n        f = _uaop_to_func[self.operator]\n        try:\n            return f(self.node.as_const(eval_ctx))\n        except Exception as e:\n            raise Impossible() from e\n\n\nclass Name(Expr):\n    \"\"\"Looks up a name or stores a value in a name.\n    The `ctx` of the node can be one of the following values:\n\n    -   `store`: store a value in the name\n    -   `load`: load that name\n    -   `param`: like `store` but if the name was defined as function parameter.\n    \"\"\"\n\n    fields = (\"name\", \"ctx\")\n    name: str\n    ctx: str\n\n    def can_assign(self) -> bool:\n        return self.name not in {\"true\", \"false\", \"none\", \"True\", \"False\", \"None\"}\n\n\nclass NSRef(Expr):\n    \"\"\"Reference to a namespace value assignment\"\"\"\n\n    fields = (\"name\", \"attr\")\n    name: str\n    attr: str\n\n    def can_assign(self) -> bool:\n        # We don't need any special checks here; NSRef assignments have a\n        # runtime check to ensure the target is a namespace object which will\n        # have been checked already as it is created using a normal assignment\n        # which goes through a `Name` node.\n        return True\n\n\nclass Literal(Expr):\n    \"\"\"Baseclass for literals.\"\"\"\n\n    abstract = True\n\n\nclass Const(Literal):\n    \"\"\"All constant values.  The parser will return this node for simple\n    constants such as ``42`` or ``\"foo\"`` but it can be used to store more\n    complex values such as lists too.  Only constants with a safe\n    representation (objects where ``eval(repr(x)) == x`` is true).\n    \"\"\"\n\n    fields = (\"value\",)\n    value: t.Any\n\n    def as_const(self, eval_ctx: EvalContext | None = None) -> t.Any:\n        return self.value\n\n    @classmethod\n    def from_untrusted(\n        cls,\n        value: t.Any,\n        lineno: int | None = None,\n        environment: \"Environment | None\" = None,\n    ) -> \"Const\":\n        \"\"\"Return a const object if the value is representable as\n        constant value in the generated code, otherwise it will raise\n        an `Impossible` exception.\n        \"\"\"\n        from .compiler import has_safe_repr\n\n        if not has_safe_repr(value):\n            raise Impossible()\n        return cls(value, lineno=lineno, environment=environment)\n\n\nclass TemplateData(Literal):\n    \"\"\"A constant template string.\"\"\"\n\n    fields = (\"data\",)\n    data: str\n\n    def as_const(self, eval_ctx: EvalContext | None = None) -> str:\n        eval_ctx = get_eval_context(self, eval_ctx)\n        if eval_ctx.volatile:\n            raise Impossible()\n        if eval_ctx.autoescape:\n            return Markup(self.data)\n        return self.data\n\n\nclass Tuple(Literal):\n    \"\"\"For loop unpacking and some other things like multiple arguments\n    for subscripts.  Like for :class:`Name` `ctx` specifies if the tuple\n    is used for loading the names or storing.\n    \"\"\"\n\n    fields = (\"items\", \"ctx\")\n    items: list[Expr]\n    ctx: str\n\n    def as_const(self, eval_ctx: EvalContext | None = None) -> tuple[t.Any, ...]:\n        eval_ctx = get_eval_context(self, eval_ctx)\n        return tuple(x.as_const(eval_ctx) for x in self.items)\n\n    def can_assign(self) -> bool:\n        for item in self.items:\n            if not item.can_assign():\n                return False\n        return True\n\n\nclass List(Literal):\n    \"\"\"Any list literal such as ``[1, 2, 3]``\"\"\"\n\n    fields = (\"items\",)\n    items: list[Expr]\n\n    def as_const(self, eval_ctx: EvalContext | None = None) -> list[t.Any]:\n        eval_ctx = get_eval_context(self, eval_ctx)\n        return [x.as_const(eval_ctx) for x in self.items]\n\n\nclass Dict(Literal):\n    \"\"\"Any dict literal such as ``{1: 2, 3: 4}``.  The items must be a list of\n    :class:`Pair` nodes.\n    \"\"\"\n\n    fields = (\"items\",)\n    items: list[\"Pair\"]\n\n    def as_const(self, eval_ctx: EvalContext | None = None) -> dict[t.Any, t.Any]:\n        eval_ctx = get_eval_context(self, eval_ctx)\n        return dict(x.as_const(eval_ctx) for x in self.items)\n\n\nclass Pair(Helper):\n    \"\"\"A key, value pair for dicts.\"\"\"\n\n    fields = (\"key\", \"value\")\n    key: Expr\n    value: Expr\n\n    def as_const(self, eval_ctx: EvalContext | None = None) -> tuple[t.Any, t.Any]:\n        eval_ctx = get_eval_context(self, eval_ctx)\n        return self.key.as_const(eval_ctx), self.value.as_const(eval_ctx)\n\n\nclass Keyword(Helper):\n    \"\"\"A key, value pair for keyword arguments where key is a string.\"\"\"\n\n    fields = (\"key\", \"value\")\n    key: str\n    value: Expr\n\n    def as_const(self, eval_ctx: EvalContext | None = None) -> tuple[str, t.Any]:\n        eval_ctx = get_eval_context(self, eval_ctx)\n        return self.key, self.value.as_const(eval_ctx)\n\n\nclass CondExpr(Expr):\n    \"\"\"A conditional expression (inline if expression).  (``{{\n    foo if bar else baz }}``)\n    \"\"\"\n\n    fields = (\"test\", \"expr1\", \"expr2\")\n    test: Expr\n    expr1: Expr\n    expr2: Expr | None\n\n    def as_const(self, eval_ctx: EvalContext | None = None) -> t.Any:\n        eval_ctx = get_eval_context(self, eval_ctx)\n        if self.test.as_const(eval_ctx):\n            return self.expr1.as_const(eval_ctx)\n\n        # if we evaluate to an undefined object, we better do that at runtime\n        if self.expr2 is None:\n            raise Impossible()\n\n        return self.expr2.as_const(eval_ctx)\n\n\ndef args_as_const(\n    node: t.Union[\"_FilterTestCommon\", \"Call\"], eval_ctx: EvalContext | None\n) -> tuple[list[t.Any], dict[t.Any, t.Any]]:\n    args = [x.as_const(eval_ctx) for x in node.args]\n    kwargs = dict(x.as_const(eval_ctx) for x in node.kwargs)\n\n    if node.dyn_args is not None:\n        try:\n            args.extend(node.dyn_args.as_const(eval_ctx))\n        except Exception as e:\n            raise Impossible() from e\n\n    if node.dyn_kwargs is not None:\n        try:\n            kwargs.update(node.dyn_kwargs.as_const(eval_ctx))\n        except Exception as e:\n            raise Impossible() from e\n\n    return args, kwargs\n\n\nclass _FilterTestCommon(Expr):\n    fields = (\"node\", \"name\", \"args\", \"kwargs\", \"dyn_args\", \"dyn_kwargs\")\n    node: Expr\n    name: str\n    args: list[Expr]\n    kwargs: list[Pair]\n    dyn_args: Expr | None\n    dyn_kwargs: Expr | None\n    abstract = True\n    _is_filter = True\n\n    def as_const(self, eval_ctx: EvalContext | None = None) -> t.Any:\n        eval_ctx = get_eval_context(self, eval_ctx)\n\n        if eval_ctx.volatile:\n            raise Impossible()\n\n        if self._is_filter:\n            env_map = eval_ctx.environment.filters\n        else:\n            env_map = eval_ctx.environment.tests\n\n        func = env_map.get(self.name)\n        pass_arg = _PassArg.from_obj(func)  # type: ignore\n\n        if func is None or pass_arg is _PassArg.context:\n            raise Impossible()\n\n        if eval_ctx.environment.is_async and (\n            getattr(func, \"jinja_async_variant\", False) is True\n            or inspect.iscoroutinefunction(func)\n        ):\n            raise Impossible()\n\n        args, kwargs = args_as_const(self, eval_ctx)\n        args.insert(0, self.node.as_const(eval_ctx))\n\n        if pass_arg is _PassArg.eval_context:\n            args.insert(0, eval_ctx)\n        elif pass_arg is _PassArg.environment:\n            args.insert(0, eval_ctx.environment)\n\n        try:\n            return func(*args, **kwargs)\n        except Exception as e:\n            raise Impossible() from e\n\n\nclass Filter(_FilterTestCommon):\n    \"\"\"Apply a filter to an expression. ``name`` is the name of the\n    filter, the other fields are the same as :class:`Call`.\n\n    If ``node`` is ``None``, the filter is being used in a filter block\n    and is applied to the content of the block.\n    \"\"\"\n\n    node: Expr | None  # type: ignore\n\n    def as_const(self, eval_ctx: EvalContext | None = None) -> t.Any:\n        if self.node is None:\n            raise Impossible()\n\n        return super().as_const(eval_ctx=eval_ctx)\n\n\nclass Test(_FilterTestCommon):\n    \"\"\"Apply a test to an expression. ``name`` is the name of the test,\n    the other field are the same as :class:`Call`.\n\n    .. versionchanged:: 3.0\n        ``as_const`` shares the same logic for filters and tests. Tests\n        check for volatile, async, and ``@pass_context`` etc.\n        decorators.\n    \"\"\"\n\n    _is_filter = False\n\n\nclass Call(Expr):\n    \"\"\"Calls an expression.  `args` is a list of arguments, `kwargs` a list\n    of keyword arguments (list of :class:`Keyword` nodes), and `dyn_args`\n    and `dyn_kwargs` has to be either `None` or a node that is used as\n    node for dynamic positional (``*args``) or keyword (``**kwargs``)\n    arguments.\n    \"\"\"\n\n    fields = (\"node\", \"args\", \"kwargs\", \"dyn_args\", \"dyn_kwargs\")\n    node: Expr\n    args: list[Expr]\n    kwargs: list[Keyword]\n    dyn_args: Expr | None\n    dyn_kwargs: Expr | None\n\n\nclass Getitem(Expr):\n    \"\"\"Get an attribute or item from an expression and prefer the item.\"\"\"\n\n    fields = (\"node\", \"arg\", \"ctx\")\n    node: Expr\n    arg: Expr\n    ctx: str\n\n    def as_const(self, eval_ctx: EvalContext | None = None) -> t.Any:\n        if self.ctx != \"load\":\n            raise Impossible()\n\n        eval_ctx = get_eval_context(self, eval_ctx)\n\n        try:\n            return eval_ctx.environment.getitem(\n                self.node.as_const(eval_ctx), self.arg.as_const(eval_ctx)\n            )\n        except Exception as e:\n            raise Impossible() from e\n\n\nclass Getattr(Expr):\n    \"\"\"Get an attribute or item from an expression that is a ascii-only\n    bytestring and prefer the attribute.\n    \"\"\"\n\n    fields = (\"node\", \"attr\", \"ctx\")\n    node: Expr\n    attr: str\n    ctx: str\n\n    def as_const(self, eval_ctx: EvalContext | None = None) -> t.Any:\n        if self.ctx != \"load\":\n            raise Impossible()\n\n        eval_ctx = get_eval_context(self, eval_ctx)\n\n        try:\n            return eval_ctx.environment.getattr(self.node.as_const(eval_ctx), self.attr)\n        except Exception as e:\n            raise Impossible() from e\n\n\nclass Slice(Expr):\n    \"\"\"Represents a slice object.  This must only be used as argument for\n    :class:`Subscript`.\n    \"\"\"\n\n    fields = (\"start\", \"stop\", \"step\")\n    start: Expr | None\n    stop: Expr | None\n    step: Expr | None\n\n    def as_const(self, eval_ctx: EvalContext | None = None) -> slice:\n        eval_ctx = get_eval_context(self, eval_ctx)\n\n        def const(obj: Expr | None) -> t.Any | None:\n            if obj is None:\n                return None\n            return obj.as_const(eval_ctx)\n\n        return slice(const(self.start), const(self.stop), const(self.step))\n\n\nclass Concat(Expr):\n    \"\"\"Concatenates the list of expressions provided after converting\n    them to strings.\n    \"\"\"\n\n    fields = (\"nodes\",)\n    nodes: list[Expr]\n\n    def as_const(self, eval_ctx: EvalContext | None = None) -> str:\n        eval_ctx = get_eval_context(self, eval_ctx)\n        return \"\".join(str(x.as_const(eval_ctx)) for x in self.nodes)\n\n\nclass Compare(Expr):\n    \"\"\"Compares an expression with some other expressions.  `ops` must be a\n    list of :class:`Operand`\\\\s.\n    \"\"\"\n\n    fields = (\"expr\", \"ops\")\n    expr: Expr\n    ops: list[\"Operand\"]\n\n    def as_const(self, eval_ctx: EvalContext | None = None) -> t.Any:\n        eval_ctx = get_eval_context(self, eval_ctx)\n        result = value = self.expr.as_const(eval_ctx)\n\n        try:\n            for op in self.ops:\n                new_value = op.expr.as_const(eval_ctx)\n                result = _cmpop_to_func[op.op](value, new_value)\n\n                if not result:\n                    return False\n\n                value = new_value\n        except Exception as e:\n            raise Impossible() from e\n\n        return result\n\n\nclass Operand(Helper):\n    \"\"\"Holds an operator and an expression.\"\"\"\n\n    fields = (\"op\", \"expr\")\n    op: str\n    expr: Expr\n\n\nclass Mul(BinExpr):\n    \"\"\"Multiplies the left with the right node.\"\"\"\n\n    operator = \"*\"\n\n\nclass Div(BinExpr):\n    \"\"\"Divides the left by the right node.\"\"\"\n\n    operator = \"/\"\n\n\nclass FloorDiv(BinExpr):\n    \"\"\"Divides the left by the right node and converts the\n    result into an integer by truncating.\n    \"\"\"\n\n    operator = \"//\"\n\n\nclass Add(BinExpr):\n    \"\"\"Add the left to the right node.\"\"\"\n\n    operator = \"+\"\n\n\nclass Sub(BinExpr):\n    \"\"\"Subtract the right from the left node.\"\"\"\n\n    operator = \"-\"\n\n\nclass Mod(BinExpr):\n    \"\"\"Left modulo right.\"\"\"\n\n    operator = \"%\"\n\n\nclass Pow(BinExpr):\n    \"\"\"Left to the power of right.\"\"\"\n\n    operator = \"**\"\n\n\nclass And(BinExpr):\n    \"\"\"Short circuited AND.\"\"\"\n\n    operator = \"and\"\n\n    def as_const(self, eval_ctx: EvalContext | None = None) -> t.Any:\n        eval_ctx = get_eval_context(self, eval_ctx)\n        return self.left.as_const(eval_ctx) and self.right.as_const(eval_ctx)\n\n\nclass Or(BinExpr):\n    \"\"\"Short circuited OR.\"\"\"\n\n    operator = \"or\"\n\n    def as_const(self, eval_ctx: EvalContext | None = None) -> t.Any:\n        eval_ctx = get_eval_context(self, eval_ctx)\n        return self.left.as_const(eval_ctx) or self.right.as_const(eval_ctx)\n\n\nclass Not(UnaryExpr):\n    \"\"\"Negate the expression.\"\"\"\n\n    operator = \"not\"\n\n\nclass Neg(UnaryExpr):\n    \"\"\"Make the expression negative.\"\"\"\n\n    operator = \"-\"\n\n\nclass Pos(UnaryExpr):\n    \"\"\"Make the expression positive (noop for most expressions)\"\"\"\n\n    operator = \"+\"\n\n\n# Helpers for extensions\n\n\nclass EnvironmentAttribute(Expr):\n    \"\"\"Loads an attribute from the environment object.  This is useful for\n    extensions that want to call a callback stored on the environment.\n    \"\"\"\n\n    fields = (\"name\",)\n    name: str\n\n\nclass ExtensionAttribute(Expr):\n    \"\"\"Returns the attribute of an extension bound to the environment.\n    The identifier is the identifier of the :class:`Extension`.\n\n    This node is usually constructed by calling the\n    :meth:`~jinja2.ext.Extension.attr` method on an extension.\n    \"\"\"\n\n    fields = (\"identifier\", \"name\")\n    identifier: str\n    name: str\n\n\nclass ImportedName(Expr):\n    \"\"\"If created with an import name the import name is returned on node\n    access.  For example ``ImportedName('cgi.escape')`` returns the `escape`\n    function from the cgi module on evaluation.  Imports are optimized by the\n    compiler so there is no need to assign them to local variables.\n    \"\"\"\n\n    fields = (\"importname\",)\n    importname: str\n\n\nclass InternalName(Expr):\n    \"\"\"An internal name in the compiler.  You cannot create these nodes\n    yourself but the parser provides a\n    :meth:`~jinja2.parser.Parser.free_identifier` method that creates\n    a new identifier for you.  This identifier is not available from the\n    template and is not treated specially by the compiler.\n    \"\"\"\n\n    fields = (\"name\",)\n    name: str\n\n    def __init__(self) -> None:\n        raise TypeError(\n            \"Can't create internal names.  Use the \"\n            \"`free_identifier` method on a parser.\"\n        )\n\n\nclass MarkSafe(Expr):\n    \"\"\"Mark the wrapped expression as safe (wrap it as `Markup`).\"\"\"\n\n    fields = (\"expr\",)\n    expr: Expr\n\n    def as_const(self, eval_ctx: EvalContext | None = None) -> Markup:\n        eval_ctx = get_eval_context(self, eval_ctx)\n        return Markup(self.expr.as_const(eval_ctx))\n\n\nclass MarkSafeIfAutoescape(Expr):\n    \"\"\"Mark the wrapped expression as safe (wrap it as `Markup`) but\n    only if autoescaping is active.\n\n    .. versionadded:: 2.5\n    \"\"\"\n\n    fields = (\"expr\",)\n    expr: Expr\n\n    def as_const(self, eval_ctx: EvalContext | None = None) -> Markup | t.Any:\n        eval_ctx = get_eval_context(self, eval_ctx)\n        if eval_ctx.volatile:\n            raise Impossible()\n        expr = self.expr.as_const(eval_ctx)\n        if eval_ctx.autoescape:\n            return Markup(expr)\n        return expr\n\n\nclass ContextReference(Expr):\n    \"\"\"Returns the current template context.  It can be used like a\n    :class:`Name` node, with a ``'load'`` ctx and will return the\n    current :class:`~jinja2.runtime.Context` object.\n\n    Here an example that assigns the current template name to a\n    variable named `foo`::\n\n        Assign(Name('foo', ctx='store'),\n               Getattr(ContextReference(), 'name'))\n\n    This is basically equivalent to using the\n    :func:`~jinja2.pass_context` decorator when using the high-level\n    API, which causes a reference to the context to be passed as the\n    first argument to a function.\n    \"\"\"\n\n\nclass DerivedContextReference(Expr):\n    \"\"\"Return the current template context including locals. Behaves\n    exactly like :class:`ContextReference`, but includes local\n    variables, such as from a ``for`` loop.\n\n    .. versionadded:: 2.11\n    \"\"\"\n\n\nclass Continue(Stmt):\n    \"\"\"Continue a loop.\"\"\"\n\n\nclass Break(Stmt):\n    \"\"\"Break a loop.\"\"\"\n\n\nclass Scope(Stmt):\n    \"\"\"An artificial scope.\"\"\"\n\n    fields = (\"body\",)\n    body: list[Node]\n\n\nclass OverlayScope(Stmt):\n    \"\"\"An overlay scope for extensions.  This is a largely unoptimized scope\n    that however can be used to introduce completely arbitrary variables into\n    a sub scope from a dictionary or dictionary like object.  The `context`\n    field has to evaluate to a dictionary object.\n\n    Example usage::\n\n        OverlayScope(context=self.call_method('get_context'),\n                     body=[...])\n\n    .. versionadded:: 2.10\n    \"\"\"\n\n    fields = (\"context\", \"body\")\n    context: Expr\n    body: list[Node]\n\n\nclass EvalContextModifier(Stmt):\n    \"\"\"Modifies the eval context.  For each option that should be modified,\n    a :class:`Keyword` has to be added to the :attr:`options` list.\n\n    Example to change the `autoescape` setting::\n\n        EvalContextModifier(options=[Keyword('autoescape', Const(True))])\n    \"\"\"\n\n    fields = (\"options\",)\n    options: list[Keyword]\n\n\nclass ScopedEvalContextModifier(EvalContextModifier):\n    \"\"\"Modifies the eval context and reverts it later.  Works exactly like\n    :class:`EvalContextModifier` but will only modify the\n    :class:`~jinja2.nodes.EvalContext` for nodes in the :attr:`body`.\n    \"\"\"\n\n    fields = (\"body\",)\n    body: list[Node]\n\n\n# make sure nobody creates custom nodes\ndef _failing_new(*args: t.Any, **kwargs: t.Any) -> \"te.NoReturn\":\n    raise TypeError(\"can't create custom node types\")\n\n\nNodeType.__new__ = staticmethod(_failing_new)  # type: ignore\ndel _failing_new\n"
  },
  {
    "path": "src/jinja2/optimizer.py",
    "content": "\"\"\"The optimizer tries to constant fold expressions and modify the AST\nin place so that it should be faster to evaluate.\n\nBecause the AST does not contain all the scoping information and the\ncompiler has to find that out, we cannot do all the optimizations we\nwant. For example, loop unrolling doesn't work because unrolled loops\nwould have a different scope. The solution would be a second syntax tree\nthat stored the scoping rules.\n\"\"\"\n\nimport typing as t\n\nfrom . import nodes\nfrom .visitor import NodeTransformer\n\nif t.TYPE_CHECKING:\n    from .environment import Environment\n\n\ndef optimize(node: nodes.Node, environment: \"Environment\") -> nodes.Node:\n    \"\"\"The context hint can be used to perform an static optimization\n    based on the context given.\"\"\"\n    optimizer = Optimizer(environment)\n    return t.cast(nodes.Node, optimizer.visit(node))\n\n\nclass Optimizer(NodeTransformer):\n    def __init__(self, environment: \"Environment | None\") -> None:\n        self.environment = environment\n\n    def generic_visit(\n        self, node: nodes.Node, *args: t.Any, **kwargs: t.Any\n    ) -> nodes.Node:\n        node = super().generic_visit(node, *args, **kwargs)\n\n        # Do constant folding. Some other nodes besides Expr have\n        # as_const, but folding them causes errors later on.\n        if isinstance(node, nodes.Expr):\n            try:\n                return nodes.Const.from_untrusted(\n                    node.as_const(args[0] if args else None),\n                    lineno=node.lineno,\n                    environment=self.environment,\n                )\n            except nodes.Impossible:\n                pass\n\n        return node\n"
  },
  {
    "path": "src/jinja2/parser.py",
    "content": "\"\"\"Parse tokens from the lexer into nodes for the compiler.\"\"\"\n\nimport typing\nimport typing as t\n\nfrom . import nodes\nfrom .exceptions import TemplateAssertionError\nfrom .exceptions import TemplateSyntaxError\nfrom .lexer import describe_token\nfrom .lexer import describe_token_expr\n\nif t.TYPE_CHECKING:\n    import typing_extensions as te\n\n    from .environment import Environment\n\n_ImportInclude = t.TypeVar(\"_ImportInclude\", nodes.Import, nodes.Include)\n_MacroCall = t.TypeVar(\"_MacroCall\", nodes.Macro, nodes.CallBlock)\n\n_statement_keywords = frozenset(\n    [\n        \"for\",\n        \"if\",\n        \"block\",\n        \"extends\",\n        \"print\",\n        \"macro\",\n        \"include\",\n        \"from\",\n        \"import\",\n        \"set\",\n        \"with\",\n        \"autoescape\",\n    ]\n)\n_compare_operators = frozenset([\"eq\", \"ne\", \"lt\", \"lteq\", \"gt\", \"gteq\"])\n\n_math_nodes: dict[str, type[nodes.Expr]] = {\n    \"add\": nodes.Add,\n    \"sub\": nodes.Sub,\n    \"mul\": nodes.Mul,\n    \"div\": nodes.Div,\n    \"floordiv\": nodes.FloorDiv,\n    \"mod\": nodes.Mod,\n}\n\n\nclass Parser:\n    \"\"\"This is the central parsing class Jinja uses.  It's passed to\n    extensions and can be used to parse expressions or statements.\n    \"\"\"\n\n    def __init__(\n        self,\n        environment: \"Environment\",\n        source: str,\n        name: str | None = None,\n        filename: str | None = None,\n        state: str | None = None,\n    ) -> None:\n        self.environment = environment\n        self.stream = environment._tokenize(source, name, filename, state)\n        self.name = name\n        self.filename = filename\n        self.closed = False\n        self.extensions: dict[\n            str, t.Callable[[Parser], nodes.Node | list[nodes.Node]]\n        ] = {}\n        for extension in environment.iter_extensions():\n            for tag in extension.tags:\n                self.extensions[tag] = extension.parse\n        self._last_identifier = 0\n        self._tag_stack: list[str] = []\n        self._end_token_stack: list[tuple[str, ...]] = []\n\n    def fail(\n        self,\n        msg: str,\n        lineno: int | None = None,\n        exc: type[TemplateSyntaxError] = TemplateSyntaxError,\n    ) -> \"te.NoReturn\":\n        \"\"\"Convenience method that raises `exc` with the message, passed\n        line number or last line number as well as the current name and\n        filename.\n        \"\"\"\n        if lineno is None:\n            lineno = self.stream.current.lineno\n        raise exc(msg, lineno, self.name, self.filename)\n\n    def _fail_ut_eof(\n        self,\n        name: str | None,\n        end_token_stack: list[tuple[str, ...]],\n        lineno: int | None,\n    ) -> \"te.NoReturn\":\n        expected: set[str] = set()\n        for exprs in end_token_stack:\n            expected.update(map(describe_token_expr, exprs))\n        if end_token_stack:\n            currently_looking: str | None = \" or \".join(\n                map(repr, map(describe_token_expr, end_token_stack[-1]))\n            )\n        else:\n            currently_looking = None\n\n        if name is None:\n            message = [\"Unexpected end of template.\"]\n        else:\n            message = [f\"Encountered unknown tag {name!r}.\"]\n\n        if currently_looking:\n            if name is not None and name in expected:\n                message.append(\n                    \"You probably made a nesting mistake. Jinja is expecting this tag,\"\n                    f\" but currently looking for {currently_looking}.\"\n                )\n            else:\n                message.append(\n                    f\"Jinja was looking for the following tags: {currently_looking}.\"\n                )\n\n        if self._tag_stack:\n            message.append(\n                \"The innermost block that needs to be closed is\"\n                f\" {self._tag_stack[-1]!r}.\"\n            )\n\n        self.fail(\" \".join(message), lineno)\n\n    def fail_unknown_tag(self, name: str, lineno: int | None = None) -> \"te.NoReturn\":\n        \"\"\"Called if the parser encounters an unknown tag.  Tries to fail\n        with a human readable error message that could help to identify\n        the problem.\n        \"\"\"\n        self._fail_ut_eof(name, self._end_token_stack, lineno)\n\n    def fail_eof(\n        self,\n        end_tokens: tuple[str, ...] | None = None,\n        lineno: int | None = None,\n    ) -> \"te.NoReturn\":\n        \"\"\"Like fail_unknown_tag but for end of template situations.\"\"\"\n        stack = list(self._end_token_stack)\n        if end_tokens is not None:\n            stack.append(end_tokens)\n        self._fail_ut_eof(None, stack, lineno)\n\n    def is_tuple_end(self, extra_end_rules: tuple[str, ...] | None = None) -> bool:\n        \"\"\"Are we at the end of a tuple?\"\"\"\n        if self.stream.current.type in (\"variable_end\", \"block_end\", \"rparen\"):\n            return True\n        elif extra_end_rules is not None:\n            return self.stream.current.test_any(extra_end_rules)  # type: ignore\n        return False\n\n    def free_identifier(self, lineno: int | None = None) -> nodes.InternalName:\n        \"\"\"Return a new free identifier as :class:`~jinja2.nodes.InternalName`.\"\"\"\n        self._last_identifier += 1\n        rv = object.__new__(nodes.InternalName)\n        nodes.Node.__init__(rv, f\"fi{self._last_identifier}\", lineno=lineno)\n        return rv\n\n    def parse_statement(self) -> nodes.Node | list[nodes.Node]:\n        \"\"\"Parse a single statement.\"\"\"\n        token = self.stream.current\n        if token.type != \"name\":\n            self.fail(\"tag name expected\", token.lineno)\n        self._tag_stack.append(token.value)\n        pop_tag = True\n        try:\n            if token.value in _statement_keywords:\n                f = getattr(self, f\"parse_{self.stream.current.value}\")\n                return f()  # type: ignore\n            if token.value == \"call\":\n                return self.parse_call_block()\n            if token.value == \"filter\":\n                return self.parse_filter_block()\n            ext = self.extensions.get(token.value)\n            if ext is not None:\n                return ext(self)\n\n            # did not work out, remove the token we pushed by accident\n            # from the stack so that the unknown tag fail function can\n            # produce a proper error message.\n            self._tag_stack.pop()\n            pop_tag = False\n            self.fail_unknown_tag(token.value, token.lineno)\n        finally:\n            if pop_tag:\n                self._tag_stack.pop()\n\n    def parse_statements(\n        self, end_tokens: tuple[str, ...], drop_needle: bool = False\n    ) -> list[nodes.Node]:\n        \"\"\"Parse multiple statements into a list until one of the end tokens\n        is reached.  This is used to parse the body of statements as it also\n        parses template data if appropriate.  The parser checks first if the\n        current token is a colon and skips it if there is one.  Then it checks\n        for the block end and parses until if one of the `end_tokens` is\n        reached.  Per default the active token in the stream at the end of\n        the call is the matched end token.  If this is not wanted `drop_needle`\n        can be set to `True` and the end token is removed.\n        \"\"\"\n        # the first token may be a colon for python compatibility\n        self.stream.skip_if(\"colon\")\n\n        # in the future it would be possible to add whole code sections\n        # by adding some sort of end of statement token and parsing those here.\n        self.stream.expect(\"block_end\")\n        result = self.subparse(end_tokens)\n\n        # we reached the end of the template too early, the subparser\n        # does not check for this, so we do that now\n        if self.stream.current.type == \"eof\":\n            self.fail_eof(end_tokens)\n\n        if drop_needle:\n            next(self.stream)\n        return result\n\n    def parse_set(self) -> nodes.Assign | nodes.AssignBlock:\n        \"\"\"Parse an assign statement.\"\"\"\n        lineno = next(self.stream).lineno\n        target = self.parse_assign_target(with_namespace=True)\n        if self.stream.skip_if(\"assign\"):\n            expr = self.parse_tuple()\n            return nodes.Assign(target, expr, lineno=lineno)\n        filter_node = self.parse_filter(None)\n        body = self.parse_statements((\"name:endset\",), drop_needle=True)\n        return nodes.AssignBlock(target, filter_node, body, lineno=lineno)\n\n    def parse_for(self) -> nodes.For:\n        \"\"\"Parse a for loop.\"\"\"\n        lineno = self.stream.expect(\"name:for\").lineno\n        target = self.parse_assign_target(extra_end_rules=(\"name:in\",))\n        self.stream.expect(\"name:in\")\n        iter = self.parse_tuple(\n            with_condexpr=False, extra_end_rules=(\"name:recursive\",)\n        )\n        test = None\n        if self.stream.skip_if(\"name:if\"):\n            test = self.parse_expression()\n        recursive = self.stream.skip_if(\"name:recursive\")\n        body = self.parse_statements((\"name:endfor\", \"name:else\"))\n        if next(self.stream).value == \"endfor\":\n            else_ = []\n        else:\n            else_ = self.parse_statements((\"name:endfor\",), drop_needle=True)\n        return nodes.For(target, iter, body, else_, test, recursive, lineno=lineno)\n\n    def parse_if(self) -> nodes.If:\n        \"\"\"Parse an if construct.\"\"\"\n        node = result = nodes.If(lineno=self.stream.expect(\"name:if\").lineno)\n        while True:\n            node.test = self.parse_tuple(with_condexpr=False)\n            node.body = self.parse_statements((\"name:elif\", \"name:else\", \"name:endif\"))\n            node.elif_ = []\n            node.else_ = []\n            token = next(self.stream)\n            if token.test(\"name:elif\"):\n                node = nodes.If(lineno=self.stream.current.lineno)\n                result.elif_.append(node)\n                continue\n            elif token.test(\"name:else\"):\n                result.else_ = self.parse_statements((\"name:endif\",), drop_needle=True)\n            break\n        return result\n\n    def parse_with(self) -> nodes.With:\n        node = nodes.With(lineno=next(self.stream).lineno)\n        targets: list[nodes.Expr] = []\n        values: list[nodes.Expr] = []\n        while self.stream.current.type != \"block_end\":\n            if targets:\n                self.stream.expect(\"comma\")\n            target = self.parse_assign_target()\n            target.set_ctx(\"param\")\n            targets.append(target)\n            self.stream.expect(\"assign\")\n            values.append(self.parse_expression())\n        node.targets = targets\n        node.values = values\n        node.body = self.parse_statements((\"name:endwith\",), drop_needle=True)\n        return node\n\n    def parse_autoescape(self) -> nodes.Scope:\n        node = nodes.ScopedEvalContextModifier(lineno=next(self.stream).lineno)\n        node.options = [nodes.Keyword(\"autoescape\", self.parse_expression())]\n        node.body = self.parse_statements((\"name:endautoescape\",), drop_needle=True)\n        return nodes.Scope([node])\n\n    def parse_block(self) -> nodes.Block:\n        node = nodes.Block(lineno=next(self.stream).lineno)\n        node.name = self.stream.expect(\"name\").value\n        node.scoped = self.stream.skip_if(\"name:scoped\")\n        node.required = self.stream.skip_if(\"name:required\")\n\n        # common problem people encounter when switching from django\n        # to jinja.  we do not support hyphens in block names, so let's\n        # raise a nicer error message in that case.\n        if self.stream.current.type == \"sub\":\n            self.fail(\n                \"Block names in Jinja have to be valid Python identifiers and may not\"\n                \" contain hyphens, use an underscore instead.\"\n            )\n\n        node.body = self.parse_statements((\"name:endblock\",), drop_needle=True)\n\n        # enforce that required blocks only contain whitespace or comments\n        # by asserting that the body, if not empty, is just TemplateData nodes\n        # with whitespace data\n        if node.required:\n            for body_node in node.body:\n                if not isinstance(body_node, nodes.Output) or any(\n                    not isinstance(output_node, nodes.TemplateData)\n                    or not output_node.data.isspace()\n                    for output_node in body_node.nodes\n                ):\n                    self.fail(\"Required blocks can only contain comments or whitespace\")\n\n        self.stream.skip_if(\"name:\" + node.name)\n        return node\n\n    def parse_extends(self) -> nodes.Extends:\n        node = nodes.Extends(lineno=next(self.stream).lineno)\n        node.template = self.parse_expression()\n        return node\n\n    def parse_import_context(\n        self, node: _ImportInclude, default: bool\n    ) -> _ImportInclude:\n        if self.stream.current.test_any(\n            \"name:with\", \"name:without\"\n        ) and self.stream.look().test(\"name:context\"):\n            node.with_context = next(self.stream).value == \"with\"\n            self.stream.skip()\n        else:\n            node.with_context = default\n        return node\n\n    def parse_include(self) -> nodes.Include:\n        node = nodes.Include(lineno=next(self.stream).lineno)\n        node.template = self.parse_expression()\n        if self.stream.current.test(\"name:ignore\") and self.stream.look().test(\n            \"name:missing\"\n        ):\n            node.ignore_missing = True\n            self.stream.skip(2)\n        else:\n            node.ignore_missing = False\n        return self.parse_import_context(node, True)\n\n    def parse_import(self) -> nodes.Import:\n        node = nodes.Import(lineno=next(self.stream).lineno)\n        node.template = self.parse_expression()\n        self.stream.expect(\"name:as\")\n        node.target = self.parse_assign_target(name_only=True).name\n        return self.parse_import_context(node, False)\n\n    def parse_from(self) -> nodes.FromImport:\n        node = nodes.FromImport(lineno=next(self.stream).lineno)\n        node.template = self.parse_expression()\n        self.stream.expect(\"name:import\")\n        node.names = []\n\n        def parse_context() -> bool:\n            if self.stream.current.value in {\n                \"with\",\n                \"without\",\n            } and self.stream.look().test(\"name:context\"):\n                node.with_context = next(self.stream).value == \"with\"\n                self.stream.skip()\n                return True\n            return False\n\n        while True:\n            if node.names:\n                self.stream.expect(\"comma\")\n            if self.stream.current.type == \"name\":\n                if parse_context():\n                    break\n                target = self.parse_assign_target(name_only=True)\n                if target.name.startswith(\"_\"):\n                    self.fail(\n                        \"names starting with an underline can not be imported\",\n                        target.lineno,\n                        exc=TemplateAssertionError,\n                    )\n                if self.stream.skip_if(\"name:as\"):\n                    alias = self.parse_assign_target(name_only=True)\n                    node.names.append((target.name, alias.name))\n                else:\n                    node.names.append(target.name)\n                if parse_context() or self.stream.current.type != \"comma\":\n                    break\n            else:\n                self.stream.expect(\"name\")\n        if not hasattr(node, \"with_context\"):\n            node.with_context = False\n        return node\n\n    def parse_signature(self, node: _MacroCall) -> None:\n        args = node.args = []\n        defaults = node.defaults = []\n        self.stream.expect(\"lparen\")\n        while self.stream.current.type != \"rparen\":\n            if args:\n                self.stream.expect(\"comma\")\n            arg = self.parse_assign_target(name_only=True)\n            arg.set_ctx(\"param\")\n            if self.stream.skip_if(\"assign\"):\n                defaults.append(self.parse_expression())\n            elif defaults:\n                self.fail(\"non-default argument follows default argument\")\n            args.append(arg)\n        self.stream.expect(\"rparen\")\n\n    def parse_call_block(self) -> nodes.CallBlock:\n        node = nodes.CallBlock(lineno=next(self.stream).lineno)\n        if self.stream.current.type == \"lparen\":\n            self.parse_signature(node)\n        else:\n            node.args = []\n            node.defaults = []\n\n        call_node = self.parse_expression()\n        if not isinstance(call_node, nodes.Call):\n            self.fail(\"expected call\", node.lineno)\n        node.call = call_node\n        node.body = self.parse_statements((\"name:endcall\",), drop_needle=True)\n        return node\n\n    def parse_filter_block(self) -> nodes.FilterBlock:\n        node = nodes.FilterBlock(lineno=next(self.stream).lineno)\n        node.filter = self.parse_filter(None, start_inline=True)  # type: ignore\n        node.body = self.parse_statements((\"name:endfilter\",), drop_needle=True)\n        return node\n\n    def parse_macro(self) -> nodes.Macro:\n        node = nodes.Macro(lineno=next(self.stream).lineno)\n        node.name = self.parse_assign_target(name_only=True).name\n        self.parse_signature(node)\n        node.body = self.parse_statements((\"name:endmacro\",), drop_needle=True)\n        return node\n\n    def parse_print(self) -> nodes.Output:\n        node = nodes.Output(lineno=next(self.stream).lineno)\n        node.nodes = []\n        while self.stream.current.type != \"block_end\":\n            if node.nodes:\n                self.stream.expect(\"comma\")\n            node.nodes.append(self.parse_expression())\n        return node\n\n    @typing.overload\n    def parse_assign_target(\n        self, with_tuple: bool = ..., name_only: \"te.Literal[True]\" = ...\n    ) -> nodes.Name: ...\n\n    @typing.overload\n    def parse_assign_target(\n        self,\n        with_tuple: bool = True,\n        name_only: bool = False,\n        extra_end_rules: tuple[str, ...] | None = None,\n        with_namespace: bool = False,\n    ) -> nodes.NSRef | nodes.Name | nodes.Tuple: ...\n\n    def parse_assign_target(\n        self,\n        with_tuple: bool = True,\n        name_only: bool = False,\n        extra_end_rules: tuple[str, ...] | None = None,\n        with_namespace: bool = False,\n    ) -> nodes.NSRef | nodes.Name | nodes.Tuple:\n        \"\"\"Parse an assignment target.  As Jinja allows assignments to\n        tuples, this function can parse all allowed assignment targets.  Per\n        default assignments to tuples are parsed, that can be disable however\n        by setting `with_tuple` to `False`.  If only assignments to names are\n        wanted `name_only` can be set to `True`.  The `extra_end_rules`\n        parameter is forwarded to the tuple parsing function.  If\n        `with_namespace` is enabled, a namespace assignment may be parsed.\n        \"\"\"\n        target: nodes.Expr\n\n        if name_only:\n            token = self.stream.expect(\"name\")\n            target = nodes.Name(token.value, \"store\", lineno=token.lineno)\n        else:\n            if with_tuple:\n                target = self.parse_tuple(\n                    simplified=True,\n                    extra_end_rules=extra_end_rules,\n                    with_namespace=with_namespace,\n                )\n            else:\n                target = self.parse_primary(with_namespace=with_namespace)\n\n            target.set_ctx(\"store\")\n\n        if not target.can_assign():\n            self.fail(\n                f\"can't assign to {type(target).__name__.lower()!r}\", target.lineno\n            )\n\n        return target  # type: ignore\n\n    def parse_expression(self, with_condexpr: bool = True) -> nodes.Expr:\n        \"\"\"Parse an expression.  Per default all expressions are parsed, if\n        the optional `with_condexpr` parameter is set to `False` conditional\n        expressions are not parsed.\n        \"\"\"\n        if with_condexpr:\n            return self.parse_condexpr()\n        return self.parse_or()\n\n    def parse_condexpr(self) -> nodes.Expr:\n        lineno = self.stream.current.lineno\n        expr1 = self.parse_or()\n        expr3: nodes.Expr | None\n\n        while self.stream.skip_if(\"name:if\"):\n            expr2 = self.parse_or()\n            if self.stream.skip_if(\"name:else\"):\n                expr3 = self.parse_condexpr()\n            else:\n                expr3 = None\n            expr1 = nodes.CondExpr(expr2, expr1, expr3, lineno=lineno)\n            lineno = self.stream.current.lineno\n        return expr1\n\n    def parse_or(self) -> nodes.Expr:\n        lineno = self.stream.current.lineno\n        left = self.parse_and()\n        while self.stream.skip_if(\"name:or\"):\n            right = self.parse_and()\n            left = nodes.Or(left, right, lineno=lineno)\n            lineno = self.stream.current.lineno\n        return left\n\n    def parse_and(self) -> nodes.Expr:\n        lineno = self.stream.current.lineno\n        left = self.parse_not()\n        while self.stream.skip_if(\"name:and\"):\n            right = self.parse_not()\n            left = nodes.And(left, right, lineno=lineno)\n            lineno = self.stream.current.lineno\n        return left\n\n    def parse_not(self) -> nodes.Expr:\n        if self.stream.current.test(\"name:not\"):\n            lineno = next(self.stream).lineno\n            return nodes.Not(self.parse_not(), lineno=lineno)\n        return self.parse_compare()\n\n    def parse_compare(self) -> nodes.Expr:\n        lineno = self.stream.current.lineno\n        expr = self.parse_math1()\n        ops = []\n        while True:\n            token_type = self.stream.current.type\n            if token_type in _compare_operators:\n                next(self.stream)\n                ops.append(nodes.Operand(token_type, self.parse_math1()))\n            elif self.stream.skip_if(\"name:in\"):\n                ops.append(nodes.Operand(\"in\", self.parse_math1()))\n            elif self.stream.current.test(\"name:not\") and self.stream.look().test(\n                \"name:in\"\n            ):\n                self.stream.skip(2)\n                ops.append(nodes.Operand(\"notin\", self.parse_math1()))\n            else:\n                break\n            lineno = self.stream.current.lineno\n        if not ops:\n            return expr\n        return nodes.Compare(expr, ops, lineno=lineno)\n\n    def parse_math1(self) -> nodes.Expr:\n        lineno = self.stream.current.lineno\n        left = self.parse_concat()\n        while self.stream.current.type in (\"add\", \"sub\"):\n            cls = _math_nodes[self.stream.current.type]\n            next(self.stream)\n            right = self.parse_concat()\n            left = cls(left, right, lineno=lineno)\n            lineno = self.stream.current.lineno\n        return left\n\n    def parse_concat(self) -> nodes.Expr:\n        lineno = self.stream.current.lineno\n        args = [self.parse_math2()]\n        while self.stream.current.type == \"tilde\":\n            next(self.stream)\n            args.append(self.parse_math2())\n        if len(args) == 1:\n            return args[0]\n        return nodes.Concat(args, lineno=lineno)\n\n    def parse_math2(self) -> nodes.Expr:\n        lineno = self.stream.current.lineno\n        left = self.parse_pow()\n        while self.stream.current.type in (\"mul\", \"div\", \"floordiv\", \"mod\"):\n            cls = _math_nodes[self.stream.current.type]\n            next(self.stream)\n            right = self.parse_pow()\n            left = cls(left, right, lineno=lineno)\n            lineno = self.stream.current.lineno\n        return left\n\n    def parse_pow(self) -> nodes.Expr:\n        lineno = self.stream.current.lineno\n        left = self.parse_unary()\n        while self.stream.current.type == \"pow\":\n            next(self.stream)\n            right = self.parse_unary()\n            left = nodes.Pow(left, right, lineno=lineno)\n            lineno = self.stream.current.lineno\n        return left\n\n    def parse_unary(self, with_filter: bool = True) -> nodes.Expr:\n        token_type = self.stream.current.type\n        lineno = self.stream.current.lineno\n        node: nodes.Expr\n\n        if token_type == \"sub\":\n            next(self.stream)\n            node = nodes.Neg(self.parse_unary(False), lineno=lineno)\n        elif token_type == \"add\":\n            next(self.stream)\n            node = nodes.Pos(self.parse_unary(False), lineno=lineno)\n        else:\n            node = self.parse_primary()\n        node = self.parse_postfix(node)\n        if with_filter:\n            node = self.parse_filter_expr(node)\n        return node\n\n    def parse_primary(self, with_namespace: bool = False) -> nodes.Expr:\n        \"\"\"Parse a name or literal value. If ``with_namespace`` is enabled, also\n        parse namespace attr refs, for use in assignments.\"\"\"\n        token = self.stream.current\n        node: nodes.Expr\n        if token.type == \"name\":\n            next(self.stream)\n            if token.value in (\"true\", \"false\", \"True\", \"False\"):\n                node = nodes.Const(token.value in (\"true\", \"True\"), lineno=token.lineno)\n            elif token.value in (\"none\", \"None\"):\n                node = nodes.Const(None, lineno=token.lineno)\n            elif with_namespace and self.stream.current.type == \"dot\":\n                # If namespace attributes are allowed at this point, and the next\n                # token is a dot, produce a namespace reference.\n                next(self.stream)\n                attr = self.stream.expect(\"name\")\n                node = nodes.NSRef(token.value, attr.value, lineno=token.lineno)\n            else:\n                node = nodes.Name(token.value, \"load\", lineno=token.lineno)\n        elif token.type == \"string\":\n            next(self.stream)\n            buf = [token.value]\n            lineno = token.lineno\n            while self.stream.current.type == \"string\":\n                buf.append(self.stream.current.value)\n                next(self.stream)\n            node = nodes.Const(\"\".join(buf), lineno=lineno)\n        elif token.type in (\"integer\", \"float\"):\n            next(self.stream)\n            node = nodes.Const(token.value, lineno=token.lineno)\n        elif token.type == \"lparen\":\n            next(self.stream)\n            node = self.parse_tuple(explicit_parentheses=True)\n            self.stream.expect(\"rparen\")\n        elif token.type == \"lbracket\":\n            node = self.parse_list()\n        elif token.type == \"lbrace\":\n            node = self.parse_dict()\n        else:\n            self.fail(f\"unexpected {describe_token(token)!r}\", token.lineno)\n        return node\n\n    def parse_tuple(\n        self,\n        simplified: bool = False,\n        with_condexpr: bool = True,\n        extra_end_rules: tuple[str, ...] | None = None,\n        explicit_parentheses: bool = False,\n        with_namespace: bool = False,\n    ) -> nodes.Tuple | nodes.Expr:\n        \"\"\"Works like `parse_expression` but if multiple expressions are\n        delimited by a comma a :class:`~jinja2.nodes.Tuple` node is created.\n        This method could also return a regular expression instead of a tuple\n        if no commas where found.\n\n        The default parsing mode is a full tuple.  If `simplified` is `True`\n        only names and literals are parsed; ``with_namespace`` allows namespace\n        attr refs as well. The `no_condexpr` parameter is forwarded to\n        :meth:`parse_expression`.\n\n        Because tuples do not require delimiters and may end in a bogus comma\n        an extra hint is needed that marks the end of a tuple.  For example\n        for loops support tuples between `for` and `in`.  In that case the\n        `extra_end_rules` is set to ``['name:in']``.\n\n        `explicit_parentheses` is true if the parsing was triggered by an\n        expression in parentheses.  This is used to figure out if an empty\n        tuple is a valid expression or not.\n        \"\"\"\n        lineno = self.stream.current.lineno\n        if simplified:\n\n            def parse() -> nodes.Expr:\n                return self.parse_primary(with_namespace=with_namespace)\n\n        else:\n\n            def parse() -> nodes.Expr:\n                return self.parse_expression(with_condexpr=with_condexpr)\n\n        args: list[nodes.Expr] = []\n        is_tuple = False\n\n        while True:\n            if args:\n                self.stream.expect(\"comma\")\n            if self.is_tuple_end(extra_end_rules):\n                break\n            args.append(parse())\n            if self.stream.current.type == \"comma\":\n                is_tuple = True\n            else:\n                break\n            lineno = self.stream.current.lineno\n\n        if not is_tuple:\n            if args:\n                return args[0]\n\n            # if we don't have explicit parentheses, an empty tuple is\n            # not a valid expression.  This would mean nothing (literally\n            # nothing) in the spot of an expression would be an empty\n            # tuple.\n            if not explicit_parentheses:\n                self.fail(\n                    \"Expected an expression,\"\n                    f\" got {describe_token(self.stream.current)!r}\"\n                )\n\n        return nodes.Tuple(args, \"load\", lineno=lineno)\n\n    def parse_list(self) -> nodes.List:\n        token = self.stream.expect(\"lbracket\")\n        items: list[nodes.Expr] = []\n        while self.stream.current.type != \"rbracket\":\n            if items:\n                self.stream.expect(\"comma\")\n            if self.stream.current.type == \"rbracket\":\n                break\n            items.append(self.parse_expression())\n        self.stream.expect(\"rbracket\")\n        return nodes.List(items, lineno=token.lineno)\n\n    def parse_dict(self) -> nodes.Dict:\n        token = self.stream.expect(\"lbrace\")\n        items: list[nodes.Pair] = []\n        while self.stream.current.type != \"rbrace\":\n            if items:\n                self.stream.expect(\"comma\")\n            if self.stream.current.type == \"rbrace\":\n                break\n            key = self.parse_expression()\n            self.stream.expect(\"colon\")\n            value = self.parse_expression()\n            items.append(nodes.Pair(key, value, lineno=key.lineno))\n        self.stream.expect(\"rbrace\")\n        return nodes.Dict(items, lineno=token.lineno)\n\n    def parse_postfix(self, node: nodes.Expr) -> nodes.Expr:\n        while True:\n            token_type = self.stream.current.type\n            if token_type == \"dot\" or token_type == \"lbracket\":\n                node = self.parse_subscript(node)\n            # calls are valid both after postfix expressions (getattr\n            # and getitem) as well as filters and tests\n            elif token_type == \"lparen\":\n                node = self.parse_call(node)\n            else:\n                break\n        return node\n\n    def parse_filter_expr(self, node: nodes.Expr) -> nodes.Expr:\n        while True:\n            token_type = self.stream.current.type\n            if token_type == \"pipe\":\n                node = self.parse_filter(node)  # type: ignore\n            elif token_type == \"name\" and self.stream.current.value == \"is\":\n                node = self.parse_test(node)\n            # calls are valid both after postfix expressions (getattr\n            # and getitem) as well as filters and tests\n            elif token_type == \"lparen\":\n                node = self.parse_call(node)\n            else:\n                break\n        return node\n\n    def parse_subscript(self, node: nodes.Expr) -> nodes.Getattr | nodes.Getitem:\n        token = next(self.stream)\n        arg: nodes.Expr\n\n        if token.type == \"dot\":\n            attr_token = self.stream.current\n            next(self.stream)\n            if attr_token.type == \"name\":\n                return nodes.Getattr(\n                    node, attr_token.value, \"load\", lineno=token.lineno\n                )\n            elif attr_token.type != \"integer\":\n                self.fail(\"expected name or number\", attr_token.lineno)\n            arg = nodes.Const(attr_token.value, lineno=attr_token.lineno)\n            return nodes.Getitem(node, arg, \"load\", lineno=token.lineno)\n        if token.type == \"lbracket\":\n            args: list[nodes.Expr] = []\n            while self.stream.current.type != \"rbracket\":\n                if args:\n                    self.stream.expect(\"comma\")\n                args.append(self.parse_subscribed())\n            self.stream.expect(\"rbracket\")\n            if len(args) == 1:\n                arg = args[0]\n            else:\n                arg = nodes.Tuple(args, \"load\", lineno=token.lineno)\n            return nodes.Getitem(node, arg, \"load\", lineno=token.lineno)\n        self.fail(\"expected subscript expression\", token.lineno)\n\n    def parse_subscribed(self) -> nodes.Expr:\n        lineno = self.stream.current.lineno\n        args: list[nodes.Expr | None]\n\n        if self.stream.current.type == \"colon\":\n            next(self.stream)\n            args = [None]\n        else:\n            node = self.parse_expression()\n            if self.stream.current.type != \"colon\":\n                return node\n            next(self.stream)\n            args = [node]\n\n        if self.stream.current.type == \"colon\":\n            args.append(None)\n        elif self.stream.current.type not in (\"rbracket\", \"comma\"):\n            args.append(self.parse_expression())\n        else:\n            args.append(None)\n\n        if self.stream.current.type == \"colon\":\n            next(self.stream)\n            if self.stream.current.type not in (\"rbracket\", \"comma\"):\n                args.append(self.parse_expression())\n            else:\n                args.append(None)\n        else:\n            args.append(None)\n\n        return nodes.Slice(lineno=lineno, *args)  # noqa: B026\n\n    def parse_call_args(\n        self,\n    ) -> tuple[\n        list[nodes.Expr],\n        list[nodes.Keyword],\n        nodes.Expr | None,\n        nodes.Expr | None,\n    ]:\n        token = self.stream.expect(\"lparen\")\n        args = []\n        kwargs = []\n        dyn_args = None\n        dyn_kwargs = None\n        require_comma = False\n\n        def ensure(expr: bool) -> None:\n            if not expr:\n                self.fail(\"invalid syntax for function call expression\", token.lineno)\n\n        while self.stream.current.type != \"rparen\":\n            if require_comma:\n                self.stream.expect(\"comma\")\n\n                # support for trailing comma\n                if self.stream.current.type == \"rparen\":\n                    break\n\n            if self.stream.current.type == \"mul\":\n                ensure(dyn_args is None and dyn_kwargs is None)\n                next(self.stream)\n                dyn_args = self.parse_expression()\n            elif self.stream.current.type == \"pow\":\n                ensure(dyn_kwargs is None)\n                next(self.stream)\n                dyn_kwargs = self.parse_expression()\n            else:\n                if (\n                    self.stream.current.type == \"name\"\n                    and self.stream.look().type == \"assign\"\n                ):\n                    # Parsing a kwarg\n                    ensure(dyn_kwargs is None)\n                    key = self.stream.current.value\n                    self.stream.skip(2)\n                    value = self.parse_expression()\n                    kwargs.append(nodes.Keyword(key, value, lineno=value.lineno))\n                else:\n                    # Parsing an arg\n                    ensure(dyn_args is None and dyn_kwargs is None and not kwargs)\n                    args.append(self.parse_expression())\n\n            require_comma = True\n\n        self.stream.expect(\"rparen\")\n        return args, kwargs, dyn_args, dyn_kwargs\n\n    def parse_call(self, node: nodes.Expr) -> nodes.Call:\n        # The lparen will be expected in parse_call_args, but the lineno\n        # needs to be recorded before the stream is advanced.\n        token = self.stream.current\n        args, kwargs, dyn_args, dyn_kwargs = self.parse_call_args()\n        return nodes.Call(node, args, kwargs, dyn_args, dyn_kwargs, lineno=token.lineno)\n\n    def parse_filter(\n        self, node: nodes.Expr | None, start_inline: bool = False\n    ) -> nodes.Expr | None:\n        while self.stream.current.type == \"pipe\" or start_inline:\n            if not start_inline:\n                next(self.stream)\n            token = self.stream.expect(\"name\")\n            name = token.value\n            while self.stream.current.type == \"dot\":\n                next(self.stream)\n                name += \".\" + self.stream.expect(\"name\").value\n            if self.stream.current.type == \"lparen\":\n                args, kwargs, dyn_args, dyn_kwargs = self.parse_call_args()\n            else:\n                args = []\n                kwargs = []\n                dyn_args = dyn_kwargs = None\n            node = nodes.Filter(\n                node, name, args, kwargs, dyn_args, dyn_kwargs, lineno=token.lineno\n            )\n            start_inline = False\n        return node\n\n    def parse_test(self, node: nodes.Expr) -> nodes.Expr:\n        token = next(self.stream)\n        if self.stream.current.test(\"name:not\"):\n            next(self.stream)\n            negated = True\n        else:\n            negated = False\n        name = self.stream.expect(\"name\").value\n        while self.stream.current.type == \"dot\":\n            next(self.stream)\n            name += \".\" + self.stream.expect(\"name\").value\n        dyn_args = dyn_kwargs = None\n        kwargs: list[nodes.Keyword] = []\n        if self.stream.current.type == \"lparen\":\n            args, kwargs, dyn_args, dyn_kwargs = self.parse_call_args()\n        elif self.stream.current.type in {\n            \"name\",\n            \"string\",\n            \"integer\",\n            \"float\",\n            \"lparen\",\n            \"lbracket\",\n            \"lbrace\",\n        } and not self.stream.current.test_any(\"name:else\", \"name:or\", \"name:and\"):\n            if self.stream.current.test(\"name:is\"):\n                self.fail(\"You cannot chain multiple tests with is\")\n            arg_node = self.parse_primary()\n            arg_node = self.parse_postfix(arg_node)\n            args = [arg_node]\n        else:\n            args = []\n        node = nodes.Test(\n            node, name, args, kwargs, dyn_args, dyn_kwargs, lineno=token.lineno\n        )\n        if negated:\n            node = nodes.Not(node, lineno=token.lineno)\n        return node\n\n    def subparse(self, end_tokens: tuple[str, ...] | None = None) -> list[nodes.Node]:\n        body: list[nodes.Node] = []\n        data_buffer: list[nodes.Node] = []\n        add_data = data_buffer.append\n\n        if end_tokens is not None:\n            self._end_token_stack.append(end_tokens)\n\n        def flush_data() -> None:\n            if data_buffer:\n                lineno = data_buffer[0].lineno\n                body.append(nodes.Output(data_buffer[:], lineno=lineno))\n                del data_buffer[:]\n\n        try:\n            while self.stream:\n                token = self.stream.current\n                if token.type == \"data\":\n                    if token.value:\n                        add_data(nodes.TemplateData(token.value, lineno=token.lineno))\n                    next(self.stream)\n                elif token.type == \"variable_begin\":\n                    next(self.stream)\n                    add_data(self.parse_tuple(with_condexpr=True))\n                    self.stream.expect(\"variable_end\")\n                elif token.type == \"block_begin\":\n                    flush_data()\n                    next(self.stream)\n                    if end_tokens is not None and self.stream.current.test_any(\n                        *end_tokens\n                    ):\n                        return body\n                    rv = self.parse_statement()\n                    if isinstance(rv, list):\n                        body.extend(rv)\n                    else:\n                        body.append(rv)\n                    self.stream.expect(\"block_end\")\n                else:\n                    raise AssertionError(\"internal parsing error\")\n\n            flush_data()\n        finally:\n            if end_tokens is not None:\n                self._end_token_stack.pop()\n        return body\n\n    def parse(self) -> nodes.Template:\n        \"\"\"Parse the whole template into a `Template` node.\"\"\"\n        result = nodes.Template(self.subparse(), lineno=1)\n        result.set_environment(self.environment)\n        return result\n"
  },
  {
    "path": "src/jinja2/py.typed",
    "content": ""
  },
  {
    "path": "src/jinja2/runtime.py",
    "content": "\"\"\"The runtime functions and state used by compiled templates.\"\"\"\n\nimport functools\nimport sys\nimport typing as t\nfrom collections import abc\nfrom itertools import chain\n\nfrom markupsafe import escape  # noqa: F401\nfrom markupsafe import Markup\nfrom markupsafe import soft_str\n\nfrom .async_utils import auto_aiter\nfrom .async_utils import auto_await  # noqa: F401\nfrom .exceptions import TemplateNotFound  # noqa: F401\nfrom .exceptions import TemplateRuntimeError  # noqa: F401\nfrom .exceptions import UndefinedError\nfrom .nodes import EvalContext\nfrom .utils import _PassArg\nfrom .utils import concat\nfrom .utils import internalcode\nfrom .utils import missing\nfrom .utils import Namespace  # noqa: F401\nfrom .utils import object_type_repr\nfrom .utils import pass_eval_context\n\nV = t.TypeVar(\"V\")\nF = t.TypeVar(\"F\", bound=t.Callable[..., t.Any])\n\nif t.TYPE_CHECKING:\n    import logging\n\n    import typing_extensions as te\n\n    from .environment import Environment\n\n    class LoopRenderFunc(te.Protocol):\n        def __call__(\n            self,\n            reciter: t.Iterable[V],\n            loop_render_func: \"LoopRenderFunc\",\n            depth: int = 0,\n        ) -> str: ...\n\n\n# these variables are exported to the template runtime\nexported = [\n    \"LoopContext\",\n    \"TemplateReference\",\n    \"Macro\",\n    \"Markup\",\n    \"TemplateRuntimeError\",\n    \"missing\",\n    \"escape\",\n    \"markup_join\",\n    \"str_join\",\n    \"identity\",\n    \"TemplateNotFound\",\n    \"Namespace\",\n    \"Undefined\",\n    \"internalcode\",\n]\nasync_exported = [\n    \"AsyncLoopContext\",\n    \"auto_aiter\",\n    \"auto_await\",\n]\n\n\ndef identity(x: V) -> V:\n    \"\"\"Returns its argument. Useful for certain things in the\n    environment.\n    \"\"\"\n    return x\n\n\ndef markup_join(seq: t.Iterable[t.Any]) -> str:\n    \"\"\"Concatenation that escapes if necessary and converts to string.\"\"\"\n    buf = []\n    iterator = map(soft_str, seq)\n    for arg in iterator:\n        buf.append(arg)\n        if hasattr(arg, \"__html__\"):\n            return Markup(\"\").join(chain(buf, iterator))\n    return concat(buf)\n\n\ndef str_join(seq: t.Iterable[t.Any]) -> str:\n    \"\"\"Simple args to string conversion and concatenation.\"\"\"\n    return concat(map(str, seq))\n\n\ndef new_context(\n    environment: \"Environment\",\n    template_name: str | None,\n    blocks: dict[str, t.Callable[[\"Context\"], t.Iterator[str]]],\n    vars: dict[str, t.Any] | None = None,\n    shared: bool = False,\n    globals: t.MutableMapping[str, t.Any] | None = None,\n    locals: t.Mapping[str, t.Any] | None = None,\n) -> \"Context\":\n    \"\"\"Internal helper for context creation.\"\"\"\n    if vars is None:\n        vars = {}\n    if shared:\n        parent = vars\n    else:\n        parent = dict(globals or (), **vars)\n    if locals:\n        # if the parent is shared a copy should be created because\n        # we don't want to modify the dict passed\n        if shared:\n            parent = dict(parent)\n        for key, value in locals.items():\n            if value is not missing:\n                parent[key] = value\n    return environment.context_class(\n        environment, parent, template_name, blocks, globals=globals\n    )\n\n\nclass TemplateReference:\n    \"\"\"The `self` in templates.\"\"\"\n\n    def __init__(self, context: \"Context\") -> None:\n        self.__context = context\n\n    def __getitem__(self, name: str) -> t.Any:\n        blocks = self.__context.blocks[name]\n        return BlockReference(name, self.__context, blocks, 0)\n\n    def __repr__(self) -> str:\n        return f\"<{type(self).__name__} {self.__context.name!r}>\"\n\n\ndef _dict_method_all(dict_method: F) -> F:\n    @functools.wraps(dict_method)\n    def f_all(self: \"Context\") -> t.Any:\n        return dict_method(self.get_all())\n\n    return t.cast(F, f_all)\n\n\n@abc.Mapping.register\nclass Context:\n    \"\"\"The template context holds the variables of a template.  It stores the\n    values passed to the template and also the names the template exports.\n    Creating instances is neither supported nor useful as it's created\n    automatically at various stages of the template evaluation and should not\n    be created by hand.\n\n    The context is immutable.  Modifications on :attr:`parent` **must not**\n    happen and modifications on :attr:`vars` are allowed from generated\n    template code only.  Template filters and global functions marked as\n    :func:`pass_context` get the active context passed as first argument\n    and are allowed to access the context read-only.\n\n    The template context supports read only dict operations (`get`,\n    `keys`, `values`, `items`, `iterkeys`, `itervalues`, `iteritems`,\n    `__getitem__`, `__contains__`).  Additionally there is a :meth:`resolve`\n    method that doesn't fail with a `KeyError` but returns an\n    :class:`Undefined` object for missing variables.\n    \"\"\"\n\n    def __init__(\n        self,\n        environment: \"Environment\",\n        parent: dict[str, t.Any],\n        name: str | None,\n        blocks: dict[str, t.Callable[[\"Context\"], t.Iterator[str]]],\n        globals: t.MutableMapping[str, t.Any] | None = None,\n    ):\n        self.parent = parent\n        self.vars: dict[str, t.Any] = {}\n        self.environment: Environment = environment\n        self.eval_ctx = EvalContext(self.environment, name)\n        self.exported_vars: set[str] = set()\n        self.name = name\n        self.globals_keys = set() if globals is None else set(globals)\n\n        # create the initial mapping of blocks.  Whenever template inheritance\n        # takes place the runtime will update this mapping with the new blocks\n        # from the template.\n        self.blocks = {k: [v] for k, v in blocks.items()}\n\n    def super(\n        self, name: str, current: t.Callable[[\"Context\"], t.Iterator[str]]\n    ) -> t.Union[\"BlockReference\", \"Undefined\"]:\n        \"\"\"Render a parent block.\"\"\"\n        try:\n            blocks = self.blocks[name]\n            index = blocks.index(current) + 1\n            blocks[index]\n        except LookupError:\n            return self.environment.undefined(\n                f\"there is no parent block called {name!r}.\", name=\"super\"\n            )\n        return BlockReference(name, self, blocks, index)\n\n    def get(self, key: str, default: t.Any = None) -> t.Any:\n        \"\"\"Look up a variable by name, or return a default if the key is\n        not found.\n\n        :param key: The variable name to look up.\n        :param default: The value to return if the key is not found.\n        \"\"\"\n        try:\n            return self[key]\n        except KeyError:\n            return default\n\n    def resolve(self, key: str) -> t.Union[t.Any, \"Undefined\"]:\n        \"\"\"Look up a variable by name, or return an :class:`Undefined`\n        object if the key is not found.\n\n        If you need to add custom behavior, override\n        :meth:`resolve_or_missing`, not this method. The various lookup\n        functions use that method, not this one.\n\n        :param key: The variable name to look up.\n        \"\"\"\n        rv = self.resolve_or_missing(key)\n\n        if rv is missing:\n            return self.environment.undefined(name=key)\n\n        return rv\n\n    def resolve_or_missing(self, key: str) -> t.Any:\n        \"\"\"Look up a variable by name, or return a ``missing`` sentinel\n        if the key is not found.\n\n        Override this method to add custom lookup behavior.\n        :meth:`resolve`, :meth:`get`, and :meth:`__getitem__` use this\n        method. Don't call this method directly.\n\n        :param key: The variable name to look up.\n        \"\"\"\n        if key in self.vars:\n            return self.vars[key]\n\n        if key in self.parent:\n            return self.parent[key]\n\n        return missing\n\n    def get_exported(self) -> dict[str, t.Any]:\n        \"\"\"Get a new dict with the exported variables.\"\"\"\n        return {k: self.vars[k] for k in self.exported_vars}\n\n    def get_all(self) -> dict[str, t.Any]:\n        \"\"\"Return the complete context as dict including the exported\n        variables.  For optimizations reasons this might not return an\n        actual copy so be careful with using it.\n        \"\"\"\n        if not self.vars:\n            return self.parent\n        if not self.parent:\n            return self.vars\n        return dict(self.parent, **self.vars)\n\n    @internalcode\n    def call(\n        __self,  # noqa: B902\n        __obj: t.Callable[..., t.Any],\n        *args: t.Any,\n        **kwargs: t.Any,\n    ) -> t.Union[t.Any, \"Undefined\"]:\n        \"\"\"Call the callable with the arguments and keyword arguments\n        provided but inject the active context or environment as first\n        argument if the callable has :func:`pass_context` or\n        :func:`pass_environment`.\n        \"\"\"\n        if __debug__:\n            __traceback_hide__ = True  # noqa\n\n        # Allow callable classes to take a context\n        if (\n            hasattr(__obj, \"__call__\")  # noqa: B004\n            and _PassArg.from_obj(__obj.__call__) is not None\n        ):\n            __obj = __obj.__call__\n\n        pass_arg = _PassArg.from_obj(__obj)\n\n        if pass_arg is _PassArg.context:\n            # the active context should have access to variables set in\n            # loops and blocks without mutating the context itself\n            if kwargs.get(\"_loop_vars\"):\n                __self = __self.derived(kwargs[\"_loop_vars\"])\n            if kwargs.get(\"_block_vars\"):\n                __self = __self.derived(kwargs[\"_block_vars\"])\n            args = (__self,) + args\n        elif pass_arg is _PassArg.eval_context:\n            args = (__self.eval_ctx,) + args\n        elif pass_arg is _PassArg.environment:\n            args = (__self.environment,) + args\n\n        kwargs.pop(\"_block_vars\", None)\n        kwargs.pop(\"_loop_vars\", None)\n\n        try:\n            return __obj(*args, **kwargs)\n        except StopIteration:\n            return __self.environment.undefined(\n                \"value was undefined because a callable raised a\"\n                \" StopIteration exception\"\n            )\n\n    def derived(self, locals: dict[str, t.Any] | None = None) -> \"Context\":\n        \"\"\"Internal helper function to create a derived context.  This is\n        used in situations where the system needs a new context in the same\n        template that is independent.\n        \"\"\"\n        context = new_context(\n            self.environment, self.name, {}, self.get_all(), True, None, locals\n        )\n        context.eval_ctx = self.eval_ctx\n        context.blocks.update((k, list(v)) for k, v in self.blocks.items())\n        return context\n\n    keys = _dict_method_all(dict.keys)\n    values = _dict_method_all(dict.values)\n    items = _dict_method_all(dict.items)\n\n    def __contains__(self, name: str) -> bool:\n        return name in self.vars or name in self.parent\n\n    def __getitem__(self, key: str) -> t.Any:\n        \"\"\"Look up a variable by name with ``[]`` syntax, or raise a\n        ``KeyError`` if the key is not found.\n        \"\"\"\n        item = self.resolve_or_missing(key)\n\n        if item is missing:\n            raise KeyError(key)\n\n        return item\n\n    def __repr__(self) -> str:\n        return f\"<{type(self).__name__} {self.get_all()!r} of {self.name!r}>\"\n\n\nclass BlockReference:\n    \"\"\"One block on a template reference.\"\"\"\n\n    def __init__(\n        self,\n        name: str,\n        context: \"Context\",\n        stack: list[t.Callable[[\"Context\"], t.Iterator[str]]],\n        depth: int,\n    ) -> None:\n        self.name = name\n        self._context = context\n        self._stack = stack\n        self._depth = depth\n\n    @property\n    def super(self) -> t.Union[\"BlockReference\", \"Undefined\"]:\n        \"\"\"Super the block.\"\"\"\n        if self._depth + 1 >= len(self._stack):\n            return self._context.environment.undefined(\n                f\"there is no parent block called {self.name!r}.\", name=\"super\"\n            )\n        return BlockReference(self.name, self._context, self._stack, self._depth + 1)\n\n    @internalcode\n    async def _async_call(self) -> str:\n        rv = self._context.environment.concat(  # type: ignore\n            [x async for x in self._stack[self._depth](self._context)]  # type: ignore\n        )\n\n        if self._context.eval_ctx.autoescape:\n            return Markup(rv)\n\n        return rv\n\n    @internalcode\n    def __call__(self) -> str:\n        if self._context.environment.is_async:\n            return self._async_call()  # type: ignore\n\n        rv = self._context.environment.concat(  # type: ignore\n            self._stack[self._depth](self._context)\n        )\n\n        if self._context.eval_ctx.autoescape:\n            return Markup(rv)\n\n        return rv\n\n\nclass LoopContext:\n    \"\"\"A wrapper iterable for dynamic ``for`` loops, with information\n    about the loop and iteration.\n    \"\"\"\n\n    #: Current iteration of the loop, starting at 0.\n    index0 = -1\n\n    _length: int | None = None\n    _after: t.Any = missing\n    _current: t.Any = missing\n    _before: t.Any = missing\n    _last_changed_value: t.Any = missing\n\n    def __init__(\n        self,\n        iterable: t.Iterable[V],\n        undefined: type[\"Undefined\"],\n        recurse: t.Optional[\"LoopRenderFunc\"] = None,\n        depth0: int = 0,\n    ) -> None:\n        \"\"\"\n        :param iterable: Iterable to wrap.\n        :param undefined: :class:`Undefined` class to use for next and\n            previous items.\n        :param recurse: The function to render the loop body when the\n            loop is marked recursive.\n        :param depth0: Incremented when looping recursively.\n        \"\"\"\n        self._iterable = iterable\n        self._iterator = self._to_iterator(iterable)\n        self._undefined = undefined\n        self._recurse = recurse\n        #: How many levels deep a recursive loop currently is, starting at 0.\n        self.depth0 = depth0\n\n    @staticmethod\n    def _to_iterator(iterable: t.Iterable[V]) -> t.Iterator[V]:\n        return iter(iterable)\n\n    @property\n    def length(self) -> int:\n        \"\"\"Length of the iterable.\n\n        If the iterable is a generator or otherwise does not have a\n        size, it is eagerly evaluated to get a size.\n        \"\"\"\n        if self._length is not None:\n            return self._length\n\n        try:\n            self._length = len(self._iterable)  # type: ignore\n        except TypeError:\n            iterable = list(self._iterator)\n            self._iterator = self._to_iterator(iterable)\n            self._length = len(iterable) + self.index + (self._after is not missing)\n\n        return self._length\n\n    def __len__(self) -> int:\n        return self.length\n\n    @property\n    def depth(self) -> int:\n        \"\"\"How many levels deep a recursive loop currently is, starting at 1.\"\"\"\n        return self.depth0 + 1\n\n    @property\n    def index(self) -> int:\n        \"\"\"Current iteration of the loop, starting at 1.\"\"\"\n        return self.index0 + 1\n\n    @property\n    def revindex0(self) -> int:\n        \"\"\"Number of iterations from the end of the loop, ending at 0.\n\n        Requires calculating :attr:`length`.\n        \"\"\"\n        return self.length - self.index\n\n    @property\n    def revindex(self) -> int:\n        \"\"\"Number of iterations from the end of the loop, ending at 1.\n\n        Requires calculating :attr:`length`.\n        \"\"\"\n        return self.length - self.index0\n\n    @property\n    def first(self) -> bool:\n        \"\"\"Whether this is the first iteration of the loop.\"\"\"\n        return self.index0 == 0\n\n    def _peek_next(self) -> t.Any:\n        \"\"\"Return the next element in the iterable, or :data:`missing`\n        if the iterable is exhausted. Only peeks one item ahead, caching\n        the result in :attr:`_last` for use in subsequent checks. The\n        cache is reset when :meth:`__next__` is called.\n        \"\"\"\n        if self._after is not missing:\n            return self._after\n\n        self._after = next(self._iterator, missing)\n        return self._after\n\n    @property\n    def last(self) -> bool:\n        \"\"\"Whether this is the last iteration of the loop.\n\n        Causes the iterable to advance early. See\n        :func:`itertools.groupby` for issues this can cause.\n        The :func:`groupby` filter avoids that issue.\n        \"\"\"\n        return self._peek_next() is missing\n\n    @property\n    def previtem(self) -> t.Union[t.Any, \"Undefined\"]:\n        \"\"\"The item in the previous iteration. Undefined during the\n        first iteration.\n        \"\"\"\n        if self.first:\n            return self._undefined(\"there is no previous item\")\n\n        return self._before\n\n    @property\n    def nextitem(self) -> t.Union[t.Any, \"Undefined\"]:\n        \"\"\"The item in the next iteration. Undefined during the last\n        iteration.\n\n        Causes the iterable to advance early. See\n        :func:`itertools.groupby` for issues this can cause.\n        The :func:`jinja-filters.groupby` filter avoids that issue.\n        \"\"\"\n        rv = self._peek_next()\n\n        if rv is missing:\n            return self._undefined(\"there is no next item\")\n\n        return rv\n\n    def cycle(self, *args: V) -> V:\n        \"\"\"Return a value from the given args, cycling through based on\n        the current :attr:`index0`.\n\n        :param args: One or more values to cycle through.\n        \"\"\"\n        if not args:\n            raise TypeError(\"no items for cycling given\")\n\n        return args[self.index0 % len(args)]\n\n    def changed(self, *value: t.Any) -> bool:\n        \"\"\"Return ``True`` if previously called with a different value\n        (including when called for the first time).\n\n        :param value: One or more values to compare to the last call.\n        \"\"\"\n        if self._last_changed_value != value:\n            self._last_changed_value = value\n            return True\n\n        return False\n\n    def __iter__(self) -> \"LoopContext\":\n        return self\n\n    def __next__(self) -> tuple[t.Any, \"LoopContext\"]:\n        if self._after is not missing:\n            rv = self._after\n            self._after = missing\n        else:\n            rv = next(self._iterator)\n\n        self.index0 += 1\n        self._before = self._current\n        self._current = rv\n        return rv, self\n\n    @internalcode\n    def __call__(self, iterable: t.Iterable[V]) -> str:\n        \"\"\"When iterating over nested data, render the body of the loop\n        recursively with the given inner iterable data.\n\n        The loop must have the ``recursive`` marker for this to work.\n        \"\"\"\n        if self._recurse is None:\n            raise TypeError(\n                \"The loop must have the 'recursive' marker to be called recursively.\"\n            )\n\n        return self._recurse(iterable, self._recurse, depth=self.depth)\n\n    def __repr__(self) -> str:\n        return f\"<{type(self).__name__} {self.index}/{self.length}>\"\n\n\nclass AsyncLoopContext(LoopContext):\n    _iterator: t.AsyncIterator[t.Any]  # type: ignore\n\n    @staticmethod\n    def _to_iterator(  # type: ignore\n        iterable: t.Iterable[V] | t.AsyncIterable[V],\n    ) -> t.AsyncIterator[V]:\n        return auto_aiter(iterable)\n\n    @property\n    async def length(self) -> int:  # type: ignore\n        if self._length is not None:\n            return self._length\n\n        try:\n            self._length = len(self._iterable)  # type: ignore\n        except TypeError:\n            iterable = [x async for x in self._iterator]\n            self._iterator = self._to_iterator(iterable)\n            self._length = len(iterable) + self.index + (self._after is not missing)\n\n        return self._length\n\n    @property\n    async def revindex0(self) -> int:  # type: ignore\n        return await self.length - self.index\n\n    @property\n    async def revindex(self) -> int:  # type: ignore\n        return await self.length - self.index0\n\n    async def _peek_next(self) -> t.Any:\n        if self._after is not missing:\n            return self._after\n\n        try:\n            self._after = await self._iterator.__anext__()\n        except StopAsyncIteration:\n            self._after = missing\n\n        return self._after\n\n    @property\n    async def last(self) -> bool:  # type: ignore\n        return await self._peek_next() is missing\n\n    @property\n    async def nextitem(self) -> t.Union[t.Any, \"Undefined\"]:\n        rv = await self._peek_next()\n\n        if rv is missing:\n            return self._undefined(\"there is no next item\")\n\n        return rv\n\n    def __aiter__(self) -> \"AsyncLoopContext\":\n        return self\n\n    async def __anext__(self) -> tuple[t.Any, \"AsyncLoopContext\"]:\n        if self._after is not missing:\n            rv = self._after\n            self._after = missing\n        else:\n            rv = await self._iterator.__anext__()\n\n        self.index0 += 1\n        self._before = self._current\n        self._current = rv\n        return rv, self\n\n\nclass Macro:\n    \"\"\"Wraps a macro function.\"\"\"\n\n    def __init__(\n        self,\n        environment: \"Environment\",\n        func: t.Callable[..., str],\n        name: str,\n        arguments: list[str],\n        catch_kwargs: bool,\n        catch_varargs: bool,\n        caller: bool,\n        default_autoescape: bool | None = None,\n    ):\n        self._environment = environment\n        self._func = func\n        self._argument_count = len(arguments)\n        self.name = name\n        self.arguments = arguments\n        self.catch_kwargs = catch_kwargs\n        self.catch_varargs = catch_varargs\n        self.caller = caller\n        self.explicit_caller = \"caller\" in arguments\n\n        if default_autoescape is None:\n            if callable(environment.autoescape):\n                default_autoescape = environment.autoescape(None)\n            else:\n                default_autoescape = environment.autoescape\n\n        self._default_autoescape = default_autoescape\n\n    @internalcode\n    @pass_eval_context\n    def __call__(self, *args: t.Any, **kwargs: t.Any) -> str:\n        # This requires a bit of explanation,  In the past we used to\n        # decide largely based on compile-time information if a macro is\n        # safe or unsafe.  While there was a volatile mode it was largely\n        # unused for deciding on escaping.  This turns out to be\n        # problematic for macros because whether a macro is safe depends not\n        # on the escape mode when it was defined, but rather when it was used.\n        #\n        # Because however we export macros from the module system and\n        # there are historic callers that do not pass an eval context (and\n        # will continue to not pass one), we need to perform an instance\n        # check here.\n        #\n        # This is considered safe because an eval context is not a valid\n        # argument to callables otherwise anyway.  Worst case here is\n        # that if no eval context is passed we fall back to the compile\n        # time autoescape flag.\n        if args and isinstance(args[0], EvalContext):\n            autoescape = args[0].autoescape\n            args = args[1:]\n        else:\n            autoescape = self._default_autoescape\n\n        # try to consume the positional arguments\n        arguments = list(args[: self._argument_count])\n        off = len(arguments)\n\n        # For information why this is necessary refer to the handling\n        # of caller in the `macro_body` handler in the compiler.\n        found_caller = False\n\n        # if the number of arguments consumed is not the number of\n        # arguments expected we start filling in keyword arguments\n        # and defaults.\n        if off != self._argument_count:\n            for name in self.arguments[len(arguments) :]:\n                try:\n                    value = kwargs.pop(name)\n                except KeyError:\n                    value = missing\n                if name == \"caller\":\n                    found_caller = True\n                arguments.append(value)\n        else:\n            found_caller = self.explicit_caller\n\n        # it's important that the order of these arguments does not change\n        # if not also changed in the compiler's `function_scoping` method.\n        # the order is caller, keyword arguments, positional arguments!\n        if self.caller and not found_caller:\n            caller = kwargs.pop(\"caller\", None)\n            if caller is None:\n                caller = self._environment.undefined(\"No caller defined\", name=\"caller\")\n            arguments.append(caller)\n\n        if self.catch_kwargs:\n            arguments.append(kwargs)\n        elif kwargs:\n            if \"caller\" in kwargs:\n                raise TypeError(\n                    f\"macro {self.name!r} was invoked with two values for the special\"\n                    \" caller argument. This is most likely a bug.\"\n                )\n            raise TypeError(\n                f\"macro {self.name!r} takes no keyword argument {next(iter(kwargs))!r}\"\n            )\n        if self.catch_varargs:\n            arguments.append(args[self._argument_count :])\n        elif len(args) > self._argument_count:\n            raise TypeError(\n                f\"macro {self.name!r} takes not more than\"\n                f\" {len(self.arguments)} argument(s)\"\n            )\n\n        return self._invoke(arguments, autoescape)\n\n    async def _async_invoke(self, arguments: list[t.Any], autoescape: bool) -> str:\n        rv = await self._func(*arguments)  # type: ignore\n\n        if autoescape:\n            return Markup(rv)\n\n        return rv  # type: ignore\n\n    def _invoke(self, arguments: list[t.Any], autoescape: bool) -> str:\n        if self._environment.is_async:\n            return self._async_invoke(arguments, autoescape)  # type: ignore\n\n        rv = self._func(*arguments)\n\n        if autoescape:\n            rv = Markup(rv)\n\n        return rv\n\n    def __repr__(self) -> str:\n        name = \"anonymous\" if self.name is None else repr(self.name)\n        return f\"<{type(self).__name__} {name}>\"\n\n\nclass Undefined:\n    \"\"\"The default undefined type. This can be printed, iterated, and treated as\n    a boolean. Any other operation will raise an :exc:`UndefinedError`.\n\n    >>> foo = Undefined(name='foo')\n    >>> str(foo)\n    ''\n    >>> not foo\n    True\n    >>> foo + 42\n    Traceback (most recent call last):\n      ...\n    jinja2.exceptions.UndefinedError: 'foo' is undefined\n    \"\"\"\n\n    __slots__ = (\n        \"_undefined_hint\",\n        \"_undefined_obj\",\n        \"_undefined_name\",\n        \"_undefined_exception\",\n    )\n\n    def __init__(\n        self,\n        hint: str | None = None,\n        obj: t.Any = missing,\n        name: str | None = None,\n        exc: type[TemplateRuntimeError] = UndefinedError,\n    ) -> None:\n        self._undefined_hint = hint\n        self._undefined_obj = obj\n        self._undefined_name = name\n        self._undefined_exception = exc\n\n    @property\n    def _undefined_message(self) -> str:\n        \"\"\"Build a message about the undefined value based on how it was\n        accessed.\n        \"\"\"\n        if self._undefined_hint:\n            return self._undefined_hint\n\n        if self._undefined_obj is missing:\n            return f\"{self._undefined_name!r} is undefined\"\n\n        if not isinstance(self._undefined_name, str):\n            return (\n                f\"{object_type_repr(self._undefined_obj)} has no\"\n                f\" element {self._undefined_name!r}\"\n            )\n\n        return (\n            f\"{object_type_repr(self._undefined_obj)!r} has no\"\n            f\" attribute {self._undefined_name!r}\"\n        )\n\n    @internalcode\n    def _fail_with_undefined_error(\n        self, *args: t.Any, **kwargs: t.Any\n    ) -> \"te.NoReturn\":\n        \"\"\"Raise an :exc:`UndefinedError` when operations are performed\n        on the undefined value.\n        \"\"\"\n        raise self._undefined_exception(self._undefined_message)\n\n    @internalcode\n    def __getattr__(self, name: str) -> t.Any:\n        # Raise AttributeError on requests for names that appear to be unimplemented\n        # dunder methods to keep Python's internal protocol probing behaviors working\n        # properly in cases where another exception type could cause unexpected or\n        # difficult-to-diagnose failures.\n        if name[:2] == \"__\" and name[-2:] == \"__\":\n            raise AttributeError(name)\n\n        return self._fail_with_undefined_error()\n\n    __add__ = __radd__ = __sub__ = __rsub__ = _fail_with_undefined_error\n    __mul__ = __rmul__ = __div__ = __rdiv__ = _fail_with_undefined_error\n    __truediv__ = __rtruediv__ = _fail_with_undefined_error\n    __floordiv__ = __rfloordiv__ = _fail_with_undefined_error\n    __mod__ = __rmod__ = _fail_with_undefined_error\n    __pos__ = __neg__ = _fail_with_undefined_error\n    __call__ = __getitem__ = _fail_with_undefined_error\n    __lt__ = __le__ = __gt__ = __ge__ = _fail_with_undefined_error\n    __int__ = __float__ = __complex__ = _fail_with_undefined_error\n    __pow__ = __rpow__ = _fail_with_undefined_error\n\n    def __eq__(self, other: t.Any) -> bool:\n        return type(self) is type(other)\n\n    def __ne__(self, other: t.Any) -> bool:\n        return not self.__eq__(other)\n\n    def __hash__(self) -> int:\n        return id(type(self))\n\n    def __str__(self) -> str:\n        return \"\"\n\n    def __len__(self) -> int:\n        return 0\n\n    def __iter__(self) -> t.Iterator[t.Any]:\n        yield from ()\n\n    async def __aiter__(self) -> t.AsyncIterator[t.Any]:\n        for _ in ():\n            yield\n\n    def __bool__(self) -> bool:\n        return False\n\n    def __repr__(self) -> str:\n        return \"Undefined\"\n\n\ndef make_logging_undefined(\n    logger: t.Optional[\"logging.Logger\"] = None, base: type[Undefined] = Undefined\n) -> type[Undefined]:\n    \"\"\"Given a logger object this returns a new undefined class that will\n    log certain failures.  It will log iterations and printing.  If no\n    logger is given a default logger is created.\n\n    Example::\n\n        logger = logging.getLogger(__name__)\n        LoggingUndefined = make_logging_undefined(\n            logger=logger,\n            base=Undefined\n        )\n\n    .. versionadded:: 2.8\n\n    :param logger: the logger to use.  If not provided, a default logger\n                   is created.\n    :param base: the base class to add logging functionality to.  This\n                 defaults to :class:`Undefined`.\n    \"\"\"\n    if logger is None:\n        import logging\n\n        logger = logging.getLogger(__name__)\n        logger.addHandler(logging.StreamHandler(sys.stderr))\n\n    def _log_message(undef: Undefined) -> None:\n        logger.warning(\"Template variable warning: %s\", undef._undefined_message)\n\n    class LoggingUndefined(base):  # type: ignore\n        __slots__ = ()\n\n        def _fail_with_undefined_error(  # type: ignore\n            self, *args: t.Any, **kwargs: t.Any\n        ) -> \"te.NoReturn\":\n            try:\n                super()._fail_with_undefined_error(*args, **kwargs)\n            except self._undefined_exception as e:\n                logger.error(\"Template variable error: %s\", e)  # type: ignore\n                raise e\n\n        def __str__(self) -> str:\n            _log_message(self)\n            return super().__str__()  # type: ignore\n\n        def __iter__(self) -> t.Iterator[t.Any]:\n            _log_message(self)\n            return super().__iter__()  # type: ignore\n\n        def __bool__(self) -> bool:\n            _log_message(self)\n            return super().__bool__()  # type: ignore\n\n    return LoggingUndefined\n\n\nclass ChainableUndefined(Undefined):\n    \"\"\"An undefined that is chainable, where both ``__getattr__`` and\n    ``__getitem__`` return itself rather than raising an\n    :exc:`UndefinedError`.\n\n    >>> foo = ChainableUndefined(name='foo')\n    >>> str(foo.bar['baz'])\n    ''\n    >>> foo.bar['baz'] + 42\n    Traceback (most recent call last):\n      ...\n    jinja2.exceptions.UndefinedError: 'foo' is undefined\n\n    .. versionadded:: 2.11.0\n    \"\"\"\n\n    __slots__ = ()\n\n    def __html__(self) -> str:\n        return str(self)\n\n    def __getattr__(self, name: str) -> \"ChainableUndefined\":\n        # Raise AttributeError on requests for names that appear to be unimplemented\n        # dunder methods to avoid confusing Python with truthy non-method objects that\n        # do not implement the protocol being probed for. e.g., copy.copy(Undefined())\n        # fails spectacularly if getattr(Undefined(), '__setstate__') returns an\n        # Undefined object instead of raising AttributeError to signal that it does not\n        # support that style of object initialization.\n        if name[:2] == \"__\" and name[-2:] == \"__\":\n            raise AttributeError(name)\n\n        return self\n\n    def __getitem__(self, _name: str) -> \"ChainableUndefined\":  # type: ignore[override]\n        return self\n\n\nclass DebugUndefined(Undefined):\n    \"\"\"An undefined that returns the debug info when printed.\n\n    >>> foo = DebugUndefined(name='foo')\n    >>> str(foo)\n    '{{ foo }}'\n    >>> not foo\n    True\n    >>> foo + 42\n    Traceback (most recent call last):\n      ...\n    jinja2.exceptions.UndefinedError: 'foo' is undefined\n    \"\"\"\n\n    __slots__ = ()\n\n    def __str__(self) -> str:\n        if self._undefined_hint:\n            message = f\"undefined value printed: {self._undefined_hint}\"\n\n        elif self._undefined_obj is missing:\n            message = self._undefined_name  # type: ignore\n\n        else:\n            message = (\n                f\"no such element: {object_type_repr(self._undefined_obj)}\"\n                f\"[{self._undefined_name!r}]\"\n            )\n\n        return f\"{{{{ {message} }}}}\"\n\n\nclass StrictUndefined(Undefined):\n    \"\"\"An undefined that barks on print and iteration as well as boolean\n    tests and all kinds of comparisons.  In other words: you can do nothing\n    with it except checking if it's defined using the `defined` test.\n\n    >>> foo = StrictUndefined(name='foo')\n    >>> str(foo)\n    Traceback (most recent call last):\n      ...\n    jinja2.exceptions.UndefinedError: 'foo' is undefined\n    >>> not foo\n    Traceback (most recent call last):\n      ...\n    jinja2.exceptions.UndefinedError: 'foo' is undefined\n    >>> foo + 42\n    Traceback (most recent call last):\n      ...\n    jinja2.exceptions.UndefinedError: 'foo' is undefined\n    \"\"\"\n\n    __slots__ = ()\n    __iter__ = __str__ = __len__ = Undefined._fail_with_undefined_error\n    __eq__ = __ne__ = __bool__ = __hash__ = Undefined._fail_with_undefined_error\n    __contains__ = Undefined._fail_with_undefined_error\n"
  },
  {
    "path": "src/jinja2/sandbox.py",
    "content": "\"\"\"A sandbox layer that ensures unsafe operations cannot be performed.\nUseful when the template itself comes from an untrusted source.\n\"\"\"\n\nimport operator\nimport types\nimport typing as t\nfrom _string import formatter_field_name_split  # type: ignore\nfrom collections import abc\nfrom collections import deque\nfrom functools import update_wrapper\nfrom string import Formatter\n\nfrom markupsafe import EscapeFormatter\nfrom markupsafe import Markup\n\nfrom .environment import Environment\nfrom .exceptions import SecurityError\nfrom .runtime import Context\nfrom .runtime import Undefined\n\nF = t.TypeVar(\"F\", bound=t.Callable[..., t.Any])\n\n#: maximum number of items a range may produce\nMAX_RANGE = 100000\n\n#: Unsafe function attributes.\nUNSAFE_FUNCTION_ATTRIBUTES: set[str] = set()\n\n#: Unsafe method attributes. Function attributes are unsafe for methods too.\nUNSAFE_METHOD_ATTRIBUTES: set[str] = set()\n\n#: unsafe generator attributes.\nUNSAFE_GENERATOR_ATTRIBUTES = {\"gi_frame\", \"gi_code\"}\n\n#: unsafe attributes on coroutines\nUNSAFE_COROUTINE_ATTRIBUTES = {\"cr_frame\", \"cr_code\"}\n\n#: unsafe attributes on async generators\nUNSAFE_ASYNC_GENERATOR_ATTRIBUTES = {\"ag_code\", \"ag_frame\"}\n\n_mutable_spec: tuple[tuple[type[t.Any], frozenset[str]], ...] = (\n    (\n        abc.MutableSet,\n        frozenset(\n            [\n                \"add\",\n                \"clear\",\n                \"difference_update\",\n                \"discard\",\n                \"pop\",\n                \"remove\",\n                \"symmetric_difference_update\",\n                \"update\",\n            ]\n        ),\n    ),\n    (\n        abc.MutableMapping,\n        frozenset([\"clear\", \"pop\", \"popitem\", \"setdefault\", \"update\"]),\n    ),\n    (\n        abc.MutableSequence,\n        frozenset(\n            [\"append\", \"clear\", \"pop\", \"reverse\", \"insert\", \"sort\", \"extend\", \"remove\"]\n        ),\n    ),\n    (\n        deque,\n        frozenset(\n            [\n                \"append\",\n                \"appendleft\",\n                \"clear\",\n                \"extend\",\n                \"extendleft\",\n                \"pop\",\n                \"popleft\",\n                \"remove\",\n                \"rotate\",\n            ]\n        ),\n    ),\n)\n\n\ndef safe_range(*args: int) -> range:\n    \"\"\"A range that can't generate ranges with a length of more than\n    MAX_RANGE items.\n    \"\"\"\n    rng = range(*args)\n\n    if len(rng) > MAX_RANGE:\n        raise OverflowError(\n            \"Range too big. The sandbox blocks ranges larger than\"\n            f\" MAX_RANGE ({MAX_RANGE}).\"\n        )\n\n    return rng\n\n\ndef unsafe(f: F) -> F:\n    \"\"\"Marks a function or method as unsafe.\n\n    .. code-block: python\n\n        @unsafe\n        def delete(self):\n            pass\n    \"\"\"\n    f.unsafe_callable = True  # type: ignore\n    return f\n\n\ndef is_internal_attribute(obj: t.Any, attr: str) -> bool:\n    \"\"\"Test if the attribute given is an internal python attribute.  For\n    example this function returns `True` for the `func_code` attribute of\n    python objects.  This is useful if the environment method\n    :meth:`~SandboxedEnvironment.is_safe_attribute` is overridden.\n\n    >>> from jinja2.sandbox import is_internal_attribute\n    >>> is_internal_attribute(str, \"mro\")\n    True\n    >>> is_internal_attribute(str, \"upper\")\n    False\n    \"\"\"\n    if isinstance(obj, types.FunctionType):\n        if attr in UNSAFE_FUNCTION_ATTRIBUTES:\n            return True\n    elif isinstance(obj, types.MethodType):\n        if attr in UNSAFE_FUNCTION_ATTRIBUTES or attr in UNSAFE_METHOD_ATTRIBUTES:\n            return True\n    elif isinstance(obj, type):\n        if attr == \"mro\":\n            return True\n    elif isinstance(obj, (types.CodeType, types.TracebackType, types.FrameType)):\n        return True\n    elif isinstance(obj, types.GeneratorType):\n        if attr in UNSAFE_GENERATOR_ATTRIBUTES:\n            return True\n    elif hasattr(types, \"CoroutineType\") and isinstance(obj, types.CoroutineType):\n        if attr in UNSAFE_COROUTINE_ATTRIBUTES:\n            return True\n    elif hasattr(types, \"AsyncGeneratorType\") and isinstance(\n        obj, types.AsyncGeneratorType\n    ):\n        if attr in UNSAFE_ASYNC_GENERATOR_ATTRIBUTES:\n            return True\n    return attr.startswith(\"__\")\n\n\ndef modifies_known_mutable(obj: t.Any, attr: str) -> bool:\n    \"\"\"This function checks if an attribute on a builtin mutable object\n    (list, dict, set or deque) or the corresponding ABCs would modify it\n    if called.\n\n    >>> modifies_known_mutable({}, \"clear\")\n    True\n    >>> modifies_known_mutable({}, \"keys\")\n    False\n    >>> modifies_known_mutable([], \"append\")\n    True\n    >>> modifies_known_mutable([], \"index\")\n    False\n\n    If called with an unsupported object, ``False`` is returned.\n\n    >>> modifies_known_mutable(\"foo\", \"upper\")\n    False\n    \"\"\"\n    for typespec, unsafe in _mutable_spec:\n        if isinstance(obj, typespec):\n            return attr in unsafe\n    return False\n\n\nclass SandboxedEnvironment(Environment):\n    \"\"\"The sandboxed environment.  It works like the regular environment but\n    tells the compiler to generate sandboxed code.  Additionally subclasses of\n    this environment may override the methods that tell the runtime what\n    attributes or functions are safe to access.\n\n    If the template tries to access insecure code a :exc:`SecurityError` is\n    raised.  However also other exceptions may occur during the rendering so\n    the caller has to ensure that all exceptions are caught.\n    \"\"\"\n\n    sandboxed = True\n\n    #: default callback table for the binary operators.  A copy of this is\n    #: available on each instance of a sandboxed environment as\n    #: :attr:`binop_table`\n    default_binop_table: dict[str, t.Callable[[t.Any, t.Any], t.Any]] = {\n        \"+\": operator.add,\n        \"-\": operator.sub,\n        \"*\": operator.mul,\n        \"/\": operator.truediv,\n        \"//\": operator.floordiv,\n        \"**\": operator.pow,\n        \"%\": operator.mod,\n    }\n\n    #: default callback table for the unary operators.  A copy of this is\n    #: available on each instance of a sandboxed environment as\n    #: :attr:`unop_table`\n    default_unop_table: dict[str, t.Callable[[t.Any], t.Any]] = {\n        \"+\": operator.pos,\n        \"-\": operator.neg,\n    }\n\n    #: a set of binary operators that should be intercepted.  Each operator\n    #: that is added to this set (empty by default) is delegated to the\n    #: :meth:`call_binop` method that will perform the operator.  The default\n    #: operator callback is specified by :attr:`binop_table`.\n    #:\n    #: The following binary operators are interceptable:\n    #: ``//``, ``%``, ``+``, ``*``, ``-``, ``/``, and ``**``\n    #:\n    #: The default operation form the operator table corresponds to the\n    #: builtin function.  Intercepted calls are always slower than the native\n    #: operator call, so make sure only to intercept the ones you are\n    #: interested in.\n    #:\n    #: .. versionadded:: 2.6\n    intercepted_binops: frozenset[str] = frozenset()\n\n    #: a set of unary operators that should be intercepted.  Each operator\n    #: that is added to this set (empty by default) is delegated to the\n    #: :meth:`call_unop` method that will perform the operator.  The default\n    #: operator callback is specified by :attr:`unop_table`.\n    #:\n    #: The following unary operators are interceptable: ``+``, ``-``\n    #:\n    #: The default operation form the operator table corresponds to the\n    #: builtin function.  Intercepted calls are always slower than the native\n    #: operator call, so make sure only to intercept the ones you are\n    #: interested in.\n    #:\n    #: .. versionadded:: 2.6\n    intercepted_unops: frozenset[str] = frozenset()\n\n    def __init__(self, *args: t.Any, **kwargs: t.Any) -> None:\n        super().__init__(*args, **kwargs)\n        self.globals[\"range\"] = safe_range\n        self.binop_table = self.default_binop_table.copy()\n        self.unop_table = self.default_unop_table.copy()\n\n    def is_safe_attribute(self, obj: t.Any, attr: str, value: t.Any) -> bool:\n        \"\"\"The sandboxed environment will call this method to check if the\n        attribute of an object is safe to access.  Per default all attributes\n        starting with an underscore are considered private as well as the\n        special attributes of internal python objects as returned by the\n        :func:`is_internal_attribute` function.\n        \"\"\"\n        return not (attr.startswith(\"_\") or is_internal_attribute(obj, attr))\n\n    def is_safe_callable(self, obj: t.Any) -> bool:\n        \"\"\"Check if an object is safely callable. By default callables\n        are considered safe unless decorated with :func:`unsafe`.\n\n        This also recognizes the Django convention of setting\n        ``func.alters_data = True``.\n        \"\"\"\n        return not (\n            getattr(obj, \"unsafe_callable\", False) or getattr(obj, \"alters_data\", False)\n        )\n\n    def call_binop(\n        self, context: Context, operator: str, left: t.Any, right: t.Any\n    ) -> t.Any:\n        \"\"\"For intercepted binary operator calls (:meth:`intercepted_binops`)\n        this function is executed instead of the builtin operator.  This can\n        be used to fine tune the behavior of certain operators.\n\n        .. versionadded:: 2.6\n        \"\"\"\n        return self.binop_table[operator](left, right)\n\n    def call_unop(self, context: Context, operator: str, arg: t.Any) -> t.Any:\n        \"\"\"For intercepted unary operator calls (:meth:`intercepted_unops`)\n        this function is executed instead of the builtin operator.  This can\n        be used to fine tune the behavior of certain operators.\n\n        .. versionadded:: 2.6\n        \"\"\"\n        return self.unop_table[operator](arg)\n\n    def getitem(self, obj: t.Any, argument: str | t.Any) -> t.Any | Undefined:\n        \"\"\"Subscribe an object from sandboxed code.\"\"\"\n        try:\n            return obj[argument]\n        except (TypeError, LookupError):\n            if isinstance(argument, str):\n                try:\n                    attr = str(argument)\n                except Exception:\n                    pass\n                else:\n                    try:\n                        value = getattr(obj, attr)\n                    except AttributeError:\n                        pass\n                    else:\n                        fmt = self.wrap_str_format(value)\n                        if fmt is not None:\n                            return fmt\n                        if self.is_safe_attribute(obj, argument, value):\n                            return value\n                        return self.unsafe_undefined(obj, argument)\n        return self.undefined(obj=obj, name=argument)\n\n    def getattr(self, obj: t.Any, attribute: str) -> t.Any | Undefined:\n        \"\"\"Subscribe an object from sandboxed code and prefer the\n        attribute.  The attribute passed *must* be a bytestring.\n        \"\"\"\n        try:\n            value = getattr(obj, attribute)\n        except AttributeError:\n            try:\n                return obj[attribute]\n            except (TypeError, LookupError):\n                pass\n        else:\n            fmt = self.wrap_str_format(value)\n            if fmt is not None:\n                return fmt\n            if self.is_safe_attribute(obj, attribute, value):\n                return value\n            return self.unsafe_undefined(obj, attribute)\n        return self.undefined(obj=obj, name=attribute)\n\n    def unsafe_undefined(self, obj: t.Any, attribute: str) -> Undefined:\n        \"\"\"Return an undefined object for unsafe attributes.\"\"\"\n        return self.undefined(\n            f\"access to attribute {attribute!r} of\"\n            f\" {type(obj).__name__!r} object is unsafe.\",\n            name=attribute,\n            obj=obj,\n            exc=SecurityError,\n        )\n\n    def wrap_str_format(self, value: t.Any) -> t.Callable[..., str] | None:\n        \"\"\"If the given value is a ``str.format`` or ``str.format_map`` method,\n        return a new function than handles sandboxing. This is done at access\n        rather than in :meth:`call`, so that calls made without ``call`` are\n        also sandboxed.\n        \"\"\"\n        if not isinstance(\n            value, (types.MethodType, types.BuiltinMethodType)\n        ) or value.__name__ not in (\"format\", \"format_map\"):\n            return None\n\n        f_self: t.Any = value.__self__\n\n        if not isinstance(f_self, str):\n            return None\n\n        str_type: type[str] = type(f_self)\n        is_format_map = value.__name__ == \"format_map\"\n        formatter: SandboxedFormatter\n\n        if isinstance(f_self, Markup):\n            formatter = SandboxedEscapeFormatter(self, escape=f_self.escape)\n        else:\n            formatter = SandboxedFormatter(self)\n\n        vformat = formatter.vformat\n\n        def wrapper(*args: t.Any, **kwargs: t.Any) -> str:\n            if is_format_map:\n                if kwargs:\n                    raise TypeError(\"format_map() takes no keyword arguments\")\n\n                if len(args) != 1:\n                    raise TypeError(\n                        f\"format_map() takes exactly one argument ({len(args)} given)\"\n                    )\n\n                kwargs = args[0]\n                args = ()\n\n            return str_type(vformat(f_self, args, kwargs))\n\n        return update_wrapper(wrapper, value)\n\n    def call(\n        __self,  # noqa: B902\n        __context: Context,\n        __obj: t.Any,\n        *args: t.Any,\n        **kwargs: t.Any,\n    ) -> t.Any:\n        \"\"\"Call an object from sandboxed code.\"\"\"\n\n        # the double prefixes are to avoid double keyword argument\n        # errors when proxying the call.\n        if not __self.is_safe_callable(__obj):\n            raise SecurityError(f\"{__obj!r} is not safely callable\")\n        return __context.call(__obj, *args, **kwargs)\n\n\nclass ImmutableSandboxedEnvironment(SandboxedEnvironment):\n    \"\"\"Works exactly like the regular `SandboxedEnvironment` but does not\n    permit modifications on the builtin mutable objects `list`, `set`, and\n    `dict` by using the :func:`modifies_known_mutable` function.\n    \"\"\"\n\n    def is_safe_attribute(self, obj: t.Any, attr: str, value: t.Any) -> bool:\n        if not super().is_safe_attribute(obj, attr, value):\n            return False\n\n        return not modifies_known_mutable(obj, attr)\n\n\nclass SandboxedFormatter(Formatter):\n    def __init__(self, env: Environment, **kwargs: t.Any) -> None:\n        self._env = env\n        super().__init__(**kwargs)\n\n    def get_field(\n        self, field_name: str, args: t.Sequence[t.Any], kwargs: t.Mapping[str, t.Any]\n    ) -> tuple[t.Any, str]:\n        first, rest = formatter_field_name_split(field_name)\n        obj = self.get_value(first, args, kwargs)\n        for is_attr, i in rest:\n            if is_attr:\n                obj = self._env.getattr(obj, i)\n            else:\n                obj = self._env.getitem(obj, i)\n        return obj, first\n\n\nclass SandboxedEscapeFormatter(SandboxedFormatter, EscapeFormatter):\n    pass\n"
  },
  {
    "path": "src/jinja2/tests.py",
    "content": "\"\"\"Built-in template tests used with the ``is`` operator.\"\"\"\n\nimport operator\nimport typing as t\nfrom collections import abc\nfrom numbers import Number\n\nfrom .runtime import Undefined\nfrom .utils import pass_environment\n\nif t.TYPE_CHECKING:\n    from .environment import Environment\n\n\ndef test_odd(value: int) -> bool:\n    \"\"\"Return true if the variable is odd.\"\"\"\n    return value % 2 == 1\n\n\ndef test_even(value: int) -> bool:\n    \"\"\"Return true if the variable is even.\"\"\"\n    return value % 2 == 0\n\n\ndef test_divisibleby(value: int, num: int) -> bool:\n    \"\"\"Check if a variable is divisible by a number.\"\"\"\n    return value % num == 0\n\n\ndef test_defined(value: t.Any) -> bool:\n    \"\"\"Return true if the variable is defined:\n\n    .. sourcecode:: jinja\n\n        {% if variable is defined %}\n            value of variable: {{ variable }}\n        {% else %}\n            variable is not defined\n        {% endif %}\n\n    See the :func:`default` filter for a simple way to set undefined\n    variables.\n    \"\"\"\n    return not isinstance(value, Undefined)\n\n\ndef test_undefined(value: t.Any) -> bool:\n    \"\"\"Like :func:`defined` but the other way round.\"\"\"\n    return isinstance(value, Undefined)\n\n\n@pass_environment\ndef test_filter(env: \"Environment\", value: str) -> bool:\n    \"\"\"Check if a filter exists by name. Useful if a filter may be\n    optionally available.\n\n    .. code-block:: jinja\n\n        {% if 'markdown' is filter %}\n            {{ value | markdown }}\n        {% else %}\n            {{ value }}\n        {% endif %}\n\n    .. versionadded:: 3.0\n    \"\"\"\n    return value in env.filters\n\n\n@pass_environment\ndef test_test(env: \"Environment\", value: str) -> bool:\n    \"\"\"Check if a test exists by name. Useful if a test may be\n    optionally available.\n\n    .. code-block:: jinja\n\n        {% if 'loud' is test %}\n            {% if value is loud %}\n                {{ value|upper }}\n            {% else %}\n                {{ value|lower }}\n            {% endif %}\n        {% else %}\n            {{ value }}\n        {% endif %}\n\n    .. versionadded:: 3.0\n    \"\"\"\n    return value in env.tests\n\n\ndef test_none(value: t.Any) -> bool:\n    \"\"\"Return true if the variable is none.\"\"\"\n    return value is None\n\n\ndef test_boolean(value: t.Any) -> bool:\n    \"\"\"Return true if the object is a boolean value.\n\n    .. versionadded:: 2.11\n    \"\"\"\n    return value is True or value is False\n\n\ndef test_false(value: t.Any) -> bool:\n    \"\"\"Return true if the object is False.\n\n    .. versionadded:: 2.11\n    \"\"\"\n    return value is False\n\n\ndef test_true(value: t.Any) -> bool:\n    \"\"\"Return true if the object is True.\n\n    .. versionadded:: 2.11\n    \"\"\"\n    return value is True\n\n\n# NOTE: The existing 'number' test matches booleans and floats\ndef test_integer(value: t.Any) -> bool:\n    \"\"\"Return true if the object is an integer.\n\n    .. versionadded:: 2.11\n    \"\"\"\n    return isinstance(value, int) and value is not True and value is not False\n\n\n# NOTE: The existing 'number' test matches booleans and integers\ndef test_float(value: t.Any) -> bool:\n    \"\"\"Return true if the object is a float.\n\n    .. versionadded:: 2.11\n    \"\"\"\n    return isinstance(value, float)\n\n\ndef test_lower(value: str) -> bool:\n    \"\"\"Return true if the variable is lowercased.\"\"\"\n    return str(value).islower()\n\n\ndef test_upper(value: str) -> bool:\n    \"\"\"Return true if the variable is uppercased.\"\"\"\n    return str(value).isupper()\n\n\ndef test_string(value: t.Any) -> bool:\n    \"\"\"Return true if the object is a string.\"\"\"\n    return isinstance(value, str)\n\n\ndef test_mapping(value: t.Any) -> bool:\n    \"\"\"Return true if the object is a mapping (dict etc.).\n\n    .. versionadded:: 2.6\n    \"\"\"\n    return isinstance(value, abc.Mapping)\n\n\ndef test_number(value: t.Any) -> bool:\n    \"\"\"Return true if the variable is a number.\"\"\"\n    return isinstance(value, Number)\n\n\ndef test_sequence(value: t.Any) -> bool:\n    \"\"\"Return true if the variable is a sequence. Sequences are variables\n    that are iterable.\n    \"\"\"\n    try:\n        len(value)\n        value.__getitem__  # noqa B018\n    except Exception:\n        return False\n\n    return True\n\n\ndef test_sameas(value: t.Any, other: t.Any) -> bool:\n    \"\"\"Check if an object points to the same memory address than another\n    object:\n\n    .. sourcecode:: jinja\n\n        {% if foo.attribute is sameas false %}\n            the foo attribute really is the `False` singleton\n        {% endif %}\n    \"\"\"\n    return value is other\n\n\ndef test_iterable(value: t.Any) -> bool:\n    \"\"\"Check if it's possible to iterate over an object.\"\"\"\n    try:\n        iter(value)\n    except TypeError:\n        return False\n\n    return True\n\n\ndef test_escaped(value: t.Any) -> bool:\n    \"\"\"Check if the value is escaped.\"\"\"\n    return hasattr(value, \"__html__\")\n\n\ndef test_in(value: t.Any, seq: t.Container[t.Any]) -> bool:\n    \"\"\"Check if value is in seq.\n\n    .. versionadded:: 2.10\n    \"\"\"\n    return value in seq\n\n\nTESTS = {\n    \"odd\": test_odd,\n    \"even\": test_even,\n    \"divisibleby\": test_divisibleby,\n    \"defined\": test_defined,\n    \"undefined\": test_undefined,\n    \"filter\": test_filter,\n    \"test\": test_test,\n    \"none\": test_none,\n    \"boolean\": test_boolean,\n    \"false\": test_false,\n    \"true\": test_true,\n    \"integer\": test_integer,\n    \"float\": test_float,\n    \"lower\": test_lower,\n    \"upper\": test_upper,\n    \"string\": test_string,\n    \"mapping\": test_mapping,\n    \"number\": test_number,\n    \"sequence\": test_sequence,\n    \"iterable\": test_iterable,\n    \"callable\": callable,\n    \"sameas\": test_sameas,\n    \"escaped\": test_escaped,\n    \"in\": test_in,\n    \"==\": operator.eq,\n    \"eq\": operator.eq,\n    \"equalto\": operator.eq,\n    \"!=\": operator.ne,\n    \"ne\": operator.ne,\n    \">\": operator.gt,\n    \"gt\": operator.gt,\n    \"greaterthan\": operator.gt,\n    \"ge\": operator.ge,\n    \">=\": operator.ge,\n    \"<\": operator.lt,\n    \"lt\": operator.lt,\n    \"lessthan\": operator.lt,\n    \"<=\": operator.le,\n    \"le\": operator.le,\n}\n"
  },
  {
    "path": "src/jinja2/utils.py",
    "content": "import enum\nimport json\nimport os\nimport re\nimport typing as t\nfrom collections import abc\nfrom collections import deque\nfrom random import choice\nfrom random import randrange\nfrom threading import Lock\nfrom types import CodeType\nfrom urllib.parse import quote_from_bytes\n\nimport markupsafe\n\nif t.TYPE_CHECKING:\n    import typing_extensions as te\n\nF = t.TypeVar(\"F\", bound=t.Callable[..., t.Any])\n\n\nclass _MissingType:\n    def __repr__(self) -> str:\n        return \"missing\"\n\n    def __reduce__(self) -> str:\n        return \"missing\"\n\n\nmissing: t.Any = _MissingType()\n\"\"\"Special singleton representing missing values for the runtime.\"\"\"\n\ninternal_code: t.MutableSet[CodeType] = set()\n\nconcat = \"\".join\n\n\ndef pass_context(f: F) -> F:\n    \"\"\"Pass the :class:`~jinja2.runtime.Context` as the first argument\n    to the decorated function when called while rendering a template.\n\n    Can be used on functions, filters, and tests.\n\n    If only ``Context.eval_context`` is needed, use\n    :func:`pass_eval_context`. If only ``Context.environment`` is\n    needed, use :func:`pass_environment`.\n\n    .. versionadded:: 3.0.0\n        Replaces ``contextfunction`` and ``contextfilter``.\n    \"\"\"\n    f.jinja_pass_arg = _PassArg.context  # type: ignore\n    return f\n\n\ndef pass_eval_context(f: F) -> F:\n    \"\"\"Pass the :class:`~jinja2.nodes.EvalContext` as the first argument\n    to the decorated function when called while rendering a template.\n    See :ref:`eval-context`.\n\n    Can be used on functions, filters, and tests.\n\n    If only ``EvalContext.environment`` is needed, use\n    :func:`pass_environment`.\n\n    .. versionadded:: 3.0.0\n        Replaces ``evalcontextfunction`` and ``evalcontextfilter``.\n    \"\"\"\n    f.jinja_pass_arg = _PassArg.eval_context  # type: ignore\n    return f\n\n\ndef pass_environment(f: F) -> F:\n    \"\"\"Pass the :class:`~jinja2.Environment` as the first argument to\n    the decorated function when called while rendering a template.\n\n    Can be used on functions, filters, and tests.\n\n    .. versionadded:: 3.0.0\n        Replaces ``environmentfunction`` and ``environmentfilter``.\n    \"\"\"\n    f.jinja_pass_arg = _PassArg.environment  # type: ignore\n    return f\n\n\nclass _PassArg(enum.Enum):\n    context = enum.auto()\n    eval_context = enum.auto()\n    environment = enum.auto()\n\n    @classmethod\n    def from_obj(cls, obj: F) -> t.Optional[\"_PassArg\"]:\n        if hasattr(obj, \"jinja_pass_arg\"):\n            return obj.jinja_pass_arg  # type: ignore\n\n        return None\n\n\ndef internalcode(f: F) -> F:\n    \"\"\"Marks the function as internally used\"\"\"\n    internal_code.add(f.__code__)\n    return f\n\n\ndef is_undefined(obj: t.Any) -> bool:\n    \"\"\"Check if the object passed is undefined.  This does nothing more than\n    performing an instance check against :class:`Undefined` but looks nicer.\n    This can be used for custom filters or tests that want to react to\n    undefined variables.  For example a custom default filter can look like\n    this::\n\n        def default(var, default=''):\n            if is_undefined(var):\n                return default\n            return var\n    \"\"\"\n    from .runtime import Undefined\n\n    return isinstance(obj, Undefined)\n\n\ndef consume(iterable: t.Iterable[t.Any]) -> None:\n    \"\"\"Consumes an iterable without doing anything with it.\"\"\"\n    for _ in iterable:\n        pass\n\n\ndef clear_caches() -> None:\n    \"\"\"Jinja keeps internal caches for environments and lexers.  These are\n    used so that Jinja doesn't have to recreate environments and lexers all\n    the time.  Normally you don't have to care about that but if you are\n    measuring memory consumption you may want to clean the caches.\n    \"\"\"\n    from .environment import get_spontaneous_environment\n    from .lexer import _lexer_cache\n\n    get_spontaneous_environment.cache_clear()\n    _lexer_cache.clear()\n\n\ndef import_string(import_name: str, silent: bool = False) -> t.Any:\n    \"\"\"Imports an object based on a string.  This is useful if you want to\n    use import paths as endpoints or something similar.  An import path can\n    be specified either in dotted notation (``xml.sax.saxutils.escape``)\n    or with a colon as object delimiter (``xml.sax.saxutils:escape``).\n\n    If the `silent` is True the return value will be `None` if the import\n    fails.\n\n    :return: imported object\n    \"\"\"\n    try:\n        if \":\" in import_name:\n            module, obj = import_name.split(\":\", 1)\n        elif \".\" in import_name:\n            module, _, obj = import_name.rpartition(\".\")\n        else:\n            return __import__(import_name)\n        return getattr(__import__(module, None, None, [obj]), obj)\n    except (ImportError, AttributeError):\n        if not silent:\n            raise\n\n\ndef open_if_exists(filename: str, mode: str = \"rb\") -> t.IO[t.Any] | None:\n    \"\"\"Returns a file descriptor for the filename if that file exists,\n    otherwise ``None``.\n    \"\"\"\n    if not os.path.isfile(filename):\n        return None\n\n    return open(filename, mode)\n\n\ndef object_type_repr(obj: t.Any) -> str:\n    \"\"\"Returns the name of the object's type.  For some recognized\n    singletons the name of the object is returned instead. (For\n    example for `None` and `Ellipsis`).\n    \"\"\"\n    if obj is None:\n        return \"None\"\n    elif obj is Ellipsis:\n        return \"Ellipsis\"\n\n    cls = type(obj)\n\n    if cls.__module__ == \"builtins\":\n        return f\"{cls.__name__} object\"\n\n    return f\"{cls.__module__}.{cls.__name__} object\"\n\n\ndef pformat(obj: t.Any) -> str:\n    \"\"\"Format an object using :func:`pprint.pformat`.\"\"\"\n    from pprint import pformat\n\n    return pformat(obj)\n\n\n_http_re = re.compile(\n    r\"\"\"\n    ^\n    (\n        (https?://|www\\.)  # scheme or www\n        (([\\w%-]+\\.)+)?  # subdomain\n        (\n            [a-z]{2,63}  # basic tld\n        |\n            xn--[\\w%]{2,59}  # idna tld\n        )\n    |\n        ([\\w%-]{2,63}\\.)+  # basic domain\n        (com|net|int|edu|gov|org|info|mil)  # basic tld\n    |\n        (https?://)  # scheme\n        (\n            (([\\d]{1,3})(\\.[\\d]{1,3}){3})  # IPv4\n        |\n            (\\[([\\da-f]{0,4}:){2}([\\da-f]{0,4}:?){1,6}])  # IPv6\n        )\n    )\n    (?::[\\d]{1,5})?  # port\n    (?:[/?#]\\S*)?  # path, query, and fragment\n    $\n    \"\"\",\n    re.IGNORECASE | re.VERBOSE,\n)\n_email_re = re.compile(r\"^\\S+@\\w[\\w.-]*\\.\\w+$\")\n\n\ndef urlize(\n    text: str,\n    trim_url_limit: int | None = None,\n    rel: str | None = None,\n    target: str | None = None,\n    extra_schemes: t.Iterable[str] | None = None,\n) -> str:\n    \"\"\"Convert URLs in text into clickable links.\n\n    This may not recognize links in some situations. Usually, a more\n    comprehensive formatter, such as a Markdown library, is a better\n    choice.\n\n    Works on ``http://``, ``https://``, ``www.``, ``mailto:``, and email\n    addresses. Links with trailing punctuation (periods, commas, closing\n    parentheses) and leading punctuation (opening parentheses) are\n    recognized excluding the punctuation. Email addresses that include\n    header fields are not recognized (for example,\n    ``mailto:address@example.com?cc=copy@example.com``).\n\n    :param text: Original text containing URLs to link.\n    :param trim_url_limit: Shorten displayed URL values to this length.\n    :param target: Add the ``target`` attribute to links.\n    :param rel: Add the ``rel`` attribute to links.\n    :param extra_schemes: Recognize URLs that start with these schemes\n        in addition to the default behavior.\n\n    .. versionchanged:: 3.0\n        The ``extra_schemes`` parameter was added.\n\n    .. versionchanged:: 3.0\n        Generate ``https://`` links for URLs without a scheme.\n\n    .. versionchanged:: 3.0\n        The parsing rules were updated. Recognize email addresses with\n        or without the ``mailto:`` scheme. Validate IP addresses. Ignore\n        parentheses and brackets in more cases.\n    \"\"\"\n    if trim_url_limit is not None:\n\n        def trim_url(x: str) -> str:\n            if len(x) > trim_url_limit:\n                return f\"{x[:trim_url_limit]}...\"\n\n            return x\n\n    else:\n\n        def trim_url(x: str) -> str:\n            return x\n\n    words = re.split(r\"(\\s+)\", str(markupsafe.escape(text)))\n    rel_attr = f' rel=\"{markupsafe.escape(rel)}\"' if rel else \"\"\n    target_attr = f' target=\"{markupsafe.escape(target)}\"' if target else \"\"\n\n    for i, word in enumerate(words):\n        head, middle, tail = \"\", word, \"\"\n        match = re.match(r\"^([(<]|&lt;)+\", middle)\n\n        if match:\n            head = match.group()\n            middle = middle[match.end() :]\n\n        # Unlike lead, which is anchored to the start of the string,\n        # need to check that the string ends with any of the characters\n        # before trying to match all of them, to avoid backtracking.\n        if middle.endswith((\")\", \">\", \".\", \",\", \"\\n\", \"&gt;\")):\n            match = re.search(r\"([)>.,\\n]|&gt;)+$\", middle)\n\n            if match:\n                tail = match.group()\n                middle = middle[: match.start()]\n\n        # Prefer balancing parentheses in URLs instead of ignoring a\n        # trailing character.\n        for start_char, end_char in (\"(\", \")\"), (\"<\", \">\"), (\"&lt;\", \"&gt;\"):\n            start_count = middle.count(start_char)\n\n            if start_count <= middle.count(end_char):\n                # Balanced, or lighter on the left\n                continue\n\n            # Move as many as possible from the tail to balance\n            for _ in range(min(start_count, tail.count(end_char))):\n                end_index = tail.index(end_char) + len(end_char)\n                # Move anything in the tail before the end char too\n                middle += tail[:end_index]\n                tail = tail[end_index:]\n\n        if _http_re.match(middle):\n            if middle.startswith(\"https://\") or middle.startswith(\"http://\"):\n                middle = (\n                    f'<a href=\"{middle}\"{rel_attr}{target_attr}>{trim_url(middle)}</a>'\n                )\n            else:\n                middle = (\n                    f'<a href=\"https://{middle}\"{rel_attr}{target_attr}>'\n                    f\"{trim_url(middle)}</a>\"\n                )\n\n        elif middle.startswith(\"mailto:\") and _email_re.match(middle[7:]):\n            middle = f'<a href=\"{middle}\">{middle[7:]}</a>'\n\n        elif (\n            \"@\" in middle\n            and not middle.startswith(\"www.\")\n            # ignore values like `@a@b`\n            and not middle.startswith(\"@\")\n            and \":\" not in middle\n            and _email_re.match(middle)\n        ):\n            middle = f'<a href=\"mailto:{middle}\">{middle}</a>'\n\n        elif extra_schemes is not None:\n            for scheme in extra_schemes:\n                if middle != scheme and middle.startswith(scheme):\n                    middle = f'<a href=\"{middle}\"{rel_attr}{target_attr}>{middle}</a>'\n\n        words[i] = f\"{head}{middle}{tail}\"\n\n    return \"\".join(words)\n\n\ndef generate_lorem_ipsum(\n    n: int = 5, html: bool = True, min: int = 20, max: int = 100\n) -> str:\n    \"\"\"Generate some lorem ipsum for the template.\"\"\"\n    from .constants import LOREM_IPSUM_WORDS\n\n    words = LOREM_IPSUM_WORDS.split()\n    result = []\n\n    for _ in range(n):\n        next_capitalized = True\n        last_comma = last_fullstop = 0\n        word = None\n        last = None\n        p = []\n\n        # each paragraph contains out of 20 to 100 words.\n        for idx, _ in enumerate(range(randrange(min, max))):\n            while True:\n                word = choice(words)\n                if word != last:\n                    last = word\n                    break\n            if next_capitalized:\n                word = word.capitalize()\n                next_capitalized = False\n            # add commas\n            if idx - randrange(3, 8) > last_comma:\n                last_comma = idx\n                last_fullstop += 2\n                word += \",\"\n            # add end of sentences\n            if idx - randrange(10, 20) > last_fullstop:\n                last_comma = last_fullstop = idx\n                word += \".\"\n                next_capitalized = True\n            p.append(word)\n\n        # ensure that the paragraph ends with a dot.\n        p_str = \" \".join(p)\n\n        if p_str.endswith(\",\"):\n            p_str = p_str[:-1] + \".\"\n        elif not p_str.endswith(\".\"):\n            p_str += \".\"\n\n        result.append(p_str)\n\n    if not html:\n        return \"\\n\\n\".join(result)\n    return markupsafe.Markup(\n        \"\\n\".join(f\"<p>{markupsafe.escape(x)}</p>\" for x in result)\n    )\n\n\ndef url_quote(obj: t.Any, charset: str = \"utf-8\", for_qs: bool = False) -> str:\n    \"\"\"Quote a string for use in a URL using the given charset.\n\n    :param obj: String or bytes to quote. Other types are converted to\n        string then encoded to bytes using the given charset.\n    :param charset: Encode text to bytes using this charset.\n    :param for_qs: Quote \"/\" and use \"+\" for spaces.\n    \"\"\"\n    if not isinstance(obj, bytes):\n        if not isinstance(obj, str):\n            obj = str(obj)\n\n        obj = obj.encode(charset)\n\n    safe = b\"\" if for_qs else b\"/\"\n    rv = quote_from_bytes(obj, safe)\n\n    if for_qs:\n        rv = rv.replace(\"%20\", \"+\")\n\n    return rv\n\n\n@abc.MutableMapping.register\nclass LRUCache:\n    \"\"\"A simple LRU Cache implementation.\"\"\"\n\n    # this is fast for small capacities (something below 1000) but doesn't\n    # scale.  But as long as it's only used as storage for templates this\n    # won't do any harm.\n\n    def __init__(self, capacity: int) -> None:\n        self.capacity = capacity\n        self._mapping: dict[t.Any, t.Any] = {}\n        self._queue: deque[t.Any] = deque()\n        self._postinit()\n\n    def _postinit(self) -> None:\n        # alias all queue methods for faster lookup\n        self._popleft = self._queue.popleft\n        self._pop = self._queue.pop\n        self._remove = self._queue.remove\n        self._wlock = Lock()\n        self._append = self._queue.append\n\n    def __getstate__(self) -> t.Mapping[str, t.Any]:\n        return {\n            \"capacity\": self.capacity,\n            \"_mapping\": self._mapping,\n            \"_queue\": self._queue,\n        }\n\n    def __setstate__(self, d: t.Mapping[str, t.Any]) -> None:\n        self.__dict__.update(d)\n        self._postinit()\n\n    def __getnewargs__(self) -> tuple[t.Any, ...]:\n        return (self.capacity,)\n\n    def copy(self) -> \"te.Self\":\n        \"\"\"Return a shallow copy of the instance.\"\"\"\n        rv = self.__class__(self.capacity)\n        rv._mapping.update(self._mapping)\n        rv._queue.extend(self._queue)\n        return rv\n\n    def get(self, key: t.Any, default: t.Any = None) -> t.Any:\n        \"\"\"Return an item from the cache dict or `default`\"\"\"\n        try:\n            return self[key]\n        except KeyError:\n            return default\n\n    def setdefault(self, key: t.Any, default: t.Any = None) -> t.Any:\n        \"\"\"Set `default` if the key is not in the cache otherwise\n        leave unchanged. Return the value of this key.\n        \"\"\"\n        try:\n            return self[key]\n        except KeyError:\n            self[key] = default\n            return default\n\n    def clear(self) -> None:\n        \"\"\"Clear the cache.\"\"\"\n        with self._wlock:\n            self._mapping.clear()\n            self._queue.clear()\n\n    def __contains__(self, key: t.Any) -> bool:\n        \"\"\"Check if a key exists in this cache.\"\"\"\n        return key in self._mapping\n\n    def __len__(self) -> int:\n        \"\"\"Return the current size of the cache.\"\"\"\n        return len(self._mapping)\n\n    def __repr__(self) -> str:\n        return f\"<{type(self).__name__} {self._mapping!r}>\"\n\n    def __getitem__(self, key: t.Any) -> t.Any:\n        \"\"\"Get an item from the cache. Moves the item up so that it has the\n        highest priority then.\n\n        Raise a `KeyError` if it does not exist.\n        \"\"\"\n        with self._wlock:\n            rv = self._mapping[key]\n\n            if self._queue[-1] != key:\n                try:\n                    self._remove(key)\n                except ValueError:\n                    # if something removed the key from the container\n                    # when we read, ignore the ValueError that we would\n                    # get otherwise.\n                    pass\n\n                self._append(key)\n\n            return rv\n\n    def __setitem__(self, key: t.Any, value: t.Any) -> None:\n        \"\"\"Sets the value for an item. Moves the item up so that it\n        has the highest priority then.\n        \"\"\"\n        with self._wlock:\n            if key in self._mapping:\n                self._remove(key)\n            elif len(self._mapping) == self.capacity:\n                del self._mapping[self._popleft()]\n\n            self._append(key)\n            self._mapping[key] = value\n\n    def __delitem__(self, key: t.Any) -> None:\n        \"\"\"Remove an item from the cache dict.\n        Raise a `KeyError` if it does not exist.\n        \"\"\"\n        with self._wlock:\n            del self._mapping[key]\n\n            try:\n                self._remove(key)\n            except ValueError:\n                pass\n\n    def items(self) -> t.Iterable[tuple[t.Any, t.Any]]:\n        \"\"\"Return a list of items.\"\"\"\n        result = [(key, self._mapping[key]) for key in list(self._queue)]\n        result.reverse()\n        return result\n\n    def values(self) -> t.Iterable[t.Any]:\n        \"\"\"Return a list of all values.\"\"\"\n        return [x[1] for x in self.items()]\n\n    def keys(self) -> t.Iterable[t.Any]:\n        \"\"\"Return a list of all keys ordered by most recent usage.\"\"\"\n        return list(self)\n\n    def __iter__(self) -> t.Iterator[t.Any]:\n        return reversed(tuple(self._queue))\n\n    def __reversed__(self) -> t.Iterator[t.Any]:\n        \"\"\"Iterate over the keys in the cache dict, oldest items\n        coming first.\n        \"\"\"\n        return iter(tuple(self._queue))\n\n    __copy__ = copy\n\n\ndef select_autoescape(\n    enabled_extensions: t.Collection[str] = (\"html\", \"htm\", \"xml\"),\n    disabled_extensions: t.Collection[str] = (),\n    default_for_string: bool = True,\n    default: bool = False,\n) -> t.Callable[[str | None], bool]:\n    \"\"\"Intelligently sets the initial value of autoescaping based on the\n    filename of the template.  This is the recommended way to configure\n    autoescaping if you do not want to write a custom function yourself.\n\n    If you want to enable it for all templates created from strings or\n    for all templates with `.html` and `.xml` extensions::\n\n        from jinja2 import Environment, select_autoescape\n        env = Environment(autoescape=select_autoescape(\n            enabled_extensions=('html', 'xml'),\n            default_for_string=True,\n        ))\n\n    Example configuration to turn it on at all times except if the template\n    ends with `.txt`::\n\n        from jinja2 import Environment, select_autoescape\n        env = Environment(autoescape=select_autoescape(\n            disabled_extensions=('txt',),\n            default_for_string=True,\n            default=True,\n        ))\n\n    The `enabled_extensions` is an iterable of all the extensions that\n    autoescaping should be enabled for.  Likewise `disabled_extensions` is\n    a list of all templates it should be disabled for.  If a template is\n    loaded from a string then the default from `default_for_string` is used.\n    If nothing matches then the initial value of autoescaping is set to the\n    value of `default`.\n\n    For security reasons this function operates case insensitive.\n\n    .. versionadded:: 2.9\n    \"\"\"\n    enabled_patterns = tuple(f\".{x.lstrip('.').lower()}\" for x in enabled_extensions)\n    disabled_patterns = tuple(f\".{x.lstrip('.').lower()}\" for x in disabled_extensions)\n\n    def autoescape(template_name: str | None) -> bool:\n        if template_name is None:\n            return default_for_string\n        template_name = template_name.lower()\n        if template_name.endswith(enabled_patterns):\n            return True\n        if template_name.endswith(disabled_patterns):\n            return False\n        return default\n\n    return autoescape\n\n\ndef htmlsafe_json_dumps(\n    obj: t.Any, dumps: t.Callable[..., str] | None = None, **kwargs: t.Any\n) -> markupsafe.Markup:\n    \"\"\"Serialize an object to a string of JSON with :func:`json.dumps`,\n    then replace HTML-unsafe characters with Unicode escapes and mark\n    the result safe with :class:`~markupsafe.Markup`.\n\n    This is available in templates as the ``|tojson`` filter.\n\n    The following characters are escaped: ``<``, ``>``, ``&``, ``'``.\n\n    The returned string is safe to render in HTML documents and\n    ``<script>`` tags. The exception is in HTML attributes that are\n    double quoted; either use single quotes or the ``|forceescape``\n    filter.\n\n    :param obj: The object to serialize to JSON.\n    :param dumps: The ``dumps`` function to use. Defaults to\n        ``env.policies[\"json.dumps_function\"]``, which defaults to\n        :func:`json.dumps`.\n    :param kwargs: Extra arguments to pass to ``dumps``. Merged onto\n        ``env.policies[\"json.dumps_kwargs\"]``.\n\n    .. versionchanged:: 3.0\n        The ``dumper`` parameter is renamed to ``dumps``.\n\n    .. versionadded:: 2.9\n    \"\"\"\n    if dumps is None:\n        dumps = json.dumps\n\n    return markupsafe.Markup(\n        dumps(obj, **kwargs)\n        .replace(\"<\", \"\\\\u003c\")\n        .replace(\">\", \"\\\\u003e\")\n        .replace(\"&\", \"\\\\u0026\")\n        .replace(\"'\", \"\\\\u0027\")\n    )\n\n\nclass Cycler:\n    \"\"\"Cycle through values by yield them one at a time, then restarting\n    once the end is reached. Available as ``cycler`` in templates.\n\n    Similar to ``loop.cycle``, but can be used outside loops or across\n    multiple loops. For example, render a list of folders and files in a\n    list, alternating giving them \"odd\" and \"even\" classes.\n\n    .. code-block:: html+jinja\n\n        {% set row_class = cycler(\"odd\", \"even\") %}\n        <ul class=\"browser\">\n        {% for folder in folders %}\n          <li class=\"folder {{ row_class.next() }}\">{{ folder }}\n        {% endfor %}\n        {% for file in files %}\n          <li class=\"file {{ row_class.next() }}\">{{ file }}\n        {% endfor %}\n        </ul>\n\n    :param items: Each positional argument will be yielded in the order\n        given for each cycle.\n\n    .. versionadded:: 2.1\n    \"\"\"\n\n    def __init__(self, *items: t.Any) -> None:\n        if not items:\n            raise RuntimeError(\"at least one item has to be provided\")\n        self.items = items\n        self.pos = 0\n\n    def reset(self) -> None:\n        \"\"\"Resets the current item to the first item.\"\"\"\n        self.pos = 0\n\n    @property\n    def current(self) -> t.Any:\n        \"\"\"Return the current item. Equivalent to the item that will be\n        returned next time :meth:`next` is called.\n        \"\"\"\n        return self.items[self.pos]\n\n    def next(self) -> t.Any:\n        \"\"\"Return the current item, then advance :attr:`current` to the\n        next item.\n        \"\"\"\n        rv = self.current\n        self.pos = (self.pos + 1) % len(self.items)\n        return rv\n\n    __next__ = next\n\n\nclass Joiner:\n    \"\"\"A joining helper for templates.\"\"\"\n\n    def __init__(self, sep: str = \", \") -> None:\n        self.sep = sep\n        self.used = False\n\n    def __call__(self) -> str:\n        if not self.used:\n            self.used = True\n            return \"\"\n        return self.sep\n\n\nclass Namespace:\n    \"\"\"A namespace object that can hold arbitrary attributes.  It may be\n    initialized from a dictionary or with keyword arguments.\"\"\"\n\n    def __init__(*args: t.Any, **kwargs: t.Any) -> None:  # noqa: B902\n        self, args = args[0], args[1:]\n        self.__attrs = dict(*args, **kwargs)\n\n    def __getattribute__(self, name: str) -> t.Any:\n        # __class__ is needed for the awaitable check in async mode\n        if name in {\"_Namespace__attrs\", \"__class__\"}:\n            return object.__getattribute__(self, name)\n        try:\n            return self.__attrs[name]\n        except KeyError:\n            raise AttributeError(name) from None\n\n    def __setitem__(self, name: str, value: t.Any) -> None:\n        self.__attrs[name] = value\n\n    def __repr__(self) -> str:\n        return f\"<Namespace {self.__attrs!r}>\"\n"
  },
  {
    "path": "src/jinja2/visitor.py",
    "content": "\"\"\"API for traversing the AST nodes. Implemented by the compiler and\nmeta introspection.\n\"\"\"\n\nimport typing as t\n\nfrom .nodes import Node\n\nif t.TYPE_CHECKING:\n    import typing_extensions as te\n\n    class VisitCallable(te.Protocol):\n        def __call__(self, node: Node, *args: t.Any, **kwargs: t.Any) -> t.Any: ...\n\n\nclass NodeVisitor:\n    \"\"\"Walks the abstract syntax tree and call visitor functions for every\n    node found.  The visitor functions may return values which will be\n    forwarded by the `visit` method.\n\n    Per default the visitor functions for the nodes are ``'visit_'`` +\n    class name of the node.  So a `TryFinally` node visit function would\n    be `visit_TryFinally`.  This behavior can be changed by overriding\n    the `get_visitor` function.  If no visitor function exists for a node\n    (return value `None`) the `generic_visit` visitor is used instead.\n    \"\"\"\n\n    def get_visitor(self, node: Node) -> \"VisitCallable | None\":\n        \"\"\"Return the visitor function for this node or `None` if no visitor\n        exists for this node.  In that case the generic visit function is\n        used instead.\n        \"\"\"\n        return getattr(self, f\"visit_{type(node).__name__}\", None)\n\n    def visit(self, node: Node, *args: t.Any, **kwargs: t.Any) -> t.Any:\n        \"\"\"Visit a node.\"\"\"\n        f = self.get_visitor(node)\n\n        if f is not None:\n            return f(node, *args, **kwargs)\n\n        return self.generic_visit(node, *args, **kwargs)\n\n    def generic_visit(self, node: Node, *args: t.Any, **kwargs: t.Any) -> t.Any:\n        \"\"\"Called if no explicit visitor function exists for a node.\"\"\"\n        for child_node in node.iter_child_nodes():\n            self.visit(child_node, *args, **kwargs)\n\n\nclass NodeTransformer(NodeVisitor):\n    \"\"\"Walks the abstract syntax tree and allows modifications of nodes.\n\n    The `NodeTransformer` will walk the AST and use the return value of the\n    visitor functions to replace or remove the old node.  If the return\n    value of the visitor function is `None` the node will be removed\n    from the previous location otherwise it's replaced with the return\n    value.  The return value may be the original node in which case no\n    replacement takes place.\n    \"\"\"\n\n    def generic_visit(self, node: Node, *args: t.Any, **kwargs: t.Any) -> Node:\n        for field, old_value in node.iter_fields():\n            if isinstance(old_value, list):\n                new_values = []\n                for value in old_value:\n                    if isinstance(value, Node):\n                        value = self.visit(value, *args, **kwargs)\n                        if value is None:\n                            continue\n                        elif not isinstance(value, Node):\n                            new_values.extend(value)\n                            continue\n                    new_values.append(value)\n                old_value[:] = new_values\n            elif isinstance(old_value, Node):\n                new_node = self.visit(old_value, *args, **kwargs)\n                if new_node is None:\n                    delattr(node, field)\n                else:\n                    setattr(node, field, new_node)\n        return node\n\n    def visit_list(self, node: Node, *args: t.Any, **kwargs: t.Any) -> list[Node]:\n        \"\"\"As transformers may return lists in some places this method\n        can be used to enforce a list as return value.\n        \"\"\"\n        rv = self.visit(node, *args, **kwargs)\n\n        if not isinstance(rv, list):\n            return [rv]\n\n        return rv\n"
  },
  {
    "path": "tests/conftest.py",
    "content": "import asyncio\nfrom pathlib import Path\n\nimport pytest\nimport trio\n\nfrom jinja2 import loaders\nfrom jinja2.environment import Environment\n\n\ndef _asyncio_run(async_fn, *args):\n    return asyncio.run(async_fn(*args))\n\n\n@pytest.fixture(params=[_asyncio_run, trio.run], ids=[\"asyncio\", \"trio\"])\ndef run_async_fn(request):\n    return request.param\n\n\n@pytest.fixture\ndef env():\n    \"\"\"returns a new environment.\"\"\"\n    return Environment()\n\n\n@pytest.fixture\ndef dict_loader():\n    \"\"\"returns DictLoader\"\"\"\n    return loaders.DictLoader({\"justdict.html\": \"FOO\"})\n\n\n@pytest.fixture\ndef package_loader():\n    \"\"\"returns PackageLoader initialized from templates\"\"\"\n    return loaders.PackageLoader(\"res\", \"templates\")\n\n\n@pytest.fixture\ndef filesystem_loader():\n    \"\"\"returns FileSystemLoader initialized to res/templates directory\"\"\"\n    here = Path(__file__).parent.resolve()\n    return loaders.FileSystemLoader(here / \"res\" / \"templates\")\n\n\n@pytest.fixture\ndef function_loader():\n    \"\"\"returns a FunctionLoader\"\"\"\n    return loaders.FunctionLoader({\"justfunction.html\": \"FOO\"}.get)\n\n\n@pytest.fixture\ndef choice_loader(dict_loader, package_loader):\n    \"\"\"returns a ChoiceLoader\"\"\"\n    return loaders.ChoiceLoader([dict_loader, package_loader])\n\n\n@pytest.fixture\ndef prefix_loader(filesystem_loader, dict_loader):\n    \"\"\"returns a PrefixLoader\"\"\"\n    return loaders.PrefixLoader({\"a\": filesystem_loader, \"b\": dict_loader})\n"
  },
  {
    "path": "tests/res/__init__.py",
    "content": ""
  },
  {
    "path": "tests/res/templates/broken.html",
    "content": "Before\n{{ fail() }}\nAfter\n"
  },
  {
    "path": "tests/res/templates/foo/test.html",
    "content": "FOO\n"
  },
  {
    "path": "tests/res/templates/mojibake.txt",
    "content": "文字化け\n"
  },
  {
    "path": "tests/res/templates/syntaxerror.html",
    "content": "Foo\n{% for item in broken %}\n  ...\n{% endif %}\n"
  },
  {
    "path": "tests/res/templates/test.html",
    "content": "BAR\n"
  },
  {
    "path": "tests/res/templates2/foo",
    "content": "Looks like the start of templates/foo/test.html\nTested by test_filesystem_loader_overlapping_names\n"
  },
  {
    "path": "tests/test_api.py",
    "content": "import shutil\nimport tempfile\nfrom pathlib import Path\n\nimport pytest\n\nfrom jinja2 import ChainableUndefined\nfrom jinja2 import DebugUndefined\nfrom jinja2 import DictLoader\nfrom jinja2 import Environment\nfrom jinja2 import is_undefined\nfrom jinja2 import make_logging_undefined\nfrom jinja2 import meta\nfrom jinja2 import StrictUndefined\nfrom jinja2 import Template\nfrom jinja2 import TemplatesNotFound\nfrom jinja2 import Undefined\nfrom jinja2 import UndefinedError\nfrom jinja2.compiler import CodeGenerator\nfrom jinja2.runtime import Context\nfrom jinja2.utils import Cycler\nfrom jinja2.utils import pass_context\nfrom jinja2.utils import pass_environment\nfrom jinja2.utils import pass_eval_context\n\n\nclass TestExtendedAPI:\n    def test_item_and_attribute(self, env):\n        from jinja2.sandbox import SandboxedEnvironment\n\n        for env in Environment(), SandboxedEnvironment():\n            tmpl = env.from_string(\"{{ foo.items()|list }}\")\n            assert tmpl.render(foo={\"items\": 42}) == \"[('items', 42)]\"\n            tmpl = env.from_string('{{ foo|attr(\"items\")()|list }}')\n            assert tmpl.render(foo={\"items\": 42}) == \"[('items', 42)]\"\n            tmpl = env.from_string('{{ foo[\"items\"] }}')\n            assert tmpl.render(foo={\"items\": 42}) == \"42\"\n\n    def test_finalize(self):\n        e = Environment(finalize=lambda v: \"\" if v is None else v)\n        t = e.from_string(\"{% for item in seq %}|{{ item }}{% endfor %}\")\n        assert t.render(seq=(None, 1, \"foo\")) == \"||1|foo\"\n\n    def test_finalize_constant_expression(self):\n        e = Environment(finalize=lambda v: \"\" if v is None else v)\n        t = e.from_string(\"<{{ none }}>\")\n        assert t.render() == \"<>\"\n\n    def test_no_finalize_template_data(self):\n        e = Environment(finalize=lambda v: type(v).__name__)\n        t = e.from_string(\"<{{ value }}>\")\n        # If template data was finalized, it would print \"strintstr\".\n        assert t.render(value=123) == \"<int>\"\n\n    def test_context_finalize(self):\n        @pass_context\n        def finalize(context, value):\n            return value * context[\"scale\"]\n\n        e = Environment(finalize=finalize)\n        t = e.from_string(\"{{ value }}\")\n        assert t.render(value=5, scale=3) == \"15\"\n\n    def test_eval_finalize(self):\n        @pass_eval_context\n        def finalize(eval_ctx, value):\n            return str(eval_ctx.autoescape) + value\n\n        e = Environment(finalize=finalize, autoescape=True)\n        t = e.from_string(\"{{ value }}\")\n        assert t.render(value=\"<script>\") == \"True&lt;script&gt;\"\n\n    def test_env_autoescape(self):\n        @pass_environment\n        def finalize(env, value):\n            return \" \".join(\n                (env.variable_start_string, repr(value), env.variable_end_string)\n            )\n\n        e = Environment(finalize=finalize)\n        t = e.from_string(\"{{ value }}\")\n        assert t.render(value=\"hello\") == \"{{ 'hello' }}\"\n\n    def test_cycler(self, env):\n        items = 1, 2, 3\n        c = Cycler(*items)\n        for item in items + items:\n            assert c.current == item\n            assert next(c) == item\n        next(c)\n        assert c.current == 2\n        c.reset()\n        assert c.current == 1\n\n    def test_expressions(self, env):\n        expr = env.compile_expression(\"foo\")\n        assert expr() is None\n        assert expr(foo=42) == 42\n        expr2 = env.compile_expression(\"foo\", undefined_to_none=False)\n        assert is_undefined(expr2())\n\n        expr = env.compile_expression(\"42 + foo\")\n        assert expr(foo=42) == 84\n\n    def test_template_passthrough(self, env):\n        t = Template(\"Content\")\n        assert env.get_template(t) is t\n        assert env.select_template([t]) is t\n        assert env.get_or_select_template([t]) is t\n        assert env.get_or_select_template(t) is t\n\n    def test_get_template_undefined(self, env):\n        \"\"\"Passing Undefined to get/select_template raises an\n        UndefinedError or shows the undefined message in the list.\n        \"\"\"\n        env.loader = DictLoader({})\n        t = Undefined(name=\"no_name_1\")\n\n        with pytest.raises(UndefinedError):\n            env.get_template(t)\n\n        with pytest.raises(UndefinedError):\n            env.get_or_select_template(t)\n\n        with pytest.raises(UndefinedError):\n            env.select_template(t)\n\n        with pytest.raises(TemplatesNotFound) as exc_info:\n            env.select_template([t, \"no_name_2\"])\n\n        exc_message = str(exc_info.value)\n        assert \"'no_name_1' is undefined\" in exc_message\n        assert \"no_name_2\" in exc_message\n\n    def test_autoescape_autoselect(self, env):\n        def select_autoescape(name):\n            if name is None or \".\" not in name:\n                return False\n            return name.endswith(\".html\")\n\n        env = Environment(\n            autoescape=select_autoescape,\n            loader=DictLoader({\"test.txt\": \"{{ foo }}\", \"test.html\": \"{{ foo }}\"}),\n        )\n        t = env.get_template(\"test.txt\")\n        assert t.render(foo=\"<foo>\") == \"<foo>\"\n        t = env.get_template(\"test.html\")\n        assert t.render(foo=\"<foo>\") == \"&lt;foo&gt;\"\n        t = env.from_string(\"{{ foo }}\")\n        assert t.render(foo=\"<foo>\") == \"<foo>\"\n\n    def test_sandbox_max_range(self, env):\n        from jinja2.sandbox import MAX_RANGE\n        from jinja2.sandbox import SandboxedEnvironment\n\n        env = SandboxedEnvironment()\n        t = env.from_string(\"{% for item in range(total) %}{{ item }}{% endfor %}\")\n\n        with pytest.raises(OverflowError):\n            t.render(total=MAX_RANGE + 1)\n\n\nclass TestMeta:\n    def test_find_undeclared_variables(self, env):\n        ast = env.parse(\"{% set foo = 42 %}{{ bar + foo }}\")\n        x = meta.find_undeclared_variables(ast)\n        assert x == {\"bar\"}\n\n        ast = env.parse(\n            \"{% set foo = 42 %}{{ bar + foo }}\"\n            \"{% macro meh(x) %}{{ x }}{% endmacro %}\"\n            \"{% for item in seq %}{{ muh(item) + meh(seq) }}\"\n            \"{% endfor %}\"\n        )\n        x = meta.find_undeclared_variables(ast)\n        assert x == {\"bar\", \"seq\", \"muh\"}\n\n        ast = env.parse(\"{% for x in range(5) %}{{ x }}{% endfor %}{{ foo }}\")\n        x = meta.find_undeclared_variables(ast)\n        assert x == {\"foo\"}\n\n    def test_find_refererenced_templates(self, env):\n        ast = env.parse('{% extends \"layout.html\" %}{% include helper %}')\n        i = meta.find_referenced_templates(ast)\n        assert next(i) == \"layout.html\"\n        assert next(i) is None\n        assert list(i) == []\n\n        ast = env.parse(\n            '{% extends \"layout.html\" %}'\n            '{% from \"test.html\" import a, b as c %}'\n            '{% import \"meh.html\" as meh %}'\n            '{% include \"muh.html\" %}'\n        )\n        i = meta.find_referenced_templates(ast)\n        assert list(i) == [\"layout.html\", \"test.html\", \"meh.html\", \"muh.html\"]\n\n    def test_find_included_templates(self, env):\n        ast = env.parse('{% include [\"foo.html\", \"bar.html\"] %}')\n        i = meta.find_referenced_templates(ast)\n        assert list(i) == [\"foo.html\", \"bar.html\"]\n\n        ast = env.parse('{% include (\"foo.html\", \"bar.html\") %}')\n        i = meta.find_referenced_templates(ast)\n        assert list(i) == [\"foo.html\", \"bar.html\"]\n\n        ast = env.parse('{% include [\"foo.html\", \"bar.html\", foo] %}')\n        i = meta.find_referenced_templates(ast)\n        assert list(i) == [\"foo.html\", \"bar.html\", None]\n\n        ast = env.parse('{% include (\"foo.html\", \"bar.html\", foo) %}')\n        i = meta.find_referenced_templates(ast)\n        assert list(i) == [\"foo.html\", \"bar.html\", None]\n\n\nclass TestStreaming:\n    def test_basic_streaming(self, env):\n        t = env.from_string(\n            \"<ul>{% for item in seq %}<li>{{ loop.index }} - {{ item }}</li>\"\n            \"{%- endfor %}</ul>\"\n        )\n        stream = t.stream(seq=list(range(3)))\n        assert next(stream) == \"<ul>\"\n        assert \"\".join(stream) == \"<li>1 - 0</li><li>2 - 1</li><li>3 - 2</li></ul>\"\n\n    def test_buffered_streaming(self, env):\n        tmpl = env.from_string(\n            \"<ul>{% for item in seq %}<li>{{ loop.index }} - {{ item }}</li>\"\n            \"{%- endfor %}</ul>\"\n        )\n        stream = tmpl.stream(seq=list(range(3)))\n        stream.enable_buffering(size=3)\n        assert next(stream) == \"<ul><li>1\"\n        assert next(stream) == \" - 0</li>\"\n\n    def test_streaming_behavior(self, env):\n        tmpl = env.from_string(\"\")\n        stream = tmpl.stream()\n        assert not stream.buffered\n        stream.enable_buffering(20)\n        assert stream.buffered\n        stream.disable_buffering()\n        assert not stream.buffered\n\n    def test_dump_stream(self, env):\n        tmp = Path(tempfile.mkdtemp())\n        try:\n            tmpl = env.from_string(\"\\u2713\")\n            stream = tmpl.stream()\n            stream.dump(str(tmp / \"dump.txt\"), \"utf-8\")\n            assert (tmp / \"dump.txt\").read_bytes() == b\"\\xe2\\x9c\\x93\"\n        finally:\n            shutil.rmtree(tmp)\n\n\nclass TestUndefined:\n    def test_stopiteration_is_undefined(self):\n        def test():\n            raise StopIteration()\n\n        t = Template(\"A{{ test() }}B\")\n        assert t.render(test=test) == \"AB\"\n        t = Template(\"A{{ test().missingattribute }}B\")\n        pytest.raises(UndefinedError, t.render, test=test)\n\n    def test_undefined_and_special_attributes(self):\n        with pytest.raises(AttributeError):\n            Undefined(\"Foo\").__dict__  # noqa B018\n\n    def test_undefined_attribute_error(self):\n        # Django's LazyObject turns the __class__ attribute into a\n        # property that resolves the wrapped function. If that wrapped\n        # function raises an AttributeError, printing the repr of the\n        # object in the undefined message would cause a RecursionError.\n        class Error:\n            @property  # type: ignore\n            def __class__(self):\n                raise AttributeError()\n\n        u = Undefined(obj=Error(), name=\"hello\")\n\n        with pytest.raises(UndefinedError):\n            getattr(u, \"recursion\", None)\n\n    def test_logging_undefined(self):\n        _messages = []\n\n        class DebugLogger:\n            def warning(self, msg, *args):\n                _messages.append(\"W:\" + msg % args)\n\n            def error(self, msg, *args):\n                _messages.append(\"E:\" + msg % args)\n\n        logging_undefined = make_logging_undefined(DebugLogger())\n        env = Environment(undefined=logging_undefined)\n        assert env.from_string(\"{{ missing }}\").render() == \"\"\n        pytest.raises(UndefinedError, env.from_string(\"{{ missing.attribute }}\").render)\n        assert env.from_string(\"{{ missing|list }}\").render() == \"[]\"\n        assert env.from_string(\"{{ missing is not defined }}\").render() == \"True\"\n        assert env.from_string(\"{{ foo.missing }}\").render(foo=42) == \"\"\n        assert env.from_string(\"{{ not missing }}\").render() == \"True\"\n        assert _messages == [\n            \"W:Template variable warning: 'missing' is undefined\",\n            \"E:Template variable error: 'missing' is undefined\",\n            \"W:Template variable warning: 'missing' is undefined\",\n            \"W:Template variable warning: 'int object' has no attribute 'missing'\",\n            \"W:Template variable warning: 'missing' is undefined\",\n        ]\n\n    def test_default_undefined(self):\n        env = Environment(undefined=Undefined)\n        assert env.from_string(\"{{ missing }}\").render() == \"\"\n        pytest.raises(UndefinedError, env.from_string(\"{{ missing.attribute }}\").render)\n        assert env.from_string(\"{{ missing|list }}\").render() == \"[]\"\n        assert env.from_string(\"{{ missing is not defined }}\").render() == \"True\"\n        assert env.from_string(\"{{ foo.missing }}\").render(foo=42) == \"\"\n        assert env.from_string(\"{{ not missing }}\").render() == \"True\"\n        pytest.raises(UndefinedError, env.from_string(\"{{ missing - 1}}\").render)\n        assert env.from_string(\"{{ 'foo' in missing }}\").render() == \"False\"\n        und1 = Undefined(name=\"x\")\n        und2 = Undefined(name=\"y\")\n        assert und1 == und2\n        assert und1 != 42\n        assert hash(und1) == hash(und2) == hash(Undefined())\n\n    def test_chainable_undefined(self):\n        env = Environment(undefined=ChainableUndefined)\n        # The following tests are copied from test_default_undefined\n        assert env.from_string(\"{{ missing }}\").render() == \"\"\n        assert env.from_string(\"{{ missing|list }}\").render() == \"[]\"\n        assert env.from_string(\"{{ missing is not defined }}\").render() == \"True\"\n        assert env.from_string(\"{{ foo.missing }}\").render(foo=42) == \"\"\n        assert env.from_string(\"{{ not missing }}\").render() == \"True\"\n        pytest.raises(UndefinedError, env.from_string(\"{{ missing - 1}}\").render)\n\n        # The following tests ensure subclass functionality works as expected\n        assert env.from_string('{{ missing.bar[\"baz\"] }}').render() == \"\"\n        assert env.from_string('{{ foo.bar[\"baz\"]._undefined_name }}').render() == \"foo\"\n        assert (\n            env.from_string('{{ foo.bar[\"baz\"]._undefined_name }}').render(foo=42)\n            == \"bar\"\n        )\n        assert (\n            env.from_string('{{ foo.bar[\"baz\"]._undefined_name }}').render(\n                foo={\"bar\": 42}\n            )\n            == \"baz\"\n        )\n\n    def test_debug_undefined(self):\n        env = Environment(undefined=DebugUndefined)\n        assert env.from_string(\"{{ missing }}\").render() == \"{{ missing }}\"\n        pytest.raises(UndefinedError, env.from_string(\"{{ missing.attribute }}\").render)\n        assert env.from_string(\"{{ missing|list }}\").render() == \"[]\"\n        assert env.from_string(\"{{ missing is not defined }}\").render() == \"True\"\n        assert (\n            env.from_string(\"{{ foo.missing }}\").render(foo=42)\n            == \"{{ no such element: int object['missing'] }}\"\n        )\n        assert env.from_string(\"{{ not missing }}\").render() == \"True\"\n        undefined_hint = \"this is testing undefined hint of DebugUndefined\"\n        assert (\n            str(DebugUndefined(hint=undefined_hint))\n            == f\"{{{{ undefined value printed: {undefined_hint} }}}}\"\n        )\n\n    def test_strict_undefined(self):\n        env = Environment(undefined=StrictUndefined)\n        pytest.raises(UndefinedError, env.from_string(\"{{ missing }}\").render)\n        pytest.raises(UndefinedError, env.from_string(\"{{ missing.attribute }}\").render)\n        pytest.raises(UndefinedError, env.from_string(\"{{ missing|list }}\").render)\n        pytest.raises(UndefinedError, env.from_string(\"{{ 'foo' in missing }}\").render)\n        assert env.from_string(\"{{ missing is not defined }}\").render() == \"True\"\n        pytest.raises(\n            UndefinedError, env.from_string(\"{{ foo.missing }}\").render, foo=42\n        )\n        pytest.raises(UndefinedError, env.from_string(\"{{ not missing }}\").render)\n        assert (\n            env.from_string('{{ missing|default(\"default\", true) }}').render()\n            == \"default\"\n        )\n        assert env.from_string('{{ \"foo\" if false }}').render() == \"\"\n\n    def test_indexing_gives_undefined(self):\n        t = Template(\"{{ var[42].foo }}\")\n        pytest.raises(UndefinedError, t.render, var=0)\n\n    def test_none_gives_proper_error(self):\n        with pytest.raises(UndefinedError, match=\"'None' has no attribute 'split'\"):\n            Environment().getattr(None, \"split\")()\n\n    def test_object_repr(self):\n        with pytest.raises(\n            UndefinedError, match=\"'int object' has no attribute 'upper'\"\n        ):\n            Undefined(obj=42, name=\"upper\")()\n\n\nclass TestLowLevel:\n    def test_custom_code_generator(self):\n        class CustomCodeGenerator(CodeGenerator):\n            def visit_Const(self, node, frame=None):\n                # This method is pure nonsense, but works fine for testing...\n                if node.value == \"foo\":\n                    self.write(repr(\"bar\"))\n                else:\n                    super().visit_Const(node, frame)\n\n        class CustomEnvironment(Environment):\n            code_generator_class = CustomCodeGenerator\n\n        env = CustomEnvironment()\n        tmpl = env.from_string('{% set foo = \"foo\" %}{{ foo }}')\n        assert tmpl.render() == \"bar\"\n\n    def test_custom_context(self):\n        class CustomContext(Context):\n            def resolve_or_missing(self, key):\n                return \"resolve-\" + key\n\n        class CustomEnvironment(Environment):\n            context_class = CustomContext\n\n        env = CustomEnvironment()\n        tmpl = env.from_string(\"{{ foo }}\")\n        assert tmpl.render() == \"resolve-foo\"\n\n\ndef test_overlay_enable_async(env):\n    assert not env.is_async\n    assert not env.overlay().is_async\n    env_async = env.overlay(enable_async=True)\n    assert env_async.is_async\n    assert not env_async.overlay(enable_async=False).is_async\n"
  },
  {
    "path": "tests/test_async.py",
    "content": "import pytest\n\nfrom jinja2 import ChainableUndefined\nfrom jinja2 import DictLoader\nfrom jinja2 import Environment\nfrom jinja2 import Template\nfrom jinja2.async_utils import auto_aiter\nfrom jinja2.exceptions import TemplateNotFound\nfrom jinja2.exceptions import TemplatesNotFound\nfrom jinja2.exceptions import UndefinedError\nfrom jinja2.nativetypes import NativeEnvironment\n\n\ndef test_basic_async(run_async_fn):\n    t = Template(\n        \"{% for item in [1, 2, 3] %}[{{ item }}]{% endfor %}\", enable_async=True\n    )\n\n    async def func():\n        return await t.render_async()\n\n    rv = run_async_fn(func)\n    assert rv == \"[1][2][3]\"\n\n\ndef test_await_on_calls(run_async_fn):\n    t = Template(\"{{ async_func() + normal_func() }}\", enable_async=True)\n\n    async def async_func():\n        return 42\n\n    def normal_func():\n        return 23\n\n    async def func():\n        return await t.render_async(async_func=async_func, normal_func=normal_func)\n\n    rv = run_async_fn(func)\n    assert rv == \"65\"\n\n\ndef test_await_on_calls_normal_render():\n    t = Template(\"{{ async_func() + normal_func() }}\", enable_async=True)\n\n    async def async_func():\n        return 42\n\n    def normal_func():\n        return 23\n\n    rv = t.render(async_func=async_func, normal_func=normal_func)\n    assert rv == \"65\"\n\n\ndef test_await_and_macros(run_async_fn):\n    t = Template(\n        \"{% macro foo(x) %}[{{ x }}][{{ async_func() }}]{% endmacro %}{{ foo(42) }}\",\n        enable_async=True,\n    )\n\n    async def async_func():\n        return 42\n\n    async def func():\n        return await t.render_async(async_func=async_func)\n\n    rv = run_async_fn(func)\n    assert rv == \"[42][42]\"\n\n\ndef test_async_blocks(run_async_fn):\n    t = Template(\n        \"{% block foo %}<Test>{% endblock %}{{ self.foo() }}\",\n        enable_async=True,\n        autoescape=True,\n    )\n\n    async def func():\n        return await t.render_async()\n\n    rv = run_async_fn(func)\n    assert rv == \"<Test><Test>\"\n\n\ndef test_async_generate():\n    t = Template(\"{% for x in [1, 2, 3] %}{{ x }}{% endfor %}\", enable_async=True)\n    rv = list(t.generate())\n    assert rv == [\"1\", \"2\", \"3\"]\n\n\ndef test_async_iteration_in_templates():\n    t = Template(\"{% for x in rng %}{{ x }}{% endfor %}\", enable_async=True)\n\n    async def async_iterator():\n        for item in [1, 2, 3]:\n            yield item\n\n    rv = list(t.generate(rng=async_iterator()))\n    assert rv == [\"1\", \"2\", \"3\"]\n\n\ndef test_async_iteration_in_templates_extended():\n    t = Template(\n        \"{% for x in rng %}{{ loop.index0 }}/{{ x }}{% endfor %}\", enable_async=True\n    )\n    stream = t.generate(rng=auto_aiter(range(1, 4)))\n    assert next(stream) == \"0\"\n    assert \"\".join(stream) == \"/11/22/3\"\n\n\n@pytest.fixture\ndef test_env_async():\n    env = Environment(\n        loader=DictLoader(\n            dict(\n                module=\"{% macro test() %}[{{ foo }}|{{ bar }}]{% endmacro %}\",\n                header=\"[{{ foo }}|{{ 23 }}]\",\n                o_printer=\"({{ o }})\",\n            )\n        ),\n        enable_async=True,\n    )\n    env.globals[\"bar\"] = 23\n    return env\n\n\nclass TestAsyncImports:\n    def test_context_imports(self, test_env_async):\n        t = test_env_async.from_string('{% import \"module\" as m %}{{ m.test() }}')\n        assert t.render(foo=42) == \"[|23]\"\n        t = test_env_async.from_string(\n            '{% import \"module\" as m without context %}{{ m.test() }}'\n        )\n        assert t.render(foo=42) == \"[|23]\"\n        t = test_env_async.from_string(\n            '{% import \"module\" as m with context %}{{ m.test() }}'\n        )\n        assert t.render(foo=42) == \"[42|23]\"\n        t = test_env_async.from_string('{% from \"module\" import test %}{{ test() }}')\n        assert t.render(foo=42) == \"[|23]\"\n        t = test_env_async.from_string(\n            '{% from \"module\" import test without context %}{{ test() }}'\n        )\n        assert t.render(foo=42) == \"[|23]\"\n        t = test_env_async.from_string(\n            '{% from \"module\" import test with context %}{{ test() }}'\n        )\n        assert t.render(foo=42) == \"[42|23]\"\n\n    def test_trailing_comma(self, test_env_async):\n        test_env_async.from_string('{% from \"foo\" import bar, baz with context %}')\n        test_env_async.from_string('{% from \"foo\" import bar, baz, with context %}')\n        test_env_async.from_string('{% from \"foo\" import bar, with context %}')\n        test_env_async.from_string('{% from \"foo\" import bar, with, context %}')\n        test_env_async.from_string('{% from \"foo\" import bar, with with context %}')\n\n    def test_exports(self, test_env_async, run_async_fn):\n        coro_fn = test_env_async.from_string(\n            \"\"\"\n            {% macro toplevel() %}...{% endmacro %}\n            {% macro __private() %}...{% endmacro %}\n            {% set variable = 42 %}\n            {% for item in [1] %}\n                {% macro notthere() %}{% endmacro %}\n            {% endfor %}\n            \"\"\"\n        )._get_default_module_async\n        m = run_async_fn(coro_fn)\n        assert run_async_fn(m.toplevel) == \"...\"\n        assert not hasattr(m, \"__missing\")\n        assert m.variable == 42\n        assert not hasattr(m, \"notthere\")\n\n    def test_import_with_globals(self, test_env_async):\n        t = test_env_async.from_string(\n            '{% import \"module\" as m %}{{ m.test() }}', globals={\"foo\": 42}\n        )\n        assert t.render() == \"[42|23]\"\n\n        t = test_env_async.from_string('{% import \"module\" as m %}{{ m.test() }}')\n        assert t.render() == \"[|23]\"\n\n    def test_import_with_globals_override(self, test_env_async):\n        t = test_env_async.from_string(\n            '{% set foo = 41 %}{% import \"module\" as m %}{{ m.test() }}',\n            globals={\"foo\": 42},\n        )\n        assert t.render() == \"[42|23]\"\n\n    def test_from_import_with_globals(self, test_env_async):\n        t = test_env_async.from_string(\n            '{% from \"module\" import test %}{{ test() }}',\n            globals={\"foo\": 42},\n        )\n        assert t.render() == \"[42|23]\"\n\n\nclass TestAsyncIncludes:\n    def test_context_include(self, test_env_async):\n        t = test_env_async.from_string('{% include \"header\" %}')\n        assert t.render(foo=42) == \"[42|23]\"\n        t = test_env_async.from_string('{% include \"header\" with context %}')\n        assert t.render(foo=42) == \"[42|23]\"\n        t = test_env_async.from_string('{% include \"header\" without context %}')\n        assert t.render(foo=42) == \"[|23]\"\n\n    def test_choice_includes(self, test_env_async):\n        t = test_env_async.from_string('{% include [\"missing\", \"header\"] %}')\n        assert t.render(foo=42) == \"[42|23]\"\n\n        t = test_env_async.from_string(\n            '{% include [\"missing\", \"missing2\"] ignore missing %}'\n        )\n        assert t.render(foo=42) == \"\"\n\n        t = test_env_async.from_string('{% include [\"missing\", \"missing2\"] %}')\n        pytest.raises(TemplateNotFound, t.render)\n        with pytest.raises(TemplatesNotFound) as e:\n            t.render()\n\n        assert e.value.templates == [\"missing\", \"missing2\"]\n        assert e.value.name == \"missing2\"\n\n        def test_includes(t, **ctx):\n            ctx[\"foo\"] = 42\n            assert t.render(ctx) == \"[42|23]\"\n\n        t = test_env_async.from_string('{% include [\"missing\", \"header\"] %}')\n        test_includes(t)\n        t = test_env_async.from_string(\"{% include x %}\")\n        test_includes(t, x=[\"missing\", \"header\"])\n        t = test_env_async.from_string('{% include [x, \"header\"] %}')\n        test_includes(t, x=\"missing\")\n        t = test_env_async.from_string(\"{% include x %}\")\n        test_includes(t, x=\"header\")\n        t = test_env_async.from_string(\"{% include x %}\")\n        test_includes(t, x=\"header\")\n        t = test_env_async.from_string(\"{% include [x] %}\")\n        test_includes(t, x=\"header\")\n\n    def test_include_ignoring_missing(self, test_env_async):\n        t = test_env_async.from_string('{% include \"missing\" %}')\n        pytest.raises(TemplateNotFound, t.render)\n        for extra in \"\", \"with context\", \"without context\":\n            t = test_env_async.from_string(\n                '{% include \"missing\" ignore missing ' + extra + \" %}\"\n            )\n            assert t.render() == \"\"\n\n    def test_context_include_with_overrides(self, test_env_async):\n        env = Environment(\n            loader=DictLoader(\n                dict(\n                    main=\"{% for item in [1, 2, 3] %}{% include 'item' %}{% endfor %}\",\n                    item=\"{{ item }}\",\n                )\n            )\n        )\n        assert env.get_template(\"main\").render() == \"123\"\n\n    def test_unoptimized_scopes(self, test_env_async):\n        t = test_env_async.from_string(\n            \"\"\"\n            {% macro outer(o) %}\n            {% macro inner() %}\n            {% include \"o_printer\" %}\n            {% endmacro %}\n            {{ inner() }}\n            {% endmacro %}\n            {{ outer(\"FOO\") }}\n        \"\"\"\n        )\n        assert t.render().strip() == \"(FOO)\"\n\n    def test_unoptimized_scopes_autoescape(self):\n        env = Environment(\n            loader=DictLoader({\"o_printer\": \"({{ o }})\"}),\n            autoescape=True,\n            enable_async=True,\n        )\n        t = env.from_string(\n            \"\"\"\n            {% macro outer(o) %}\n            {% macro inner() %}\n            {% include \"o_printer\" %}\n            {% endmacro %}\n            {{ inner() }}\n            {% endmacro %}\n            {{ outer(\"FOO\") }}\n        \"\"\"\n        )\n        assert t.render().strip() == \"(FOO)\"\n\n\nclass TestAsyncForLoop:\n    def test_simple(self, test_env_async):\n        tmpl = test_env_async.from_string(\"{% for item in seq %}{{ item }}{% endfor %}\")\n        assert tmpl.render(seq=list(range(10))) == \"0123456789\"\n\n    def test_else(self, test_env_async):\n        tmpl = test_env_async.from_string(\n            \"{% for item in seq %}XXX{% else %}...{% endfor %}\"\n        )\n        assert tmpl.render() == \"...\"\n\n    def test_empty_blocks(self, test_env_async):\n        tmpl = test_env_async.from_string(\n            \"<{% for item in seq %}{% else %}{% endfor %}>\"\n        )\n        assert tmpl.render() == \"<>\"\n\n    @pytest.mark.parametrize(\n        \"transform\", [lambda x: x, iter, reversed, lambda x: (i for i in x), auto_aiter]\n    )\n    def test_context_vars(self, test_env_async, transform):\n        t = test_env_async.from_string(\n            \"{% for item in seq %}{{ loop.index }}|{{ loop.index0 }}\"\n            \"|{{ loop.revindex }}|{{ loop.revindex0 }}|{{ loop.first }}\"\n            \"|{{ loop.last }}|{{ loop.length }}\\n{% endfor %}\"\n        )\n        out = t.render(seq=transform([42, 24]))\n        assert out == \"1|0|2|1|True|False|2\\n2|1|1|0|False|True|2\\n\"\n\n    def test_cycling(self, test_env_async):\n        tmpl = test_env_async.from_string(\n            \"\"\"{% for item in seq %}{{\n            loop.cycle('<1>', '<2>') }}{% endfor %}{%\n            for item in seq %}{{ loop.cycle(*through) }}{% endfor %}\"\"\"\n        )\n        output = tmpl.render(seq=list(range(4)), through=(\"<1>\", \"<2>\"))\n        assert output == \"<1><2>\" * 4\n\n    def test_lookaround(self, test_env_async):\n        tmpl = test_env_async.from_string(\n            \"\"\"{% for item in seq -%}\n            {{ loop.previtem|default('x') }}-{{ item }}-{{\n            loop.nextitem|default('x') }}|\n        {%- endfor %}\"\"\"\n        )\n        output = tmpl.render(seq=list(range(4)))\n        assert output == \"x-0-1|0-1-2|1-2-3|2-3-x|\"\n\n    def test_changed(self, test_env_async):\n        tmpl = test_env_async.from_string(\n            \"\"\"{% for item in seq -%}\n            {{ loop.changed(item) }},\n        {%- endfor %}\"\"\"\n        )\n        output = tmpl.render(seq=[None, None, 1, 2, 2, 3, 4, 4, 4])\n        assert output == \"True,False,True,True,False,True,True,False,False,\"\n\n    def test_scope(self, test_env_async):\n        tmpl = test_env_async.from_string(\"{% for item in seq %}{% endfor %}{{ item }}\")\n        output = tmpl.render(seq=list(range(10)))\n        assert not output\n\n    def test_varlen(self, test_env_async):\n        def inner():\n            yield from range(5)\n\n        tmpl = test_env_async.from_string(\n            \"{% for item in iter %}{{ item }}{% endfor %}\"\n        )\n        output = tmpl.render(iter=inner())\n        assert output == \"01234\"\n\n    def test_noniter(self, test_env_async):\n        tmpl = test_env_async.from_string(\"{% for item in none %}...{% endfor %}\")\n        pytest.raises(TypeError, tmpl.render)\n\n    def test_recursive(self, test_env_async):\n        tmpl = test_env_async.from_string(\n            \"\"\"{% for item in seq recursive -%}\n            [{{ item.a }}{% if item.b %}<{{ loop(item.b) }}>{% endif %}]\n        {%- endfor %}\"\"\"\n        )\n        assert (\n            tmpl.render(\n                seq=[\n                    dict(a=1, b=[dict(a=1), dict(a=2)]),\n                    dict(a=2, b=[dict(a=1), dict(a=2)]),\n                    dict(a=3, b=[dict(a=\"a\")]),\n                ]\n            )\n            == \"[1<[1][2]>][2<[1][2]>][3<[a]>]\"\n        )\n\n    def test_recursive_lookaround(self, test_env_async):\n        tmpl = test_env_async.from_string(\n            \"\"\"{% for item in seq recursive -%}\n            [{{ loop.previtem.a if loop.previtem is defined else 'x' }}.{{\n            item.a }}.{{ loop.nextitem.a if loop.nextitem is defined else 'x'\n            }}{% if item.b %}<{{ loop(item.b) }}>{% endif %}]\n        {%- endfor %}\"\"\"\n        )\n        assert (\n            tmpl.render(\n                seq=[\n                    dict(a=1, b=[dict(a=1), dict(a=2)]),\n                    dict(a=2, b=[dict(a=1), dict(a=2)]),\n                    dict(a=3, b=[dict(a=\"a\")]),\n                ]\n            )\n            == \"[x.1.2<[x.1.2][1.2.x]>][1.2.3<[x.1.2][1.2.x]>][2.3.x<[x.a.x]>]\"\n        )\n\n    def test_recursive_depth0(self, test_env_async):\n        tmpl = test_env_async.from_string(\n            \"{% for item in seq recursive %}[{{ loop.depth0 }}:{{ item.a }}\"\n            \"{% if item.b %}<{{ loop(item.b) }}>{% endif %}]{% endfor %}\"\n        )\n        assert (\n            tmpl.render(\n                seq=[\n                    dict(a=1, b=[dict(a=1), dict(a=2)]),\n                    dict(a=2, b=[dict(a=1), dict(a=2)]),\n                    dict(a=3, b=[dict(a=\"a\")]),\n                ]\n            )\n            == \"[0:1<[1:1][1:2]>][0:2<[1:1][1:2]>][0:3<[1:a]>]\"\n        )\n\n    def test_recursive_depth(self, test_env_async):\n        tmpl = test_env_async.from_string(\n            \"{% for item in seq recursive %}[{{ loop.depth }}:{{ item.a }}\"\n            \"{% if item.b %}<{{ loop(item.b) }}>{% endif %}]{% endfor %}\"\n        )\n        assert (\n            tmpl.render(\n                seq=[\n                    dict(a=1, b=[dict(a=1), dict(a=2)]),\n                    dict(a=2, b=[dict(a=1), dict(a=2)]),\n                    dict(a=3, b=[dict(a=\"a\")]),\n                ]\n            )\n            == \"[1:1<[2:1][2:2]>][1:2<[2:1][2:2]>][1:3<[2:a]>]\"\n        )\n\n    def test_looploop(self, test_env_async):\n        tmpl = test_env_async.from_string(\n            \"\"\"{% for row in table %}\n            {%- set rowloop = loop -%}\n            {% for cell in row -%}\n                [{{ rowloop.index }}|{{ loop.index }}]\n            {%- endfor %}\n        {%- endfor %}\"\"\"\n        )\n        assert tmpl.render(table=[\"ab\", \"cd\"]) == \"[1|1][1|2][2|1][2|2]\"\n\n    def test_reversed_bug(self, test_env_async):\n        tmpl = test_env_async.from_string(\n            \"{% for i in items %}{{ i }}{% if not loop.last %},{% endif %}{% endfor %}\"\n        )\n        assert tmpl.render(items=reversed([3, 2, 1])) == \"1,2,3\"\n\n    def test_loop_errors(self, test_env_async, run_async_fn):\n        tmpl = test_env_async.from_string(\n            \"\"\"{% for item in [1] if loop.index\n                                      == 0 %}...{% endfor %}\"\"\"\n        )\n        with pytest.raises(UndefinedError):\n            run_async_fn(tmpl.render_async)\n\n        tmpl = test_env_async.from_string(\n            \"\"\"{% for item in [] %}...{% else\n            %}{{ loop }}{% endfor %}\"\"\"\n        )\n        assert run_async_fn(tmpl.render_async) == \"\"\n\n    def test_loop_filter(self, test_env_async):\n        tmpl = test_env_async.from_string(\n            \"{% for item in range(10) if item is even %}[{{ item }}]{% endfor %}\"\n        )\n        assert tmpl.render() == \"[0][2][4][6][8]\"\n        tmpl = test_env_async.from_string(\n            \"\"\"\n            {%- for item in range(10) if item is even %}[{{\n                loop.index }}:{{ item }}]{% endfor %}\"\"\"\n        )\n        assert tmpl.render() == \"[1:0][2:2][3:4][4:6][5:8]\"\n\n    def test_scoped_special_var(self, test_env_async):\n        t = test_env_async.from_string(\n            \"{% for s in seq %}[{{ loop.first }}{% for c in s %}\"\n            \"|{{ loop.first }}{% endfor %}]{% endfor %}\"\n        )\n        assert t.render(seq=(\"ab\", \"cd\")) == \"[True|True|False][False|True|False]\"\n\n    def test_scoped_loop_var(self, test_env_async):\n        t = test_env_async.from_string(\n            \"{% for x in seq %}{{ loop.first }}\"\n            \"{% for y in seq %}{% endfor %}{% endfor %}\"\n        )\n        assert t.render(seq=\"ab\") == \"TrueFalse\"\n        t = test_env_async.from_string(\n            \"{% for x in seq %}{% for y in seq %}\"\n            \"{{ loop.first }}{% endfor %}{% endfor %}\"\n        )\n        assert t.render(seq=\"ab\") == \"TrueFalseTrueFalse\"\n\n    def test_recursive_empty_loop_iter(self, test_env_async):\n        t = test_env_async.from_string(\n            \"\"\"\n        {%- for item in foo recursive -%}{%- endfor -%}\n        \"\"\"\n        )\n        assert t.render(dict(foo=[])) == \"\"\n\n    def test_call_in_loop(self, test_env_async):\n        t = test_env_async.from_string(\n            \"\"\"\n        {%- macro do_something() -%}\n            [{{ caller() }}]\n        {%- endmacro %}\n\n        {%- for i in [1, 2, 3] %}\n            {%- call do_something() -%}\n                {{ i }}\n            {%- endcall %}\n        {%- endfor -%}\n        \"\"\"\n        )\n        assert t.render() == \"[1][2][3]\"\n\n    def test_scoping_bug(self, test_env_async):\n        t = test_env_async.from_string(\n            \"\"\"\n        {%- for item in foo %}...{{ item }}...{% endfor %}\n        {%- macro item(a) %}...{{ a }}...{% endmacro %}\n        {{- item(2) -}}\n        \"\"\"\n        )\n        assert t.render(foo=(1,)) == \"...1......2...\"\n\n    def test_unpacking(self, test_env_async):\n        tmpl = test_env_async.from_string(\n            \"{% for a, b, c in [[1, 2, 3]] %}{{ a }}|{{ b }}|{{ c }}{% endfor %}\"\n        )\n        assert tmpl.render() == \"1|2|3\"\n\n    def test_recursive_loop_filter(self, test_env_async):\n        t = test_env_async.from_string(\n            \"\"\"\n        <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n        <urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n          {%- for page in [site.root] if page.url != this recursive %}\n          <url><loc>{{ page.url }}</loc></url>\n          {{- loop(page.children) }}\n          {%- endfor %}\n        </urlset>\n        \"\"\"\n        )\n        sm = t.render(\n            this=\"/foo\",\n            site={\"root\": {\"url\": \"/\", \"children\": [{\"url\": \"/foo\"}, {\"url\": \"/bar\"}]}},\n        )\n        lines = [x.strip() for x in sm.splitlines() if x.strip()]\n        assert lines == [\n            '<?xml version=\"1.0\" encoding=\"UTF-8\"?>',\n            '<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">',\n            \"<url><loc>/</loc></url>\",\n            \"<url><loc>/bar</loc></url>\",\n            \"</urlset>\",\n        ]\n\n    def test_nonrecursive_loop_filter(self, test_env_async):\n        t = test_env_async.from_string(\n            \"\"\"\n        <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n        <urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n          {%- for page in items if page.url != this %}\n          <url><loc>{{ page.url }}</loc></url>\n          {%- endfor %}\n        </urlset>\n        \"\"\"\n        )\n        sm = t.render(\n            this=\"/foo\", items=[{\"url\": \"/\"}, {\"url\": \"/foo\"}, {\"url\": \"/bar\"}]\n        )\n        lines = [x.strip() for x in sm.splitlines() if x.strip()]\n        assert lines == [\n            '<?xml version=\"1.0\" encoding=\"UTF-8\"?>',\n            '<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">',\n            \"<url><loc>/</loc></url>\",\n            \"<url><loc>/bar</loc></url>\",\n            \"</urlset>\",\n        ]\n\n    def test_bare_async(self, test_env_async):\n        t = test_env_async.from_string('{% extends \"header\" %}')\n        assert t.render(foo=42) == \"[42|23]\"\n\n    def test_awaitable_property_slicing(self, test_env_async):\n        t = test_env_async.from_string(\"{% for x in a.b[:1] %}{{ x }}{% endfor %}\")\n        assert t.render(a=dict(b=[1, 2, 3])) == \"1\"\n\n\ndef test_namespace_awaitable(test_env_async, run_async_fn):\n    async def _test():\n        t = test_env_async.from_string(\n            '{% set ns = namespace(foo=\"Bar\") %}{{ ns.foo }}'\n        )\n        actual = await t.render_async()\n        assert actual == \"Bar\"\n\n    run_async_fn(_test)\n\n\ndef test_chainable_undefined_aiter(run_async_fn):\n    async def _test():\n        t = Template(\n            \"{% for x in a['b']['c'] %}{{ x }}{% endfor %}\",\n            enable_async=True,\n            undefined=ChainableUndefined,\n        )\n        rv = await t.render_async(a={})\n        assert rv == \"\"\n\n    run_async_fn(_test)\n\n\n@pytest.fixture\ndef async_native_env():\n    return NativeEnvironment(enable_async=True)\n\n\ndef test_native_async(async_native_env, run_async_fn):\n    async def _test():\n        t = async_native_env.from_string(\"{{ x }}\")\n        rv = await t.render_async(x=23)\n        assert rv == 23\n\n    run_async_fn(_test)\n\n\ndef test_native_list_async(async_native_env, run_async_fn):\n    async def _test():\n        t = async_native_env.from_string(\"{{ x }}\")\n        rv = await t.render_async(x=list(range(3)))\n        assert rv == [0, 1, 2]\n\n    run_async_fn(_test)\n\n\ndef test_getitem_after_filter():\n    env = Environment(enable_async=True)\n    env.filters[\"add_each\"] = lambda v, x: [i + x for i in v]\n    t = env.from_string(\"{{ (a|add_each(2))[1:] }}\")\n    out = t.render(a=range(3))\n    assert out == \"[3, 4]\"\n\n\ndef test_getitem_after_call():\n    env = Environment(enable_async=True)\n    env.globals[\"add_each\"] = lambda v, x: [i + x for i in v]\n    t = env.from_string(\"{{ add_each(a, 2)[1:] }}\")\n    out = t.render(a=range(3))\n    assert out == \"[3, 4]\"\n\n\ndef test_basic_generate_async(run_async_fn):\n    t = Template(\n        \"{% for item in [1, 2, 3] %}[{{ item }}]{% endfor %}\", enable_async=True\n    )\n\n    async def func():\n        agen = t.generate_async()\n        try:\n            return await agen.__anext__()\n        finally:\n            await agen.aclose()\n\n    rv = run_async_fn(func)\n    assert rv == \"[\"\n\n\ndef test_include_generate_async(run_async_fn, test_env_async):\n    t = test_env_async.from_string('{% include \"header\" %}')\n\n    async def func():\n        agen = t.generate_async()\n        try:\n            return await agen.__anext__()\n        finally:\n            await agen.aclose()\n\n    rv = run_async_fn(func)\n    assert rv == \"[\"\n\n\ndef test_blocks_generate_async(run_async_fn):\n    t = Template(\n        \"{% block foo %}<Test>{% endblock %}{{ self.foo() }}\",\n        enable_async=True,\n        autoescape=True,\n    )\n\n    async def func():\n        agen = t.generate_async()\n        try:\n            return await agen.__anext__()\n        finally:\n            await agen.aclose()\n\n    rv = run_async_fn(func)\n    assert rv == \"<Test>\"\n\n\ndef test_async_extend(run_async_fn, test_env_async):\n    t = test_env_async.from_string('{% extends \"header\" %}')\n\n    async def func():\n        agen = t.generate_async()\n        try:\n            return await agen.__anext__()\n        finally:\n            await agen.aclose()\n\n    rv = run_async_fn(func)\n    assert rv == \"[\"\n"
  },
  {
    "path": "tests/test_async_filters.py",
    "content": "import contextlib\nfrom collections import namedtuple\n\nimport pytest\nfrom markupsafe import Markup\n\nfrom jinja2 import Environment\nfrom jinja2.async_utils import auto_aiter\n\n\nasync def make_aiter(iter):\n    for item in iter:\n        yield item\n\n\ndef mark_dualiter(parameter, factory):\n    def decorator(f):\n        return pytest.mark.parametrize(\n            parameter, [lambda: factory(), lambda: make_aiter(factory())]\n        )(f)\n\n    return decorator\n\n\n@pytest.fixture\ndef env_async():\n    return Environment(enable_async=True)\n\n\n@contextlib.asynccontextmanager\nasync def closing_factory():\n    async with contextlib.AsyncExitStack() as stack:\n\n        def closing(maybe_agen):\n            try:\n                aclose = maybe_agen.aclose\n            except AttributeError:\n                pass\n            else:\n                stack.push_async_callback(aclose)\n            return maybe_agen\n\n        yield closing\n\n\n@mark_dualiter(\"foo\", lambda: range(10))\ndef test_first(env_async, foo, run_async_fn):\n    async def test():\n        async with closing_factory() as closing:\n            tmpl = env_async.from_string(\"{{ closing(foo())|first }}\")\n            return await tmpl.render_async(foo=foo, closing=closing)\n\n    out = run_async_fn(test)\n    assert out == \"0\"\n\n\n@mark_dualiter(\n    \"items\",\n    lambda: [\n        {\"foo\": 1, \"bar\": 2},\n        {\"foo\": 2, \"bar\": 3},\n        {\"foo\": 1, \"bar\": 1},\n        {\"foo\": 3, \"bar\": 4},\n    ],\n)\ndef test_groupby(env_async, items):\n    tmpl = env_async.from_string(\n        \"\"\"\n    {%- for grouper, list in items()|groupby('foo') -%}\n        {{ grouper }}{% for x in list %}: {{ x.foo }}, {{ x.bar }}{% endfor %}|\n    {%- endfor %}\"\"\"\n    )\n    assert tmpl.render(items=items).split(\"|\") == [\n        \"1: 1, 2: 1, 1\",\n        \"2: 2, 3\",\n        \"3: 3, 4\",\n        \"\",\n    ]\n\n\n@pytest.mark.parametrize(\n    (\"case_sensitive\", \"expect\"),\n    [\n        (False, \"a: 1, 3\\nb: 2\\n\"),\n        (True, \"A: 3\\na: 1\\nb: 2\\n\"),\n    ],\n)\ndef test_groupby_case(env_async, case_sensitive, expect):\n    tmpl = env_async.from_string(\n        \"{% for k, vs in data|groupby('k', case_sensitive=cs) %}\"\n        \"{{ k }}: {{ vs|join(', ', attribute='v') }}\\n\"\n        \"{% endfor %}\"\n    )\n    out = tmpl.render(\n        data=[{\"k\": \"a\", \"v\": 1}, {\"k\": \"b\", \"v\": 2}, {\"k\": \"A\", \"v\": 3}],\n        cs=case_sensitive,\n    )\n    assert out == expect\n\n\n@mark_dualiter(\"items\", lambda: [(\"a\", 1), (\"a\", 2), (\"b\", 1)])\ndef test_groupby_tuple_index(env_async, items):\n    tmpl = env_async.from_string(\n        \"\"\"\n    {%- for grouper, list in items()|groupby(0) -%}\n        {{ grouper }}{% for x in list %}:{{ x.1 }}{% endfor %}|\n    {%- endfor %}\"\"\"\n    )\n    assert tmpl.render(items=items) == \"a:1:2|b:1|\"\n\n\ndef make_articles():\n    Date = namedtuple(\"Date\", \"day,month,year\")\n    Article = namedtuple(\"Article\", \"title,date\")\n    return [\n        Article(\"aha\", Date(1, 1, 1970)),\n        Article(\"interesting\", Date(2, 1, 1970)),\n        Article(\"really?\", Date(3, 1, 1970)),\n        Article(\"totally not\", Date(1, 1, 1971)),\n    ]\n\n\n@mark_dualiter(\"articles\", make_articles)\ndef test_groupby_multidot(env_async, articles):\n    tmpl = env_async.from_string(\n        \"\"\"\n    {%- for year, list in articles()|groupby('date.year') -%}\n        {{ year }}{% for x in list %}[{{ x.title }}]{% endfor %}|\n    {%- endfor %}\"\"\"\n    )\n    assert tmpl.render(articles=articles).split(\"|\") == [\n        \"1970[aha][interesting][really?]\",\n        \"1971[totally not]\",\n        \"\",\n    ]\n\n\n@mark_dualiter(\"int_items\", lambda: [1, 2, 3])\ndef test_join_env_int(env_async, int_items):\n    tmpl = env_async.from_string('{{ items()|join(\"|\") }}')\n    out = tmpl.render(items=int_items)\n    assert out == \"1|2|3\"\n\n\n@mark_dualiter(\"string_items\", lambda: [\"<foo>\", Markup(\"<span>foo</span>\")])\ndef test_join_string_list(string_items):\n    env2 = Environment(autoescape=True, enable_async=True)\n    tmpl = env2.from_string('{{ [\"<foo>\", \"<span>foo</span>\"|safe]|join }}')\n    assert tmpl.render(items=string_items) == \"&lt;foo&gt;<span>foo</span>\"\n\n\ndef make_users():\n    User = namedtuple(\"User\", \"username\")\n    return map(User, [\"foo\", \"bar\"])\n\n\n@mark_dualiter(\"users\", make_users)\ndef test_join_attribute(env_async, users):\n    tmpl = env_async.from_string(\"\"\"{{ users()|join(', ', 'username') }}\"\"\")\n    assert tmpl.render(users=users) == \"foo, bar\"\n\n\n@mark_dualiter(\"items\", lambda: [1, 2, 3, 4, 5])\ndef test_simple_reject(env_async, items):\n    tmpl = env_async.from_string('{{ items()|reject(\"odd\")|join(\"|\") }}')\n    assert tmpl.render(items=items) == \"2|4\"\n\n\n@mark_dualiter(\"items\", lambda: [None, False, 0, 1, 2, 3, 4, 5])\ndef test_bool_reject(env_async, items):\n    tmpl = env_async.from_string('{{ items()|reject|join(\"|\") }}')\n    assert tmpl.render(items=items) == \"None|False|0\"\n\n\n@mark_dualiter(\"items\", lambda: [1, 2, 3, 4, 5])\ndef test_simple_select(env_async, items):\n    tmpl = env_async.from_string('{{ items()|select(\"odd\")|join(\"|\") }}')\n    assert tmpl.render(items=items) == \"1|3|5\"\n\n\n@mark_dualiter(\"items\", lambda: [None, False, 0, 1, 2, 3, 4, 5])\ndef test_bool_select(env_async, items):\n    tmpl = env_async.from_string('{{ items()|select|join(\"|\") }}')\n    assert tmpl.render(items=items) == \"1|2|3|4|5\"\n\n\ndef make_users():  # type: ignore\n    User = namedtuple(\"User\", \"name,is_active\")\n    return [\n        User(\"john\", True),\n        User(\"jane\", True),\n        User(\"mike\", False),\n    ]\n\n\n@mark_dualiter(\"users\", make_users)\ndef test_simple_select_attr(env_async, users):\n    tmpl = env_async.from_string(\n        '{{ users()|selectattr(\"is_active\")|map(attribute=\"name\")|join(\"|\") }}'\n    )\n    assert tmpl.render(users=users) == \"john|jane\"\n\n\n@mark_dualiter(\"items\", lambda: list(\"123\"))\ndef test_simple_map(env_async, items):\n    tmpl = env_async.from_string('{{ items()|map(\"int\")|sum }}')\n    assert tmpl.render(items=items) == \"6\"\n\n\ndef test_map_sum(env_async):  # async map + async filter\n    tmpl = env_async.from_string('{{ [[1,2], [3], [4,5,6]]|map(\"sum\")|list }}')\n    assert tmpl.render() == \"[3, 3, 15]\"\n\n\n@mark_dualiter(\"users\", make_users)\ndef test_attribute_map(env_async, users):\n    tmpl = env_async.from_string('{{ users()|map(attribute=\"name\")|join(\"|\") }}')\n    assert tmpl.render(users=users) == \"john|jane|mike\"\n\n\ndef test_empty_map(env_async):\n    tmpl = env_async.from_string('{{ none|map(\"upper\")|list }}')\n    assert tmpl.render() == \"[]\"\n\n\n@mark_dualiter(\"items\", lambda: [1, 2, 3, 4, 5, 6])\ndef test_sum(env_async, items):\n    tmpl = env_async.from_string(\"\"\"{{ items()|sum }}\"\"\")\n    assert tmpl.render(items=items) == \"21\"\n\n\n@mark_dualiter(\"items\", lambda: [{\"value\": 23}, {\"value\": 1}, {\"value\": 18}])\ndef test_sum_attributes(env_async, items):\n    tmpl = env_async.from_string(\"\"\"{{ items()|sum('value') }}\"\"\")\n    assert tmpl.render(items=items)\n\n\ndef test_sum_attributes_nested(env_async):\n    tmpl = env_async.from_string(\"\"\"{{ values|sum('real.value') }}\"\"\")\n    assert (\n        tmpl.render(\n            values=[\n                {\"real\": {\"value\": 23}},\n                {\"real\": {\"value\": 1}},\n                {\"real\": {\"value\": 18}},\n            ]\n        )\n        == \"42\"\n    )\n\n\ndef test_sum_attributes_tuple(env_async):\n    tmpl = env_async.from_string(\"\"\"{{ values.items()|sum('1') }}\"\"\")\n    assert tmpl.render(values={\"foo\": 23, \"bar\": 1, \"baz\": 18}) == \"42\"\n\n\n@mark_dualiter(\"items\", lambda: range(10))\ndef test_slice(env_async, items):\n    tmpl = env_async.from_string(\n        \"{{ items()|slice(3)|list }}|{{ items()|slice(3, 'X')|list }}\"\n    )\n    out = tmpl.render(items=items)\n    assert out == (\n        \"[[0, 1, 2, 3], [4, 5, 6], [7, 8, 9]]|\"\n        \"[[0, 1, 2, 3], [4, 5, 6, 'X'], [7, 8, 9, 'X']]\"\n    )\n\n\ndef test_unique_with_async_gen(env_async):\n    items = [\"a\", \"b\", \"c\", \"c\", \"a\", \"d\", \"z\"]\n    tmpl = env_async.from_string(\"{{ items|reject('==', 'z')|unique|list }}\")\n    out = tmpl.render(items=items)\n    assert out == \"['a', 'b', 'c', 'd']\"\n\n\ndef test_custom_async_filter(env_async, run_async_fn):\n    async def customfilter(val):\n        return str(val)\n\n    async def test():\n        env_async.filters[\"customfilter\"] = customfilter\n        tmpl = env_async.from_string(\n            \"{{ 'static'|customfilter }} {{ arg|customfilter }}\"\n        )\n        return await tmpl.render_async(arg=\"dynamic\")\n\n    out = run_async_fn(test)\n    assert out == \"static dynamic\"\n\n\n@mark_dualiter(\"items\", lambda: range(10))\ndef test_custom_async_iteratable_filter(env_async, items, run_async_fn):\n    async def customfilter(iterable):\n        items = []\n        async for item in auto_aiter(iterable):\n            items.append(str(item))\n            if len(items) == 3:\n                break\n        return \",\".join(items)\n\n    async def test():\n        async with closing_factory() as closing:\n            env_async.filters[\"customfilter\"] = customfilter\n            tmpl = env_async.from_string(\n                \"{{ closing(items())|customfilter }} .. {{ [3, 4, 5, 6]|customfilter }}\"\n            )\n            return await tmpl.render_async(items=items, closing=closing)\n\n    out = run_async_fn(test)\n    assert out == \"0,1,2 .. 3,4,5\"\n"
  },
  {
    "path": "tests/test_bytecode_cache.py",
    "content": "import pytest\n\nfrom jinja2 import Environment\nfrom jinja2.bccache import Bucket\nfrom jinja2.bccache import FileSystemBytecodeCache\nfrom jinja2.bccache import MemcachedBytecodeCache\nfrom jinja2.exceptions import TemplateNotFound\n\n\n@pytest.fixture\ndef env(package_loader, tmp_path):\n    bytecode_cache = FileSystemBytecodeCache(str(tmp_path))\n    return Environment(loader=package_loader, bytecode_cache=bytecode_cache)\n\n\nclass TestByteCodeCache:\n    def test_simple(self, env):\n        tmpl = env.get_template(\"test.html\")\n        assert tmpl.render().strip() == \"BAR\"\n        pytest.raises(TemplateNotFound, env.get_template, \"missing.html\")\n\n\nclass MockMemcached:\n    class Error(Exception):\n        pass\n\n    key = None\n    value = None\n    timeout = None\n\n    def get(self, key):\n        return self.value\n\n    def set(self, key, value, timeout=None):\n        self.key = key\n        self.value = value\n        self.timeout = timeout\n\n    def get_side_effect(self, key):\n        raise self.Error()\n\n    def set_side_effect(self, *args):\n        raise self.Error()\n\n\nclass TestMemcachedBytecodeCache:\n    def test_dump_load(self):\n        memcached = MockMemcached()\n        m = MemcachedBytecodeCache(memcached)\n\n        b = Bucket(None, \"key\", \"\")\n        b.code = \"code\"\n        m.dump_bytecode(b)\n        assert memcached.key == \"jinja2/bytecode/key\"\n\n        b = Bucket(None, \"key\", \"\")\n        m.load_bytecode(b)\n        assert b.code == \"code\"\n\n    def test_exception(self):\n        memcached = MockMemcached()\n        memcached.get = memcached.get_side_effect\n        memcached.set = memcached.set_side_effect\n        m = MemcachedBytecodeCache(memcached)\n        b = Bucket(None, \"key\", \"\")\n        b.code = \"code\"\n\n        m.dump_bytecode(b)\n        m.load_bytecode(b)\n\n        m.ignore_memcache_errors = False\n\n        with pytest.raises(MockMemcached.Error):\n            m.dump_bytecode(b)\n\n        with pytest.raises(MockMemcached.Error):\n            m.load_bytecode(b)\n"
  },
  {
    "path": "tests/test_compile.py",
    "content": "import os\nimport re\n\nimport pytest\n\nfrom jinja2 import UndefinedError\nfrom jinja2.environment import Environment\nfrom jinja2.loaders import DictLoader\n\n\ndef test_filters_deterministic(tmp_path):\n    src = \"\".join(f\"{{{{ {i}|filter{i} }}}}\" for i in range(10))\n    env = Environment(loader=DictLoader({\"foo\": src}))\n    env.filters.update(dict.fromkeys((f\"filter{i}\" for i in range(10)), lambda: None))\n    env.compile_templates(tmp_path, zip=None)\n    name = os.listdir(tmp_path)[0]\n    content = (tmp_path / name).read_text(\"utf8\")\n    expect = [f\"filters['filter{i}']\" for i in range(10)]\n    found = re.findall(r\"filters\\['filter\\d']\", content)\n    assert found == expect\n\n\ndef test_import_as_with_context_deterministic(tmp_path):\n    src = \"\\n\".join(f'{{% import \"bar\" as bar{i} with context %}}' for i in range(10))\n    env = Environment(loader=DictLoader({\"foo\": src}))\n    env.compile_templates(tmp_path, zip=None)\n    name = os.listdir(tmp_path)[0]\n    content = (tmp_path / name).read_text(\"utf8\")\n    expect = [f\"'bar{i}': \" for i in range(10)]\n    found = re.findall(r\"'bar\\d': \", content)[:10]\n    assert found == expect\n\n\ndef test_top_level_set_vars_unpacking_deterministic(tmp_path):\n    src = \"\\n\".join(f\"{{% set a{i}, b{i}, c{i} = tuple_var{i} %}}\" for i in range(10))\n    env = Environment(loader=DictLoader({\"foo\": src}))\n    env.compile_templates(tmp_path, zip=None)\n    name = os.listdir(tmp_path)[0]\n    content = (tmp_path / name).read_text(\"utf8\")\n    expect = [\n        f\"context.vars.update({{'a{i}': l_0_a{i}, 'b{i}': l_0_b{i}, 'c{i}': l_0_c{i}}})\"\n        for i in range(10)\n    ]\n    found = re.findall(\n        r\"context\\.vars\\.update\\(\\{'a\\d': l_0_a\\d, 'b\\d': l_0_b\\d, 'c\\d': l_0_c\\d\\}\\)\",\n        content,\n    )[:10]\n    assert found == expect\n    expect = [\n        f\"context.exported_vars.update(('a{i}', 'b{i}', 'c{i}'))\" for i in range(10)\n    ]\n    found = re.findall(\n        r\"context\\.exported_vars\\.update\\(\\('a\\d', 'b\\d', 'c\\d'\\)\\)\",\n        content,\n    )[:10]\n    assert found == expect\n\n\ndef test_loop_set_vars_unpacking_deterministic(tmp_path):\n    src = \"\\n\".join(f\"  {{% set a{i}, b{i}, c{i} = tuple_var{i} %}}\" for i in range(10))\n    src = f\"{{% for i in seq %}}\\n{src}\\n{{% endfor %}}\"\n    env = Environment(loader=DictLoader({\"foo\": src}))\n    env.compile_templates(tmp_path, zip=None)\n    name = os.listdir(tmp_path)[0]\n    content = (tmp_path / name).read_text(\"utf8\")\n    expect = [\n        f\"_loop_vars.update({{'a{i}': l_1_a{i}, 'b{i}': l_1_b{i}, 'c{i}': l_1_c{i}}})\"\n        for i in range(10)\n    ]\n    found = re.findall(\n        r\"_loop_vars\\.update\\(\\{'a\\d': l_1_a\\d, 'b\\d': l_1_b\\d, 'c\\d': l_1_c\\d\\}\\)\",\n        content,\n    )[:10]\n    assert found == expect\n\n\ndef test_block_set_vars_unpacking_deterministic(tmp_path):\n    src = \"\\n\".join(f\"  {{% set a{i}, b{i}, c{i} = tuple_var{i} %}}\" for i in range(10))\n    src = f\"{{% block test %}}\\n{src}\\n{{% endblock test %}}\"\n    env = Environment(loader=DictLoader({\"foo\": src}))\n    env.compile_templates(tmp_path, zip=None)\n    name = os.listdir(tmp_path)[0]\n    content = (tmp_path / name).read_text(\"utf8\")\n    expect = [\n        f\"_block_vars.update({{'a{i}': l_0_a{i}, 'b{i}': l_0_b{i}, 'c{i}': l_0_c{i}}})\"\n        for i in range(10)\n    ]\n    found = re.findall(\n        r\"_block_vars\\.update\\(\\{'a\\d': l_0_a\\d, 'b\\d': l_0_b\\d, 'c\\d': l_0_c\\d\\}\\)\",\n        content,\n    )[:10]\n    assert found == expect\n\n\ndef test_undefined_import_curly_name():\n    env = Environment(\n        loader=DictLoader(\n            {\n                \"{bad}\": \"{% from 'macro' import m %}{{ m() }}\",\n                \"macro\": \"\",\n            }\n        )\n    )\n\n    # Must not raise `NameError: 'bad' is not defined`, as that would indicate\n    # that `{bad}` is being interpreted as an f-string. It must be escaped.\n    with pytest.raises(UndefinedError):\n        env.get_template(\"{bad}\").render()\n"
  },
  {
    "path": "tests/test_core_tags.py",
    "content": "import pytest\n\nfrom jinja2 import DictLoader\nfrom jinja2 import Environment\nfrom jinja2 import TemplateRuntimeError\nfrom jinja2 import TemplateSyntaxError\nfrom jinja2 import UndefinedError\n\n\n@pytest.fixture\ndef env_trim():\n    return Environment(trim_blocks=True)\n\n\nclass TestForLoop:\n    def test_simple(self, env):\n        tmpl = env.from_string(\"{% for item in seq %}{{ item }}{% endfor %}\")\n        assert tmpl.render(seq=list(range(10))) == \"0123456789\"\n\n    def test_else(self, env):\n        tmpl = env.from_string(\"{% for item in seq %}XXX{% else %}...{% endfor %}\")\n        assert tmpl.render() == \"...\"\n\n    def test_else_scoping_item(self, env):\n        tmpl = env.from_string(\"{% for item in [] %}{% else %}{{ item }}{% endfor %}\")\n        assert tmpl.render(item=42) == \"42\"\n\n    def test_empty_blocks(self, env):\n        tmpl = env.from_string(\"<{% for item in seq %}{% else %}{% endfor %}>\")\n        assert tmpl.render() == \"<>\"\n\n    def test_context_vars(self, env):\n        slist = [42, 24]\n        for seq in [slist, iter(slist), reversed(slist), (_ for _ in slist)]:\n            tmpl = env.from_string(\n                \"\"\"{% for item in seq -%}\n            {{ loop.index }}|{{ loop.index0 }}|{{ loop.revindex }}|{{\n                loop.revindex0 }}|{{ loop.first }}|{{ loop.last }}|{{\n               loop.length }}###{% endfor %}\"\"\"\n            )\n            one, two, _ = tmpl.render(seq=seq).split(\"###\")\n            (\n                one_index,\n                one_index0,\n                one_revindex,\n                one_revindex0,\n                one_first,\n                one_last,\n                one_length,\n            ) = one.split(\"|\")\n            (\n                two_index,\n                two_index0,\n                two_revindex,\n                two_revindex0,\n                two_first,\n                two_last,\n                two_length,\n            ) = two.split(\"|\")\n\n            assert int(one_index) == 1 and int(two_index) == 2\n            assert int(one_index0) == 0 and int(two_index0) == 1\n            assert int(one_revindex) == 2 and int(two_revindex) == 1\n            assert int(one_revindex0) == 1 and int(two_revindex0) == 0\n            assert one_first == \"True\" and two_first == \"False\"\n            assert one_last == \"False\" and two_last == \"True\"\n            assert one_length == two_length == \"2\"\n\n    def test_cycling(self, env):\n        tmpl = env.from_string(\n            \"\"\"{% for item in seq %}{{\n            loop.cycle('<1>', '<2>') }}{% endfor %}{%\n            for item in seq %}{{ loop.cycle(*through) }}{% endfor %}\"\"\"\n        )\n        output = tmpl.render(seq=list(range(4)), through=(\"<1>\", \"<2>\"))\n        assert output == \"<1><2>\" * 4\n\n    def test_lookaround(self, env):\n        tmpl = env.from_string(\n            \"\"\"{% for item in seq -%}\n            {{ loop.previtem|default('x') }}-{{ item }}-{{\n            loop.nextitem|default('x') }}|\n        {%- endfor %}\"\"\"\n        )\n        output = tmpl.render(seq=list(range(4)))\n        assert output == \"x-0-1|0-1-2|1-2-3|2-3-x|\"\n\n    def test_changed(self, env):\n        tmpl = env.from_string(\n            \"\"\"{% for item in seq -%}\n            {{ loop.changed(item) }},\n        {%- endfor %}\"\"\"\n        )\n        output = tmpl.render(seq=[None, None, 1, 2, 2, 3, 4, 4, 4])\n        assert output == \"True,False,True,True,False,True,True,False,False,\"\n\n    def test_scope(self, env):\n        tmpl = env.from_string(\"{% for item in seq %}{% endfor %}{{ item }}\")\n        output = tmpl.render(seq=list(range(10)))\n        assert not output\n\n    def test_varlen(self, env):\n        tmpl = env.from_string(\"{% for item in iter %}{{ item }}{% endfor %}\")\n        output = tmpl.render(iter=range(5))\n        assert output == \"01234\"\n\n    def test_noniter(self, env):\n        tmpl = env.from_string(\"{% for item in none %}...{% endfor %}\")\n        pytest.raises(TypeError, tmpl.render)\n\n    def test_recursive(self, env):\n        tmpl = env.from_string(\n            \"\"\"{% for item in seq recursive -%}\n            [{{ item.a }}{% if item.b %}<{{ loop(item.b) }}>{% endif %}]\n        {%- endfor %}\"\"\"\n        )\n        assert (\n            tmpl.render(\n                seq=[\n                    dict(a=1, b=[dict(a=1), dict(a=2)]),\n                    dict(a=2, b=[dict(a=1), dict(a=2)]),\n                    dict(a=3, b=[dict(a=\"a\")]),\n                ]\n            )\n            == \"[1<[1][2]>][2<[1][2]>][3<[a]>]\"\n        )\n\n    def test_recursive_lookaround(self, env):\n        tmpl = env.from_string(\n            \"\"\"{% for item in seq recursive -%}\n            [{{ loop.previtem.a if loop.previtem is defined else 'x' }}.{{\n            item.a }}.{{ loop.nextitem.a if loop.nextitem is defined else 'x'\n            }}{% if item.b %}<{{ loop(item.b) }}>{% endif %}]\n        {%- endfor %}\"\"\"\n        )\n        assert (\n            tmpl.render(\n                seq=[\n                    dict(a=1, b=[dict(a=1), dict(a=2)]),\n                    dict(a=2, b=[dict(a=1), dict(a=2)]),\n                    dict(a=3, b=[dict(a=\"a\")]),\n                ]\n            )\n            == \"[x.1.2<[x.1.2][1.2.x]>][1.2.3<[x.1.2][1.2.x]>][2.3.x<[x.a.x]>]\"\n        )\n\n    def test_recursive_depth0(self, env):\n        tmpl = env.from_string(\n            \"\"\"{% for item in seq recursive -%}\n        [{{ loop.depth0 }}:{{ item.a }}{% if item.b %}<{{ loop(item.b) }}>{% endif %}]\n        {%- endfor %}\"\"\"\n        )\n        assert (\n            tmpl.render(\n                seq=[\n                    dict(a=1, b=[dict(a=1), dict(a=2)]),\n                    dict(a=2, b=[dict(a=1), dict(a=2)]),\n                    dict(a=3, b=[dict(a=\"a\")]),\n                ]\n            )\n            == \"[0:1<[1:1][1:2]>][0:2<[1:1][1:2]>][0:3<[1:a]>]\"\n        )\n\n    def test_recursive_depth(self, env):\n        tmpl = env.from_string(\n            \"\"\"{% for item in seq recursive -%}\n        [{{ loop.depth }}:{{ item.a }}{% if item.b %}<{{ loop(item.b) }}>{% endif %}]\n        {%- endfor %}\"\"\"\n        )\n        assert (\n            tmpl.render(\n                seq=[\n                    dict(a=1, b=[dict(a=1), dict(a=2)]),\n                    dict(a=2, b=[dict(a=1), dict(a=2)]),\n                    dict(a=3, b=[dict(a=\"a\")]),\n                ]\n            )\n            == \"[1:1<[2:1][2:2]>][1:2<[2:1][2:2]>][1:3<[2:a]>]\"\n        )\n\n    def test_looploop(self, env):\n        tmpl = env.from_string(\n            \"\"\"{% for row in table %}\n            {%- set rowloop = loop -%}\n            {% for cell in row -%}\n                [{{ rowloop.index }}|{{ loop.index }}]\n            {%- endfor %}\n        {%- endfor %}\"\"\"\n        )\n        assert tmpl.render(table=[\"ab\", \"cd\"]) == \"[1|1][1|2][2|1][2|2]\"\n\n    def test_reversed_bug(self, env):\n        tmpl = env.from_string(\n            \"{% for i in items %}{{ i }}{% if not loop.last %},{% endif %}{% endfor %}\"\n        )\n        assert tmpl.render(items=reversed([3, 2, 1])) == \"1,2,3\"\n\n    def test_loop_errors(self, env):\n        tmpl = env.from_string(\n            \"\"\"{% for item in [1] if loop.index\n                                      == 0 %}...{% endfor %}\"\"\"\n        )\n        pytest.raises(UndefinedError, tmpl.render)\n        tmpl = env.from_string(\n            \"\"\"{% for item in [] %}...{% else\n            %}{{ loop }}{% endfor %}\"\"\"\n        )\n        assert tmpl.render() == \"\"\n\n    def test_loop_filter(self, env):\n        tmpl = env.from_string(\n            \"{% for item in range(10) if item is even %}[{{ item }}]{% endfor %}\"\n        )\n        assert tmpl.render() == \"[0][2][4][6][8]\"\n        tmpl = env.from_string(\n            \"\"\"\n            {%- for item in range(10) if item is even %}[{{\n                loop.index }}:{{ item }}]{% endfor %}\"\"\"\n        )\n        assert tmpl.render() == \"[1:0][2:2][3:4][4:6][5:8]\"\n\n    def test_loop_unassignable(self, env):\n        pytest.raises(\n            TemplateSyntaxError, env.from_string, \"{% for loop in seq %}...{% endfor %}\"\n        )\n\n    def test_scoped_special_var(self, env):\n        t = env.from_string(\n            \"{% for s in seq %}[{{ loop.first }}{% for c in s %}\"\n            \"|{{ loop.first }}{% endfor %}]{% endfor %}\"\n        )\n        assert t.render(seq=(\"ab\", \"cd\")) == \"[True|True|False][False|True|False]\"\n\n    def test_scoped_loop_var(self, env):\n        t = env.from_string(\n            \"{% for x in seq %}{{ loop.first }}\"\n            \"{% for y in seq %}{% endfor %}{% endfor %}\"\n        )\n        assert t.render(seq=\"ab\") == \"TrueFalse\"\n        t = env.from_string(\n            \"{% for x in seq %}{% for y in seq %}\"\n            \"{{ loop.first }}{% endfor %}{% endfor %}\"\n        )\n        assert t.render(seq=\"ab\") == \"TrueFalseTrueFalse\"\n\n    def test_recursive_empty_loop_iter(self, env):\n        t = env.from_string(\n            \"\"\"\n        {%- for item in foo recursive -%}{%- endfor -%}\n        \"\"\"\n        )\n        assert t.render(dict(foo=[])) == \"\"\n\n    def test_call_in_loop(self, env):\n        t = env.from_string(\n            \"\"\"\n        {%- macro do_something() -%}\n            [{{ caller() }}]\n        {%- endmacro %}\n\n        {%- for i in [1, 2, 3] %}\n            {%- call do_something() -%}\n                {{ i }}\n            {%- endcall %}\n        {%- endfor -%}\n        \"\"\"\n        )\n        assert t.render() == \"[1][2][3]\"\n\n    def test_scoping_bug(self, env):\n        t = env.from_string(\n            \"\"\"\n        {%- for item in foo %}...{{ item }}...{% endfor %}\n        {%- macro item(a) %}...{{ a }}...{% endmacro %}\n        {{- item(2) -}}\n        \"\"\"\n        )\n        assert t.render(foo=(1,)) == \"...1......2...\"\n\n    def test_unpacking(self, env):\n        tmpl = env.from_string(\n            \"{% for a, b, c in [[1, 2, 3]] %}{{ a }}|{{ b }}|{{ c }}{% endfor %}\"\n        )\n        assert tmpl.render() == \"1|2|3\"\n\n    def test_intended_scoping_with_set(self, env):\n        tmpl = env.from_string(\n            \"{% for item in seq %}{{ x }}{% set x = item %}{{ x }}{% endfor %}\"\n        )\n        assert tmpl.render(x=0, seq=[1, 2, 3]) == \"010203\"\n\n        tmpl = env.from_string(\n            \"{% set x = 9 %}{% for item in seq %}{{ x }}\"\n            \"{% set x = item %}{{ x }}{% endfor %}\"\n        )\n        assert tmpl.render(x=0, seq=[1, 2, 3]) == \"919293\"\n\n\nclass TestIfCondition:\n    def test_simple(self, env):\n        tmpl = env.from_string(\"\"\"{% if true %}...{% endif %}\"\"\")\n        assert tmpl.render() == \"...\"\n\n    def test_elif(self, env):\n        tmpl = env.from_string(\n            \"\"\"{% if false %}XXX{% elif true\n            %}...{% else %}XXX{% endif %}\"\"\"\n        )\n        assert tmpl.render() == \"...\"\n\n    def test_elif_deep(self, env):\n        elifs = \"\\n\".join(f\"{{% elif a == {i} %}}{i}\" for i in range(1, 1000))\n        tmpl = env.from_string(f\"{{% if a == 0 %}}0{elifs}{{% else %}}x{{% endif %}}\")\n        for x in (0, 10, 999):\n            assert tmpl.render(a=x).strip() == str(x)\n        assert tmpl.render(a=1000).strip() == \"x\"\n\n    def test_else(self, env):\n        tmpl = env.from_string(\"{% if false %}XXX{% else %}...{% endif %}\")\n        assert tmpl.render() == \"...\"\n\n    def test_empty(self, env):\n        tmpl = env.from_string(\"[{% if true %}{% else %}{% endif %}]\")\n        assert tmpl.render() == \"[]\"\n\n    def test_complete(self, env):\n        tmpl = env.from_string(\n            \"{% if a %}A{% elif b %}B{% elif c == d %}C{% else %}D{% endif %}\"\n        )\n        assert tmpl.render(a=0, b=False, c=42, d=42.0) == \"C\"\n\n    def test_no_scope(self, env):\n        tmpl = env.from_string(\"{% if a %}{% set foo = 1 %}{% endif %}{{ foo }}\")\n        assert tmpl.render(a=True) == \"1\"\n        tmpl = env.from_string(\"{% if true %}{% set foo = 1 %}{% endif %}{{ foo }}\")\n        assert tmpl.render() == \"1\"\n\n\nclass TestMacros:\n    def test_simple(self, env_trim):\n        tmpl = env_trim.from_string(\n            \"\"\"\\\n{% macro say_hello(name) %}Hello {{ name }}!{% endmacro %}\n{{ say_hello('Peter') }}\"\"\"\n        )\n        assert tmpl.render() == \"Hello Peter!\"\n\n    def test_scoping(self, env_trim):\n        tmpl = env_trim.from_string(\n            \"\"\"\\\n{% macro level1(data1) %}\n{% macro level2(data2) %}{{ data1 }}|{{ data2 }}{% endmacro %}\n{{ level2('bar') }}{% endmacro %}\n{{ level1('foo') }}\"\"\"\n        )\n        assert tmpl.render() == \"foo|bar\"\n\n    def test_arguments(self, env_trim):\n        tmpl = env_trim.from_string(\n            \"\"\"\\\n{% macro m(a, b, c='c', d='d') %}{{ a }}|{{ b }}|{{ c }}|{{ d }}{% endmacro %}\n{{ m() }}|{{ m('a') }}|{{ m('a', 'b') }}|{{ m(1, 2, 3) }}\"\"\"\n        )\n        assert tmpl.render() == \"||c|d|a||c|d|a|b|c|d|1|2|3|d\"\n\n    def test_arguments_defaults_nonsense(self, env_trim):\n        pytest.raises(\n            TemplateSyntaxError,\n            env_trim.from_string,\n            \"\"\"\\\n{% macro m(a, b=1, c) %}a={{ a }}, b={{ b }}, c={{ c }}{% endmacro %}\"\"\",\n        )\n\n    def test_caller_defaults_nonsense(self, env_trim):\n        pytest.raises(\n            TemplateSyntaxError,\n            env_trim.from_string,\n            \"\"\"\\\n{% macro a() %}{{ caller() }}{% endmacro %}\n{% call(x, y=1, z) a() %}{% endcall %}\"\"\",\n        )\n\n    def test_varargs(self, env_trim):\n        tmpl = env_trim.from_string(\n            \"\"\"\\\n{% macro test() %}{{ varargs|join('|') }}{% endmacro %}\\\n{{ test(1, 2, 3) }}\"\"\"\n        )\n        assert tmpl.render() == \"1|2|3\"\n\n    def test_simple_call(self, env_trim):\n        tmpl = env_trim.from_string(\n            \"\"\"\\\n{% macro test() %}[[{{ caller() }}]]{% endmacro %}\\\n{% call test() %}data{% endcall %}\"\"\"\n        )\n        assert tmpl.render() == \"[[data]]\"\n\n    def test_complex_call(self, env_trim):\n        tmpl = env_trim.from_string(\n            \"\"\"\\\n{% macro test() %}[[{{ caller('data') }}]]{% endmacro %}\\\n{% call(data) test() %}{{ data }}{% endcall %}\"\"\"\n        )\n        assert tmpl.render() == \"[[data]]\"\n\n    def test_caller_undefined(self, env_trim):\n        tmpl = env_trim.from_string(\n            \"\"\"\\\n{% set caller = 42 %}\\\n{% macro test() %}{{ caller is not defined }}{% endmacro %}\\\n{{ test() }}\"\"\"\n        )\n        assert tmpl.render() == \"True\"\n\n    def test_include(self, env_trim):\n        env_trim = Environment(\n            loader=DictLoader(\n                {\"include\": \"{% macro test(foo) %}[{{ foo }}]{% endmacro %}\"}\n            )\n        )\n        tmpl = env_trim.from_string('{% from \"include\" import test %}{{ test(\"foo\") }}')\n        assert tmpl.render() == \"[foo]\"\n\n    def test_macro_api(self, env_trim):\n        tmpl = env_trim.from_string(\n            \"{% macro foo(a, b) %}{% endmacro %}\"\n            \"{% macro bar() %}{{ varargs }}{{ kwargs }}{% endmacro %}\"\n            \"{% macro baz() %}{{ caller() }}{% endmacro %}\"\n        )\n        assert tmpl.module.foo.arguments == (\"a\", \"b\")\n        assert tmpl.module.foo.name == \"foo\"\n        assert not tmpl.module.foo.caller\n        assert not tmpl.module.foo.catch_kwargs\n        assert not tmpl.module.foo.catch_varargs\n        assert tmpl.module.bar.arguments == ()\n        assert not tmpl.module.bar.caller\n        assert tmpl.module.bar.catch_kwargs\n        assert tmpl.module.bar.catch_varargs\n        assert tmpl.module.baz.caller\n\n    def test_callself(self, env_trim):\n        tmpl = env_trim.from_string(\n            \"{% macro foo(x) %}{{ x }}{% if x > 1 %}|\"\n            \"{{ foo(x - 1) }}{% endif %}{% endmacro %}\"\n            \"{{ foo(5) }}\"\n        )\n        assert tmpl.render() == \"5|4|3|2|1\"\n\n    def test_macro_defaults_self_ref(self, env):\n        tmpl = env.from_string(\n            \"\"\"\n            {%- set x = 42 %}\n            {%- macro m(a, b=x, x=23) %}{{ a }}|{{ b }}|{{ x }}{% endmacro -%}\n        \"\"\"\n        )\n        assert tmpl.module.m(1) == \"1||23\"\n        assert tmpl.module.m(1, 2) == \"1|2|23\"\n        assert tmpl.module.m(1, 2, 3) == \"1|2|3\"\n        assert tmpl.module.m(1, x=7) == \"1|7|7\"\n\n\nclass TestSet:\n    def test_normal(self, env_trim):\n        tmpl = env_trim.from_string(\"{% set foo = 1 %}{{ foo }}\")\n        assert tmpl.render() == \"1\"\n        assert tmpl.module.foo == 1\n\n    def test_block(self, env_trim):\n        tmpl = env_trim.from_string(\"{% set foo %}42{% endset %}{{ foo }}\")\n        assert tmpl.render() == \"42\"\n        assert tmpl.module.foo == \"42\"\n\n    def test_block_escaping(self):\n        env = Environment(autoescape=True)\n        tmpl = env.from_string(\n            \"{% set foo %}<em>{{ test }}</em>{% endset %}foo: {{ foo }}\"\n        )\n        assert tmpl.render(test=\"<unsafe>\") == \"foo: <em>&lt;unsafe&gt;</em>\"\n\n    def test_set_invalid(self, env_trim):\n        pytest.raises(\n            TemplateSyntaxError, env_trim.from_string, \"{% set foo['bar'] = 1 %}\"\n        )\n        tmpl = env_trim.from_string(\"{% set foo.bar = 1 %}\")\n        exc_info = pytest.raises(TemplateRuntimeError, tmpl.render, foo={})\n        assert \"non-namespace object\" in exc_info.value.message\n\n    def test_namespace_redefined(self, env_trim):\n        tmpl = env_trim.from_string(\"{% set ns = namespace() %}{% set ns.bar = 'hi' %}\")\n        exc_info = pytest.raises(TemplateRuntimeError, tmpl.render, namespace=dict)\n        assert \"non-namespace object\" in exc_info.value.message\n\n    def test_namespace(self, env_trim):\n        tmpl = env_trim.from_string(\n            \"{% set ns = namespace() %}{% set ns.bar = '42' %}{{ ns.bar }}\"\n        )\n        assert tmpl.render() == \"42\"\n\n    def test_namespace_block(self, env_trim):\n        tmpl = env_trim.from_string(\n            \"{% set ns = namespace() %}{% set ns.bar %}42{% endset %}{{ ns.bar }}\"\n        )\n        assert tmpl.render() == \"42\"\n\n    def test_init_namespace(self, env_trim):\n        tmpl = env_trim.from_string(\n            \"{% set ns = namespace(d, self=37) %}\"\n            \"{% set ns.b = 42 %}\"\n            \"{{ ns.a }}|{{ ns.self }}|{{ ns.b }}\"\n        )\n        assert tmpl.render(d={\"a\": 13}) == \"13|37|42\"\n\n    def test_namespace_loop(self, env_trim):\n        tmpl = env_trim.from_string(\n            \"{% set ns = namespace(found=false) %}\"\n            \"{% for x in range(4) %}\"\n            \"{% if x == v %}\"\n            \"{% set ns.found = true %}\"\n            \"{% endif %}\"\n            \"{% endfor %}\"\n            \"{{ ns.found }}\"\n        )\n        assert tmpl.render(v=3) == \"True\"\n        assert tmpl.render(v=4) == \"False\"\n\n    def test_namespace_macro(self, env_trim):\n        tmpl = env_trim.from_string(\n            \"{% set ns = namespace() %}\"\n            \"{% set ns.a = 13 %}\"\n            \"{% macro magic(x) %}\"\n            \"{% set x.b = 37 %}\"\n            \"{% endmacro %}\"\n            \"{{ magic(ns) }}\"\n            \"{{ ns.a }}|{{ ns.b }}\"\n        )\n        assert tmpl.render() == \"13|37\"\n\n    def test_namespace_set_tuple(self, env_trim):\n        tmpl = env_trim.from_string(\n            \"{% set ns = namespace(a=12, b=36) %}\"\n            \"{% set ns.a, ns.b = ns.a + 1, ns.b + 1 %}\"\n            \"{{ ns.a }}|{{ ns.b }}\"\n        )\n        assert tmpl.render() == \"13|37\"\n\n    def test_block_escaping_filtered(self):\n        env = Environment(autoescape=True)\n        tmpl = env.from_string(\n            \"{% set foo | trim %}<em>{{ test }}</em>    {% endset %}foo: {{ foo }}\"\n        )\n        assert tmpl.render(test=\"<unsafe>\") == \"foo: <em>&lt;unsafe&gt;</em>\"\n\n    def test_block_filtered(self, env_trim):\n        tmpl = env_trim.from_string(\n            \"{% set foo | trim | length | string %} 42    {% endset %}{{ foo }}\"\n        )\n        assert tmpl.render() == \"2\"\n        assert tmpl.module.foo == \"2\"\n\n    def test_block_filtered_set(self, env_trim):\n        def _myfilter(val, arg):\n            assert arg == \" xxx \"\n            return val\n\n        env_trim.filters[\"myfilter\"] = _myfilter\n        tmpl = env_trim.from_string(\n            '{% set a = \" xxx \" %}'\n            \"{% set foo | myfilter(a) | trim | length | string %}\"\n            ' {% set b = \" yy \" %} 42 {{ a }}{{ b }}   '\n            \"{% endset %}\"\n            \"{{ foo }}\"\n        )\n        assert tmpl.render() == \"11\"\n        assert tmpl.module.foo == \"11\"\n\n\nclass TestWith:\n    def test_with(self, env):\n        tmpl = env.from_string(\n            \"\"\"\\\n        {% with a=42, b=23 -%}\n            {{ a }} = {{ b }}\n        {% endwith -%}\n            {{ a }} = {{ b }}\\\n        \"\"\"\n        )\n        assert [x.strip() for x in tmpl.render(a=1, b=2).splitlines()] == [\n            \"42 = 23\",\n            \"1 = 2\",\n        ]\n\n    def test_with_argument_scoping(self, env):\n        tmpl = env.from_string(\n            \"\"\"\\\n        {%- with a=1, b=2, c=b, d=e, e=5 -%}\n            {{ a }}|{{ b }}|{{ c }}|{{ d }}|{{ e }}\n        {%- endwith -%}\n        \"\"\"\n        )\n        assert tmpl.render(b=3, e=4) == \"1|2|3|4|5\"\n"
  },
  {
    "path": "tests/test_debug.py",
    "content": "import pickle\nimport re\nfrom traceback import format_exception\n\nimport pytest\n\nfrom jinja2 import ChoiceLoader\nfrom jinja2 import DictLoader\nfrom jinja2 import Environment\nfrom jinja2 import TemplateSyntaxError\n\n\n@pytest.fixture\ndef fs_env(filesystem_loader):\n    \"\"\"returns a new environment.\"\"\"\n    return Environment(loader=filesystem_loader)\n\n\nclass TestDebug:\n    def assert_traceback_matches(self, callback, expected_tb):\n        with pytest.raises(Exception) as exc_info:\n            callback()\n\n        tb = format_exception(exc_info.type, exc_info.value, exc_info.tb)\n        m = re.search(expected_tb.strip(), \"\".join(tb))\n        assert m is not None, (\n            f\"Traceback did not match:\\n\\n{''.join(tb)}\\nexpected:\\n{expected_tb}\"\n        )\n\n    def test_runtime_error(self, fs_env):\n        def test():\n            tmpl.render(fail=lambda: 1 / 0)\n\n        tmpl = fs_env.get_template(\"broken.html\")\n        self.assert_traceback_matches(\n            test,\n            r\"\"\"\n  File \".*?broken.html\", line 2, in (top-level template code|<module>)\n    \\{\\{ fail\\(\\) \\}\\}(\n    \\^{12})?\n  File \".*debug?.pyc?\", line \\d+, in <lambda>\n    tmpl\\.render\\(fail=lambda: 1 / 0\\)(\n                             ~~\\^~~)?\nZeroDivisionError: (int(eger)? )?division (or modulo )?by zero\n\"\"\",\n        )\n\n    def test_syntax_error(self, fs_env):\n        # The trailing .*? is for PyPy 2 and 3, which don't seem to\n        # clear the exception's original traceback, leaving the syntax\n        # error in the middle of other compiler frames.\n        self.assert_traceback_matches(\n            lambda: fs_env.get_template(\"syntaxerror.html\"),\n            \"\"\"(?sm)\n  File \".*?syntaxerror.html\", line 4, in (template|<module>)\n    \\\\{% endif %\\\\}.*?\n(jinja2\\\\.exceptions\\\\.)?TemplateSyntaxError: Encountered unknown tag 'endif'. Jinja \\\nwas looking for the following tags: 'endfor' or 'else'. The innermost block that needs \\\nto be closed is 'for'.\n    \"\"\",\n        )\n\n    def test_regular_syntax_error(self, fs_env):\n        def test():\n            raise TemplateSyntaxError(\"wtf\", 42)\n\n        self.assert_traceback_matches(\n            test,\n            r\"\"\"\n  File \".*debug.pyc?\", line \\d+, in test\n    raise TemplateSyntaxError\\(\"wtf\", 42\\)(\n    \\^{36})?\n(jinja2\\.exceptions\\.)?TemplateSyntaxError: wtf\n  line 42\"\"\",\n        )\n\n    def test_pickleable_syntax_error(self, fs_env):\n        original = TemplateSyntaxError(\"bad template\", 42, \"test\", \"test.txt\")\n        unpickled = pickle.loads(pickle.dumps(original))\n        assert str(original) == str(unpickled)\n        assert original.name == unpickled.name\n\n    def test_include_syntax_error_source(self, filesystem_loader):\n        e = Environment(\n            loader=ChoiceLoader(\n                [\n                    filesystem_loader,\n                    DictLoader({\"inc\": \"a\\n{% include 'syntaxerror.html' %}\\nb\"}),\n                ]\n            )\n        )\n        t = e.get_template(\"inc\")\n\n        with pytest.raises(TemplateSyntaxError) as exc_info:\n            t.render()\n\n        assert exc_info.value.source is not None\n\n    def test_local_extraction(self):\n        from jinja2.debug import get_template_locals\n        from jinja2.runtime import missing\n\n        locals = get_template_locals(\n            {\n                \"l_0_foo\": 42,\n                \"l_1_foo\": 23,\n                \"l_2_foo\": 13,\n                \"l_0_bar\": 99,\n                \"l_1_bar\": missing,\n                \"l_0_baz\": missing,\n            }\n        )\n        assert locals == {\"foo\": 13, \"bar\": 99}\n\n    def test_get_corresponding_lineno_traceback(self, fs_env):\n        tmpl = fs_env.get_template(\"test.html\")\n        assert tmpl.get_corresponding_lineno(1) == 1\n"
  },
  {
    "path": "tests/test_ext.py",
    "content": "import re\nfrom io import BytesIO\n\nimport pytest\n\nfrom jinja2 import DictLoader\nfrom jinja2 import Environment\nfrom jinja2 import nodes\nfrom jinja2 import pass_context\nfrom jinja2 import TemplateSyntaxError\nfrom jinja2.exceptions import TemplateAssertionError\nfrom jinja2.ext import Extension\nfrom jinja2.lexer import count_newlines\nfrom jinja2.lexer import Token\n\nimportable_object = 23\n\n_gettext_re = re.compile(r\"_\\((.*?)\\)\", re.DOTALL)\n\n\ni18n_templates = {\n    \"default.html\": '<title>{{ page_title|default(_(\"missing\")) }}</title>'\n    \"{% block body %}{% endblock %}\",\n    \"child.html\": '{% extends \"default.html\" %}{% block body %}'\n    \"{% trans %}watch out{% endtrans %}{% endblock %}\",\n    \"plural.html\": \"{% trans user_count %}One user online{% pluralize %}\"\n    \"{{ user_count }} users online{% endtrans %}\",\n    \"plural2.html\": \"{% trans user_count=get_user_count() %}{{ user_count }}s\"\n    \"{% pluralize %}{{ user_count }}p{% endtrans %}\",\n    \"stringformat.html\": '{{ _(\"User: %(num)s\")|format(num=user_count) }}',\n}\n\nnewstyle_i18n_templates = {\n    \"default.html\": '<title>{{ page_title|default(_(\"missing\")) }}</title>'\n    \"{% block body %}{% endblock %}\",\n    \"child.html\": '{% extends \"default.html\" %}{% block body %}'\n    \"{% trans %}watch out{% endtrans %}{% endblock %}\",\n    \"plural.html\": \"{% trans user_count %}One user online{% pluralize %}\"\n    \"{{ user_count }} users online{% endtrans %}\",\n    \"stringformat.html\": '{{ _(\"User: %(num)s\", num=user_count) }}',\n    \"ngettext.html\": '{{ ngettext(\"%(num)s apple\", \"%(num)s apples\", apples) }}',\n    \"ngettext_long.html\": \"{% trans num=apples %}{{ num }} apple{% pluralize %}\"\n    \"{{ num }} apples{% endtrans %}\",\n    \"pgettext.html\": '{{ pgettext(\"fruit\", \"Apple\") }}',\n    \"npgettext.html\": '{{ npgettext(\"fruit\", \"%(num)s apple\", \"%(num)s apples\",'\n    \" apples) }}\",\n    \"pgettext_block\": \"{% trans 'fruit' num=apples %}Apple{% endtrans %}\",\n    \"npgettext_block\": \"{% trans 'fruit' num=apples %}{{ num }} apple\"\n    \"{% pluralize %}{{ num }} apples{% endtrans %}\",\n    \"transvars1.html\": \"{% trans %}User: {{ num }}{% endtrans %}\",\n    \"transvars2.html\": \"{% trans num=count %}User: {{ num }}{% endtrans %}\",\n    \"transvars3.html\": \"{% trans count=num %}User: {{ count }}{% endtrans %}\",\n    \"novars.html\": \"{% trans %}%(hello)s{% endtrans %}\",\n    \"vars.html\": \"{% trans %}{{ foo }}%(foo)s{% endtrans %}\",\n    \"explicitvars.html\": '{% trans foo=\"42\" %}%(foo)s{% endtrans %}',\n}\n\n\nlanguages = {\n    \"de\": {\n        \"missing\": \"fehlend\",\n        \"watch out\": \"pass auf\",\n        \"One user online\": \"Ein Benutzer online\",\n        \"%(user_count)s users online\": \"%(user_count)s Benutzer online\",\n        \"User: %(num)s\": \"Benutzer: %(num)s\",\n        \"User: %(count)s\": \"Benutzer: %(count)s\",\n        \"Apple\": {None: \"Apfel\", \"fruit\": \"Apple\"},\n        \"%(num)s apple\": {None: \"%(num)s Apfel\", \"fruit\": \"%(num)s Apple\"},\n        \"%(num)s apples\": {None: \"%(num)s Äpfel\", \"fruit\": \"%(num)s Apples\"},\n    }\n}\n\n\ndef _get_with_context(value, ctx=None):\n    if isinstance(value, dict):\n        return value.get(ctx, value)\n\n    return value\n\n\n@pass_context\ndef gettext(context, string):\n    language = context.get(\"LANGUAGE\", \"en\")\n    value = languages.get(language, {}).get(string, string)\n    return _get_with_context(value)\n\n\n@pass_context\ndef ngettext(context, s, p, n):\n    language = context.get(\"LANGUAGE\", \"en\")\n\n    if n != 1:\n        value = languages.get(language, {}).get(p, p)\n        return _get_with_context(value)\n\n    value = languages.get(language, {}).get(s, s)\n    return _get_with_context(value)\n\n\n@pass_context\ndef pgettext(context, c, s):\n    language = context.get(\"LANGUAGE\", \"en\")\n    value = languages.get(language, {}).get(s, s)\n    return _get_with_context(value, c)\n\n\n@pass_context\ndef npgettext(context, c, s, p, n):\n    language = context.get(\"LANGUAGE\", \"en\")\n\n    if n != 1:\n        value = languages.get(language, {}).get(p, p)\n        return _get_with_context(value, c)\n\n    value = languages.get(language, {}).get(s, s)\n    return _get_with_context(value, c)\n\n\ni18n_env = Environment(\n    loader=DictLoader(i18n_templates), extensions=[\"jinja2.ext.i18n\"]\n)\ni18n_env.globals.update(\n    {\n        \"_\": gettext,\n        \"gettext\": gettext,\n        \"ngettext\": ngettext,\n        \"pgettext\": pgettext,\n        \"npgettext\": npgettext,\n    }\n)\ni18n_env_trimmed = Environment(extensions=[\"jinja2.ext.i18n\"])\n\ni18n_env_trimmed.policies[\"ext.i18n.trimmed\"] = True\ni18n_env_trimmed.globals.update(\n    {\n        \"_\": gettext,\n        \"gettext\": gettext,\n        \"ngettext\": ngettext,\n        \"pgettext\": pgettext,\n        \"npgettext\": npgettext,\n    }\n)\n\nnewstyle_i18n_env = Environment(\n    loader=DictLoader(newstyle_i18n_templates), extensions=[\"jinja2.ext.i18n\"]\n)\nnewstyle_i18n_env.install_gettext_callables(  # type: ignore\n    gettext, ngettext, newstyle=True, pgettext=pgettext, npgettext=npgettext\n)\n\n\nclass ExampleExtension(Extension):\n    tags = {\"test\"}\n    ext_attr = 42\n    context_reference_node_cls = nodes.ContextReference\n\n    def parse(self, parser):\n        return nodes.Output(\n            [\n                self.call_method(\n                    \"_dump\",\n                    [\n                        nodes.EnvironmentAttribute(\"sandboxed\"),\n                        self.attr(\"ext_attr\"),\n                        nodes.ImportedName(__name__ + \".importable_object\"),\n                        self.context_reference_node_cls(),\n                    ],\n                )\n            ]\n        ).set_lineno(next(parser.stream).lineno)\n\n    def _dump(self, sandboxed, ext_attr, imported_object, context):\n        return (\n            f\"{sandboxed}|{ext_attr}|{imported_object}|{context.blocks}\"\n            f\"|{context.get('test_var')}\"\n        )\n\n\nclass DerivedExampleExtension(ExampleExtension):\n    context_reference_node_cls = nodes.DerivedContextReference  # type: ignore\n\n\nclass PreprocessorExtension(Extension):\n    def preprocess(self, source, name, filename=None):\n        return source.replace(\"[[TEST]]\", \"({{ foo }})\")\n\n\nclass StreamFilterExtension(Extension):\n    def filter_stream(self, stream):\n        for token in stream:\n            if token.type == \"data\":\n                yield from self.interpolate(token)\n            else:\n                yield token\n\n    def interpolate(self, token):\n        pos = 0\n        end = len(token.value)\n        lineno = token.lineno\n        while True:\n            match = _gettext_re.search(token.value, pos)\n            if match is None:\n                break\n            value = token.value[pos : match.start()]\n            if value:\n                yield Token(lineno, \"data\", value)\n            lineno += count_newlines(token.value)\n            yield Token(lineno, \"variable_begin\", None)\n            yield Token(lineno, \"name\", \"gettext\")\n            yield Token(lineno, \"lparen\", None)\n            yield Token(lineno, \"string\", match.group(1))\n            yield Token(lineno, \"rparen\", None)\n            yield Token(lineno, \"variable_end\", None)\n            pos = match.end()\n        if pos < end:\n            yield Token(lineno, \"data\", token.value[pos:])\n\n\nclass TestExtensions:\n    def test_extend_late(self):\n        env = Environment()\n        t = env.from_string('{% autoescape true %}{{ \"<test>\" }}{% endautoescape %}')\n        assert t.render() == \"&lt;test&gt;\"\n\n    def test_loop_controls(self):\n        env = Environment(extensions=[\"jinja2.ext.loopcontrols\"])\n\n        tmpl = env.from_string(\n            \"\"\"\n            {%- for item in [1, 2, 3, 4] %}\n                {%- if item % 2 == 0 %}{% continue %}{% endif -%}\n                {{ item }}\n            {%- endfor %}\"\"\"\n        )\n        assert tmpl.render() == \"13\"\n\n        tmpl = env.from_string(\n            \"\"\"\n            {%- for item in [1, 2, 3, 4] %}\n                {%- if item > 2 %}{% break %}{% endif -%}\n                {{ item }}\n            {%- endfor %}\"\"\"\n        )\n        assert tmpl.render() == \"12\"\n\n    def test_do(self):\n        env = Environment(extensions=[\"jinja2.ext.do\"])\n        tmpl = env.from_string(\n            \"\"\"\n            {%- set items = [] %}\n            {%- for char in \"foo\" %}\n                {%- do items.append(loop.index0 ~ char) %}\n            {%- endfor %}{{ items|join(', ') }}\"\"\"\n        )\n        assert tmpl.render() == \"0f, 1o, 2o\"\n\n    def test_extension_nodes(self):\n        env = Environment(extensions=[ExampleExtension])\n        tmpl = env.from_string(\"{% test %}\")\n        assert tmpl.render() == \"False|42|23|{}|None\"\n\n    def test_contextreference_node_passes_context(self):\n        env = Environment(extensions=[ExampleExtension])\n        tmpl = env.from_string('{% set test_var=\"test_content\" %}{% test %}')\n        assert tmpl.render() == \"False|42|23|{}|test_content\"\n\n    def test_contextreference_node_can_pass_locals(self):\n        env = Environment(extensions=[DerivedExampleExtension])\n        tmpl = env.from_string(\n            '{% for test_var in [\"test_content\"] %}{% test %}{% endfor %}'\n        )\n        assert tmpl.render() == \"False|42|23|{}|test_content\"\n\n    def test_identifier(self):\n        assert ExampleExtension.identifier == __name__ + \".ExampleExtension\"\n\n    def test_rebinding(self):\n        original = Environment(extensions=[ExampleExtension])\n        overlay = original.overlay()\n        for env in original, overlay:\n            for ext in env.extensions.values():\n                assert ext.environment is env\n\n    def test_preprocessor_extension(self):\n        env = Environment(extensions=[PreprocessorExtension])\n        tmpl = env.from_string(\"{[[TEST]]}\")\n        assert tmpl.render(foo=42) == \"{(42)}\"\n\n    def test_streamfilter_extension(self):\n        env = Environment(extensions=[StreamFilterExtension])\n        env.globals[\"gettext\"] = lambda x: x.upper()\n        tmpl = env.from_string(\"Foo _(bar) Baz\")\n        out = tmpl.render()\n        assert out == \"Foo BAR Baz\"\n\n    def test_extension_ordering(self):\n        class T1(Extension):\n            priority = 1\n\n        class T2(Extension):\n            priority = 2\n\n        env = Environment(extensions=[T1, T2])\n        ext = list(env.iter_extensions())\n        assert ext[0].__class__ is T1\n        assert ext[1].__class__ is T2\n\n    def test_debug(self):\n        env = Environment(extensions=[\"jinja2.ext.debug\"])\n        t = env.from_string(\"Hello\\n{% debug %}\\nGoodbye\")\n        out = t.render()\n\n        for value in (\"context\", \"cycler\", \"filters\", \"abs\", \"tests\", \"!=\"):\n            assert f\"'{value}'\" in out\n\n\nclass TestInternationalization:\n    def test_trans(self):\n        tmpl = i18n_env.get_template(\"child.html\")\n        assert tmpl.render(LANGUAGE=\"de\") == \"<title>fehlend</title>pass auf\"\n\n    def test_trans_plural(self):\n        tmpl = i18n_env.get_template(\"plural.html\")\n        assert tmpl.render(LANGUAGE=\"de\", user_count=1) == \"Ein Benutzer online\"\n        assert tmpl.render(LANGUAGE=\"de\", user_count=2) == \"2 Benutzer online\"\n\n    def test_trans_plural_with_functions(self):\n        tmpl = i18n_env.get_template(\"plural2.html\")\n\n        def get_user_count():\n            get_user_count.called += 1\n            return 1\n\n        get_user_count.called = 0\n        assert tmpl.render(LANGUAGE=\"de\", get_user_count=get_user_count) == \"1s\"\n        assert get_user_count.called == 1\n\n    def test_complex_plural(self):\n        tmpl = i18n_env.from_string(\n            \"{% trans foo=42, count=2 %}{{ count }} item{% \"\n            \"pluralize count %}{{ count }} items{% endtrans %}\"\n        )\n        assert tmpl.render() == \"2 items\"\n        pytest.raises(\n            TemplateAssertionError,\n            i18n_env.from_string,\n            \"{% trans foo %}...{% pluralize bar %}...{% endtrans %}\",\n        )\n\n    def test_trans_stringformatting(self):\n        tmpl = i18n_env.get_template(\"stringformat.html\")\n        assert tmpl.render(LANGUAGE=\"de\", user_count=5) == \"Benutzer: 5\"\n\n    def test_trimmed(self):\n        tmpl = i18n_env.from_string(\n            \"{%- trans trimmed %}  hello\\n  world  {% endtrans -%}\"\n        )\n        assert tmpl.render() == \"hello world\"\n\n    def test_trimmed_policy(self):\n        s = \"{%- trans %}  hello\\n  world  {% endtrans -%}\"\n        tmpl = i18n_env.from_string(s)\n        trimmed_tmpl = i18n_env_trimmed.from_string(s)\n        assert tmpl.render() == \"  hello\\n  world  \"\n        assert trimmed_tmpl.render() == \"hello world\"\n\n    def test_trimmed_policy_override(self):\n        tmpl = i18n_env_trimmed.from_string(\n            \"{%- trans notrimmed %}  hello\\n  world  {% endtrans -%}\"\n        )\n        assert tmpl.render() == \"  hello\\n  world  \"\n\n    def test_trimmed_vars(self):\n        tmpl = i18n_env.from_string(\n            '{%- trans trimmed x=\"world\" %}  hello\\n  {{ x }} {% endtrans -%}'\n        )\n        assert tmpl.render() == \"hello world\"\n\n    def test_trimmed_varname_trimmed(self):\n        # unlikely variable name, but when used as a variable\n        # it should not enable trimming\n        tmpl = i18n_env.from_string(\n            \"{%- trans trimmed = 'world' %}  hello\\n  {{ trimmed }}  {% endtrans -%}\"\n        )\n        assert tmpl.render() == \"  hello\\n  world  \"\n\n    def test_extract(self):\n        from jinja2.ext import babel_extract\n\n        source = BytesIO(\n            b\"\"\"\n            {{ gettext('Hello World') }}\n            {% trans %}Hello World{% endtrans %}\n            {% trans %}{{ users }} user{% pluralize %}{{ users }} users{% endtrans %}\n            \"\"\"\n        )\n        assert list(babel_extract(source, (\"gettext\", \"ngettext\", \"_\"), [], {})) == [\n            (2, \"gettext\", \"Hello World\", []),\n            (3, \"gettext\", \"Hello World\", []),\n            (4, \"ngettext\", (\"%(users)s user\", \"%(users)s users\", None), []),\n        ]\n\n    def test_extract_trimmed(self):\n        from jinja2.ext import babel_extract\n\n        source = BytesIO(\n            b\"\"\"\n            {{ gettext(' Hello  \\n  World') }}\n            {% trans trimmed %} Hello  \\n  World{% endtrans %}\n            {% trans trimmed %}{{ users }} \\n user\n            {%- pluralize %}{{ users }} \\n users{% endtrans %}\n            \"\"\"\n        )\n        assert list(babel_extract(source, (\"gettext\", \"ngettext\", \"_\"), [], {})) == [\n            (2, \"gettext\", \" Hello  \\n  World\", []),\n            (4, \"gettext\", \"Hello World\", []),\n            (6, \"ngettext\", (\"%(users)s user\", \"%(users)s users\", None), []),\n        ]\n\n    def test_extract_trimmed_option(self):\n        from jinja2.ext import babel_extract\n\n        source = BytesIO(\n            b\"\"\"\n            {{ gettext(' Hello  \\n  World') }}\n            {% trans %} Hello  \\n  World{% endtrans %}\n            {% trans %}{{ users }} \\n user\n            {%- pluralize %}{{ users }} \\n users{% endtrans %}\n            \"\"\"\n        )\n        opts = {\"trimmed\": \"true\"}\n        assert list(babel_extract(source, (\"gettext\", \"ngettext\", \"_\"), [], opts)) == [\n            (2, \"gettext\", \" Hello  \\n  World\", []),\n            (4, \"gettext\", \"Hello World\", []),\n            (6, \"ngettext\", (\"%(users)s user\", \"%(users)s users\", None), []),\n        ]\n\n    def test_comment_extract(self):\n        from jinja2.ext import babel_extract\n\n        source = BytesIO(\n            b\"\"\"\n            {# trans first #}\n            {{ gettext('Hello World') }}\n            {% trans %}Hello World{% endtrans %}{# trans second #}\n            {#: third #}\n            {% trans %}{{ users }} user{% pluralize %}{{ users }} users{% endtrans %}\n            \"\"\"\n        )\n        assert list(\n            babel_extract(source, (\"gettext\", \"ngettext\", \"_\"), [\"trans\", \":\"], {})\n        ) == [\n            (3, \"gettext\", \"Hello World\", [\"first\"]),\n            (4, \"gettext\", \"Hello World\", [\"second\"]),\n            (6, \"ngettext\", (\"%(users)s user\", \"%(users)s users\", None), [\"third\"]),\n        ]\n\n    def test_extract_context(self):\n        from jinja2.ext import babel_extract\n\n        source = BytesIO(\n            b\"\"\"\n             {{ pgettext(\"babel\", \"Hello World\") }}\n             {{ npgettext(\"babel\", \"%(users)s user\", \"%(users)s users\", users) }}\n             \"\"\"\n        )\n        assert list(babel_extract(source, (\"pgettext\", \"npgettext\", \"_\"), [], {})) == [\n            (2, \"pgettext\", (\"babel\", \"Hello World\"), []),\n            (3, \"npgettext\", (\"babel\", \"%(users)s user\", \"%(users)s users\", None), []),\n        ]\n\n    def test_nested_trans_error(self):\n        s = \"{% trans %}foo{% trans %}{% endtrans %}\"\n        with pytest.raises(TemplateSyntaxError) as excinfo:\n            i18n_env.from_string(s)\n        assert \"trans blocks can't be nested\" in str(excinfo.value)\n\n    def test_trans_block_error(self):\n        s = \"{% trans %}foo{% wibble bar %}{% endwibble %}{% endtrans %}\"\n        with pytest.raises(TemplateSyntaxError) as excinfo:\n            i18n_env.from_string(s)\n        assert \"saw `wibble`\" in str(excinfo.value)\n\n\nclass TestScope:\n    def test_basic_scope_behavior(self):\n        # This is what the old with statement compiled down to\n        class ScopeExt(Extension):\n            tags = {\"scope\"}\n\n            def parse(self, parser):\n                node = nodes.Scope(lineno=next(parser.stream).lineno)\n                assignments = []\n                while parser.stream.current.type != \"block_end\":\n                    lineno = parser.stream.current.lineno\n                    if assignments:\n                        parser.stream.expect(\"comma\")\n                    target = parser.parse_assign_target()\n                    parser.stream.expect(\"assign\")\n                    expr = parser.parse_expression()\n                    assignments.append(nodes.Assign(target, expr, lineno=lineno))\n                node.body = assignments + list(\n                    parser.parse_statements((\"name:endscope\",), drop_needle=True)\n                )\n                return node\n\n        env = Environment(extensions=[ScopeExt])\n        tmpl = env.from_string(\n            \"\"\"\\\n        {%- scope a=1, b=2, c=b, d=e, e=5 -%}\n            {{ a }}|{{ b }}|{{ c }}|{{ d }}|{{ e }}\n        {%- endscope -%}\n        \"\"\"\n        )\n        assert tmpl.render(b=3, e=4) == \"1|2|2|4|5\"\n\n\nclass TestNewstyleInternationalization:\n    def test_trans(self):\n        tmpl = newstyle_i18n_env.get_template(\"child.html\")\n        assert tmpl.render(LANGUAGE=\"de\") == \"<title>fehlend</title>pass auf\"\n\n    def test_trans_plural(self):\n        tmpl = newstyle_i18n_env.get_template(\"plural.html\")\n        assert tmpl.render(LANGUAGE=\"de\", user_count=1) == \"Ein Benutzer online\"\n        assert tmpl.render(LANGUAGE=\"de\", user_count=2) == \"2 Benutzer online\"\n\n    def test_complex_plural(self):\n        tmpl = newstyle_i18n_env.from_string(\n            \"{% trans foo=42, count=2 %}{{ count }} item{% \"\n            \"pluralize count %}{{ count }} items{% endtrans %}\"\n        )\n        assert tmpl.render() == \"2 items\"\n        pytest.raises(\n            TemplateAssertionError,\n            i18n_env.from_string,\n            \"{% trans foo %}...{% pluralize bar %}...{% endtrans %}\",\n        )\n\n    def test_trans_stringformatting(self):\n        tmpl = newstyle_i18n_env.get_template(\"stringformat.html\")\n        assert tmpl.render(LANGUAGE=\"de\", user_count=5) == \"Benutzer: 5\"\n\n    def test_newstyle_plural(self):\n        tmpl = newstyle_i18n_env.get_template(\"ngettext.html\")\n        assert tmpl.render(LANGUAGE=\"de\", apples=1) == \"1 Apfel\"\n        assert tmpl.render(LANGUAGE=\"de\", apples=5) == \"5 Äpfel\"\n\n    def test_autoescape_support(self):\n        env = Environment(extensions=[\"jinja2.ext.i18n\"])\n        env.install_gettext_callables(\n            lambda x: \"<strong>Wert: %(name)s</strong>\",\n            lambda s, p, n: s,\n            newstyle=True,\n        )\n        t = env.from_string(\n            '{% autoescape ae %}{{ gettext(\"foo\", name=\"<test>\") }}{% endautoescape %}'\n        )\n        assert t.render(ae=True) == \"<strong>Wert: &lt;test&gt;</strong>\"\n        assert t.render(ae=False) == \"<strong>Wert: <test></strong>\"\n\n    def test_autoescape_macros(self):\n        env = Environment(autoescape=False)\n        template = (\n            \"{% macro m() %}<html>{% endmacro %}\"\n            \"{% autoescape true %}{{ m() }}{% endautoescape %}\"\n        )\n        assert env.from_string(template).render() == \"<html>\"\n\n    def test_num_used_twice(self):\n        tmpl = newstyle_i18n_env.get_template(\"ngettext_long.html\")\n        assert tmpl.render(apples=5, LANGUAGE=\"de\") == \"5 Äpfel\"\n\n    def test_num_called_num(self):\n        source = newstyle_i18n_env.compile(\n            \"\"\"\n            {% trans num=3 %}{{ num }} apple{% pluralize\n            %}{{ num }} apples{% endtrans %}\n        \"\"\",\n            raw=True,\n        )\n        # quite hacky, but the only way to properly test that.  The idea is\n        # that the generated code does not pass num twice (although that\n        # would work) for better performance.  This only works on the\n        # newstyle gettext of course\n        assert (\n            re.search(r\"u?'%\\(num\\)s apple', u?'%\\(num\\)s apples', 3\", source)\n            is not None\n        )\n\n    def test_trans_vars(self):\n        t1 = newstyle_i18n_env.get_template(\"transvars1.html\")\n        t2 = newstyle_i18n_env.get_template(\"transvars2.html\")\n        t3 = newstyle_i18n_env.get_template(\"transvars3.html\")\n        assert t1.render(num=1, LANGUAGE=\"de\") == \"Benutzer: 1\"\n        assert t2.render(count=23, LANGUAGE=\"de\") == \"Benutzer: 23\"\n        assert t3.render(num=42, LANGUAGE=\"de\") == \"Benutzer: 42\"\n\n    def test_novars_vars_escaping(self):\n        t = newstyle_i18n_env.get_template(\"novars.html\")\n        assert t.render() == \"%(hello)s\"\n        t = newstyle_i18n_env.get_template(\"vars.html\")\n        assert t.render(foo=\"42\") == \"42%(foo)s\"\n        t = newstyle_i18n_env.get_template(\"explicitvars.html\")\n        assert t.render() == \"%(foo)s\"\n\n    def test_context(self):\n        tmpl = newstyle_i18n_env.get_template(\"pgettext.html\")\n        assert tmpl.render(LANGUAGE=\"de\") == \"Apple\"\n\n    def test_context_plural(self):\n        tmpl = newstyle_i18n_env.get_template(\"npgettext.html\")\n        assert tmpl.render(LANGUAGE=\"de\", apples=1) == \"1 Apple\"\n        assert tmpl.render(LANGUAGE=\"de\", apples=5) == \"5 Apples\"\n\n    def test_context_block(self):\n        tmpl = newstyle_i18n_env.get_template(\"pgettext_block\")\n        assert tmpl.render(LANGUAGE=\"de\") == \"Apple\"\n\n    def test_context_plural_block(self):\n        tmpl = newstyle_i18n_env.get_template(\"npgettext_block\")\n        assert tmpl.render(LANGUAGE=\"de\", apples=1) == \"1 Apple\"\n        assert tmpl.render(LANGUAGE=\"de\", apples=5) == \"5 Apples\"\n\n\nclass TestAutoEscape:\n    def test_scoped_setting(self):\n        env = Environment(autoescape=True)\n        tmpl = env.from_string(\n            \"\"\"\n            {{ \"<HelloWorld>\" }}\n            {% autoescape false %}\n                {{ \"<HelloWorld>\" }}\n            {% endautoescape %}\n            {{ \"<HelloWorld>\" }}\n        \"\"\"\n        )\n        assert tmpl.render().split() == [\n            \"&lt;HelloWorld&gt;\",\n            \"<HelloWorld>\",\n            \"&lt;HelloWorld&gt;\",\n        ]\n\n        env = Environment(autoescape=False)\n        tmpl = env.from_string(\n            \"\"\"\n            {{ \"<HelloWorld>\" }}\n            {% autoescape true %}\n                {{ \"<HelloWorld>\" }}\n            {% endautoescape %}\n            {{ \"<HelloWorld>\" }}\n        \"\"\"\n        )\n        assert tmpl.render().split() == [\n            \"<HelloWorld>\",\n            \"&lt;HelloWorld&gt;\",\n            \"<HelloWorld>\",\n        ]\n\n    def test_nonvolatile(self):\n        env = Environment(autoescape=True)\n        tmpl = env.from_string('{{ {\"foo\": \"<test>\"}|xmlattr|escape }}')\n        assert tmpl.render() == ' foo=\"&lt;test&gt;\"'\n        tmpl = env.from_string(\n            '{% autoescape false %}{{ {\"foo\": \"<test>\"}'\n            \"|xmlattr|escape }}{% endautoescape %}\"\n        )\n        assert tmpl.render() == \" foo=&#34;&amp;lt;test&amp;gt;&#34;\"\n\n    def test_volatile(self):\n        env = Environment(autoescape=True)\n        tmpl = env.from_string(\n            '{% autoescape foo %}{{ {\"foo\": \"<test>\"}'\n            \"|xmlattr|escape }}{% endautoescape %}\"\n        )\n        assert tmpl.render(foo=False) == \" foo=&#34;&amp;lt;test&amp;gt;&#34;\"\n        assert tmpl.render(foo=True) == ' foo=\"&lt;test&gt;\"'\n\n    def test_scoping(self):\n        env = Environment()\n        tmpl = env.from_string(\n            '{% autoescape true %}{% set x = \"<x>\" %}{{ x }}'\n            '{% endautoescape %}{{ x }}{{ \"<y>\" }}'\n        )\n        assert tmpl.render(x=1) == \"&lt;x&gt;1<y>\"\n\n    def test_volatile_scoping(self):\n        env = Environment()\n        tmplsource = \"\"\"\n        {% autoescape val %}\n            {% macro foo(x) %}\n                [{{ x }}]\n            {% endmacro %}\n            {{ foo().__class__.__name__ }}\n        {% endautoescape %}\n        {{ '<testing>' }}\n        \"\"\"\n        tmpl = env.from_string(tmplsource)\n        assert tmpl.render(val=True).split()[0] == \"Markup\"\n        assert tmpl.render(val=False).split()[0] == \"str\"\n\n        # looking at the source we should see <testing> there in raw\n        # (and then escaped as well)\n        env = Environment()\n        pysource = env.compile(tmplsource, raw=True)\n        assert \"<testing>\\\\n\" in pysource\n\n        env = Environment(autoescape=True)\n        pysource = env.compile(tmplsource, raw=True)\n        assert \"&lt;testing&gt;\\\\n\" in pysource\n\n    def test_overlay_scopes(self):\n        class MagicScopeExtension(Extension):\n            tags = {\"overlay\"}\n\n            def parse(self, parser):\n                node = nodes.OverlayScope(lineno=next(parser.stream).lineno)\n                node.body = list(\n                    parser.parse_statements((\"name:endoverlay\",), drop_needle=True)\n                )\n                node.context = self.call_method(\"get_scope\")\n                return node\n\n            def get_scope(self):\n                return {\"x\": [1, 2, 3]}\n\n        env = Environment(extensions=[MagicScopeExtension])\n\n        tmpl = env.from_string(\n            \"\"\"\n            {{- x }}|{% set z = 99 %}\n            {%- overlay %}\n                {{- y }}|{{ z }}|{% for item in x %}[{{ item }}]{% endfor %}\n            {%- endoverlay %}|\n            {{- x -}}\n        \"\"\"\n        )\n        assert tmpl.render(x=42, y=23) == \"42|23|99|[1][2][3]|42\"\n"
  },
  {
    "path": "tests/test_filters.py",
    "content": "import random\nfrom collections import namedtuple\n\nimport pytest\nfrom markupsafe import Markup\n\nfrom jinja2 import Environment\nfrom jinja2 import StrictUndefined\nfrom jinja2 import TemplateRuntimeError\nfrom jinja2 import UndefinedError\nfrom jinja2.exceptions import TemplateAssertionError\n\n\nclass Magic:\n    def __init__(self, value):\n        self.value = value\n\n    def __str__(self):\n        return str(self.value)\n\n\nclass Magic2:\n    def __init__(self, value1, value2):\n        self.value1 = value1\n        self.value2 = value2\n\n    def __str__(self):\n        return f\"({self.value1},{self.value2})\"\n\n\nclass TestFilter:\n    def test_filter_calling(self, env):\n        rv = env.call_filter(\"sum\", [1, 2, 3])\n        assert rv == 6\n\n    def test_capitalize(self, env):\n        tmpl = env.from_string('{{ \"foo bar\"|capitalize }}')\n        assert tmpl.render() == \"Foo bar\"\n\n    def test_center(self, env):\n        tmpl = env.from_string('{{ \"foo\"|center(9) }}')\n        assert tmpl.render() == \"   foo   \"\n\n    def test_default(self, env):\n        tmpl = env.from_string(\n            \"{{ missing|default('no') }}|{{ false|default('no') }}|\"\n            \"{{ false|default('no', true) }}|{{ given|default('no') }}\"\n        )\n        assert tmpl.render(given=\"yes\") == \"no|False|no|yes\"\n\n    @pytest.mark.parametrize(\n        \"args,expect\",\n        (\n            (\"\", \"[('aa', 0), ('AB', 3), ('b', 1), ('c', 2)]\"),\n            (\"true\", \"[('AB', 3), ('aa', 0), ('b', 1), ('c', 2)]\"),\n            ('by=\"value\"', \"[('aa', 0), ('b', 1), ('c', 2), ('AB', 3)]\"),\n            (\"reverse=true\", \"[('c', 2), ('b', 1), ('AB', 3), ('aa', 0)]\"),\n        ),\n    )\n    def test_dictsort(self, env, args, expect):\n        t = env.from_string(f\"{{{{ foo|dictsort({args}) }}}}\")\n        out = t.render(foo={\"aa\": 0, \"b\": 1, \"c\": 2, \"AB\": 3})\n        assert out == expect\n\n    def test_batch(self, env):\n        tmpl = env.from_string(\"{{ foo|batch(3)|list }}|{{ foo|batch(3, 'X')|list }}\")\n        out = tmpl.render(foo=list(range(10)))\n        assert out == (\n            \"[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]|\"\n            \"[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 'X', 'X']]\"\n        )\n\n    def test_slice(self, env):\n        tmpl = env.from_string(\"{{ foo|slice(3)|list }}|{{ foo|slice(3, 'X')|list }}\")\n        out = tmpl.render(foo=list(range(10)))\n        assert out == (\n            \"[[0, 1, 2, 3], [4, 5, 6], [7, 8, 9]]|\"\n            \"[[0, 1, 2, 3], [4, 5, 6, 'X'], [7, 8, 9, 'X']]\"\n        )\n\n    def test_escape(self, env):\n        tmpl = env.from_string(\"\"\"{{ '<\">&'|escape }}\"\"\")\n        out = tmpl.render()\n        assert out == \"&lt;&#34;&gt;&amp;\"\n\n    @pytest.mark.parametrize(\n        (\"chars\", \"expect\"), [(None, \"..stays..\"), (\".\", \"  ..stays\"), (\" .\", \"stays\")]\n    )\n    def test_trim(self, env, chars, expect):\n        tmpl = env.from_string(\"{{ foo|trim(chars) }}\")\n        out = tmpl.render(foo=\"  ..stays..\", chars=chars)\n        assert out == expect\n\n    def test_striptags(self, env):\n        tmpl = env.from_string(\"\"\"{{ foo|striptags }}\"\"\")\n        out = tmpl.render(\n            foo='  <p>just a small   \\n <a href=\"#\">'\n            \"example</a> link</p>\\n<p>to a webpage</p> \"\n            \"<!-- <p>and some commented stuff</p> -->\"\n        )\n        assert out == \"just a small example link to a webpage\"\n\n    def test_filesizeformat(self, env):\n        tmpl = env.from_string(\n            \"{{ 100|filesizeformat }}|\"\n            \"{{ 1000|filesizeformat }}|\"\n            \"{{ 1000000|filesizeformat }}|\"\n            \"{{ 1000000000|filesizeformat }}|\"\n            \"{{ 1000000000000|filesizeformat }}|\"\n            \"{{ 100|filesizeformat(true) }}|\"\n            \"{{ 1000|filesizeformat(true) }}|\"\n            \"{{ 1000000|filesizeformat(true) }}|\"\n            \"{{ 1000000000|filesizeformat(true) }}|\"\n            \"{{ 1000000000000|filesizeformat(true) }}\"\n        )\n        out = tmpl.render()\n        assert out == (\n            \"100 Bytes|1.0 kB|1.0 MB|1.0 GB|1.0 TB|100 Bytes|\"\n            \"1000 Bytes|976.6 KiB|953.7 MiB|931.3 GiB\"\n        )\n\n    def test_filesizeformat_issue59(self, env):\n        tmpl = env.from_string(\n            \"{{ 300|filesizeformat }}|\"\n            \"{{ 3000|filesizeformat }}|\"\n            \"{{ 3000000|filesizeformat }}|\"\n            \"{{ 3000000000|filesizeformat }}|\"\n            \"{{ 3000000000000|filesizeformat }}|\"\n            \"{{ 300|filesizeformat(true) }}|\"\n            \"{{ 3000|filesizeformat(true) }}|\"\n            \"{{ 3000000|filesizeformat(true) }}\"\n        )\n        out = tmpl.render()\n        assert out == (\n            \"300 Bytes|3.0 kB|3.0 MB|3.0 GB|3.0 TB|300 Bytes|2.9 KiB|2.9 MiB\"\n        )\n\n    def test_first(self, env):\n        tmpl = env.from_string(\"{{ foo|first }}\")\n        out = tmpl.render(foo=list(range(10)))\n        assert out == \"0\"\n\n    @pytest.mark.parametrize(\n        (\"value\", \"expect\"), ((\"42\", \"42.0\"), (\"abc\", \"0.0\"), (\"32.32\", \"32.32\"))\n    )\n    def test_float(self, env, value, expect):\n        t = env.from_string(\"{{ value|float }}\")\n        assert t.render(value=value) == expect\n\n    def test_float_default(self, env):\n        t = env.from_string(\"{{ value|float(default=1.0) }}\")\n        assert t.render(value=\"abc\") == \"1.0\"\n\n    def test_format(self, env):\n        tmpl = env.from_string(\"{{ '%s|%s'|format('a', 'b') }}\")\n        out = tmpl.render()\n        assert out == \"a|b\"\n\n    @staticmethod\n    def _test_indent_multiline_template(env, markup=False):\n        text = \"\\n\".join([\"\", \"foo bar\", '\"baz\"', \"\"])\n        if markup:\n            text = Markup(text)\n        t = env.from_string(\"{{ foo|indent(2, false, false) }}\")\n        assert t.render(foo=text) == '\\n  foo bar\\n  \"baz\"\\n'\n        t = env.from_string(\"{{ foo|indent(2, false, true) }}\")\n        assert t.render(foo=text) == '\\n  foo bar\\n  \"baz\"\\n  '\n        t = env.from_string(\"{{ foo|indent(2, true, false) }}\")\n        assert t.render(foo=text) == '  \\n  foo bar\\n  \"baz\"\\n'\n        t = env.from_string(\"{{ foo|indent(2, true, true) }}\")\n        assert t.render(foo=text) == '  \\n  foo bar\\n  \"baz\"\\n  '\n\n    def test_indent(self, env):\n        self._test_indent_multiline_template(env)\n        t = env.from_string('{{ \"jinja\"|indent }}')\n        assert t.render() == \"jinja\"\n        t = env.from_string('{{ \"jinja\"|indent(first=true) }}')\n        assert t.render() == \"    jinja\"\n        t = env.from_string('{{ \"jinja\"|indent(blank=true) }}')\n        assert t.render() == \"jinja\"\n\n    def test_indent_markup_input(self, env):\n        \"\"\"\n        Tests cases where the filter input is a Markup type\n        \"\"\"\n        self._test_indent_multiline_template(env, markup=True)\n\n    def test_indent_width_string(self, env):\n        t = env.from_string(\"{{ 'jinja\\nflask'|indent(width='>>> ', first=True) }}\")\n        assert t.render() == \">>> jinja\\n>>> flask\"\n\n    @pytest.mark.parametrize(\n        (\"value\", \"expect\"),\n        (\n            (\"42\", \"42\"),\n            (\"abc\", \"0\"),\n            (\"32.32\", \"32\"),\n            (\"12345678901234567890\", \"12345678901234567890\"),\n            (\"1e10000\", \"0\"),\n        ),\n    )\n    def test_int(self, env, value, expect):\n        t = env.from_string(\"{{ value|int }}\")\n        assert t.render(value=value) == expect\n\n    @pytest.mark.parametrize(\n        (\"value\", \"base\", \"expect\"),\n        ((\"0x4d32\", 16, \"19762\"), (\"011\", 8, \"9\"), (\"0x33Z\", 16, \"0\")),\n    )\n    def test_int_base(self, env, value, base, expect):\n        t = env.from_string(\"{{ value|int(base=base) }}\")\n        assert t.render(value=value, base=base) == expect\n\n    def test_int_default(self, env):\n        t = env.from_string(\"{{ value|int(default=1) }}\")\n        assert t.render(value=\"abc\") == \"1\"\n\n    def test_int_special_method(self, env):\n        class IntIsh:\n            def __int__(self):\n                return 42\n\n        t = env.from_string(\"{{ value|int }}\")\n        assert t.render(value=IntIsh()) == \"42\"\n\n    def test_join(self, env):\n        tmpl = env.from_string('{{ [1, 2, 3]|join(\"|\") }}')\n        out = tmpl.render()\n        assert out == \"1|2|3\"\n\n        env2 = Environment(autoescape=True)\n        tmpl = env2.from_string('{{ [\"<foo>\", \"<span>foo</span>\"|safe]|join }}')\n        assert tmpl.render() == \"&lt;foo&gt;<span>foo</span>\"\n\n    def test_join_attribute(self, env):\n        User = namedtuple(\"User\", \"username\")\n        tmpl = env.from_string(\"\"\"{{ users|join(', ', 'username') }}\"\"\")\n        assert tmpl.render(users=map(User, [\"foo\", \"bar\"])) == \"foo, bar\"\n\n    def test_last(self, env):\n        tmpl = env.from_string(\"\"\"{{ foo|last }}\"\"\")\n        out = tmpl.render(foo=list(range(10)))\n        assert out == \"9\"\n\n    def test_length(self, env):\n        tmpl = env.from_string(\"\"\"{{ \"hello world\"|length }}\"\"\")\n        out = tmpl.render()\n        assert out == \"11\"\n\n    def test_lower(self, env):\n        tmpl = env.from_string(\"\"\"{{ \"FOO\"|lower }}\"\"\")\n        out = tmpl.render()\n        assert out == \"foo\"\n\n    def test_items(self, env):\n        d = {i: c for i, c in enumerate(\"abc\")}\n        tmpl = env.from_string(\"\"\"{{ d|items|list }}\"\"\")\n        out = tmpl.render(d=d)\n        assert out == \"[(0, 'a'), (1, 'b'), (2, 'c')]\"\n\n    def test_items_undefined(self, env):\n        tmpl = env.from_string(\"\"\"{{ d|items|list }}\"\"\")\n        out = tmpl.render()\n        assert out == \"[]\"\n\n    def test_pprint(self, env):\n        from pprint import pformat\n\n        tmpl = env.from_string(\"\"\"{{ data|pprint }}\"\"\")\n        data = list(range(1000))\n        assert tmpl.render(data=data) == pformat(data)\n\n    def test_random(self, env, request):\n        # restore the random state when the test ends\n        state = random.getstate()\n        request.addfinalizer(lambda: random.setstate(state))\n        # generate the random values from a known seed\n        random.seed(\"jinja\")\n        expected = [random.choice(\"1234567890\") for _ in range(10)]\n\n        # check that the random sequence is generated again by a template\n        # ensures that filter result is not constant folded\n        random.seed(\"jinja\")\n        t = env.from_string('{{ \"1234567890\"|random }}')\n\n        for value in expected:\n            assert t.render() == value\n\n    def test_reverse(self, env):\n        tmpl = env.from_string(\n            \"{{ 'foobar'|reverse|join }}|{{ [1, 2, 3]|reverse|list }}\"\n        )\n        assert tmpl.render() == \"raboof|[3, 2, 1]\"\n\n    def test_string(self, env):\n        x = [1, 2, 3, 4, 5]\n        tmpl = env.from_string(\"\"\"{{ obj|string }}\"\"\")\n        assert tmpl.render(obj=x) == str(x)\n\n    def test_title(self, env):\n        tmpl = env.from_string(\"\"\"{{ \"foo bar\"|title }}\"\"\")\n        assert tmpl.render() == \"Foo Bar\"\n        tmpl = env.from_string(\"\"\"{{ \"foo's bar\"|title }}\"\"\")\n        assert tmpl.render() == \"Foo's Bar\"\n        tmpl = env.from_string(\"\"\"{{ \"foo   bar\"|title }}\"\"\")\n        assert tmpl.render() == \"Foo   Bar\"\n        tmpl = env.from_string(\"\"\"{{ \"f bar f\"|title }}\"\"\")\n        assert tmpl.render() == \"F Bar F\"\n        tmpl = env.from_string(\"\"\"{{ \"foo-bar\"|title }}\"\"\")\n        assert tmpl.render() == \"Foo-Bar\"\n        tmpl = env.from_string(\"\"\"{{ \"foo\\tbar\"|title }}\"\"\")\n        assert tmpl.render() == \"Foo\\tBar\"\n        tmpl = env.from_string(\"\"\"{{ \"FOO\\tBAR\"|title }}\"\"\")\n        assert tmpl.render() == \"Foo\\tBar\"\n        tmpl = env.from_string(\"\"\"{{ \"foo (bar)\"|title }}\"\"\")\n        assert tmpl.render() == \"Foo (Bar)\"\n        tmpl = env.from_string(\"\"\"{{ \"foo {bar}\"|title }}\"\"\")\n        assert tmpl.render() == \"Foo {Bar}\"\n        tmpl = env.from_string(\"\"\"{{ \"foo [bar]\"|title }}\"\"\")\n        assert tmpl.render() == \"Foo [Bar]\"\n        tmpl = env.from_string(\"\"\"{{ \"foo <bar>\"|title }}\"\"\")\n        assert tmpl.render() == \"Foo <Bar>\"\n\n        class Foo:\n            def __str__(self):\n                return \"foo-bar\"\n\n        tmpl = env.from_string(\"\"\"{{ data|title }}\"\"\")\n        out = tmpl.render(data=Foo())\n        assert out == \"Foo-Bar\"\n\n    def test_truncate(self, env):\n        tmpl = env.from_string(\n            '{{ data|truncate(15, true, \">>>\") }}|'\n            '{{ data|truncate(15, false, \">>>\") }}|'\n            \"{{ smalldata|truncate(15) }}\"\n        )\n        out = tmpl.render(data=\"foobar baz bar\" * 1000, smalldata=\"foobar baz bar\")\n        assert out == \"foobar baz b>>>|foobar baz>>>|foobar baz bar\"\n\n    def test_truncate_very_short(self, env):\n        tmpl = env.from_string(\n            '{{ \"foo bar baz\"|truncate(9) }}|{{ \"foo bar baz\"|truncate(9, true) }}'\n        )\n        out = tmpl.render()\n        assert out == \"foo bar baz|foo bar baz\"\n\n    def test_truncate_end_length(self, env):\n        tmpl = env.from_string('{{ \"Joel is a slug\"|truncate(7, true) }}')\n        out = tmpl.render()\n        assert out == \"Joel...\"\n\n    def test_upper(self, env):\n        tmpl = env.from_string('{{ \"foo\"|upper }}')\n        assert tmpl.render() == \"FOO\"\n\n    def test_urlize(self, env):\n        tmpl = env.from_string('{{ \"foo example.org bar\"|urlize }}')\n        assert tmpl.render() == (\n            'foo <a href=\"https://example.org\" rel=\"noopener\">example.org</a> bar'\n        )\n        tmpl = env.from_string('{{ \"foo http://www.example.com/ bar\"|urlize }}')\n        assert tmpl.render() == (\n            'foo <a href=\"http://www.example.com/\" rel=\"noopener\">'\n            \"http://www.example.com/</a> bar\"\n        )\n        tmpl = env.from_string('{{ \"foo mailto:email@example.com bar\"|urlize }}')\n        assert tmpl.render() == (\n            'foo <a href=\"mailto:email@example.com\">email@example.com</a> bar'\n        )\n        tmpl = env.from_string('{{ \"foo email@example.com bar\"|urlize }}')\n        assert tmpl.render() == (\n            'foo <a href=\"mailto:email@example.com\">email@example.com</a> bar'\n        )\n\n    def test_urlize_rel_policy(self):\n        env = Environment()\n        env.policies[\"urlize.rel\"] = None\n        tmpl = env.from_string('{{ \"foo http://www.example.com/ bar\"|urlize }}')\n        assert tmpl.render() == (\n            'foo <a href=\"http://www.example.com/\">http://www.example.com/</a> bar'\n        )\n\n    def test_urlize_target_parameter(self, env):\n        tmpl = env.from_string(\n            '{{ \"foo http://www.example.com/ bar\"|urlize(target=\"_blank\") }}'\n        )\n        assert (\n            tmpl.render()\n            == 'foo <a href=\"http://www.example.com/\" rel=\"noopener\" target=\"_blank\">'\n            \"http://www.example.com/</a> bar\"\n        )\n\n    def test_urlize_extra_schemes_parameter(self, env):\n        tmpl = env.from_string(\n            '{{ \"foo tel:+1-514-555-1234 ftp://localhost bar\"|'\n            'urlize(extra_schemes=[\"tel:\", \"ftp:\"]) }}'\n        )\n        assert tmpl.render() == (\n            'foo <a href=\"tel:+1-514-555-1234\" rel=\"noopener\">'\n            'tel:+1-514-555-1234</a> <a href=\"ftp://localhost\" rel=\"noopener\">'\n            \"ftp://localhost</a> bar\"\n        )\n\n    def test_wordcount(self, env):\n        tmpl = env.from_string('{{ \"foo bar baz\"|wordcount }}')\n        assert tmpl.render() == \"3\"\n\n        strict_env = Environment(undefined=StrictUndefined)\n        t = strict_env.from_string(\"{{ s|wordcount }}\")\n        with pytest.raises(UndefinedError):\n            t.render()\n\n    def test_block(self, env):\n        tmpl = env.from_string(\"{% filter lower|escape %}<HEHE>{% endfilter %}\")\n        assert tmpl.render() == \"&lt;hehe&gt;\"\n\n    def test_chaining(self, env):\n        tmpl = env.from_string(\"\"\"{{ ['<foo>', '<bar>']|first|upper|escape }}\"\"\")\n        assert tmpl.render() == \"&lt;FOO&gt;\"\n\n    def test_sum(self, env):\n        tmpl = env.from_string(\"\"\"{{ [1, 2, 3, 4, 5, 6]|sum }}\"\"\")\n        assert tmpl.render() == \"21\"\n\n    def test_sum_attributes(self, env):\n        tmpl = env.from_string(\"\"\"{{ values|sum('value') }}\"\"\")\n        assert tmpl.render(values=[{\"value\": 23}, {\"value\": 1}, {\"value\": 18}]) == \"42\"\n\n    def test_sum_attributes_nested(self, env):\n        tmpl = env.from_string(\"\"\"{{ values|sum('real.value') }}\"\"\")\n        assert (\n            tmpl.render(\n                values=[\n                    {\"real\": {\"value\": 23}},\n                    {\"real\": {\"value\": 1}},\n                    {\"real\": {\"value\": 18}},\n                ]\n            )\n            == \"42\"\n        )\n\n    def test_sum_attributes_tuple(self, env):\n        tmpl = env.from_string(\"\"\"{{ values.items()|sum('1') }}\"\"\")\n        assert tmpl.render(values={\"foo\": 23, \"bar\": 1, \"baz\": 18}) == \"42\"\n\n    def test_abs(self, env):\n        tmpl = env.from_string(\"\"\"{{ -1|abs }}|{{ 1|abs }}\"\"\")\n        assert tmpl.render() == \"1|1\", tmpl.render()\n\n    def test_round_positive(self, env):\n        tmpl = env.from_string(\n            \"{{ 2.7|round }}|{{ 2.1|round }}|\"\n            \"{{ 2.1234|round(3, 'floor') }}|\"\n            \"{{ 2.1|round(0, 'ceil') }}\"\n        )\n        assert tmpl.render() == \"3.0|2.0|2.123|3.0\", tmpl.render()\n\n    def test_round_negative(self, env):\n        tmpl = env.from_string(\n            \"{{ 21.3|round(-1)}}|\"\n            \"{{ 21.3|round(-1, 'ceil')}}|\"\n            \"{{ 21.3|round(-1, 'floor')}}\"\n        )\n        assert tmpl.render() == \"20.0|30.0|20.0\", tmpl.render()\n\n    def test_xmlattr(self, env):\n        tmpl = env.from_string(\n            \"{{ {'foo': 42, 'bar': 23, 'fish': none, \"\n            \"'spam': missing, 'blub:blub': '<?>'}|xmlattr }}\"\n        )\n        out = tmpl.render().split()\n        assert len(out) == 3\n        assert 'foo=\"42\"' in out\n        assert 'bar=\"23\"' in out\n        assert 'blub:blub=\"&lt;?&gt;\"' in out\n\n    @pytest.mark.parametrize(\"sep\", (\"\\t\", \"\\n\", \"\\f\", \" \", \"/\", \">\", \"=\"))\n    def test_xmlattr_key_invalid(self, env: Environment, sep: str) -> None:\n        with pytest.raises(ValueError, match=\"Invalid character\"):\n            env.from_string(\"{{ {key: 'my_class'}|xmlattr }}\").render(\n                key=f\"class{sep}onclick=alert(1)\"\n            )\n\n    def test_sort1(self, env):\n        tmpl = env.from_string(\"{{ [2, 3, 1]|sort }}|{{ [2, 3, 1]|sort(true) }}\")\n        assert tmpl.render() == \"[1, 2, 3]|[3, 2, 1]\"\n\n    def test_sort2(self, env):\n        tmpl = env.from_string('{{ \"\".join([\"c\", \"A\", \"b\", \"D\"]|sort) }}')\n        assert tmpl.render() == \"AbcD\"\n\n    def test_sort3(self, env):\n        tmpl = env.from_string(\"\"\"{{ ['foo', 'Bar', 'blah']|sort }}\"\"\")\n        assert tmpl.render() == \"['Bar', 'blah', 'foo']\"\n\n    def test_sort4(self, env):\n        tmpl = env.from_string(\"\"\"{{ items|sort(attribute='value')|join }}\"\"\")\n        assert tmpl.render(items=map(Magic, [3, 2, 4, 1])) == \"1234\"\n\n    def test_sort5(self, env):\n        tmpl = env.from_string(\"\"\"{{ items|sort(attribute='value.0')|join }}\"\"\")\n        assert tmpl.render(items=map(Magic, [[3], [2], [4], [1]])) == \"[1][2][3][4]\"\n\n    def test_sort6(self, env):\n        tmpl = env.from_string(\"\"\"{{ items|sort(attribute='value1,value2')|join }}\"\"\")\n        assert (\n            tmpl.render(\n                items=map(\n                    lambda x: Magic2(x[0], x[1]), [(3, 1), (2, 2), (2, 1), (2, 5)]\n                )\n            )\n            == \"(2,1)(2,2)(2,5)(3,1)\"\n        )\n\n    def test_sort7(self, env):\n        tmpl = env.from_string(\"\"\"{{ items|sort(attribute='value2,value1')|join }}\"\"\")\n        assert (\n            tmpl.render(\n                items=map(\n                    lambda x: Magic2(x[0], x[1]), [(3, 1), (2, 2), (2, 1), (2, 5)]\n                )\n            )\n            == \"(2,1)(3,1)(2,2)(2,5)\"\n        )\n\n    def test_sort8(self, env):\n        tmpl = env.from_string(\n            \"\"\"{{ items|sort(attribute='value1.0,value2.0')|join }}\"\"\"\n        )\n        assert (\n            tmpl.render(\n                items=map(\n                    lambda x: Magic2(x[0], x[1]),\n                    [([3], [1]), ([2], [2]), ([2], [1]), ([2], [5])],\n                )\n            )\n            == \"([2],[1])([2],[2])([2],[5])([3],[1])\"\n        )\n\n    def test_unique(self, env):\n        t = env.from_string('{{ \"\".join([\"b\", \"A\", \"a\", \"b\"]|unique) }}')\n        assert t.render() == \"bA\"\n\n    def test_unique_case_sensitive(self, env):\n        t = env.from_string('{{ \"\".join([\"b\", \"A\", \"a\", \"b\"]|unique(true)) }}')\n        assert t.render() == \"bAa\"\n\n    def test_unique_attribute(self, env):\n        t = env.from_string(\"{{ items|unique(attribute='value')|join }}\")\n        assert t.render(items=map(Magic, [3, 2, 4, 1, 2])) == \"3241\"\n\n    @pytest.mark.parametrize(\n        \"source,expect\",\n        (\n            ('{{ [\"a\", \"B\"]|min }}', \"a\"),\n            ('{{ [\"a\", \"B\"]|min(case_sensitive=true) }}', \"B\"),\n            (\"{{ []|min }}\", \"\"),\n            ('{{ [\"a\", \"B\"]|max }}', \"B\"),\n            ('{{ [\"a\", \"B\"]|max(case_sensitive=true) }}', \"a\"),\n            (\"{{ []|max }}\", \"\"),\n        ),\n    )\n    def test_min_max(self, env, source, expect):\n        t = env.from_string(source)\n        assert t.render() == expect\n\n    @pytest.mark.parametrize((\"name\", \"expect\"), [(\"min\", \"1\"), (\"max\", \"9\")])\n    def test_min_max_attribute(self, env, name, expect):\n        t = env.from_string(\"{{ items|\" + name + '(attribute=\"value\") }}')\n        assert t.render(items=map(Magic, [5, 1, 9])) == expect\n\n    def test_groupby(self, env):\n        tmpl = env.from_string(\n            \"\"\"\n        {%- for grouper, list in [{'foo': 1, 'bar': 2},\n                                  {'foo': 2, 'bar': 3},\n                                  {'foo': 1, 'bar': 1},\n                                  {'foo': 3, 'bar': 4}]|groupby('foo') -%}\n            {{ grouper }}{% for x in list %}: {{ x.foo }}, {{ x.bar }}{% endfor %}|\n        {%- endfor %}\"\"\"\n        )\n        assert tmpl.render().split(\"|\") == [\"1: 1, 2: 1, 1\", \"2: 2, 3\", \"3: 3, 4\", \"\"]\n\n    def test_groupby_tuple_index(self, env):\n        tmpl = env.from_string(\n            \"\"\"\n        {%- for grouper, list in [('a', 1), ('a', 2), ('b', 1)]|groupby(0) -%}\n            {{ grouper }}{% for x in list %}:{{ x.1 }}{% endfor %}|\n        {%- endfor %}\"\"\"\n        )\n        assert tmpl.render() == \"a:1:2|b:1|\"\n\n    def test_groupby_multidot(self, env):\n        Date = namedtuple(\"Date\", \"day,month,year\")\n        Article = namedtuple(\"Article\", \"title,date\")\n        articles = [\n            Article(\"aha\", Date(1, 1, 1970)),\n            Article(\"interesting\", Date(2, 1, 1970)),\n            Article(\"really?\", Date(3, 1, 1970)),\n            Article(\"totally not\", Date(1, 1, 1971)),\n        ]\n        tmpl = env.from_string(\n            \"\"\"\n        {%- for year, list in articles|groupby('date.year') -%}\n            {{ year }}{% for x in list %}[{{ x.title }}]{% endfor %}|\n        {%- endfor %}\"\"\"\n        )\n        assert tmpl.render(articles=articles).split(\"|\") == [\n            \"1970[aha][interesting][really?]\",\n            \"1971[totally not]\",\n            \"\",\n        ]\n\n    def test_groupby_default(self, env):\n        tmpl = env.from_string(\n            \"{% for city, items in users|groupby('city', default='NY') %}\"\n            \"{{ city }}: {{ items|map(attribute='name')|join(', ') }}\\n\"\n            \"{% endfor %}\"\n        )\n        out = tmpl.render(\n            users=[\n                {\"name\": \"emma\", \"city\": \"NY\"},\n                {\"name\": \"smith\", \"city\": \"WA\"},\n                {\"name\": \"john\"},\n            ]\n        )\n        assert out == \"NY: emma, john\\nWA: smith\\n\"\n\n    @pytest.mark.parametrize(\n        (\"case_sensitive\", \"expect\"),\n        [\n            (False, \"a: 1, 3\\nb: 2\\n\"),\n            (True, \"A: 3\\na: 1\\nb: 2\\n\"),\n        ],\n    )\n    def test_groupby_case(self, env, case_sensitive, expect):\n        tmpl = env.from_string(\n            \"{% for k, vs in data|groupby('k', case_sensitive=cs) %}\"\n            \"{{ k }}: {{ vs|join(', ', attribute='v') }}\\n\"\n            \"{% endfor %}\"\n        )\n        out = tmpl.render(\n            data=[{\"k\": \"a\", \"v\": 1}, {\"k\": \"b\", \"v\": 2}, {\"k\": \"A\", \"v\": 3}],\n            cs=case_sensitive,\n        )\n        assert out == expect\n\n    def test_filtertag(self, env):\n        tmpl = env.from_string(\n            \"{% filter upper|replace('FOO', 'foo') %}foobar{% endfilter %}\"\n        )\n        assert tmpl.render() == \"fooBAR\"\n\n    def test_replace(self, env):\n        env = Environment()\n        tmpl = env.from_string('{{ string|replace(\"o\", 42) }}')\n        assert tmpl.render(string=\"<foo>\") == \"<f4242>\"\n        env = Environment(autoescape=True)\n        tmpl = env.from_string('{{ string|replace(\"o\", 42) }}')\n        assert tmpl.render(string=\"<foo>\") == \"&lt;f4242&gt;\"\n        tmpl = env.from_string('{{ string|replace(\"<\", 42) }}')\n        assert tmpl.render(string=\"<foo>\") == \"42foo&gt;\"\n        tmpl = env.from_string('{{ string|replace(\"o\", \">x<\") }}')\n        assert tmpl.render(string=Markup(\"foo\")) == \"f&gt;x&lt;&gt;x&lt;\"\n\n    def test_forceescape(self, env):\n        tmpl = env.from_string(\"{{ x|forceescape }}\")\n        assert tmpl.render(x=Markup(\"<div />\")) == \"&lt;div /&gt;\"\n\n    def test_safe(self, env):\n        env = Environment(autoescape=True)\n        tmpl = env.from_string('{{ \"<div>foo</div>\"|safe }}')\n        assert tmpl.render() == \"<div>foo</div>\"\n        tmpl = env.from_string('{{ \"<div>foo</div>\" }}')\n        assert tmpl.render() == \"&lt;div&gt;foo&lt;/div&gt;\"\n\n    @pytest.mark.parametrize(\n        (\"value\", \"expect\"),\n        [\n            (\"Hello, world!\", \"Hello%2C%20world%21\"),\n            (\"Hello, world\\u203d\", \"Hello%2C%20world%E2%80%BD\"),\n            ({\"f\": 1}, \"f=1\"),\n            ([(\"f\", 1), (\"z\", 2)], \"f=1&amp;z=2\"),\n            ({\"\\u203d\": 1}, \"%E2%80%BD=1\"),\n            ({0: 1}, \"0=1\"),\n            ([(\"a b/c\", \"a b/c\")], \"a+b%2Fc=a+b%2Fc\"),\n            (\"a b/c\", \"a%20b/c\"),\n        ],\n    )\n    def test_urlencode(self, value, expect):\n        e = Environment(autoescape=True)\n        t = e.from_string(\"{{ value|urlencode }}\")\n        assert t.render(value=value) == expect\n\n    def test_simple_map(self, env):\n        env = Environment()\n        tmpl = env.from_string('{{ [\"1\", \"2\", \"3\"]|map(\"int\")|sum }}')\n        assert tmpl.render() == \"6\"\n\n    def test_map_sum(self, env):\n        tmpl = env.from_string('{{ [[1,2], [3], [4,5,6]]|map(\"sum\")|list }}')\n        assert tmpl.render() == \"[3, 3, 15]\"\n\n    def test_attribute_map(self, env):\n        User = namedtuple(\"User\", \"name\")\n        env = Environment()\n        users = [\n            User(\"john\"),\n            User(\"jane\"),\n            User(\"mike\"),\n        ]\n        tmpl = env.from_string('{{ users|map(attribute=\"name\")|join(\"|\") }}')\n        assert tmpl.render(users=users) == \"john|jane|mike\"\n\n    def test_empty_map(self, env):\n        env = Environment()\n        tmpl = env.from_string('{{ none|map(\"upper\")|list }}')\n        assert tmpl.render() == \"[]\"\n\n    def test_map_default(self, env):\n        Fullname = namedtuple(\"Fullname\", \"firstname,lastname\")\n        Firstname = namedtuple(\"Firstname\", \"firstname\")\n        env = Environment()\n        tmpl = env.from_string(\n            '{{ users|map(attribute=\"lastname\", default=\"smith\")|join(\", \") }}'\n        )\n        test_list = env.from_string(\n            '{{ users|map(attribute=\"lastname\", default=[\"smith\",\"x\"])|join(\", \") }}'\n        )\n        test_str = env.from_string(\n            '{{ users|map(attribute=\"lastname\", default=\"\")|join(\", \") }}'\n        )\n        users = [\n            Fullname(\"john\", \"lennon\"),\n            Fullname(\"jane\", \"edwards\"),\n            Fullname(\"jon\", None),\n            Firstname(\"mike\"),\n        ]\n        assert tmpl.render(users=users) == \"lennon, edwards, None, smith\"\n        assert test_list.render(users=users) == \"lennon, edwards, None, ['smith', 'x']\"\n        assert test_str.render(users=users) == \"lennon, edwards, None, \"\n\n    def test_simple_select(self, env):\n        env = Environment()\n        tmpl = env.from_string('{{ [1, 2, 3, 4, 5]|select(\"odd\")|join(\"|\") }}')\n        assert tmpl.render() == \"1|3|5\"\n\n    def test_bool_select(self, env):\n        env = Environment()\n        tmpl = env.from_string('{{ [none, false, 0, 1, 2, 3, 4, 5]|select|join(\"|\") }}')\n        assert tmpl.render() == \"1|2|3|4|5\"\n\n    def test_simple_reject(self, env):\n        env = Environment()\n        tmpl = env.from_string('{{ [1, 2, 3, 4, 5]|reject(\"odd\")|join(\"|\") }}')\n        assert tmpl.render() == \"2|4\"\n\n    def test_bool_reject(self, env):\n        env = Environment()\n        tmpl = env.from_string('{{ [none, false, 0, 1, 2, 3, 4, 5]|reject|join(\"|\") }}')\n        assert tmpl.render() == \"None|False|0\"\n\n    def test_simple_select_attr(self, env):\n        User = namedtuple(\"User\", \"name,is_active\")\n        env = Environment()\n        users = [\n            User(\"john\", True),\n            User(\"jane\", True),\n            User(\"mike\", False),\n        ]\n        tmpl = env.from_string(\n            '{{ users|selectattr(\"is_active\")|map(attribute=\"name\")|join(\"|\") }}'\n        )\n        assert tmpl.render(users=users) == \"john|jane\"\n\n    def test_simple_reject_attr(self, env):\n        User = namedtuple(\"User\", \"name,is_active\")\n        env = Environment()\n        users = [\n            User(\"john\", True),\n            User(\"jane\", True),\n            User(\"mike\", False),\n        ]\n        tmpl = env.from_string(\n            '{{ users|rejectattr(\"is_active\")|map(attribute=\"name\")|join(\"|\") }}'\n        )\n        assert tmpl.render(users=users) == \"mike\"\n\n    def test_func_select_attr(self, env):\n        User = namedtuple(\"User\", \"id,name\")\n        env = Environment()\n        users = [\n            User(1, \"john\"),\n            User(2, \"jane\"),\n            User(3, \"mike\"),\n        ]\n        tmpl = env.from_string(\n            '{{ users|selectattr(\"id\", \"odd\")|map(attribute=\"name\")|join(\"|\") }}'\n        )\n        assert tmpl.render(users=users) == \"john|mike\"\n\n    def test_func_reject_attr(self, env):\n        User = namedtuple(\"User\", \"id,name\")\n        env = Environment()\n        users = [\n            User(1, \"john\"),\n            User(2, \"jane\"),\n            User(3, \"mike\"),\n        ]\n        tmpl = env.from_string(\n            '{{ users|rejectattr(\"id\", \"odd\")|map(attribute=\"name\")|join(\"|\") }}'\n        )\n        assert tmpl.render(users=users) == \"jane\"\n\n    def test_json_dump(self):\n        env = Environment(autoescape=True)\n        t = env.from_string(\"{{ x|tojson }}\")\n        assert t.render(x={\"foo\": \"bar\"}) == '{\"foo\": \"bar\"}'\n        assert t.render(x=\"\\\"ba&r'\") == r'\"\\\"ba\\u0026r\\u0027\"'\n        assert t.render(x=\"<bar>\") == r'\"\\u003cbar\\u003e\"'\n\n        def my_dumps(value, **options):\n            assert options == {\"foo\": \"bar\"}\n            return \"42\"\n\n        env.policies[\"json.dumps_function\"] = my_dumps\n        env.policies[\"json.dumps_kwargs\"] = {\"foo\": \"bar\"}\n        assert t.render(x=23) == \"42\"\n\n    def test_wordwrap(self, env):\n        env.newline_sequence = \"\\n\"\n        t = env.from_string(\"{{ s|wordwrap(20) }}\")\n        result = t.render(s=\"Hello!\\nThis is Jinja saying something.\")\n        assert result == \"Hello!\\nThis is Jinja saying\\nsomething.\"\n\n    def test_filter_undefined(self, env):\n        with pytest.raises(TemplateAssertionError, match=\"No filter named 'f'\"):\n            env.from_string(\"{{ var|f }}\")\n\n    def test_filter_undefined_in_if(self, env):\n        t = env.from_string(\"{%- if x is defined -%}{{ x|f }}{%- else -%}x{% endif %}\")\n        assert t.render() == \"x\"\n        with pytest.raises(TemplateRuntimeError, match=\"No filter named 'f'\"):\n            t.render(x=42)\n\n    def test_filter_undefined_in_elif(self, env):\n        t = env.from_string(\n            \"{%- if x is defined -%}{{ x }}{%- elif y is defined -%}\"\n            \"{{ y|f }}{%- else -%}foo{%- endif -%}\"\n        )\n        assert t.render() == \"foo\"\n        with pytest.raises(TemplateRuntimeError, match=\"No filter named 'f'\"):\n            t.render(y=42)\n\n    def test_filter_undefined_in_else(self, env):\n        t = env.from_string(\n            \"{%- if x is not defined -%}foo{%- else -%}{{ x|f }}{%- endif -%}\"\n        )\n        assert t.render() == \"foo\"\n        with pytest.raises(TemplateRuntimeError, match=\"No filter named 'f'\"):\n            t.render(x=42)\n\n    def test_filter_undefined_in_nested_if(self, env):\n        t = env.from_string(\n            \"{%- if x is not defined -%}foo{%- else -%}{%- if y \"\n            \"is defined -%}{{ y|f }}{%- endif -%}{{ x }}{%- endif -%}\"\n        )\n        assert t.render() == \"foo\"\n        assert t.render(x=42) == \"42\"\n        with pytest.raises(TemplateRuntimeError, match=\"No filter named 'f'\"):\n            t.render(x=24, y=42)\n\n    def test_filter_undefined_in_condexpr(self, env):\n        t1 = env.from_string(\"{{ x|f if x is defined else 'foo' }}\")\n        t2 = env.from_string(\"{{ 'foo' if x is not defined else x|f }}\")\n        assert t1.render() == t2.render() == \"foo\"\n\n        with pytest.raises(TemplateRuntimeError, match=\"No filter named 'f'\"):\n            t1.render(x=42)\n\n        with pytest.raises(TemplateRuntimeError, match=\"No filter named 'f'\"):\n            t2.render(x=42)\n"
  },
  {
    "path": "tests/test_idtracking.py",
    "content": "from jinja2 import nodes\nfrom jinja2.idtracking import symbols_for_node\n\n\ndef test_basics():\n    for_loop = nodes.For(\n        nodes.Name(\"foo\", \"store\"),\n        nodes.Name(\"seq\", \"load\"),\n        [nodes.Output([nodes.Name(\"foo\", \"load\")])],\n        [],\n        None,\n        False,\n    )\n    tmpl = nodes.Template(\n        [nodes.Assign(nodes.Name(\"foo\", \"store\"), nodes.Name(\"bar\", \"load\")), for_loop]\n    )\n\n    sym = symbols_for_node(tmpl)\n    assert sym.refs == {\n        \"foo\": \"l_0_foo\",\n        \"bar\": \"l_0_bar\",\n        \"seq\": \"l_0_seq\",\n    }\n    assert sym.loads == {\n        \"l_0_foo\": (\"undefined\", None),\n        \"l_0_bar\": (\"resolve\", \"bar\"),\n        \"l_0_seq\": (\"resolve\", \"seq\"),\n    }\n\n    sym = symbols_for_node(for_loop, sym)\n    assert sym.refs == {\n        \"foo\": \"l_1_foo\",\n    }\n    assert sym.loads == {\n        \"l_1_foo\": (\"param\", None),\n    }\n\n\ndef test_complex():\n    title_block = nodes.Block(\n        \"title\", [nodes.Output([nodes.TemplateData(\"Page Title\")])], False, False\n    )\n\n    render_title_macro = nodes.Macro(\n        \"render_title\",\n        [nodes.Name(\"title\", \"param\")],\n        [],\n        [\n            nodes.Output(\n                [\n                    nodes.TemplateData('\\n  <div class=\"title\">\\n    <h1>'),\n                    nodes.Name(\"title\", \"load\"),\n                    nodes.TemplateData(\"</h1>\\n    <p>\"),\n                    nodes.Name(\"subtitle\", \"load\"),\n                    nodes.TemplateData(\"</p>\\n    \"),\n                ]\n            ),\n            nodes.Assign(\n                nodes.Name(\"subtitle\", \"store\"), nodes.Const(\"something else\")\n            ),\n            nodes.Output(\n                [\n                    nodes.TemplateData(\"\\n    <p>\"),\n                    nodes.Name(\"subtitle\", \"load\"),\n                    nodes.TemplateData(\"</p>\\n  </div>\\n\"),\n                    nodes.If(\n                        nodes.Name(\"something\", \"load\"),\n                        [\n                            nodes.Assign(\n                                nodes.Name(\"title_upper\", \"store\"),\n                                nodes.Filter(\n                                    nodes.Name(\"title\", \"load\"),\n                                    \"upper\",\n                                    [],\n                                    [],\n                                    None,\n                                    None,\n                                ),\n                            ),\n                            nodes.Output(\n                                [\n                                    nodes.Name(\"title_upper\", \"load\"),\n                                    nodes.Call(\n                                        nodes.Name(\"render_title\", \"load\"),\n                                        [nodes.Const(\"Aha\")],\n                                        [],\n                                        None,\n                                        None,\n                                    ),\n                                ]\n                            ),\n                        ],\n                        [],\n                        [],\n                    ),\n                ]\n            ),\n        ],\n    )\n\n    for_loop = nodes.For(\n        nodes.Name(\"item\", \"store\"),\n        nodes.Name(\"seq\", \"load\"),\n        [\n            nodes.Output(\n                [\n                    nodes.TemplateData(\"\\n    <li>\"),\n                    nodes.Name(\"item\", \"load\"),\n                    nodes.TemplateData(\"</li>\\n    <span>\"),\n                ]\n            ),\n            nodes.Include(nodes.Const(\"helper.html\"), True, False),\n            nodes.Output([nodes.TemplateData(\"</span>\\n  \")]),\n        ],\n        [],\n        None,\n        False,\n    )\n\n    body_block = nodes.Block(\n        \"body\",\n        [\n            nodes.Output(\n                [\n                    nodes.TemplateData(\"\\n  \"),\n                    nodes.Call(\n                        nodes.Name(\"render_title\", \"load\"),\n                        [nodes.Name(\"item\", \"load\")],\n                        [],\n                        None,\n                        None,\n                    ),\n                    nodes.TemplateData(\"\\n  <ul>\\n  \"),\n                ]\n            ),\n            for_loop,\n            nodes.Output([nodes.TemplateData(\"\\n  </ul>\\n\")]),\n        ],\n        False,\n        False,\n    )\n\n    tmpl = nodes.Template(\n        [\n            nodes.Extends(nodes.Const(\"layout.html\")),\n            title_block,\n            render_title_macro,\n            body_block,\n        ]\n    )\n\n    tmpl_sym = symbols_for_node(tmpl)\n    assert tmpl_sym.refs == {\n        \"render_title\": \"l_0_render_title\",\n    }\n    assert tmpl_sym.loads == {\n        \"l_0_render_title\": (\"undefined\", None),\n    }\n    assert tmpl_sym.stores == {\"render_title\"}\n    assert tmpl_sym.dump_stores() == {\n        \"render_title\": \"l_0_render_title\",\n    }\n\n    macro_sym = symbols_for_node(render_title_macro, tmpl_sym)\n    assert macro_sym.refs == {\n        \"subtitle\": \"l_1_subtitle\",\n        \"something\": \"l_1_something\",\n        \"title\": \"l_1_title\",\n        \"title_upper\": \"l_1_title_upper\",\n    }\n    assert macro_sym.loads == {\n        \"l_1_subtitle\": (\"resolve\", \"subtitle\"),\n        \"l_1_something\": (\"resolve\", \"something\"),\n        \"l_1_title\": (\"param\", None),\n        \"l_1_title_upper\": (\"resolve\", \"title_upper\"),\n    }\n    assert macro_sym.stores == {\"title\", \"title_upper\", \"subtitle\"}\n    assert macro_sym.find_ref(\"render_title\") == \"l_0_render_title\"\n    assert macro_sym.dump_stores() == {\n        \"title\": \"l_1_title\",\n        \"title_upper\": \"l_1_title_upper\",\n        \"subtitle\": \"l_1_subtitle\",\n        \"render_title\": \"l_0_render_title\",\n    }\n\n    body_sym = symbols_for_node(body_block)\n    assert body_sym.refs == {\n        \"item\": \"l_0_item\",\n        \"seq\": \"l_0_seq\",\n        \"render_title\": \"l_0_render_title\",\n    }\n    assert body_sym.loads == {\n        \"l_0_item\": (\"resolve\", \"item\"),\n        \"l_0_seq\": (\"resolve\", \"seq\"),\n        \"l_0_render_title\": (\"resolve\", \"render_title\"),\n    }\n    assert body_sym.stores == set()\n\n    for_sym = symbols_for_node(for_loop, body_sym)\n    assert for_sym.refs == {\n        \"item\": \"l_1_item\",\n    }\n    assert for_sym.loads == {\n        \"l_1_item\": (\"param\", None),\n    }\n    assert for_sym.stores == {\"item\"}\n    assert for_sym.dump_stores() == {\n        \"item\": \"l_1_item\",\n    }\n\n\ndef test_if_branching_stores():\n    tmpl = nodes.Template(\n        [\n            nodes.If(\n                nodes.Name(\"expression\", \"load\"),\n                [nodes.Assign(nodes.Name(\"variable\", \"store\"), nodes.Const(42))],\n                [],\n                [],\n            )\n        ]\n    )\n\n    sym = symbols_for_node(tmpl)\n    assert sym.refs == {\"variable\": \"l_0_variable\", \"expression\": \"l_0_expression\"}\n    assert sym.stores == {\"variable\"}\n    assert sym.loads == {\n        \"l_0_variable\": (\"resolve\", \"variable\"),\n        \"l_0_expression\": (\"resolve\", \"expression\"),\n    }\n    assert sym.dump_stores() == {\n        \"variable\": \"l_0_variable\",\n    }\n\n\ndef test_if_branching_stores_undefined():\n    tmpl = nodes.Template(\n        [\n            nodes.Assign(nodes.Name(\"variable\", \"store\"), nodes.Const(23)),\n            nodes.If(\n                nodes.Name(\"expression\", \"load\"),\n                [nodes.Assign(nodes.Name(\"variable\", \"store\"), nodes.Const(42))],\n                [],\n                [],\n            ),\n        ]\n    )\n\n    sym = symbols_for_node(tmpl)\n    assert sym.refs == {\"variable\": \"l_0_variable\", \"expression\": \"l_0_expression\"}\n    assert sym.stores == {\"variable\"}\n    assert sym.loads == {\n        \"l_0_variable\": (\"undefined\", None),\n        \"l_0_expression\": (\"resolve\", \"expression\"),\n    }\n    assert sym.dump_stores() == {\n        \"variable\": \"l_0_variable\",\n    }\n\n\ndef test_if_branching_multi_scope():\n    for_loop = nodes.For(\n        nodes.Name(\"item\", \"store\"),\n        nodes.Name(\"seq\", \"load\"),\n        [\n            nodes.If(\n                nodes.Name(\"expression\", \"load\"),\n                [nodes.Assign(nodes.Name(\"x\", \"store\"), nodes.Const(42))],\n                [],\n                [],\n            ),\n            nodes.Include(nodes.Const(\"helper.html\"), True, False),\n        ],\n        [],\n        None,\n        False,\n    )\n\n    tmpl = nodes.Template(\n        [nodes.Assign(nodes.Name(\"x\", \"store\"), nodes.Const(23)), for_loop]\n    )\n\n    tmpl_sym = symbols_for_node(tmpl)\n    for_sym = symbols_for_node(for_loop, tmpl_sym)\n    assert for_sym.stores == {\"item\", \"x\"}\n    assert for_sym.loads == {\n        \"l_1_x\": (\"alias\", \"l_0_x\"),\n        \"l_1_item\": (\"param\", None),\n        \"l_1_expression\": (\"resolve\", \"expression\"),\n    }\n"
  },
  {
    "path": "tests/test_imports.py",
    "content": "import pytest\n\nfrom jinja2.environment import Environment\nfrom jinja2.exceptions import TemplateNotFound\nfrom jinja2.exceptions import TemplatesNotFound\nfrom jinja2.exceptions import TemplateSyntaxError\nfrom jinja2.exceptions import UndefinedError\nfrom jinja2.loaders import DictLoader\n\n\n@pytest.fixture\ndef test_env():\n    env = Environment(\n        loader=DictLoader(\n            dict(\n                module=\"{% macro test() %}[{{ foo }}|{{ bar }}]{% endmacro %}\",\n                header=\"[{{ foo }}|{{ 23 }}]\",\n                o_printer=\"({{ o }})\",\n            )\n        )\n    )\n    env.globals[\"bar\"] = 23\n    return env\n\n\nclass TestImports:\n    def test_context_imports(self, test_env):\n        t = test_env.from_string('{% import \"module\" as m %}{{ m.test() }}')\n        assert t.render(foo=42) == \"[|23]\"\n        t = test_env.from_string(\n            '{% import \"module\" as m without context %}{{ m.test() }}'\n        )\n        assert t.render(foo=42) == \"[|23]\"\n        t = test_env.from_string(\n            '{% import \"module\" as m with context %}{{ m.test() }}'\n        )\n        assert t.render(foo=42) == \"[42|23]\"\n        t = test_env.from_string('{% from \"module\" import test %}{{ test() }}')\n        assert t.render(foo=42) == \"[|23]\"\n        t = test_env.from_string(\n            '{% from \"module\" import test without context %}{{ test() }}'\n        )\n        assert t.render(foo=42) == \"[|23]\"\n        t = test_env.from_string(\n            '{% from \"module\" import test with context %}{{ test() }}'\n        )\n        assert t.render(foo=42) == \"[42|23]\"\n\n    def test_import_needs_name(self, test_env):\n        test_env.from_string('{% from \"foo\" import bar %}')\n        test_env.from_string('{% from \"foo\" import bar, baz %}')\n\n        with pytest.raises(TemplateSyntaxError):\n            test_env.from_string('{% from \"foo\" import %}')\n\n    def test_no_trailing_comma(self, test_env):\n        with pytest.raises(TemplateSyntaxError):\n            test_env.from_string('{% from \"foo\" import bar, %}')\n\n        with pytest.raises(TemplateSyntaxError):\n            test_env.from_string('{% from \"foo\" import bar,, %}')\n\n        with pytest.raises(TemplateSyntaxError):\n            test_env.from_string('{% from \"foo\" import, %}')\n\n    def test_trailing_comma_with_context(self, test_env):\n        test_env.from_string('{% from \"foo\" import bar, baz with context %}')\n        test_env.from_string('{% from \"foo\" import bar, baz, with context %}')\n        test_env.from_string('{% from \"foo\" import bar, with context %}')\n        test_env.from_string('{% from \"foo\" import bar, with, context %}')\n        test_env.from_string('{% from \"foo\" import bar, with with context %}')\n\n        with pytest.raises(TemplateSyntaxError):\n            test_env.from_string('{% from \"foo\" import bar,, with context %}')\n\n        with pytest.raises(TemplateSyntaxError):\n            test_env.from_string('{% from \"foo\" import bar with context, %}')\n\n    def test_exports(self, test_env):\n        m = test_env.from_string(\n            \"\"\"\n            {% macro toplevel() %}...{% endmacro %}\n            {% macro __private() %}...{% endmacro %}\n            {% set variable = 42 %}\n            {% for item in [1] %}\n                {% macro notthere() %}{% endmacro %}\n            {% endfor %}\n        \"\"\"\n        ).module\n        assert m.toplevel() == \"...\"\n        assert not hasattr(m, \"__missing\")\n        assert m.variable == 42\n        assert not hasattr(m, \"notthere\")\n\n    def test_not_exported(self, test_env):\n        t = test_env.from_string(\"{% from 'module' import nothing %}{{ nothing() }}\")\n\n        with pytest.raises(UndefinedError, match=\"does not export the requested name\"):\n            t.render()\n\n    def test_import_with_globals(self, test_env):\n        t = test_env.from_string(\n            '{% import \"module\" as m %}{{ m.test() }}', globals={\"foo\": 42}\n        )\n        assert t.render() == \"[42|23]\"\n\n        t = test_env.from_string('{% import \"module\" as m %}{{ m.test() }}')\n        assert t.render() == \"[|23]\"\n\n    def test_import_with_globals_override(self, test_env):\n        t = test_env.from_string(\n            '{% set foo = 41 %}{% import \"module\" as m %}{{ m.test() }}',\n            globals={\"foo\": 42},\n        )\n        assert t.render() == \"[42|23]\"\n\n    def test_from_import_with_globals(self, test_env):\n        t = test_env.from_string(\n            '{% from \"module\" import test %}{{ test() }}',\n            globals={\"foo\": 42},\n        )\n        assert t.render() == \"[42|23]\"\n\n\nclass TestIncludes:\n    def test_context_include(self, test_env):\n        t = test_env.from_string('{% include \"header\" %}')\n        assert t.render(foo=42) == \"[42|23]\"\n        t = test_env.from_string('{% include \"header\" with context %}')\n        assert t.render(foo=42) == \"[42|23]\"\n        t = test_env.from_string('{% include \"header\" without context %}')\n        assert t.render(foo=42) == \"[|23]\"\n\n    def test_choice_includes(self, test_env):\n        t = test_env.from_string('{% include [\"missing\", \"header\"] %}')\n        assert t.render(foo=42) == \"[42|23]\"\n\n        t = test_env.from_string('{% include [\"missing\", \"missing2\"] ignore missing %}')\n        assert t.render(foo=42) == \"\"\n\n        t = test_env.from_string('{% include [\"missing\", \"missing2\"] %}')\n        pytest.raises(TemplateNotFound, t.render)\n        with pytest.raises(TemplatesNotFound) as e:\n            t.render()\n\n        assert e.value.templates == [\"missing\", \"missing2\"]\n        assert e.value.name == \"missing2\"\n\n        def test_includes(t, **ctx):\n            ctx[\"foo\"] = 42\n            assert t.render(ctx) == \"[42|23]\"\n\n        t = test_env.from_string('{% include [\"missing\", \"header\"] %}')\n        test_includes(t)\n        t = test_env.from_string(\"{% include x %}\")\n        test_includes(t, x=[\"missing\", \"header\"])\n        t = test_env.from_string('{% include [x, \"header\"] %}')\n        test_includes(t, x=\"missing\")\n        t = test_env.from_string(\"{% include x %}\")\n        test_includes(t, x=\"header\")\n        t = test_env.from_string(\"{% include [x] %}\")\n        test_includes(t, x=\"header\")\n\n    def test_include_ignoring_missing(self, test_env):\n        t = test_env.from_string('{% include \"missing\" %}')\n        pytest.raises(TemplateNotFound, t.render)\n        for extra in \"\", \"with context\", \"without context\":\n            t = test_env.from_string(\n                '{% include \"missing\" ignore missing ' + extra + \" %}\"\n            )\n            assert t.render() == \"\"\n\n    def test_context_include_with_overrides(self, test_env):\n        env = Environment(\n            loader=DictLoader(\n                dict(\n                    main=\"{% for item in [1, 2, 3] %}{% include 'item' %}{% endfor %}\",\n                    item=\"{{ item }}\",\n                )\n            )\n        )\n        assert env.get_template(\"main\").render() == \"123\"\n\n    def test_unoptimized_scopes(self, test_env):\n        t = test_env.from_string(\n            \"\"\"\n            {% macro outer(o) %}\n            {% macro inner() %}\n            {% include \"o_printer\" %}\n            {% endmacro %}\n            {{ inner() }}\n            {% endmacro %}\n            {{ outer(\"FOO\") }}\n        \"\"\"\n        )\n        assert t.render().strip() == \"(FOO)\"\n\n    def test_import_from_with_context(self):\n        env = Environment(\n            loader=DictLoader({\"a\": \"{% macro x() %}{{ foobar }}{% endmacro %}\"})\n        )\n        t = env.from_string(\n            \"{% set foobar = 42 %}{% from 'a' import x with context %}{{ x() }}\"\n        )\n        assert t.render() == \"42\"\n"
  },
  {
    "path": "tests/test_inheritance.py",
    "content": "import pytest\n\nfrom jinja2 import DictLoader\nfrom jinja2 import Environment\nfrom jinja2 import TemplateRuntimeError\nfrom jinja2 import TemplateSyntaxError\n\nLAYOUTTEMPLATE = \"\"\"\\\n|{% block block1 %}block 1 from layout{% endblock %}\n|{% block block2 %}block 2 from layout{% endblock %}\n|{% block block3 %}\n{% block block4 %}nested block 4 from layout{% endblock %}\n{% endblock %}|\"\"\"\n\nLEVEL1TEMPLATE = \"\"\"\\\n{% extends \"layout\" %}\n{% block block1 %}block 1 from level1{% endblock %}\"\"\"\n\nLEVEL2TEMPLATE = \"\"\"\\\n{% extends \"level1\" %}\n{% block block2 %}{% block block5 %}nested block 5 from level2{%\nendblock %}{% endblock %}\"\"\"\n\nLEVEL3TEMPLATE = \"\"\"\\\n{% extends \"level2\" %}\n{% block block5 %}block 5 from level3{% endblock %}\n{% block block4 %}block 4 from level3{% endblock %}\n\"\"\"\n\nLEVEL4TEMPLATE = \"\"\"\\\n{% extends \"level3\" %}\n{% block block3 %}block 3 from level4{% endblock %}\n\"\"\"\n\nWORKINGTEMPLATE = \"\"\"\\\n{% extends \"layout\" %}\n{% block block1 %}\n  {% if false %}\n    {% block block2 %}\n      this should work\n    {% endblock %}\n  {% endif %}\n{% endblock %}\n\"\"\"\n\nDOUBLEEXTENDS = \"\"\"\\\n{% extends \"layout\" %}\n{% extends \"layout\" %}\n{% block block1 %}\n  {% if false %}\n    {% block block2 %}\n      this should work\n    {% endblock %}\n  {% endif %}\n{% endblock %}\n\"\"\"\n\n\n@pytest.fixture\ndef env():\n    return Environment(\n        loader=DictLoader(\n            {\n                \"layout\": LAYOUTTEMPLATE,\n                \"level1\": LEVEL1TEMPLATE,\n                \"level2\": LEVEL2TEMPLATE,\n                \"level3\": LEVEL3TEMPLATE,\n                \"level4\": LEVEL4TEMPLATE,\n                \"working\": WORKINGTEMPLATE,\n                \"doublee\": DOUBLEEXTENDS,\n            }\n        ),\n        trim_blocks=True,\n    )\n\n\nclass TestInheritance:\n    def test_layout(self, env):\n        tmpl = env.get_template(\"layout\")\n        assert tmpl.render() == (\n            \"|block 1 from layout|block 2 from layout|nested block 4 from layout|\"\n        )\n\n    def test_level1(self, env):\n        tmpl = env.get_template(\"level1\")\n        assert tmpl.render() == (\n            \"|block 1 from level1|block 2 from layout|nested block 4 from layout|\"\n        )\n\n    def test_level2(self, env):\n        tmpl = env.get_template(\"level2\")\n        assert tmpl.render() == (\n            \"|block 1 from level1|nested block 5 from \"\n            \"level2|nested block 4 from layout|\"\n        )\n\n    def test_level3(self, env):\n        tmpl = env.get_template(\"level3\")\n        assert tmpl.render() == (\n            \"|block 1 from level1|block 5 from level3|block 4 from level3|\"\n        )\n\n    def test_level4(self, env):\n        tmpl = env.get_template(\"level4\")\n        assert tmpl.render() == (\n            \"|block 1 from level1|block 5 from level3|block 3 from level4|\"\n        )\n\n    def test_super(self, env):\n        env = Environment(\n            loader=DictLoader(\n                {\n                    \"a\": \"{% block intro %}INTRO{% endblock %}|\"\n                    \"BEFORE|{% block data %}INNER{% endblock %}|AFTER\",\n                    \"b\": '{% extends \"a\" %}{% block data %}({{ '\n                    \"super() }}){% endblock %}\",\n                    \"c\": '{% extends \"b\" %}{% block intro %}--{{ '\n                    \"super() }}--{% endblock %}\\n{% block data \"\n                    \"%}[{{ super() }}]{% endblock %}\",\n                }\n            )\n        )\n        tmpl = env.get_template(\"c\")\n        assert tmpl.render() == \"--INTRO--|BEFORE|[(INNER)]|AFTER\"\n\n    def test_working(self, env):\n        env.get_template(\"working\")\n\n    def test_reuse_blocks(self, env):\n        tmpl = env.from_string(\n            \"{{ self.foo() }}|{% block foo %}42{% endblock %}|{{ self.foo() }}\"\n        )\n        assert tmpl.render() == \"42|42|42\"\n\n    def test_preserve_blocks(self, env):\n        env = Environment(\n            loader=DictLoader(\n                {\n                    \"a\": \"{% if false %}{% block x %}A{% endblock %}\"\n                    \"{% endif %}{{ self.x() }}\",\n                    \"b\": '{% extends \"a\" %}{% block x %}B{{ super() }}{% endblock %}',\n                }\n            )\n        )\n        tmpl = env.get_template(\"b\")\n        assert tmpl.render() == \"BA\"\n\n    def test_dynamic_inheritance(self, env):\n        env = Environment(\n            loader=DictLoader(\n                {\n                    \"default1\": \"DEFAULT1{% block x %}{% endblock %}\",\n                    \"default2\": \"DEFAULT2{% block x %}{% endblock %}\",\n                    \"child\": \"{% extends default %}{% block x %}CHILD{% endblock %}\",\n                }\n            )\n        )\n        tmpl = env.get_template(\"child\")\n        for m in range(1, 3):\n            assert tmpl.render(default=f\"default{m}\") == f\"DEFAULT{m}CHILD\"\n\n    def test_multi_inheritance(self, env):\n        env = Environment(\n            loader=DictLoader(\n                {\n                    \"default1\": \"DEFAULT1{% block x %}{% endblock %}\",\n                    \"default2\": \"DEFAULT2{% block x %}{% endblock %}\",\n                    \"child\": (\n                        \"{% if default %}{% extends default %}{% else %}\"\n                        \"{% extends 'default1' %}{% endif %}\"\n                        \"{% block x %}CHILD{% endblock %}\"\n                    ),\n                }\n            )\n        )\n        tmpl = env.get_template(\"child\")\n        assert tmpl.render(default=\"default2\") == \"DEFAULT2CHILD\"\n        assert tmpl.render(default=\"default1\") == \"DEFAULT1CHILD\"\n        assert tmpl.render() == \"DEFAULT1CHILD\"\n\n    def test_scoped_block(self, env):\n        env = Environment(\n            loader=DictLoader(\n                {\n                    \"default.html\": \"{% for item in seq %}[{% block item scoped %}\"\n                    \"{% endblock %}]{% endfor %}\"\n                }\n            )\n        )\n        t = env.from_string(\n            \"{% extends 'default.html' %}{% block item %}{{ item }}{% endblock %}\"\n        )\n        assert t.render(seq=list(range(5))) == \"[0][1][2][3][4]\"\n\n    def test_super_in_scoped_block(self, env):\n        env = Environment(\n            loader=DictLoader(\n                {\n                    \"default.html\": \"{% for item in seq %}[{% block item scoped %}\"\n                    \"{{ item }}{% endblock %}]{% endfor %}\"\n                }\n            )\n        )\n        t = env.from_string(\n            '{% extends \"default.html\" %}{% block item %}'\n            \"{{ super() }}|{{ item * 2 }}{% endblock %}\"\n        )\n        assert t.render(seq=list(range(5))) == \"[0|0][1|2][2|4][3|6][4|8]\"\n\n    def test_scoped_block_after_inheritance(self, env):\n        env = Environment(\n            loader=DictLoader(\n                {\n                    \"layout.html\": \"\"\"\n            {% block useless %}{% endblock %}\n            \"\"\",\n                    \"index.html\": \"\"\"\n            {%- extends 'layout.html' %}\n            {% from 'helpers.html' import foo with context %}\n            {% block useless %}\n                {% for x in [1, 2, 3] %}\n                    {% block testing scoped %}\n                        {{ foo(x) }}\n                    {% endblock %}\n                {% endfor %}\n            {% endblock %}\n            \"\"\",\n                    \"helpers.html\": \"\"\"\n            {% macro foo(x) %}{{ the_foo + x }}{% endmacro %}\n            \"\"\",\n                }\n            )\n        )\n        rv = env.get_template(\"index.html\").render(the_foo=42).split()\n        assert rv == [\"43\", \"44\", \"45\"]\n\n    def test_level1_required(self, env):\n        env = Environment(\n            loader=DictLoader(\n                {\n                    \"default\": \"{% block x required %}{# comment #}\\n {% endblock %}\",\n                    \"level1\": \"{% extends 'default' %}{% block x %}[1]{% endblock %}\",\n                }\n            )\n        )\n        rv = env.get_template(\"level1\").render()\n        assert rv == \"[1]\"\n\n    def test_level2_required(self, env):\n        env = Environment(\n            loader=DictLoader(\n                {\n                    \"default\": \"{% block x required %}{% endblock %}\",\n                    \"level1\": \"{% extends 'default' %}{% block x %}[1]{% endblock %}\",\n                    \"level2\": \"{% extends 'default' %}{% block x %}[2]{% endblock %}\",\n                }\n            )\n        )\n        rv1 = env.get_template(\"level1\").render()\n        rv2 = env.get_template(\"level2\").render()\n\n        assert rv1 == \"[1]\"\n        assert rv2 == \"[2]\"\n\n    def test_level3_required(self, env):\n        env = Environment(\n            loader=DictLoader(\n                {\n                    \"default\": \"{% block x required %}{% endblock %}\",\n                    \"level1\": \"{% extends 'default' %}\",\n                    \"level2\": \"{% extends 'level1' %}{% block x %}[2]{% endblock %}\",\n                    \"level3\": \"{% extends 'level2' %}\",\n                }\n            )\n        )\n        t1 = env.get_template(\"level1\")\n        t2 = env.get_template(\"level2\")\n        t3 = env.get_template(\"level3\")\n\n        with pytest.raises(TemplateRuntimeError, match=\"Required block 'x' not found\"):\n            assert t1.render()\n\n        assert t2.render() == \"[2]\"\n        assert t3.render() == \"[2]\"\n\n    def test_invalid_required(self, env):\n        env = Environment(\n            loader=DictLoader(\n                {\n                    \"empty\": \"{% block x required %}{% endblock %}\",\n                    \"blank\": \"{% block x required %} {# c #}{% endblock %}\",\n                    \"text\": \"{% block x required %}data {# c #}{% endblock %}\",\n                    \"block\": \"{% block x required %}{% block y %}\"\n                    \"{% endblock %}{% endblock %}\",\n                    \"if\": \"{% block x required %}{% if true %}\"\n                    \"{% endif %}{% endblock %}\",\n                    \"top\": \"{% extends t %}{% block x %}CHILD{% endblock %}\",\n                }\n            )\n        )\n        t = env.get_template(\"top\")\n        assert t.render(t=\"empty\") == \"CHILD\"\n        assert t.render(t=\"blank\") == \"CHILD\"\n\n        required_block_check = pytest.raises(\n            TemplateSyntaxError,\n            match=\"Required blocks can only contain comments or whitespace\",\n        )\n\n        with required_block_check:\n            t.render(t=\"text\")\n\n        with required_block_check:\n            t.render(t=\"block\")\n\n        with required_block_check:\n            t.render(t=\"if\")\n\n    def test_required_with_scope(self, env):\n        env = Environment(\n            loader=DictLoader(\n                {\n                    \"default1\": \"{% for item in seq %}[{% block item scoped required %}\"\n                    \"{% endblock %}]{% endfor %}\",\n                    \"child1\": \"{% extends 'default1' %}{% block item %}\"\n                    \"{{ item }}{% endblock %}\",\n                    \"default2\": \"{% for item in seq %}[{% block item required scoped %}\"\n                    \"{% endblock %}]{% endfor %}\",\n                    \"child2\": \"{% extends 'default2' %}{% block item %}\"\n                    \"{{ item }}{% endblock %}\",\n                }\n            )\n        )\n        t1 = env.get_template(\"child1\")\n        t2 = env.get_template(\"child2\")\n\n        assert t1.render(seq=list(range(3))) == \"[0][1][2]\"\n\n        # scoped must come before required\n        with pytest.raises(TemplateSyntaxError):\n            t2.render(seq=list(range(3)))\n\n    def test_duplicate_required_or_scoped(self, env):\n        env = Environment(\n            loader=DictLoader(\n                {\n                    \"default1\": \"{% for item in seq %}[{% block item \"\n                    \"scoped scoped %}}{{% endblock %}}]{{% endfor %}}\",\n                    \"default2\": \"{% for item in seq %}[{% block item \"\n                    \"required required %}}{{% endblock %}}]{{% endfor %}}\",\n                    \"child\": \"{% if default %}{% extends default %}{% else %}\"\n                    \"{% extends 'default1' %}{% endif %}{%- block x %}\"\n                    \"CHILD{% endblock %}\",\n                }\n            )\n        )\n        tmpl = env.get_template(\"child\")\n\n        with pytest.raises(TemplateSyntaxError):\n            tmpl.render(default=\"default1\", seq=list(range(3)))\n\n        with pytest.raises(TemplateSyntaxError):\n            tmpl.render(default=\"default2\", seq=list(range(3)))\n\n\nclass TestBugFix:\n    def test_fixed_macro_scoping_bug(self, env):\n        assert Environment(\n            loader=DictLoader(\n                {\n                    \"test.html\": \"\"\"\\\n        {% extends 'details.html' %}\n\n        {% macro my_macro() %}\n        my_macro\n        {% endmacro %}\n\n        {% block inner_box %}\n            {{ my_macro() }}\n        {% endblock %}\n            \"\"\",\n                    \"details.html\": \"\"\"\\\n        {% extends 'standard.html' %}\n\n        {% macro my_macro() %}\n        my_macro\n        {% endmacro %}\n\n        {% block content %}\n            {% block outer_box %}\n                outer_box\n                {% block inner_box %}\n                    inner_box\n                {% endblock %}\n            {% endblock %}\n        {% endblock %}\n        \"\"\",\n                    \"standard.html\": \"\"\"\n        {% block content %}&nbsp;{% endblock %}\n        \"\"\",\n                }\n            )\n        ).get_template(\"test.html\").render().split() == [\"outer_box\", \"my_macro\"]\n\n    def test_double_extends(self, env):\n        \"\"\"Ensures that a template with more than 1 {% extends ... %} usage\n        raises a ``TemplateError``.\n        \"\"\"\n        with pytest.raises(TemplateRuntimeError, match=\"extended multiple times\"):\n            env.get_template(\"doublee\").render()\n"
  },
  {
    "path": "tests/test_lexnparse.py",
    "content": "import pytest\n\nfrom jinja2 import Environment\nfrom jinja2 import nodes\nfrom jinja2 import Template\nfrom jinja2 import TemplateSyntaxError\nfrom jinja2 import UndefinedError\nfrom jinja2.lexer import Token\nfrom jinja2.lexer import TOKEN_BLOCK_BEGIN\nfrom jinja2.lexer import TOKEN_BLOCK_END\nfrom jinja2.lexer import TOKEN_EOF\nfrom jinja2.lexer import TokenStream\n\n\nclass TestTokenStream:\n    test_tokens = [\n        Token(1, TOKEN_BLOCK_BEGIN, \"\"),\n        Token(2, TOKEN_BLOCK_END, \"\"),\n    ]\n\n    def test_simple(self, env):\n        ts = TokenStream(self.test_tokens, \"foo\", \"bar\")\n        assert ts.current.type is TOKEN_BLOCK_BEGIN\n        assert bool(ts)\n        assert not bool(ts.eos)\n        next(ts)\n        assert ts.current.type is TOKEN_BLOCK_END\n        assert bool(ts)\n        assert not bool(ts.eos)\n        next(ts)\n        assert ts.current.type is TOKEN_EOF\n        assert not bool(ts)\n        assert bool(ts.eos)\n\n    def test_iter(self, env):\n        token_types = [t.type for t in TokenStream(self.test_tokens, \"foo\", \"bar\")]\n        assert token_types == [\n            \"block_begin\",\n            \"block_end\",\n        ]\n\n\nclass TestLexer:\n    def test_raw1(self, env):\n        tmpl = env.from_string(\n            \"{% raw %}foo{% endraw %}|{%raw%}{{ bar }}|{% baz %}{%       endraw    %}\"\n        )\n        assert tmpl.render() == \"foo|{{ bar }}|{% baz %}\"\n\n    def test_raw2(self, env):\n        tmpl = env.from_string(\"1  {%- raw -%}   2   {%- endraw -%}   3\")\n        assert tmpl.render() == \"123\"\n\n    def test_raw3(self, env):\n        # The second newline after baz exists because it is AFTER the\n        # {% raw %} and is ignored.\n        env = Environment(lstrip_blocks=True, trim_blocks=True)\n        tmpl = env.from_string(\"bar\\n{% raw %}\\n  {{baz}}2 spaces\\n{% endraw %}\\nfoo\")\n        assert tmpl.render(baz=\"test\") == \"bar\\n\\n  {{baz}}2 spaces\\nfoo\"\n\n    def test_raw4(self, env):\n        # The trailing dash of the {% raw -%} cleans both the spaces and\n        # newlines up to the first character of data.\n        env = Environment(lstrip_blocks=True, trim_blocks=False)\n        tmpl = env.from_string(\n            \"bar\\n{%- raw -%}\\n\\n  \\n  2 spaces\\n space{%- endraw -%}\\nfoo\"\n        )\n        assert tmpl.render() == \"bar2 spaces\\n spacefoo\"\n\n    def test_balancing(self, env):\n        env = Environment(\"{%\", \"%}\", \"${\", \"}\")\n        tmpl = env.from_string(\n            \"\"\"{% for item in seq\n            %}${{'foo': item}|upper}{% endfor %}\"\"\"\n        )\n        assert tmpl.render(seq=list(range(3))) == \"{'FOO': 0}{'FOO': 1}{'FOO': 2}\"\n\n    def test_comments(self, env):\n        env = Environment(\"<!--\", \"-->\", \"{\", \"}\")\n        tmpl = env.from_string(\n            \"\"\"\\\n<ul>\n<!--- for item in seq -->\n  <li>{item}</li>\n<!--- endfor -->\n</ul>\"\"\"\n        )\n        assert tmpl.render(seq=list(range(3))) == (\n            \"<ul>\\n  <li>0</li>\\n  <li>1</li>\\n  <li>2</li>\\n</ul>\"\n        )\n\n    def test_string_escapes(self, env):\n        for char in \"\\0\", \"\\u2668\", \"\\xe4\", \"\\t\", \"\\r\", \"\\n\":\n            tmpl = env.from_string(f\"{{{{ {char!r} }}}}\")\n            assert tmpl.render() == char\n        assert env.from_string('{{ \"\\N{HOT SPRINGS}\" }}').render() == \"\\u2668\"\n\n    def test_bytefallback(self, env):\n        from pprint import pformat\n\n        tmpl = env.from_string(\"\"\"{{ 'foo'|pprint }}|{{ 'bär'|pprint }}\"\"\")\n        assert tmpl.render() == pformat(\"foo\") + \"|\" + pformat(\"bär\")\n\n    def test_operators(self, env):\n        from jinja2.lexer import operators\n\n        for test, expect in operators.items():\n            if test in \"([{}])\":\n                continue\n            stream = env.lexer.tokenize(f\"{{{{ {test} }}}}\")\n            next(stream)\n            assert stream.current.type == expect\n\n    def test_normalizing(self, env):\n        for seq in \"\\r\", \"\\r\\n\", \"\\n\":\n            env = Environment(newline_sequence=seq)\n            tmpl = env.from_string(\"1\\n2\\r\\n3\\n4\\n\")\n            result = tmpl.render()\n            assert result.replace(seq, \"X\") == \"1X2X3X4\"\n\n    def test_trailing_newline(self, env):\n        for keep in [True, False]:\n            env = Environment(keep_trailing_newline=keep)\n            for template, expected in [\n                (\"\", {}),\n                (\"no\\nnewline\", {}),\n                (\"with\\nnewline\\n\", {False: \"with\\nnewline\"}),\n                (\"with\\nseveral\\n\\n\\n\", {False: \"with\\nseveral\\n\\n\"}),\n            ]:\n                tmpl = env.from_string(template)\n                expect = expected.get(keep, template)\n                result = tmpl.render()\n                assert result == expect, (keep, template, result, expect)\n\n    @pytest.mark.parametrize(\n        (\"name\", \"valid\"),\n        [\n            (\"foo\", True),\n            (\"föö\", True),\n            (\"き\", True),\n            (\"_\", True),\n            (\"1a\", False),  # invalid ascii start\n            (\"a-\", False),  # invalid ascii continue\n            (\"\\U0001f40da\", False),  # invalid unicode start\n            (\"a🐍\\U0001f40d\", False),  # invalid unicode continue\n            # start characters not matched by \\w\n            (\"\\u1885\", True),\n            (\"\\u1886\", True),\n            (\"\\u2118\", True),\n            (\"\\u212e\", True),\n            # continue character not matched by \\w\n            (\"\\xb7\", False),\n            (\"a\\xb7\", True),\n        ],\n    )\n    def test_name(self, env, name, valid):\n        t = \"{{ \" + name + \" }}\"\n\n        if valid:\n            # valid for version being tested, shouldn't raise\n            env.from_string(t)\n        else:\n            pytest.raises(TemplateSyntaxError, env.from_string, t)\n\n    def test_lineno_with_strip(self, env):\n        tokens = env.lex(\n            \"\"\"\\\n<html>\n    <body>\n    {%- block content -%}\n        <hr>\n        {{ item }}\n    {% endblock %}\n    </body>\n</html>\"\"\"\n        )\n        for tok in tokens:\n            lineno, token_type, value = tok\n            if token_type == \"name\" and value == \"item\":\n                assert lineno == 5\n                break\n\n\nclass TestParser:\n    def test_php_syntax(self, env):\n        env = Environment(\"<?\", \"?>\", \"<?=\", \"?>\", \"<!--\", \"-->\")\n        tmpl = env.from_string(\n            \"\"\"\\\n<!-- I'm a comment, I'm not interesting -->\\\n<? for item in seq -?>\n    <?= item ?>\n<?- endfor ?>\"\"\"\n        )\n        assert tmpl.render(seq=list(range(5))) == \"01234\"\n\n    def test_erb_syntax(self, env):\n        env = Environment(\"<%\", \"%>\", \"<%=\", \"%>\", \"<%#\", \"%>\")\n        tmpl = env.from_string(\n            \"\"\"\\\n<%# I'm a comment, I'm not interesting %>\\\n<% for item in seq -%>\n    <%= item %>\n<%- endfor %>\"\"\"\n        )\n        assert tmpl.render(seq=list(range(5))) == \"01234\"\n\n    def test_comment_syntax(self, env):\n        env = Environment(\"<!--\", \"-->\", \"${\", \"}\", \"<!--#\", \"-->\")\n        tmpl = env.from_string(\n            \"\"\"\\\n<!--# I'm a comment, I'm not interesting -->\\\n<!-- for item in seq --->\n    ${item}\n<!--- endfor -->\"\"\"\n        )\n        assert tmpl.render(seq=list(range(5))) == \"01234\"\n\n    def test_balancing(self, env):\n        tmpl = env.from_string(\"\"\"{{{'foo':'bar'}.foo}}\"\"\")\n        assert tmpl.render() == \"bar\"\n\n    def test_start_comment(self, env):\n        tmpl = env.from_string(\n            \"\"\"{# foo comment\nand bar comment #}\n{% macro blub() %}foo{% endmacro %}\n{{ blub() }}\"\"\"\n        )\n        assert tmpl.render().strip() == \"foo\"\n\n    def test_line_syntax(self, env):\n        env = Environment(\"<%\", \"%>\", \"${\", \"}\", \"<%#\", \"%>\", \"%\")\n        tmpl = env.from_string(\n            \"\"\"\\\n<%# regular comment %>\n% for item in seq:\n    ${item}\n% endfor\"\"\"\n        )\n        assert [\n            int(x.strip()) for x in tmpl.render(seq=list(range(5))).split()\n        ] == list(range(5))\n\n        env = Environment(\"<%\", \"%>\", \"${\", \"}\", \"<%#\", \"%>\", \"%\", \"##\")\n        tmpl = env.from_string(\n            \"\"\"\\\n<%# regular comment %>\n% for item in seq:\n    ${item} ## the rest of the stuff\n% endfor\"\"\"\n        )\n        assert [\n            int(x.strip()) for x in tmpl.render(seq=list(range(5))).split()\n        ] == list(range(5))\n\n    def test_line_syntax_priority(self, env):\n        # XXX: why is the whitespace there in front of the newline?\n        env = Environment(\"{%\", \"%}\", \"${\", \"}\", \"/*\", \"*/\", \"##\", \"#\")\n        tmpl = env.from_string(\n            \"\"\"\\\n/* ignore me.\n   I'm a multiline comment */\n## for item in seq:\n* ${item}          # this is just extra stuff\n## endfor\"\"\"\n        )\n        assert tmpl.render(seq=[1, 2]).strip() == \"* 1\\n* 2\"\n        env = Environment(\"{%\", \"%}\", \"${\", \"}\", \"/*\", \"*/\", \"#\", \"##\")\n        tmpl = env.from_string(\n            \"\"\"\\\n/* ignore me.\n   I'm a multiline comment */\n# for item in seq:\n* ${item}          ## this is just extra stuff\n    ## extra stuff i just want to ignore\n# endfor\"\"\"\n        )\n        assert tmpl.render(seq=[1, 2]).strip() == \"* 1\\n\\n* 2\"\n\n    def test_error_messages(self, env):\n        def assert_error(code, expected):\n            with pytest.raises(TemplateSyntaxError, match=expected):\n                Template(code)\n\n        assert_error(\n            \"{% for item in seq %}...{% endif %}\",\n            \"Encountered unknown tag 'endif'. Jinja was looking \"\n            \"for the following tags: 'endfor' or 'else'. The \"\n            \"innermost block that needs to be closed is 'for'.\",\n        )\n        assert_error(\n            \"{% if foo %}{% for item in seq %}...{% endfor %}{% endfor %}\",\n            \"Encountered unknown tag 'endfor'. Jinja was looking for \"\n            \"the following tags: 'elif' or 'else' or 'endif'. The \"\n            \"innermost block that needs to be closed is 'if'.\",\n        )\n        assert_error(\n            \"{% if foo %}\",\n            \"Unexpected end of template. Jinja was looking for the \"\n            \"following tags: 'elif' or 'else' or 'endif'. The \"\n            \"innermost block that needs to be closed is 'if'.\",\n        )\n        assert_error(\n            \"{% for item in seq %}\",\n            \"Unexpected end of template. Jinja was looking for the \"\n            \"following tags: 'endfor' or 'else'. The innermost block \"\n            \"that needs to be closed is 'for'.\",\n        )\n        assert_error(\n            \"{% block foo-bar-baz %}\",\n            \"Block names in Jinja have to be valid Python identifiers \"\n            \"and may not contain hyphens, use an underscore instead.\",\n        )\n        assert_error(\"{% unknown_tag %}\", \"Encountered unknown tag 'unknown_tag'.\")\n\n\nclass TestSyntax:\n    def test_call(self, env):\n        env = Environment()\n        env.globals[\"foo\"] = lambda a, b, c, e, g: a + b + c + e + g\n        tmpl = env.from_string(\"{{ foo('a', c='d', e='f', *['b'], **{'g': 'h'}) }}\")\n        assert tmpl.render() == \"abdfh\"\n\n    def test_slicing(self, env):\n        tmpl = env.from_string(\"{{ [1, 2, 3][:] }}|{{ [1, 2, 3][::-1] }}\")\n        assert tmpl.render() == \"[1, 2, 3]|[3, 2, 1]\"\n\n    def test_attr(self, env):\n        tmpl = env.from_string(\"{{ foo.bar }}|{{ foo['bar'] }}\")\n        assert tmpl.render(foo={\"bar\": 42}) == \"42|42\"\n\n    def test_subscript(self, env):\n        tmpl = env.from_string(\"{{ foo[0] }}|{{ foo[-1] }}\")\n        assert tmpl.render(foo=[0, 1, 2]) == \"0|2\"\n\n    def test_tuple(self, env):\n        tmpl = env.from_string(\"{{ () }}|{{ (1,) }}|{{ (1, 2) }}\")\n        assert tmpl.render() == \"()|(1,)|(1, 2)\"\n\n    def test_math(self, env):\n        tmpl = env.from_string(\"{{ (1 + 1 * 2) - 3 / 2 }}|{{ 2**3 }}\")\n        assert tmpl.render() == \"1.5|8\"\n\n    def test_div(self, env):\n        tmpl = env.from_string(\"{{ 3 // 2 }}|{{ 3 / 2 }}|{{ 3 % 2 }}\")\n        assert tmpl.render() == \"1|1.5|1\"\n\n    def test_unary(self, env):\n        tmpl = env.from_string(\"{{ +3 }}|{{ -3 }}\")\n        assert tmpl.render() == \"3|-3\"\n\n    def test_concat(self, env):\n        tmpl = env.from_string(\"{{ [1, 2] ~ 'foo' }}\")\n        assert tmpl.render() == \"[1, 2]foo\"\n\n    @pytest.mark.parametrize(\n        (\"a\", \"op\", \"b\"),\n        [\n            (1, \">\", 0),\n            (1, \">=\", 1),\n            (2, \"<\", 3),\n            (3, \"<=\", 4),\n            (4, \"==\", 4),\n            (4, \"!=\", 5),\n        ],\n    )\n    def test_compare(self, env, a, op, b):\n        t = env.from_string(f\"{{{{ {a} {op} {b} }}}}\")\n        assert t.render() == \"True\"\n\n    def test_compare_parens(self, env):\n        t = env.from_string(\"{{ i * (j < 5) }}\")\n        assert t.render(i=2, j=3) == \"2\"\n\n    @pytest.mark.parametrize(\n        (\"src\", \"expect\"),\n        [\n            (\"{{ 4 < 2 < 3 }}\", \"False\"),\n            (\"{{ a < b < c }}\", \"False\"),\n            (\"{{ 4 > 2 > 3 }}\", \"False\"),\n            (\"{{ a > b > c }}\", \"False\"),\n            (\"{{ 4 > 2 < 3 }}\", \"True\"),\n            (\"{{ a > b < c }}\", \"True\"),\n        ],\n    )\n    def test_compare_compound(self, env, src, expect):\n        t = env.from_string(src)\n        assert t.render(a=4, b=2, c=3) == expect\n\n    def test_inop(self, env):\n        tmpl = env.from_string(\"{{ 1 in [1, 2, 3] }}|{{ 1 not in [1, 2, 3] }}\")\n        assert tmpl.render() == \"True|False\"\n\n    @pytest.mark.parametrize(\"value\", (\"[]\", \"{}\", \"()\"))\n    def test_collection_literal(self, env, value):\n        t = env.from_string(f\"{{{{ {value} }}}}\")\n        assert t.render() == value\n\n    @pytest.mark.parametrize(\n        (\"value\", \"expect\"),\n        (\n            (\"1\", \"1\"),\n            (\"123\", \"123\"),\n            (\"12_34_56\", \"123456\"),\n            (\"1.2\", \"1.2\"),\n            (\"34.56\", \"34.56\"),\n            (\"3_4.5_6\", \"34.56\"),\n            (\"1e0\", \"1.0\"),\n            (\"10e1\", \"100.0\"),\n            (\"2.5e100\", \"2.5e+100\"),\n            (\"2.5e+100\", \"2.5e+100\"),\n            (\"25.6e-10\", \"2.56e-09\"),\n            (\"1_2.3_4e5_6\", \"1.234e+57\"),\n            (\"0\", \"0\"),\n            (\"0_00\", \"0\"),\n            (\"0b1001_1111\", \"159\"),\n            (\"0o123\", \"83\"),\n            (\"0o1_23\", \"83\"),\n            (\"0x123abc\", \"1194684\"),\n            (\"0x12_3abc\", \"1194684\"),\n        ),\n    )\n    def test_numeric_literal(self, env, value, expect):\n        t = env.from_string(f\"{{{{ {value} }}}}\")\n        assert t.render() == expect\n\n    def test_bool(self, env):\n        tmpl = env.from_string(\n            \"{{ true and false }}|{{ false or true }}|{{ not false }}\"\n        )\n        assert tmpl.render() == \"False|True|True\"\n\n    def test_grouping(self, env):\n        tmpl = env.from_string(\n            \"{{ (true and false) or (false and true) and not false }}\"\n        )\n        assert tmpl.render() == \"False\"\n\n    def test_django_attr(self, env):\n        tmpl = env.from_string(\"{{ [1, 2, 3].0 }}|{{ [[1]].0.0 }}\")\n        assert tmpl.render() == \"1|1\"\n\n    def test_conditional_expression(self, env):\n        tmpl = env.from_string(\"\"\"{{ 0 if true else 1 }}\"\"\")\n        assert tmpl.render() == \"0\"\n\n    def test_short_conditional_expression(self, env):\n        tmpl = env.from_string(\"<{{ 1 if false }}>\")\n        assert tmpl.render() == \"<>\"\n\n        tmpl = env.from_string(\"<{{ (1 if false).bar }}>\")\n        pytest.raises(UndefinedError, tmpl.render)\n\n    def test_filter_priority(self, env):\n        tmpl = env.from_string('{{ \"foo\"|upper + \"bar\"|upper }}')\n        assert tmpl.render() == \"FOOBAR\"\n\n    def test_function_calls(self, env):\n        tests = [\n            (True, \"*foo, bar\"),\n            (True, \"*foo, *bar\"),\n            (True, \"**foo, *bar\"),\n            (True, \"**foo, bar\"),\n            (True, \"**foo, **bar\"),\n            (True, \"**foo, bar=42\"),\n            (False, \"foo, bar\"),\n            (False, \"foo, bar=42\"),\n            (False, \"foo, bar=23, *args\"),\n            (False, \"foo, *args, bar=23\"),\n            (False, \"a, b=c, *d, **e\"),\n            (False, \"*foo, bar=42\"),\n            (False, \"*foo, **bar\"),\n            (False, \"*foo, bar=42, **baz\"),\n            (False, \"foo, *args, bar=23, **baz\"),\n        ]\n        for should_fail, sig in tests:\n            if should_fail:\n                with pytest.raises(TemplateSyntaxError):\n                    env.from_string(f\"{{{{ foo({sig}) }}}}\")\n            else:\n                env.from_string(f\"foo({sig})\")\n\n    def test_tuple_expr(self, env):\n        for tmpl in [\n            \"{{ () }}\",\n            \"{{ (1, 2) }}\",\n            \"{{ (1, 2,) }}\",\n            \"{{ 1, }}\",\n            \"{{ 1, 2 }}\",\n            \"{% for foo, bar in seq %}...{% endfor %}\",\n            \"{% for x in foo, bar %}...{% endfor %}\",\n            \"{% for x in foo, %}...{% endfor %}\",\n        ]:\n            assert env.from_string(tmpl)\n\n    def test_trailing_comma(self, env):\n        tmpl = env.from_string(\"{{ (1, 2,) }}|{{ [1, 2,] }}|{{ {1: 2,} }}\")\n        assert tmpl.render().lower() == \"(1, 2)|[1, 2]|{1: 2}\"\n\n    def test_block_end_name(self, env):\n        env.from_string(\"{% block foo %}...{% endblock foo %}\")\n        pytest.raises(\n            TemplateSyntaxError, env.from_string, \"{% block x %}{% endblock y %}\"\n        )\n\n    def test_constant_casing(self, env):\n        for const in True, False, None:\n            const = str(const)\n            tmpl = env.from_string(\n                f\"{{{{ {const} }}}}|{{{{ {const.lower()} }}}}|{{{{ {const.upper()} }}}}\"\n            )\n            assert tmpl.render() == f\"{const}|{const}|\"\n\n    def test_test_chaining(self, env):\n        pytest.raises(\n            TemplateSyntaxError, env.from_string, \"{{ foo is string is sequence }}\"\n        )\n        assert env.from_string(\"{{ 42 is string or 42 is number }}\").render() == \"True\"\n\n    def test_string_concatenation(self, env):\n        tmpl = env.from_string('{{ \"foo\" \"bar\" \"baz\" }}')\n        assert tmpl.render() == \"foobarbaz\"\n\n    def test_notin(self, env):\n        bar = range(100)\n        tmpl = env.from_string(\"\"\"{{ not 42 in bar }}\"\"\")\n        assert tmpl.render(bar=bar) == \"False\"\n\n    def test_operator_precedence(self, env):\n        tmpl = env.from_string(\"\"\"{{ 2 * 3 + 4 % 2 + 1 - 2 }}\"\"\")\n        assert tmpl.render() == \"5\"\n\n    def test_implicit_subscribed_tuple(self, env):\n        class Foo:\n            def __getitem__(self, x):\n                return x\n\n        t = env.from_string(\"{{ foo[1, 2] }}\")\n        assert t.render(foo=Foo()) == \"(1, 2)\"\n\n    def test_raw2(self, env):\n        tmpl = env.from_string(\"{% raw %}{{ FOO }} and {% BAR %}{% endraw %}\")\n        assert tmpl.render() == \"{{ FOO }} and {% BAR %}\"\n\n    def test_const(self, env):\n        tmpl = env.from_string(\n            \"{{ true }}|{{ false }}|{{ none }}|\"\n            \"{{ none is defined }}|{{ missing is defined }}\"\n        )\n        assert tmpl.render() == \"True|False|None|True|False\"\n\n    def test_neg_filter_priority(self, env):\n        node = env.parse(\"{{ -1|foo }}\")\n        assert isinstance(node.body[0].nodes[0], nodes.Filter)\n        assert isinstance(node.body[0].nodes[0].node, nodes.Neg)\n\n    def test_const_assign(self, env):\n        constass1 = \"\"\"{% set true = 42 %}\"\"\"\n        constass2 = \"\"\"{% for none in seq %}{% endfor %}\"\"\"\n        for tmpl in constass1, constass2:\n            pytest.raises(TemplateSyntaxError, env.from_string, tmpl)\n\n    def test_localset(self, env):\n        tmpl = env.from_string(\n            \"\"\"{% set foo = 0 %}\\\n{% for item in [1, 2] %}{% set foo = 1 %}{% endfor %}\\\n{{ foo }}\"\"\"\n        )\n        assert tmpl.render() == \"0\"\n\n    def test_parse_unary(self, env):\n        tmpl = env.from_string('{{ -foo[\"bar\"] }}')\n        assert tmpl.render(foo={\"bar\": 42}) == \"-42\"\n        tmpl = env.from_string('{{ -foo[\"bar\"]|abs }}')\n        assert tmpl.render(foo={\"bar\": 42}) == \"42\"\n\n\nclass TestLstripBlocks:\n    def test_lstrip(self, env):\n        env = Environment(lstrip_blocks=True, trim_blocks=False)\n        tmpl = env.from_string(\"\"\"    {% if True %}\\n    {% endif %}\"\"\")\n        assert tmpl.render() == \"\\n\"\n\n    def test_lstrip_trim(self, env):\n        env = Environment(lstrip_blocks=True, trim_blocks=True)\n        tmpl = env.from_string(\"\"\"    {% if True %}\\n    {% endif %}\"\"\")\n        assert tmpl.render() == \"\"\n\n    def test_no_lstrip(self, env):\n        env = Environment(lstrip_blocks=True, trim_blocks=False)\n        tmpl = env.from_string(\"\"\"    {%+ if True %}\\n    {%+ endif %}\"\"\")\n        assert tmpl.render() == \"    \\n    \"\n\n    def test_lstrip_blocks_false_with_no_lstrip(self, env):\n        # Test that + is a NOP (but does not cause an error) if lstrip_blocks=False\n        env = Environment(lstrip_blocks=False, trim_blocks=False)\n        tmpl = env.from_string(\"\"\"    {% if True %}\\n    {% endif %}\"\"\")\n        assert tmpl.render() == \"    \\n    \"\n        tmpl = env.from_string(\"\"\"    {%+ if True %}\\n    {%+ endif %}\"\"\")\n        assert tmpl.render() == \"    \\n    \"\n\n    def test_lstrip_endline(self, env):\n        env = Environment(lstrip_blocks=True, trim_blocks=False)\n        tmpl = env.from_string(\"\"\"    hello{% if True %}\\n    goodbye{% endif %}\"\"\")\n        assert tmpl.render() == \"    hello\\n    goodbye\"\n\n    def test_lstrip_inline(self, env):\n        env = Environment(lstrip_blocks=True, trim_blocks=False)\n        tmpl = env.from_string(\"\"\"    {% if True %}hello    {% endif %}\"\"\")\n        assert tmpl.render() == \"hello    \"\n\n    def test_lstrip_nested(self, env):\n        env = Environment(lstrip_blocks=True, trim_blocks=False)\n        tmpl = env.from_string(\n            \"\"\"    {% if True %}a {% if True %}b {% endif %}c {% endif %}\"\"\"\n        )\n        assert tmpl.render() == \"a b c \"\n\n    def test_lstrip_left_chars(self, env):\n        env = Environment(lstrip_blocks=True, trim_blocks=False)\n        tmpl = env.from_string(\n            \"\"\"    abc {% if True %}\n        hello{% endif %}\"\"\"\n        )\n        assert tmpl.render() == \"    abc \\n        hello\"\n\n    def test_lstrip_embeded_strings(self, env):\n        env = Environment(lstrip_blocks=True, trim_blocks=False)\n        tmpl = env.from_string(\"\"\"    {% set x = \" {% str %} \" %}{{ x }}\"\"\")\n        assert tmpl.render() == \" {% str %} \"\n\n    def test_lstrip_preserve_leading_newlines(self, env):\n        env = Environment(lstrip_blocks=True, trim_blocks=False)\n        tmpl = env.from_string(\"\"\"\\n\\n\\n{% set hello = 1 %}\"\"\")\n        assert tmpl.render() == \"\\n\\n\\n\"\n\n    def test_lstrip_comment(self, env):\n        env = Environment(lstrip_blocks=True, trim_blocks=False)\n        tmpl = env.from_string(\n            \"\"\"    {# if True #}\nhello\n    {#endif#}\"\"\"\n        )\n        assert tmpl.render() == \"\\nhello\\n\"\n\n    def test_lstrip_angle_bracket_simple(self, env):\n        env = Environment(\n            \"<%\",\n            \"%>\",\n            \"${\",\n            \"}\",\n            \"<%#\",\n            \"%>\",\n            \"%\",\n            \"##\",\n            lstrip_blocks=True,\n            trim_blocks=True,\n        )\n        tmpl = env.from_string(\"\"\"    <% if True %>hello    <% endif %>\"\"\")\n        assert tmpl.render() == \"hello    \"\n\n    def test_lstrip_angle_bracket_comment(self, env):\n        env = Environment(\n            \"<%\",\n            \"%>\",\n            \"${\",\n            \"}\",\n            \"<%#\",\n            \"%>\",\n            \"%\",\n            \"##\",\n            lstrip_blocks=True,\n            trim_blocks=True,\n        )\n        tmpl = env.from_string(\"\"\"    <%# if True %>hello    <%# endif %>\"\"\")\n        assert tmpl.render() == \"hello    \"\n\n    def test_lstrip_angle_bracket(self, env):\n        env = Environment(\n            \"<%\",\n            \"%>\",\n            \"${\",\n            \"}\",\n            \"<%#\",\n            \"%>\",\n            \"%\",\n            \"##\",\n            lstrip_blocks=True,\n            trim_blocks=True,\n        )\n        tmpl = env.from_string(\n            \"\"\"\\\n    <%# regular comment %>\n    <% for item in seq %>\n${item} ## the rest of the stuff\n   <% endfor %>\"\"\"\n        )\n        assert tmpl.render(seq=range(5)) == \"\".join(f\"{x}\\n\" for x in range(5))\n\n    def test_lstrip_angle_bracket_compact(self, env):\n        env = Environment(\n            \"<%\",\n            \"%>\",\n            \"${\",\n            \"}\",\n            \"<%#\",\n            \"%>\",\n            \"%\",\n            \"##\",\n            lstrip_blocks=True,\n            trim_blocks=True,\n        )\n        tmpl = env.from_string(\n            \"\"\"\\\n    <%#regular comment%>\n    <%for item in seq%>\n${item} ## the rest of the stuff\n   <%endfor%>\"\"\"\n        )\n        assert tmpl.render(seq=range(5)) == \"\".join(f\"{x}\\n\" for x in range(5))\n\n    def test_lstrip_blocks_outside_with_new_line(self):\n        env = Environment(lstrip_blocks=True, trim_blocks=False)\n        tmpl = env.from_string(\n            \"  {% if kvs %}(\\n\"\n            \"   {% for k, v in kvs %}{{ k }}={{ v }} {% endfor %}\\n\"\n            \"  ){% endif %}\"\n        )\n        out = tmpl.render(kvs=[(\"a\", 1), (\"b\", 2)])\n        assert out == \"(\\na=1 b=2 \\n  )\"\n\n    def test_lstrip_trim_blocks_outside_with_new_line(self):\n        env = Environment(lstrip_blocks=True, trim_blocks=True)\n        tmpl = env.from_string(\n            \"  {% if kvs %}(\\n\"\n            \"   {% for k, v in kvs %}{{ k }}={{ v }} {% endfor %}\\n\"\n            \"  ){% endif %}\"\n        )\n        out = tmpl.render(kvs=[(\"a\", 1), (\"b\", 2)])\n        assert out == \"(\\na=1 b=2   )\"\n\n    def test_lstrip_blocks_inside_with_new_line(self):\n        env = Environment(lstrip_blocks=True, trim_blocks=False)\n        tmpl = env.from_string(\n            \"  ({% if kvs %}\\n\"\n            \"   {% for k, v in kvs %}{{ k }}={{ v }} {% endfor %}\\n\"\n            \"  {% endif %})\"\n        )\n        out = tmpl.render(kvs=[(\"a\", 1), (\"b\", 2)])\n        assert out == \"  (\\na=1 b=2 \\n)\"\n\n    def test_lstrip_trim_blocks_inside_with_new_line(self):\n        env = Environment(lstrip_blocks=True, trim_blocks=True)\n        tmpl = env.from_string(\n            \"  ({% if kvs %}\\n\"\n            \"   {% for k, v in kvs %}{{ k }}={{ v }} {% endfor %}\\n\"\n            \"  {% endif %})\"\n        )\n        out = tmpl.render(kvs=[(\"a\", 1), (\"b\", 2)])\n        assert out == \"  (a=1 b=2 )\"\n\n    def test_lstrip_blocks_without_new_line(self):\n        env = Environment(lstrip_blocks=True, trim_blocks=False)\n        tmpl = env.from_string(\n            \"  {% if kvs %}\"\n            \"   {% for k, v in kvs %}{{ k }}={{ v }} {% endfor %}\"\n            \"  {% endif %}\"\n        )\n        out = tmpl.render(kvs=[(\"a\", 1), (\"b\", 2)])\n        assert out == \"   a=1 b=2   \"\n\n    def test_lstrip_trim_blocks_without_new_line(self):\n        env = Environment(lstrip_blocks=True, trim_blocks=True)\n        tmpl = env.from_string(\n            \"  {% if kvs %}\"\n            \"   {% for k, v in kvs %}{{ k }}={{ v }} {% endfor %}\"\n            \"  {% endif %}\"\n        )\n        out = tmpl.render(kvs=[(\"a\", 1), (\"b\", 2)])\n        assert out == \"   a=1 b=2   \"\n\n    def test_lstrip_blocks_consume_after_without_new_line(self):\n        env = Environment(lstrip_blocks=True, trim_blocks=False)\n        tmpl = env.from_string(\n            \"  {% if kvs -%}\"\n            \"   {% for k, v in kvs %}{{ k }}={{ v }} {% endfor -%}\"\n            \"  {% endif -%}\"\n        )\n        out = tmpl.render(kvs=[(\"a\", 1), (\"b\", 2)])\n        assert out == \"a=1 b=2 \"\n\n    def test_lstrip_trim_blocks_consume_before_without_new_line(self):\n        env = Environment(lstrip_blocks=False, trim_blocks=False)\n        tmpl = env.from_string(\n            \"  {%- if kvs %}\"\n            \"   {%- for k, v in kvs %}{{ k }}={{ v }} {% endfor -%}\"\n            \"  {%- endif %}\"\n        )\n        out = tmpl.render(kvs=[(\"a\", 1), (\"b\", 2)])\n        assert out == \"a=1 b=2 \"\n\n    def test_lstrip_trim_blocks_comment(self):\n        env = Environment(lstrip_blocks=True, trim_blocks=True)\n        tmpl = env.from_string(\" {# 1 space #}\\n  {# 2 spaces #}    {# 4 spaces #}\")\n        out = tmpl.render()\n        assert out == \" \" * 4\n\n    def test_lstrip_trim_blocks_raw(self):\n        env = Environment(lstrip_blocks=True, trim_blocks=True)\n        tmpl = env.from_string(\"{{x}}\\n{%- raw %} {% endraw -%}\\n{{ y }}\")\n        out = tmpl.render(x=1, y=2)\n        assert out == \"1 2\"\n\n    def test_php_syntax_with_manual(self, env):\n        env = Environment(\n            \"<?\", \"?>\", \"<?=\", \"?>\", \"<!--\", \"-->\", lstrip_blocks=True, trim_blocks=True\n        )\n        tmpl = env.from_string(\n            \"\"\"\\\n    <!-- I'm a comment, I'm not interesting -->\n    <? for item in seq -?>\n        <?= item ?>\n    <?- endfor ?>\"\"\"\n        )\n        assert tmpl.render(seq=range(5)) == \"01234\"\n\n    def test_php_syntax(self, env):\n        env = Environment(\n            \"<?\", \"?>\", \"<?=\", \"?>\", \"<!--\", \"-->\", lstrip_blocks=True, trim_blocks=True\n        )\n        tmpl = env.from_string(\n            \"\"\"\\\n    <!-- I'm a comment, I'm not interesting -->\n    <? for item in seq ?>\n        <?= item ?>\n    <? endfor ?>\"\"\"\n        )\n        assert tmpl.render(seq=range(5)) == \"\".join(f\"        {x}\\n\" for x in range(5))\n\n    def test_php_syntax_compact(self, env):\n        env = Environment(\n            \"<?\", \"?>\", \"<?=\", \"?>\", \"<!--\", \"-->\", lstrip_blocks=True, trim_blocks=True\n        )\n        tmpl = env.from_string(\n            \"\"\"\\\n    <!-- I'm a comment, I'm not interesting -->\n    <?for item in seq?>\n        <?=item?>\n    <?endfor?>\"\"\"\n        )\n        assert tmpl.render(seq=range(5)) == \"\".join(f\"        {x}\\n\" for x in range(5))\n\n    def test_erb_syntax(self, env):\n        env = Environment(\n            \"<%\", \"%>\", \"<%=\", \"%>\", \"<%#\", \"%>\", lstrip_blocks=True, trim_blocks=True\n        )\n        tmpl = env.from_string(\n            \"\"\"\\\n<%# I'm a comment, I'm not interesting %>\n    <% for item in seq %>\n    <%= item %>\n    <% endfor %>\n\"\"\"\n        )\n        assert tmpl.render(seq=range(5)) == \"\".join(f\"    {x}\\n\" for x in range(5))\n\n    def test_erb_syntax_with_manual(self, env):\n        env = Environment(\n            \"<%\", \"%>\", \"<%=\", \"%>\", \"<%#\", \"%>\", lstrip_blocks=True, trim_blocks=True\n        )\n        tmpl = env.from_string(\n            \"\"\"\\\n<%# I'm a comment, I'm not interesting %>\n    <% for item in seq -%>\n        <%= item %>\n    <%- endfor %>\"\"\"\n        )\n        assert tmpl.render(seq=range(5)) == \"01234\"\n\n    def test_erb_syntax_no_lstrip(self, env):\n        env = Environment(\n            \"<%\", \"%>\", \"<%=\", \"%>\", \"<%#\", \"%>\", lstrip_blocks=True, trim_blocks=True\n        )\n        tmpl = env.from_string(\n            \"\"\"\\\n<%# I'm a comment, I'm not interesting %>\n    <%+ for item in seq -%>\n        <%= item %>\n    <%- endfor %>\"\"\"\n        )\n        assert tmpl.render(seq=range(5)) == \"    01234\"\n\n    def test_comment_syntax(self, env):\n        env = Environment(\n            \"<!--\",\n            \"-->\",\n            \"${\",\n            \"}\",\n            \"<!--#\",\n            \"-->\",\n            lstrip_blocks=True,\n            trim_blocks=True,\n        )\n        tmpl = env.from_string(\n            \"\"\"\\\n<!--# I'm a comment, I'm not interesting -->\\\n<!-- for item in seq --->\n    ${item}\n<!--- endfor -->\"\"\"\n        )\n        assert tmpl.render(seq=range(5)) == \"01234\"\n\n\nclass TestTrimBlocks:\n    def test_trim(self, env):\n        env = Environment(trim_blocks=True, lstrip_blocks=False)\n        tmpl = env.from_string(\"    {% if True %}\\n    {% endif %}\")\n        assert tmpl.render() == \"        \"\n\n    def test_no_trim(self, env):\n        env = Environment(trim_blocks=True, lstrip_blocks=False)\n        tmpl = env.from_string(\"    {% if True +%}\\n    {% endif %}\")\n        assert tmpl.render() == \"    \\n    \"\n\n    def test_no_trim_outer(self, env):\n        env = Environment(trim_blocks=True, lstrip_blocks=False)\n        tmpl = env.from_string(\"{% if True %}X{% endif +%}\\nmore things\")\n        assert tmpl.render() == \"X\\nmore things\"\n\n    def test_lstrip_no_trim(self, env):\n        env = Environment(trim_blocks=True, lstrip_blocks=True)\n        tmpl = env.from_string(\"    {% if True +%}\\n    {% endif %}\")\n        assert tmpl.render() == \"\\n\"\n\n    def test_trim_blocks_false_with_no_trim(self, env):\n        # Test that + is a NOP (but does not cause an error) if trim_blocks=False\n        env = Environment(trim_blocks=False, lstrip_blocks=False)\n        tmpl = env.from_string(\"    {% if True %}\\n    {% endif %}\")\n        assert tmpl.render() == \"    \\n    \"\n        tmpl = env.from_string(\"    {% if True +%}\\n    {% endif %}\")\n        assert tmpl.render() == \"    \\n    \"\n\n        tmpl = env.from_string(\"    {# comment #}\\n    \")\n        assert tmpl.render() == \"    \\n    \"\n        tmpl = env.from_string(\"    {# comment +#}\\n    \")\n        assert tmpl.render() == \"    \\n    \"\n\n        tmpl = env.from_string(\"    {% raw %}{% endraw %}\\n    \")\n        assert tmpl.render() == \"    \\n    \"\n        tmpl = env.from_string(\"    {% raw %}{% endraw +%}\\n    \")\n        assert tmpl.render() == \"    \\n    \"\n\n    def test_trim_nested(self, env):\n        env = Environment(trim_blocks=True, lstrip_blocks=True)\n        tmpl = env.from_string(\n            \"    {% if True %}\\na {% if True %}\\nb {% endif %}\\nc {% endif %}\"\n        )\n        assert tmpl.render() == \"a b c \"\n\n    def test_no_trim_nested(self, env):\n        env = Environment(trim_blocks=True, lstrip_blocks=True)\n        tmpl = env.from_string(\n            \"    {% if True +%}\\na {% if True +%}\\nb {% endif +%}\\nc {% endif %}\"\n        )\n        assert tmpl.render() == \"\\na \\nb \\nc \"\n\n    def test_comment_trim(self, env):\n        env = Environment(trim_blocks=True, lstrip_blocks=True)\n        tmpl = env.from_string(\"\"\"    {# comment #}\\n\\n  \"\"\")\n        assert tmpl.render() == \"\\n  \"\n\n    def test_comment_no_trim(self, env):\n        env = Environment(trim_blocks=True, lstrip_blocks=True)\n        tmpl = env.from_string(\"\"\"    {# comment +#}\\n\\n  \"\"\")\n        assert tmpl.render() == \"\\n\\n  \"\n\n    def test_multiple_comment_trim_lstrip(self, env):\n        env = Environment(trim_blocks=True, lstrip_blocks=True)\n        tmpl = env.from_string(\n            \"   {# comment #}\\n\\n{# comment2 #}\\n   \\n{# comment3 #}\\n\\n \"\n        )\n        assert tmpl.render() == \"\\n   \\n\\n \"\n\n    def test_multiple_comment_no_trim_lstrip(self, env):\n        env = Environment(trim_blocks=True, lstrip_blocks=True)\n        tmpl = env.from_string(\n            \"   {# comment +#}\\n\\n{# comment2 +#}\\n   \\n{# comment3 +#}\\n\\n \"\n        )\n        assert tmpl.render() == \"\\n\\n\\n   \\n\\n\\n \"\n\n    def test_raw_trim_lstrip(self, env):\n        env = Environment(trim_blocks=True, lstrip_blocks=True)\n        tmpl = env.from_string(\"{{x}}{% raw %}\\n\\n    {% endraw %}\\n\\n{{ y }}\")\n        assert tmpl.render(x=1, y=2) == \"1\\n\\n\\n2\"\n\n    def test_raw_no_trim_lstrip(self, env):\n        env = Environment(trim_blocks=False, lstrip_blocks=True)\n        tmpl = env.from_string(\"{{x}}{% raw %}\\n\\n      {% endraw +%}\\n\\n{{ y }}\")\n        assert tmpl.render(x=1, y=2) == \"1\\n\\n\\n\\n2\"\n\n        # raw blocks do not process inner text, so start tag cannot ignore trim\n        with pytest.raises(TemplateSyntaxError):\n            tmpl = env.from_string(\"{{x}}{% raw +%}\\n\\n  {% endraw +%}\\n\\n{{ y }}\")\n\n    def test_no_trim_angle_bracket(self, env):\n        env = Environment(\n            \"<%\", \"%>\", \"${\", \"}\", \"<%#\", \"%>\", lstrip_blocks=True, trim_blocks=True\n        )\n        tmpl = env.from_string(\"    <% if True +%>\\n\\n    <% endif %>\")\n        assert tmpl.render() == \"\\n\\n\"\n\n        tmpl = env.from_string(\"    <%# comment +%>\\n\\n   \")\n        assert tmpl.render() == \"\\n\\n   \"\n\n    def test_no_trim_php_syntax(self, env):\n        env = Environment(\n            \"<?\",\n            \"?>\",\n            \"<?=\",\n            \"?>\",\n            \"<!--\",\n            \"-->\",\n            lstrip_blocks=False,\n            trim_blocks=True,\n        )\n        tmpl = env.from_string(\"    <? if True +?>\\n\\n    <? endif ?>\")\n        assert tmpl.render() == \"    \\n\\n    \"\n        tmpl = env.from_string(\"    <!-- comment +-->\\n\\n    \")\n        assert tmpl.render() == \"    \\n\\n    \"\n"
  },
  {
    "path": "tests/test_loader.py",
    "content": "import importlib.abc\nimport importlib.machinery\nimport importlib.util\nimport os\nimport shutil\nimport sys\nimport tempfile\nimport time\nimport weakref\nfrom pathlib import Path\n\nimport pytest\n\nfrom jinja2 import Environment\nfrom jinja2 import loaders\nfrom jinja2 import PackageLoader\nfrom jinja2.exceptions import TemplateNotFound\nfrom jinja2.loaders import split_template_path\n\n\nclass TestLoaders:\n    def test_dict_loader(self, dict_loader):\n        env = Environment(loader=dict_loader)\n        tmpl = env.get_template(\"justdict.html\")\n        assert tmpl.render().strip() == \"FOO\"\n        pytest.raises(TemplateNotFound, env.get_template, \"missing.html\")\n\n    def test_package_loader(self, package_loader):\n        env = Environment(loader=package_loader)\n        tmpl = env.get_template(\"test.html\")\n        assert tmpl.render().strip() == \"BAR\"\n        pytest.raises(TemplateNotFound, env.get_template, \"missing.html\")\n\n    def test_filesystem_loader_overlapping_names(self, filesystem_loader):\n        t2_dir = Path(filesystem_loader.searchpath[0]) / \"..\" / \"templates2\"\n        # Make \"foo\" show up before \"foo/test.html\".\n        filesystem_loader.searchpath.insert(0, t2_dir)\n        e = Environment(loader=filesystem_loader)\n        e.get_template(\"foo\")\n        # This would raise NotADirectoryError if \"t2/foo\" wasn't skipped.\n        e.get_template(\"foo/test.html\")\n\n    def test_choice_loader(self, choice_loader):\n        env = Environment(loader=choice_loader)\n        tmpl = env.get_template(\"justdict.html\")\n        assert tmpl.render().strip() == \"FOO\"\n        tmpl = env.get_template(\"test.html\")\n        assert tmpl.render().strip() == \"BAR\"\n        pytest.raises(TemplateNotFound, env.get_template, \"missing.html\")\n\n    def test_function_loader(self, function_loader):\n        env = Environment(loader=function_loader)\n        tmpl = env.get_template(\"justfunction.html\")\n        assert tmpl.render().strip() == \"FOO\"\n        pytest.raises(TemplateNotFound, env.get_template, \"missing.html\")\n\n    def test_prefix_loader(self, prefix_loader):\n        env = Environment(loader=prefix_loader)\n        tmpl = env.get_template(\"a/test.html\")\n        assert tmpl.render().strip() == \"BAR\"\n        tmpl = env.get_template(\"b/justdict.html\")\n        assert tmpl.render().strip() == \"FOO\"\n        pytest.raises(TemplateNotFound, env.get_template, \"missing\")\n\n    def test_caching(self):\n        changed = False\n\n        class TestLoader(loaders.BaseLoader):\n            def get_source(self, environment, template):\n                return \"foo\", None, lambda: not changed\n\n        env = Environment(loader=TestLoader(), cache_size=-1)\n        tmpl = env.get_template(\"template\")\n        assert tmpl is env.get_template(\"template\")\n        changed = True\n        assert tmpl is not env.get_template(\"template\")\n        changed = False\n\n    def test_no_cache(self):\n        mapping = {\"foo\": \"one\"}\n        env = Environment(loader=loaders.DictLoader(mapping), cache_size=0)\n        assert env.get_template(\"foo\") is not env.get_template(\"foo\")\n\n    def test_limited_size_cache(self):\n        mapping = {\"one\": \"foo\", \"two\": \"bar\", \"three\": \"baz\"}\n        loader = loaders.DictLoader(mapping)\n        env = Environment(loader=loader, cache_size=2)\n        t1 = env.get_template(\"one\")\n        t2 = env.get_template(\"two\")\n        assert t2 is env.get_template(\"two\")\n        assert t1 is env.get_template(\"one\")\n        env.get_template(\"three\")\n        loader_ref = weakref.ref(loader)\n        assert (loader_ref, \"one\") in env.cache\n        assert (loader_ref, \"two\") not in env.cache\n        assert (loader_ref, \"three\") in env.cache\n\n    def test_cache_loader_change(self):\n        loader1 = loaders.DictLoader({\"foo\": \"one\"})\n        loader2 = loaders.DictLoader({\"foo\": \"two\"})\n        env = Environment(loader=loader1, cache_size=2)\n        assert env.get_template(\"foo\").render() == \"one\"\n        env.loader = loader2\n        assert env.get_template(\"foo\").render() == \"two\"\n\n    def test_dict_loader_cache_invalidates(self):\n        mapping = {\"foo\": \"one\"}\n        env = Environment(loader=loaders.DictLoader(mapping))\n        assert env.get_template(\"foo\").render() == \"one\"\n        mapping[\"foo\"] = \"two\"\n        assert env.get_template(\"foo\").render() == \"two\"\n\n    def test_split_template_path(self):\n        assert split_template_path(\"foo/bar\") == [\"foo\", \"bar\"]\n        assert split_template_path(\"./foo/bar\") == [\"foo\", \"bar\"]\n        pytest.raises(TemplateNotFound, split_template_path, \"../foo\")\n\n\nclass TestFileSystemLoader:\n    searchpath = (Path(__file__) / \"..\" / \"res\" / \"templates\").resolve()\n\n    @staticmethod\n    def _test_common(env):\n        tmpl = env.get_template(\"test.html\")\n        assert tmpl.render().strip() == \"BAR\"\n        tmpl = env.get_template(\"foo/test.html\")\n        assert tmpl.render().strip() == \"FOO\"\n        pytest.raises(TemplateNotFound, env.get_template, \"missing.html\")\n\n    def test_searchpath_as_str(self):\n        filesystem_loader = loaders.FileSystemLoader(str(self.searchpath))\n\n        env = Environment(loader=filesystem_loader)\n        self._test_common(env)\n\n    def test_searchpath_as_pathlib(self):\n        filesystem_loader = loaders.FileSystemLoader(self.searchpath)\n        env = Environment(loader=filesystem_loader)\n        self._test_common(env)\n\n    def test_searchpath_as_list_including_pathlib(self):\n        filesystem_loader = loaders.FileSystemLoader(\n            [\"/tmp/templates\", self.searchpath]\n        )\n        env = Environment(loader=filesystem_loader)\n        self._test_common(env)\n\n    def test_caches_template_based_on_mtime(self):\n        filesystem_loader = loaders.FileSystemLoader(self.searchpath)\n\n        env = Environment(loader=filesystem_loader)\n        tmpl1 = env.get_template(\"test.html\")\n        tmpl2 = env.get_template(\"test.html\")\n        assert tmpl1 is tmpl2\n\n        os.utime(self.searchpath / \"test.html\", (time.time(), time.time()))\n        tmpl3 = env.get_template(\"test.html\")\n        assert tmpl1 is not tmpl3\n\n    @pytest.mark.parametrize(\n        (\"encoding\", \"expect\"),\n        [\n            (\"utf-8\", \"文字化け\"),\n            (\"iso-8859-1\", \"æ\\x96\\x87\\xe5\\xad\\x97\\xe5\\x8c\\x96\\xe3\\x81\\x91\"),\n        ],\n    )\n    def test_uses_specified_encoding(self, encoding, expect):\n        loader = loaders.FileSystemLoader(self.searchpath, encoding=encoding)\n        e = Environment(loader=loader)\n        t = e.get_template(\"mojibake.txt\")\n        assert t.render() == expect\n\n    def test_filename_normpath(self):\n        \"\"\"Nested template names should only contain ``os.sep`` in the\n        loaded filename.\n        \"\"\"\n        loader = loaders.FileSystemLoader(self.searchpath)\n        e = Environment(loader=loader)\n        t = e.get_template(\"foo/test.html\")\n        assert t.filename == str(self.searchpath / \"foo\" / \"test.html\")\n\n    def test_error_includes_paths(self, env, filesystem_loader):\n        env.loader = filesystem_loader\n\n        with pytest.raises(TemplateNotFound) as info:\n            env.get_template(\"missing\")\n\n        e_str = str(info.value)\n        assert e_str.startswith(\"'missing' not found in search path: \")\n\n        filesystem_loader.searchpath.append(\"other\")\n\n        with pytest.raises(TemplateNotFound) as info:\n            env.get_template(\"missing\")\n\n        e_str = str(info.value)\n        assert e_str.startswith(\"'missing' not found in search paths: \")\n        assert \", 'other'\" in e_str\n\n\nclass TestModuleLoader:\n    archive = None\n    mod_env = None\n\n    def compile_down(self, prefix_loader, zip=\"deflated\"):\n        log = []\n        self.reg_env = Environment(loader=prefix_loader)\n        if zip is not None:\n            fd, self.archive = tempfile.mkstemp(suffix=\".zip\")\n            os.close(fd)\n        else:\n            self.archive = tempfile.mkdtemp()\n        self.reg_env.compile_templates(self.archive, zip=zip, log_function=log.append)\n        self.mod_env = Environment(loader=loaders.ModuleLoader(self.archive))\n        return \"\".join(log)\n\n    def teardown_method(self):\n        if self.archive is not None:\n            if os.path.isfile(self.archive):\n                os.remove(self.archive)\n            else:\n                shutil.rmtree(self.archive)\n            self.archive = None\n            self.mod_env = None\n\n    def test_log(self, prefix_loader):\n        log = self.compile_down(prefix_loader)\n        assert (\n            'Compiled \"a/foo/test.html\" as '\n            \"tmpl_a790caf9d669e39ea4d280d597ec891c4ef0404a\" in log\n        )\n        assert \"Finished compiling templates\" in log\n        assert (\n            'Could not compile \"a/syntaxerror.html\": '\n            \"Encountered unknown tag 'endif'\" in log\n        )\n\n    def _test_common(self):\n        tmpl1 = self.reg_env.get_template(\"a/test.html\")\n        tmpl2 = self.mod_env.get_template(\"a/test.html\")\n        assert tmpl1.render() == tmpl2.render()\n\n        tmpl1 = self.reg_env.get_template(\"b/justdict.html\")\n        tmpl2 = self.mod_env.get_template(\"b/justdict.html\")\n        assert tmpl1.render() == tmpl2.render()\n\n    def test_deflated_zip_compile(self, prefix_loader):\n        self.compile_down(prefix_loader, zip=\"deflated\")\n        self._test_common()\n\n    def test_stored_zip_compile(self, prefix_loader):\n        self.compile_down(prefix_loader, zip=\"stored\")\n        self._test_common()\n\n    def test_filesystem_compile(self, prefix_loader):\n        self.compile_down(prefix_loader, zip=None)\n        self._test_common()\n\n    def test_weak_references(self, prefix_loader):\n        self.compile_down(prefix_loader)\n        self.mod_env.get_template(\"a/test.html\")\n        key = loaders.ModuleLoader.get_template_key(\"a/test.html\")\n        name = self.mod_env.loader.module.__name__\n\n        assert hasattr(self.mod_env.loader.module, key)\n        assert name in sys.modules\n\n        # unset all, ensure the module is gone from sys.modules\n        self.mod_env = None\n\n        try:\n            import gc\n\n            gc.collect()\n        except BaseException:\n            pass\n\n        assert name not in sys.modules\n\n    def test_choice_loader(self, prefix_loader):\n        self.compile_down(prefix_loader)\n        self.mod_env.loader = loaders.ChoiceLoader(\n            [self.mod_env.loader, loaders.DictLoader({\"DICT_SOURCE\": \"DICT_TEMPLATE\"})]\n        )\n        tmpl1 = self.mod_env.get_template(\"a/test.html\")\n        assert tmpl1.render() == \"BAR\"\n        tmpl2 = self.mod_env.get_template(\"DICT_SOURCE\")\n        assert tmpl2.render() == \"DICT_TEMPLATE\"\n\n    def test_prefix_loader(self, prefix_loader):\n        self.compile_down(prefix_loader)\n        self.mod_env.loader = loaders.PrefixLoader(\n            {\n                \"MOD\": self.mod_env.loader,\n                \"DICT\": loaders.DictLoader({\"test.html\": \"DICT_TEMPLATE\"}),\n            }\n        )\n        tmpl1 = self.mod_env.get_template(\"MOD/a/test.html\")\n        assert tmpl1.render() == \"BAR\"\n        tmpl2 = self.mod_env.get_template(\"DICT/test.html\")\n        assert tmpl2.render() == \"DICT_TEMPLATE\"\n\n    def test_path_as_pathlib(self, prefix_loader):\n        self.compile_down(prefix_loader)\n\n        mod_path = self.mod_env.loader.module.__path__[0]\n        mod_loader = loaders.ModuleLoader(Path(mod_path))\n        self.mod_env = Environment(loader=mod_loader)\n\n        self._test_common()\n\n    def test_supports_pathlib_in_list_of_paths(self, prefix_loader):\n        self.compile_down(prefix_loader)\n\n        mod_path = self.mod_env.loader.module.__path__[0]\n        mod_loader = loaders.ModuleLoader([Path(mod_path), \"/tmp/templates\"])\n        self.mod_env = Environment(loader=mod_loader)\n\n        self._test_common()\n\n\n@pytest.fixture()\ndef package_dir_loader(monkeypatch):\n    monkeypatch.syspath_prepend(Path(__file__).parent)\n    return PackageLoader(\"res\")\n\n\n@pytest.mark.parametrize(\n    (\"template\", \"expect\"), [(\"foo/test.html\", \"FOO\"), (\"test.html\", \"BAR\")]\n)\ndef test_package_dir_source(package_dir_loader, template, expect):\n    source, name, up_to_date = package_dir_loader.get_source(None, template)\n    assert source.rstrip() == expect\n    assert name.endswith(os.path.join(*split_template_path(template)))\n    assert up_to_date()\n\n\ndef test_package_dir_list(package_dir_loader):\n    templates = package_dir_loader.list_templates()\n    assert \"foo/test.html\" in templates\n    assert \"test.html\" in templates\n\n\n@pytest.fixture()\ndef package_file_loader(monkeypatch):\n    monkeypatch.syspath_prepend(Path(__file__).parent / \"res\")\n    return PackageLoader(\"__init__\")\n\n\n@pytest.mark.parametrize(\n    (\"template\", \"expect\"), [(\"foo/test.html\", \"FOO\"), (\"test.html\", \"BAR\")]\n)\ndef test_package_file_source(package_file_loader, template, expect):\n    source, name, up_to_date = package_file_loader.get_source(None, template)\n    assert source.rstrip() == expect\n    assert name.endswith(os.path.join(*split_template_path(template)))\n    assert up_to_date()\n\n\ndef test_package_file_list(package_file_loader):\n    templates = package_file_loader.list_templates()\n    assert \"foo/test.html\" in templates\n    assert \"test.html\" in templates\n\n\n@pytest.fixture()\ndef package_zip_loader(monkeypatch):\n    package_zip = (Path(__file__) / \"..\" / \"res\" / \"package.zip\").resolve()\n    monkeypatch.syspath_prepend(package_zip)\n    return PackageLoader(\"t_pack\")\n\n\n@pytest.mark.parametrize(\n    (\"template\", \"expect\"), [(\"foo/test.html\", \"FOO\"), (\"test.html\", \"BAR\")]\n)\ndef test_package_zip_source(package_zip_loader, template, expect):\n    source, name, up_to_date = package_zip_loader.get_source(None, template)\n    assert source.rstrip() == expect\n    assert name.endswith(os.path.join(*split_template_path(template)))\n    assert up_to_date is None\n\n\n@pytest.mark.xfail(\n    sys.implementation.name == \"pypy\",\n    reason=\"zipimporter doesn't have a '_files' attribute\",\n    raises=TypeError,\n)\ndef test_package_zip_list(package_zip_loader):\n    assert package_zip_loader.list_templates() == [\"foo/test.html\", \"test.html\"]\n\n\n@pytest.mark.parametrize(\"package_path\", [\"\", \".\", \"./\"])\ndef test_package_zip_omit_curdir(package_zip_loader, package_path):\n    \"\"\"PackageLoader should not add or include \".\" or \"./\" in the root\n    path, it is invalid in zip paths.\n    \"\"\"\n    loader = PackageLoader(\"t_pack\", package_path)\n    assert loader.package_path == \"\"\n    source, _, _ = loader.get_source(None, \"templates/foo/test.html\")\n    assert source.rstrip() == \"FOO\"\n\n\ndef test_pep_451_import_hook():\n    class ImportHook(importlib.abc.MetaPathFinder, importlib.abc.Loader):\n        def find_spec(self, name, path=None, target=None):\n            if name != \"res\":\n                return None\n\n            spec = importlib.machinery.PathFinder.find_spec(name)\n            return importlib.util.spec_from_file_location(\n                name,\n                spec.origin,\n                loader=self,\n                submodule_search_locations=spec.submodule_search_locations,\n            )\n\n        def create_module(self, spec):\n            return None  # default behaviour is fine\n\n        def exec_module(self, module):\n            return None  # we need this to satisfy the interface, it's wrong\n\n    # ensure we restore `sys.meta_path` after putting in our loader\n    before = sys.meta_path[:]\n\n    try:\n        sys.meta_path.insert(0, ImportHook())\n        package_loader = PackageLoader(\"res\")\n        assert \"test.html\" in package_loader.list_templates()\n    finally:\n        sys.meta_path[:] = before\n\n\ndef test_package_loader_no_dir() -> None:\n    with pytest.raises(ValueError, match=\"could not find a 'templates' directory\"):\n        PackageLoader(\"jinja2\")\n"
  },
  {
    "path": "tests/test_nativetypes.py",
    "content": "import math\n\nimport pytest\n\nfrom jinja2.exceptions import UndefinedError\nfrom jinja2.nativetypes import NativeEnvironment\nfrom jinja2.nativetypes import NativeTemplate\nfrom jinja2.runtime import Undefined\n\n\n@pytest.fixture\ndef env():\n    return NativeEnvironment()\n\n\n@pytest.fixture\ndef async_native_env():\n    return NativeEnvironment(enable_async=True)\n\n\ndef test_is_defined_native_return(env):\n    t = env.from_string(\"{{ missing is defined }}\")\n    assert not t.render()\n\n\ndef test_undefined_native_return(env):\n    t = env.from_string(\"{{ missing }}\")\n    assert isinstance(t.render(), Undefined)\n\n\ndef test_adding_undefined_native_return(env):\n    t = env.from_string(\"{{ 3 + missing }}\")\n\n    with pytest.raises(UndefinedError):\n        t.render()\n\n\ndef test_cast_int(env):\n    t = env.from_string(\"{{ value|int }}\")\n    result = t.render(value=\"3\")\n    assert isinstance(result, int)\n    assert result == 3\n\n\ndef test_list_add(env):\n    t = env.from_string(\"{{ a + b }}\")\n    result = t.render(a=[\"a\", \"b\"], b=[\"c\", \"d\"])\n    assert isinstance(result, list)\n    assert result == [\"a\", \"b\", \"c\", \"d\"]\n\n\ndef test_multi_expression_add(env):\n    t = env.from_string(\"{{ a }} + {{ b }}\")\n    result = t.render(a=[\"a\", \"b\"], b=[\"c\", \"d\"])\n    assert not isinstance(result, list)\n    assert result == \"['a', 'b'] + ['c', 'd']\"\n\n\ndef test_loops(env):\n    t = env.from_string(\"{% for x in value %}{{ x }}{% endfor %}\")\n    result = t.render(value=[\"a\", \"b\", \"c\", \"d\"])\n    assert isinstance(result, str)\n    assert result == \"abcd\"\n\n\ndef test_loops_with_ints(env):\n    t = env.from_string(\"{% for x in value %}{{ x }}{% endfor %}\")\n    result = t.render(value=[1, 2, 3, 4])\n    assert isinstance(result, int)\n    assert result == 1234\n\n\ndef test_loop_look_alike(env):\n    t = env.from_string(\"{% for x in value %}{{ x }}{% endfor %}\")\n    result = t.render(value=[1])\n    assert isinstance(result, int)\n    assert result == 1\n\n\n@pytest.mark.parametrize(\n    (\"source\", \"expect\"),\n    (\n        (\"{{ value }}\", True),\n        (\"{{ value }}\", False),\n        (\"{{ 1 == 1 }}\", True),\n        (\"{{ 2 + 2 == 5 }}\", False),\n        (\"{{ None is none }}\", True),\n        (\"{{ '' == None }}\", False),\n    ),\n)\ndef test_booleans(env, source, expect):\n    t = env.from_string(source)\n    result = t.render(value=expect)\n    assert isinstance(result, bool)\n    assert result is expect\n\n\ndef test_variable_dunder(env):\n    t = env.from_string(\"{{ x.__class__ }}\")\n    result = t.render(x=True)\n    assert isinstance(result, type)\n\n\ndef test_constant_dunder(env):\n    t = env.from_string(\"{{ true.__class__ }}\")\n    result = t.render()\n    assert isinstance(result, type)\n\n\ndef test_constant_dunder_to_string(env):\n    t = env.from_string(\"{{ true.__class__|string }}\")\n    result = t.render()\n    assert not isinstance(result, type)\n    assert result in {\"<type 'bool'>\", \"<class 'bool'>\"}\n\n\ndef test_string_literal_var(env):\n    t = env.from_string(\"[{{ 'all' }}]\")\n    result = t.render()\n    assert isinstance(result, str)\n    assert result == \"[all]\"\n\n\ndef test_string_top_level(env):\n    t = env.from_string(\"'Jinja'\")\n    result = t.render()\n    assert result == \"Jinja\"\n\n\ndef test_string_concatenation(async_native_env, run_async_fn):\n    async def async_render():\n        t = async_native_env.from_string(\n            \"{%- macro x(y) -%}{{ y }}{%- endmacro -%}{{- x('not') }} {{ x('bad') -}}\"\n        )\n        result = await t.render_async()\n        assert isinstance(result, str)\n        assert result == \"not bad\"\n\n    run_async_fn(async_render)\n\n\ndef test_tuple_of_variable_strings(env):\n    t = env.from_string(\"'{{ a }}', 'data', '{{ b }}', b'{{ c }}'\")\n    result = t.render(a=1, b=2, c=\"bytes\")\n    assert isinstance(result, tuple)\n    assert result == (\"1\", \"data\", \"2\", b\"bytes\")\n\n\ndef test_concat_strings_with_quotes(env):\n    t = env.from_string(\"--host='{{ host }}' --user \\\"{{ user }}\\\"\")\n    result = t.render(host=\"localhost\", user=\"Jinja\")\n    assert result == \"--host='localhost' --user \\\"Jinja\\\"\"\n\n\ndef test_no_intermediate_eval(env):\n    t = env.from_string(\"0.000{{ a }}\")\n    result = t.render(a=7)\n    assert isinstance(result, float)\n    # If intermediate eval happened, 0.000 would render 0.0, then 7\n    # would be appended, resulting in 0.07.\n    assert math.isclose(result, 0.0007)\n\n\ndef test_spontaneous_env():\n    t = NativeTemplate(\"{{ true }}\")\n    assert isinstance(t.environment, NativeEnvironment)\n\n\ndef test_leading_spaces(env):\n    t = env.from_string(\" {{ True }}\")\n    result = t.render()\n    assert result == \" True\"\n\n\ndef test_macro(env):\n    t = env.from_string(\"{%- macro x() -%}{{- [1,2] -}}{%- endmacro -%}{{- x()[1] -}}\")\n    result = t.render()\n    assert result == 2\n    assert isinstance(result, int)\n\n\ndef test_block(env):\n    t = env.from_string(\n        \"{% block b %}{% for i in range(1) %}{{ loop.index }}{% endfor %}\"\n        \"{% endblock %}{{ self.b() }}\"\n    )\n    result = t.render()\n    assert result == 11\n    assert isinstance(result, int)\n"
  },
  {
    "path": "tests/test_nodes.py",
    "content": "def test_template_hash(env):\n    template = env.parse(\"hash test\")\n    hash(template)\n"
  },
  {
    "path": "tests/test_pickle.py",
    "content": "import pickle\n\n\ndef test_environment(env):\n    env = pickle.loads(pickle.dumps(env))\n    assert env.from_string(\"x={{ x }}\").render(x=42) == \"x=42\"\n"
  },
  {
    "path": "tests/test_regression.py",
    "content": "import pytest\n\nfrom jinja2 import DictLoader\nfrom jinja2 import Environment\nfrom jinja2 import PrefixLoader\nfrom jinja2 import Template\nfrom jinja2 import TemplateAssertionError\nfrom jinja2 import TemplateNotFound\nfrom jinja2 import TemplateSyntaxError\nfrom jinja2.utils import pass_context\n\n\nclass TestCorner:\n    def test_assigned_scoping(self, env):\n        t = env.from_string(\n            \"\"\"\n        {%- for item in (1, 2, 3, 4) -%}\n            [{{ item }}]\n        {%- endfor %}\n        {{- item -}}\n        \"\"\"\n        )\n        assert t.render(item=42) == \"[1][2][3][4]42\"\n\n        t = env.from_string(\n            \"\"\"\n        {%- for item in (1, 2, 3, 4) -%}\n            [{{ item }}]\n        {%- endfor %}\n        {%- set item = 42 %}\n        {{- item -}}\n        \"\"\"\n        )\n        assert t.render() == \"[1][2][3][4]42\"\n\n        t = env.from_string(\n            \"\"\"\n        {%- set item = 42 %}\n        {%- for item in (1, 2, 3, 4) -%}\n            [{{ item }}]\n        {%- endfor %}\n        {{- item -}}\n        \"\"\"\n        )\n        assert t.render() == \"[1][2][3][4]42\"\n\n    def test_closure_scoping(self, env):\n        t = env.from_string(\n            \"\"\"\n        {%- set wrapper = \"<FOO>\" %}\n        {%- for item in (1, 2, 3, 4) %}\n            {%- macro wrapper() %}[{{ item }}]{% endmacro %}\n            {{- wrapper() }}\n        {%- endfor %}\n        {{- wrapper -}}\n        \"\"\"\n        )\n        assert t.render() == \"[1][2][3][4]<FOO>\"\n\n        t = env.from_string(\n            \"\"\"\n        {%- for item in (1, 2, 3, 4) %}\n            {%- macro wrapper() %}[{{ item }}]{% endmacro %}\n            {{- wrapper() }}\n        {%- endfor %}\n        {%- set wrapper = \"<FOO>\" %}\n        {{- wrapper -}}\n        \"\"\"\n        )\n        assert t.render() == \"[1][2][3][4]<FOO>\"\n\n        t = env.from_string(\n            \"\"\"\n        {%- for item in (1, 2, 3, 4) %}\n            {%- macro wrapper() %}[{{ item }}]{% endmacro %}\n            {{- wrapper() }}\n        {%- endfor %}\n        {{- wrapper -}}\n        \"\"\"\n        )\n        assert t.render(wrapper=23) == \"[1][2][3][4]23\"\n\n\nclass TestBug:\n    def test_keyword_folding(self, env):\n        env = Environment()\n        env.filters[\"testing\"] = lambda value, some: value + some\n        assert (\n            env.from_string(\"{{ 'test'|testing(some='stuff') }}\").render()\n            == \"teststuff\"\n        )\n\n    def test_extends_output_bugs(self, env):\n        env = Environment(\n            loader=DictLoader({\"parent.html\": \"(({% block title %}{% endblock %}))\"})\n        )\n\n        t = env.from_string(\n            '{% if expr %}{% extends \"parent.html\" %}{% endif %}'\n            \"[[{% block title %}title{% endblock %}]]\"\n            \"{% for item in [1, 2, 3] %}({{ item }}){% endfor %}\"\n        )\n        assert t.render(expr=False) == \"[[title]](1)(2)(3)\"\n        assert t.render(expr=True) == \"((title))\"\n\n    def test_urlize_filter_escaping(self, env):\n        tmpl = env.from_string('{{ \"http://www.example.org/<foo\"|urlize }}')\n        assert (\n            tmpl.render() == '<a href=\"http://www.example.org/&lt;foo\" rel=\"noopener\">'\n            \"http://www.example.org/&lt;foo</a>\"\n        )\n\n    def test_urlize_filter_closing_punctuation(self, env):\n        tmpl = env.from_string(\n            '{{ \"(see http://www.example.org/?page=subj_<desc.h>)\"|urlize }}'\n        )\n        assert tmpl.render() == (\n            '(see <a href=\"http://www.example.org/?page=subj_&lt;desc.h&gt;\" '\n            'rel=\"noopener\">http://www.example.org/?page=subj_&lt;desc.h&gt;</a>)'\n        )\n\n    def test_loop_call_loop(self, env):\n        tmpl = env.from_string(\n            \"\"\"\n\n        {% macro test() %}\n            {{ caller() }}\n        {% endmacro %}\n\n        {% for num1 in range(5) %}\n            {% call test() %}\n                {% for num2 in range(10) %}\n                    {{ loop.index }}\n                {% endfor %}\n            {% endcall %}\n        {% endfor %}\n\n        \"\"\"\n        )\n\n        assert tmpl.render().split() == [str(x) for x in range(1, 11)] * 5\n\n    def test_weird_inline_comment(self, env):\n        env = Environment(line_statement_prefix=\"%\")\n        pytest.raises(\n            TemplateSyntaxError,\n            env.from_string,\n            \"% for item in seq {# missing #}\\n...% endfor\",\n        )\n\n    def test_old_macro_loop_scoping_bug(self, env):\n        tmpl = env.from_string(\n            \"{% for i in (1, 2) %}{{ i }}{% endfor %}\"\n            \"{% macro i() %}3{% endmacro %}{{ i() }}\"\n        )\n        assert tmpl.render() == \"123\"\n\n    def test_partial_conditional_assignments(self, env):\n        tmpl = env.from_string(\"{% if b %}{% set a = 42 %}{% endif %}{{ a }}\")\n        assert tmpl.render(a=23) == \"23\"\n        assert tmpl.render(b=True) == \"42\"\n\n    def test_stacked_locals_scoping_bug(self, env):\n        env = Environment(line_statement_prefix=\"#\")\n        t = env.from_string(\n            \"\"\"\\\n# for j in [1, 2]:\n#   set x = 1\n#   for i in [1, 2]:\n#     print x\n#     if i % 2 == 0:\n#       set x = x + 1\n#     endif\n#   endfor\n# endfor\n# if a\n#   print 'A'\n# elif b\n#   print 'B'\n# elif c == d\n#   print 'C'\n# else\n#   print 'D'\n# endif\n    \"\"\"\n        )\n        assert t.render(a=0, b=False, c=42, d=42.0) == \"1111C\"\n\n    def test_stacked_locals_scoping_bug_twoframe(self, env):\n        t = Template(\n            \"\"\"\n            {% set x = 1 %}\n            {% for item in foo %}\n                {% if item == 1 %}\n                    {% set x = 2 %}\n                {% endif %}\n            {% endfor %}\n            {{ x }}\n        \"\"\"\n        )\n        rv = t.render(foo=[1]).strip()\n        assert rv == \"1\"\n\n    def test_call_with_args(self, env):\n        t = Template(\n            \"\"\"{% macro dump_users(users) -%}\n        <ul>\n          {%- for user in users -%}\n            <li><p>{{ user.username|e }}</p>{{ caller(user) }}</li>\n          {%- endfor -%}\n          </ul>\n        {%- endmacro -%}\n\n        {% call(user) dump_users(list_of_user) -%}\n          <dl>\n            <dl>Realname</dl>\n            <dd>{{ user.realname|e }}</dd>\n            <dl>Description</dl>\n            <dd>{{ user.description }}</dd>\n          </dl>\n        {% endcall %}\"\"\"\n        )\n\n        assert [\n            x.strip()\n            for x in t.render(\n                list_of_user=[\n                    {\n                        \"username\": \"apo\",\n                        \"realname\": \"something else\",\n                        \"description\": \"test\",\n                    }\n                ]\n            ).splitlines()\n        ] == [\n            \"<ul><li><p>apo</p><dl>\",\n            \"<dl>Realname</dl>\",\n            \"<dd>something else</dd>\",\n            \"<dl>Description</dl>\",\n            \"<dd>test</dd>\",\n            \"</dl>\",\n            \"</li></ul>\",\n        ]\n\n    def test_empty_if_condition_fails(self, env):\n        pytest.raises(TemplateSyntaxError, Template, \"{% if %}....{% endif %}\")\n        pytest.raises(\n            TemplateSyntaxError, Template, \"{% if foo %}...{% elif %}...{% endif %}\"\n        )\n        pytest.raises(TemplateSyntaxError, Template, \"{% for x in %}..{% endfor %}\")\n\n    def test_recursive_loop_compile(self, env):\n        Template(\n            \"\"\"\n            {% for p in foo recursive%}\n                {{p.bar}}\n                {% for f in p.fields recursive%}\n                    {{f.baz}}\n                    {{p.bar}}\n                    {% if f.rec %}\n                        {{ loop(f.sub) }}\n                    {% endif %}\n                {% endfor %}\n            {% endfor %}\n            \"\"\"\n        )\n        Template(\n            \"\"\"\n            {% for p in foo%}\n                {{p.bar}}\n                {% for f in p.fields recursive%}\n                    {{f.baz}}\n                    {{p.bar}}\n                    {% if f.rec %}\n                        {{ loop(f.sub) }}\n                    {% endif %}\n                {% endfor %}\n            {% endfor %}\n            \"\"\"\n        )\n\n    def test_else_loop_bug(self, env):\n        t = Template(\n            \"\"\"\n            {% for x in y %}\n                {{ loop.index0 }}\n            {% else %}\n                {% for i in range(3) %}{{ i }}{% endfor %}\n            {% endfor %}\n        \"\"\"\n        )\n        assert t.render(y=[]).strip() == \"012\"\n\n    def test_correct_prefix_loader_name(self, env):\n        env = Environment(loader=PrefixLoader({\"foo\": DictLoader({})}))\n        with pytest.raises(TemplateNotFound) as e:\n            env.get_template(\"foo/bar.html\")\n\n        assert e.value.name == \"foo/bar.html\"\n\n    def test_pass_context_callable_class(self, env):\n        class CallableClass:\n            @pass_context\n            def __call__(self, ctx):\n                return ctx.resolve(\"hello\")\n\n        tpl = Template(\"\"\"{{ callableclass() }}\"\"\")\n        output = tpl.render(callableclass=CallableClass(), hello=\"TEST\")\n        expected = \"TEST\"\n\n        assert output == expected\n\n    def test_block_set_with_extends(self):\n        env = Environment(\n            loader=DictLoader({\"main\": \"{% block body %}[{{ x }}]{% endblock %}\"})\n        )\n        t = env.from_string('{% extends \"main\" %}{% set x %}42{% endset %}')\n        assert t.render() == \"[42]\"\n\n    def test_nested_for_else(self, env):\n        tmpl = env.from_string(\n            \"{% for x in y %}{{ loop.index0 }}{% else %}\"\n            \"{% for i in range(3) %}{{ i }}{% endfor %}\"\n            \"{% endfor %}\"\n        )\n        assert tmpl.render() == \"012\"\n\n    def test_macro_var_bug(self, env):\n        tmpl = env.from_string(\n            \"\"\"\n        {% set i = 1 %}\n        {% macro test() %}\n            {% for i in range(0, 10) %}{{ i }}{% endfor %}\n        {% endmacro %}{{ test() }}\n        \"\"\"\n        )\n        assert tmpl.render().strip() == \"0123456789\"\n\n    def test_macro_var_bug_advanced(self, env):\n        tmpl = env.from_string(\n            \"\"\"\n        {% macro outer() %}\n            {% set i = 1 %}\n            {% macro test() %}\n                {% for i in range(0, 10) %}{{ i }}{% endfor %}\n            {% endmacro %}{{ test() }}\n        {% endmacro %}{{ outer() }}\n        \"\"\"\n        )\n        assert tmpl.render().strip() == \"0123456789\"\n\n    def test_callable_defaults(self):\n        env = Environment()\n        env.globals[\"get_int\"] = lambda: 42\n        t = env.from_string(\n            \"\"\"\n        {% macro test(a, b, c=get_int()) -%}\n             {{ a + b + c }}\n        {%- endmacro %}\n        {{ test(1, 2) }}|{{ test(1, 2, 3) }}\n        \"\"\"\n        )\n        assert t.render().strip() == \"45|6\"\n\n    def test_macro_escaping(self):\n        env = Environment(autoescape=lambda x: False)\n        template = \"{% macro m() %}<html>{% endmacro %}\"\n        template += \"{% autoescape true %}{{ m() }}{% endautoescape %}\"\n        assert env.from_string(template).render()\n\n    def test_macro_scoping(self, env):\n        tmpl = env.from_string(\n            \"\"\"\n        {% set n=[1,2,3,4,5] %}\n        {% for n in [[1,2,3], [3,4,5], [5,6,7]] %}\n\n        {% macro x(l) %}\n          {{ l.pop() }}\n          {% if l %}{{ x(l) }}{% endif %}\n        {% endmacro %}\n\n        {{ x(n) }}\n\n        {% endfor %}\n        \"\"\"\n        )\n        assert list(map(int, tmpl.render().split())) == [3, 2, 1, 5, 4, 3, 7, 6, 5]\n\n    def test_scopes_and_blocks(self):\n        env = Environment(\n            loader=DictLoader(\n                {\n                    \"a.html\": \"\"\"\n                {%- set foo = 'bar' -%}\n                {% include 'x.html' -%}\n            \"\"\",\n                    \"b.html\": \"\"\"\n                {%- set foo = 'bar' -%}\n                {% block test %}{% include 'x.html' %}{% endblock -%}\n                \"\"\",\n                    \"c.html\": \"\"\"\n                {%- set foo = 'bar' -%}\n                {% block test %}{% set foo = foo\n                    %}{% include 'x.html' %}{% endblock -%}\n            \"\"\",\n                    \"x.html\": \"\"\"{{ foo }}|{{ test }}\"\"\",\n                }\n            )\n        )\n\n        a = env.get_template(\"a.html\")\n        b = env.get_template(\"b.html\")\n        c = env.get_template(\"c.html\")\n\n        assert a.render(test=\"x\").strip() == \"bar|x\"\n        assert b.render(test=\"x\").strip() == \"bar|x\"\n        assert c.render(test=\"x\").strip() == \"bar|x\"\n\n    def test_scopes_and_include(self):\n        env = Environment(\n            loader=DictLoader(\n                {\n                    \"include.html\": \"{{ var }}\",\n                    \"base.html\": '{% include \"include.html\" %}',\n                    \"child.html\": '{% extends \"base.html\" %}{% set var = 42 %}',\n                }\n            )\n        )\n        t = env.get_template(\"child.html\")\n        assert t.render() == \"42\"\n\n    def test_caller_scoping(self, env):\n        t = env.from_string(\n            \"\"\"\n        {% macro detail(icon, value) -%}\n          {% if value -%}\n            <p><span class=\"fa fa-fw fa-{{ icon }}\"></span>\n                {%- if caller is undefined -%}\n                    {{ value }}\n                {%- else -%}\n                    {{ caller(value, *varargs) }}\n                {%- endif -%}</p>\n          {%- endif %}\n        {%- endmacro %}\n\n\n        {% macro link_detail(icon, value, href) -%}\n          {% call(value, href) detail(icon, value, href) -%}\n            <a href=\"{{ href }}\">{{ value }}</a>\n          {%- endcall %}\n        {%- endmacro %}\n        \"\"\"\n        )\n\n        assert t.module.link_detail(\"circle\", \"Index\", \"/\") == (\n            '<p><span class=\"fa fa-fw fa-circle\"></span><a href=\"/\">Index</a></p>'\n        )\n\n    def test_variable_reuse(self, env):\n        t = env.from_string(\"{% for x in x.y %}{{ x }}{% endfor %}\")\n        assert t.render(x={\"y\": [0, 1, 2]}) == \"012\"\n\n        t = env.from_string(\"{% for x in x.y %}{{ loop.index0 }}|{{ x }}{% endfor %}\")\n        assert t.render(x={\"y\": [0, 1, 2]}) == \"0|01|12|2\"\n\n        t = env.from_string(\"{% for x in x.y recursive %}{{ x }}{% endfor %}\")\n        assert t.render(x={\"y\": [0, 1, 2]}) == \"012\"\n\n    def test_double_caller(self, env):\n        t = env.from_string(\n            \"{% macro x(caller=none) %}[{% if caller %}\"\n            \"{{ caller() }}{% endif %}]{% endmacro %}\"\n            \"{{ x() }}{% call x() %}aha!{% endcall %}\"\n        )\n        assert t.render() == \"[][aha!]\"\n\n    def test_double_caller_no_default(self, env):\n        with pytest.raises(TemplateAssertionError) as exc_info:\n            env.from_string(\n                \"{% macro x(caller) %}[{% if caller %}\"\n                \"{{ caller() }}{% endif %}]{% endmacro %}\"\n            )\n        assert exc_info.match(\n            r'\"caller\" argument must be omitted or ' r\"be given a default\"\n        )\n\n        t = env.from_string(\n            \"{% macro x(caller=none) %}[{% if caller %}\"\n            \"{{ caller() }}{% endif %}]{% endmacro %}\"\n        )\n        with pytest.raises(TypeError) as exc_info:\n            t.module.x(None, caller=lambda: 42)\n        assert exc_info.match(\n            r\"\\'x\\' was invoked with two values for the \" r\"special caller argument\"\n        )\n\n    def test_macro_blocks(self, env):\n        t = env.from_string(\n            \"{% macro x() %}{% block foo %}x{% endblock %}{% endmacro %}{{ x() }}\"\n        )\n        assert t.render() == \"x\"\n\n    def test_scoped_block(self, env):\n        t = env.from_string(\n            \"{% set x = 1 %}{% with x = 2 %}{% block y scoped %}\"\n            \"{{ x }}{% endblock %}{% endwith %}\"\n        )\n        assert t.render() == \"2\"\n\n    def test_recursive_loop_filter(self, env):\n        t = env.from_string(\n            \"\"\"\n        <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n        <urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n          {%- for page in [site.root] if page.url != this recursive %}\n          <url><loc>{{ page.url }}</loc></url>\n          {{- loop(page.children) }}\n          {%- endfor %}\n        </urlset>\n        \"\"\"\n        )\n        sm = t.render(\n            this=\"/foo\",\n            site={\"root\": {\"url\": \"/\", \"children\": [{\"url\": \"/foo\"}, {\"url\": \"/bar\"}]}},\n        )\n        lines = [x.strip() for x in sm.splitlines() if x.strip()]\n        assert lines == [\n            '<?xml version=\"1.0\" encoding=\"UTF-8\"?>',\n            '<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">',\n            \"<url><loc>/</loc></url>\",\n            \"<url><loc>/bar</loc></url>\",\n            \"</urlset>\",\n        ]\n\n    def test_empty_if(self, env):\n        t = env.from_string(\"{% if foo %}{% else %}42{% endif %}\")\n        assert t.render(foo=False) == \"42\"\n\n    def test_subproperty_if(self, env):\n        t = env.from_string(\n            \"{% if object1.subproperty1 is eq object2.subproperty2 %}42{% endif %}\"\n        )\n        assert (\n            t.render(\n                object1={\"subproperty1\": \"value\"}, object2={\"subproperty2\": \"value\"}\n            )\n            == \"42\"\n        )\n\n    def test_set_and_include(self):\n        env = Environment(\n            loader=DictLoader(\n                {\n                    \"inc\": \"bar\",\n                    \"main\": '{% set foo = \"foo\" %}{{ foo }}{% include \"inc\" %}',\n                }\n            )\n        )\n        assert env.get_template(\"main\").render() == \"foobar\"\n\n    def test_loop_include(self):\n        env = Environment(\n            loader=DictLoader(\n                {\n                    \"inc\": \"{{ i }}\",\n                    \"main\": '{% for i in [1, 2, 3] %}{% include \"inc\" %}{% endfor %}',\n                }\n            )\n        )\n        assert env.get_template(\"main\").render() == \"123\"\n\n    def test_grouper_repr(self):\n        from jinja2.filters import _GroupTuple\n\n        t = _GroupTuple(\"foo\", [1, 2])\n        assert t.grouper == \"foo\"\n        assert t.list == [1, 2]\n        assert repr(t) == \"('foo', [1, 2])\"\n        assert str(t) == \"('foo', [1, 2])\"\n\n    def test_custom_context(self, env):\n        from jinja2.runtime import Context\n\n        class MyContext(Context):\n            pass\n\n        class MyEnvironment(Environment):\n            context_class = MyContext\n\n        loader = DictLoader({\"base\": \"{{ foobar }}\", \"test\": '{% extends \"base\" %}'})\n        env = MyEnvironment(loader=loader)\n        assert env.get_template(\"test\").render(foobar=\"test\") == \"test\"\n\n    def test_recursive_loop_bug(self, env):\n        tmpl = env.from_string(\n            \"{%- for value in values recursive %}1{% else %}0{% endfor -%}\"\n        )\n        assert tmpl.render(values=[]) == \"0\"\n\n    def test_markup_and_chainable_undefined(self):\n        from markupsafe import Markup\n\n        from jinja2.runtime import ChainableUndefined\n\n        assert str(Markup(ChainableUndefined())) == \"\"\n\n    def test_scoped_block_loop_vars(self, env):\n        tmpl = env.from_string(\n            \"\"\"\\\nStart\n{% for i in [\"foo\", \"bar\"] -%}\n{% block body scoped -%}\n{{ loop.index }}) {{ i }}{% if loop.last %} last{% endif -%}\n{%- endblock %}\n{% endfor -%}\nEnd\"\"\"\n        )\n        assert tmpl.render() == \"Start\\n1) foo\\n2) bar last\\nEnd\"\n\n    def test_pass_context_loop_vars(self, env):\n        @pass_context\n        def test(ctx):\n            return f\"{ctx['i']}{ctx['j']}\"\n\n        tmpl = env.from_string(\n            \"\"\"\\\n{% set i = 42 %}\n{%- for idx in range(2) -%}\n{{ i }}{{ j }}\n{% set i = idx -%}\n{%- set j = loop.index -%}\n{{ test() }}\n{{ i }}{{ j }}\n{% endfor -%}\n{{ i }}{{ j }}\"\"\"\n        )\n        tmpl.globals[\"test\"] = test\n        assert tmpl.render() == \"42\\n01\\n01\\n42\\n12\\n12\\n42\"\n\n    def test_pass_context_scoped_loop_vars(self, env):\n        @pass_context\n        def test(ctx):\n            return f\"{ctx['i']}\"\n\n        tmpl = env.from_string(\n            \"\"\"\\\n{% set i = 42 %}\n{%- for idx in range(2) -%}\n{{ i }}\n{%- set i = loop.index0 -%}\n{% block body scoped %}\n{{ test() }}\n{% endblock -%}\n{% endfor -%}\n{{ i }}\"\"\"\n        )\n        tmpl.globals[\"test\"] = test\n        assert tmpl.render() == \"42\\n0\\n42\\n1\\n42\"\n\n    def test_pass_context_in_blocks(self, env):\n        @pass_context\n        def test(ctx):\n            return f\"{ctx['i']}\"\n\n        tmpl = env.from_string(\n            \"\"\"\\\n{%- set i = 42 -%}\n{{ i }}\n{% block body -%}\n{% set i = 24 -%}\n{{ test() }}\n{% endblock -%}\n{{ i }}\"\"\"\n        )\n        tmpl.globals[\"test\"] = test\n        assert tmpl.render() == \"42\\n24\\n42\"\n\n    def test_pass_context_block_and_loop(self, env):\n        @pass_context\n        def test(ctx):\n            return f\"{ctx['i']}\"\n\n        tmpl = env.from_string(\n            \"\"\"\\\n{%- set i = 42 -%}\n{% for idx in range(2) -%}\n{{ test() }}\n{%- set i = idx -%}\n{% block body scoped %}\n{{ test() }}\n{% set i = 24 -%}\n{{ test() }}\n{% endblock -%}\n{{ test() }}\n{% endfor -%}\n{{ test() }}\"\"\"\n        )\n        tmpl.globals[\"test\"] = test\n\n        # values set within a block or loop should not\n        # show up outside of it\n        assert tmpl.render() == \"42\\n0\\n24\\n0\\n42\\n1\\n24\\n1\\n42\"\n\n    @pytest.mark.parametrize(\"op\", [\"extends\", \"include\"])\n    def test_cached_extends(self, op):\n        env = Environment(\n            loader=DictLoader(\n                {\"base\": \"{{ x }} {{ y }}\", \"main\": f\"{{% {op} 'base' %}}\"}\n            )\n        )\n        env.globals[\"x\"] = \"x\"\n        env.globals[\"y\"] = \"y\"\n\n        # template globals overlay env globals\n        tmpl = env.get_template(\"main\", globals={\"x\": \"bar\"})\n        assert tmpl.render() == \"bar y\"\n\n        # base was loaded indirectly, it just has env globals\n        tmpl = env.get_template(\"base\")\n        assert tmpl.render() == \"x y\"\n\n        # set template globals for base, no longer uses env globals\n        tmpl = env.get_template(\"base\", globals={\"x\": 42})\n        assert tmpl.render() == \"42 y\"\n\n        # templates are cached, they keep template globals set earlier\n        tmpl = env.get_template(\"main\")\n        assert tmpl.render() == \"bar y\"\n\n        tmpl = env.get_template(\"base\")\n        assert tmpl.render() == \"42 y\"\n\n    def test_nested_loop_scoping(self, env):\n        tmpl = env.from_string(\n            \"{% set output %}{% for x in [1,2,3] %}hello{% endfor %}\"\n            \"{% endset %}{{ output }}\"\n        )\n        assert tmpl.render() == \"hellohellohello\"\n\n    def test_pass_context_with_select(self, env):\n        @pass_context\n        def is_foo(ctx, s):\n            assert ctx is not None\n            return s == \"foo\"\n\n        env.tests[\"foo\"] = is_foo\n        tmpl = env.from_string(\n            \"{% for x in ['one', 'foo'] | select('foo') %}{{ x }}{% endfor %}\"\n        )\n        assert tmpl.render() == \"foo\"\n\n\ndef test_load_parameter_when_set_in_all_if_branches(env):\n    tmpl = env.from_string(\n        \"{% if True %}{{ a.b }}{% set a = 1 %}\"\n        \"{% elif False %}{% set a = 2 %}\"\n        \"{% else %}{% set a = 3 %}{% endif %}\"\n        \"{{ a }}\"\n    )\n    assert tmpl.render(a={\"b\": 0}) == \"01\"\n\n\n@pytest.mark.parametrize(\"unicode_char\", [\"\\N{FORM FEED}\", \"\\x85\"])\ndef test_unicode_whitespace(env, unicode_char):\n    content = \"Lorem ipsum\\n\" + unicode_char + \"\\nMore text\"\n    tmpl = env.from_string(content)\n    assert tmpl.render() == content\n"
  },
  {
    "path": "tests/test_runtime.py",
    "content": "import copy\nimport itertools\nimport pickle\n\nimport pytest\n\nfrom jinja2 import ChainableUndefined\nfrom jinja2 import DebugUndefined\nfrom jinja2 import StrictUndefined\nfrom jinja2 import Template\nfrom jinja2 import TemplateRuntimeError\nfrom jinja2 import Undefined\nfrom jinja2.runtime import LoopContext\n\nTEST_IDX_TEMPLATE_STR_1 = (\n    \"[{% for i in lst|reverse %}(len={{ loop.length }},\"\n    \" revindex={{ loop.revindex }}, index={{ loop.index }}, val={{ i }}){% endfor %}]\"\n)\nTEST_IDX0_TEMPLATE_STR_1 = (\n    \"[{% for i in lst|reverse %}(len={{ loop.length }},\"\n    \" revindex0={{ loop.revindex0 }}, index0={{ loop.index0 }}, val={{ i }})\"\n    \"{% endfor %}]\"\n)\n\n\ndef test_loop_idx():\n    t = Template(TEST_IDX_TEMPLATE_STR_1)\n    lst = [10]\n    excepted_render = \"[(len=1, revindex=1, index=1, val=10)]\"\n    assert excepted_render == t.render(lst=lst)\n\n\ndef test_loop_idx0():\n    t = Template(TEST_IDX0_TEMPLATE_STR_1)\n    lst = [10]\n    excepted_render = \"[(len=1, revindex0=0, index0=0, val=10)]\"\n    assert excepted_render == t.render(lst=lst)\n\n\ndef test_loopcontext0():\n    in_lst = []\n    lc = LoopContext(reversed(in_lst), None)\n    assert lc.length == len(in_lst)\n\n\ndef test_loopcontext1():\n    in_lst = [10]\n    lc = LoopContext(reversed(in_lst), None)\n    assert lc.length == len(in_lst)\n\n\ndef test_loopcontext2():\n    in_lst = [10, 11]\n    lc = LoopContext(reversed(in_lst), None)\n    assert lc.length == len(in_lst)\n\n\ndef test_iterator_not_advanced_early():\n    t = Template(\"{% for _, g in gs %}{{ loop.index }} {{ g|list }}\\n{% endfor %}\")\n    out = t.render(\n        gs=itertools.groupby([(1, \"a\"), (1, \"b\"), (2, \"c\"), (3, \"d\")], lambda x: x[0])\n    )\n    # groupby groups depend on the current position of the iterator. If\n    # it was advanced early, the lists would appear empty.\n    assert out == \"1 [(1, 'a'), (1, 'b')]\\n2 [(2, 'c')]\\n3 [(3, 'd')]\\n\"\n\n\ndef test_mock_not_pass_arg_marker():\n    \"\"\"If a callable class has a ``__getattr__`` that returns True-like\n    values for arbitrary attrs, it should not be incorrectly identified\n    as a ``pass_context`` function.\n    \"\"\"\n\n    class Calc:\n        def __getattr__(self, item):\n            return object()\n\n        def __call__(self, *args, **kwargs):\n            return len(args) + len(kwargs)\n\n    t = Template(\"{{ calc() }}\")\n    out = t.render(calc=Calc())\n    # Would be \"1\" if context argument was passed.\n    assert out == \"0\"\n\n\n_undefined_types = (Undefined, ChainableUndefined, DebugUndefined, StrictUndefined)\n\n\n@pytest.mark.parametrize(\"undefined_type\", _undefined_types)\ndef test_undefined_copy(undefined_type):\n    undef = undefined_type(\"a hint\", [\"foo\"], \"a name\", TemplateRuntimeError)\n    copied = copy.copy(undef)\n\n    assert copied is not undef\n    assert copied._undefined_hint is undef._undefined_hint\n    assert copied._undefined_obj is undef._undefined_obj\n    assert copied._undefined_name is undef._undefined_name\n    assert copied._undefined_exception is undef._undefined_exception\n\n\n@pytest.mark.parametrize(\"undefined_type\", _undefined_types)\ndef test_undefined_deepcopy(undefined_type):\n    undef = undefined_type(\"a hint\", [\"foo\"], \"a name\", TemplateRuntimeError)\n    copied = copy.deepcopy(undef)\n\n    assert copied._undefined_hint is undef._undefined_hint\n    assert copied._undefined_obj is not undef._undefined_obj\n    assert copied._undefined_obj == undef._undefined_obj\n    assert copied._undefined_name is undef._undefined_name\n    assert copied._undefined_exception is undef._undefined_exception\n\n\n@pytest.mark.parametrize(\"undefined_type\", _undefined_types)\ndef test_undefined_pickle(undefined_type):\n    undef = undefined_type(\"a hint\", [\"foo\"], \"a name\", TemplateRuntimeError)\n    copied = pickle.loads(pickle.dumps(undef))\n\n    assert copied._undefined_hint is not undef._undefined_hint\n    assert copied._undefined_hint == undef._undefined_hint\n    assert copied._undefined_obj is not undef._undefined_obj\n    assert copied._undefined_obj == undef._undefined_obj\n    assert copied._undefined_name is not undef._undefined_name\n    assert copied._undefined_name == undef._undefined_name\n    assert copied._undefined_exception is undef._undefined_exception\n"
  },
  {
    "path": "tests/test_security.py",
    "content": "import pytest\nfrom markupsafe import escape\n\nfrom jinja2 import Environment\nfrom jinja2.exceptions import SecurityError\nfrom jinja2.exceptions import TemplateRuntimeError\nfrom jinja2.exceptions import TemplateSyntaxError\nfrom jinja2.nodes import EvalContext\nfrom jinja2.sandbox import ImmutableSandboxedEnvironment\nfrom jinja2.sandbox import SandboxedEnvironment\nfrom jinja2.sandbox import unsafe\n\n\nclass PrivateStuff:\n    def bar(self):\n        return 23\n\n    @unsafe\n    def foo(self):\n        return 42\n\n    def __repr__(self):\n        return \"PrivateStuff\"\n\n\nclass PublicStuff:\n    def bar(self):\n        return 23\n\n    def _foo(self):\n        return 42\n\n    def __repr__(self):\n        return \"PublicStuff\"\n\n\nclass TestSandbox:\n    def test_unsafe(self, env):\n        env = SandboxedEnvironment()\n        pytest.raises(\n            SecurityError, env.from_string(\"{{ foo.foo() }}\").render, foo=PrivateStuff()\n        )\n        assert env.from_string(\"{{ foo.bar() }}\").render(foo=PrivateStuff()) == \"23\"\n\n        pytest.raises(\n            SecurityError, env.from_string(\"{{ foo._foo() }}\").render, foo=PublicStuff()\n        )\n        assert env.from_string(\"{{ foo.bar() }}\").render(foo=PublicStuff()) == \"23\"\n        assert env.from_string(\"{{ foo.__class__ }}\").render(foo=42) == \"\"\n        assert env.from_string(\"{{ foo.func_code }}\").render(foo=lambda: None) == \"\"\n        # security error comes from __class__ already.\n        pytest.raises(\n            SecurityError,\n            env.from_string(\"{{ foo.__class__.__subclasses__() }}\").render,\n            foo=42,\n        )\n\n    def test_immutable_environment(self, env):\n        env = ImmutableSandboxedEnvironment()\n        pytest.raises(SecurityError, env.from_string(\"{{ [].append(23) }}\").render)\n        pytest.raises(SecurityError, env.from_string(\"{{ [].clear() }}\").render)\n        pytest.raises(SecurityError, env.from_string(\"{{ [1].pop() }}\").render)\n        pytest.raises(SecurityError, env.from_string(\"{{ {1:2}.clear() }}\").render)\n\n    def test_restricted(self, env):\n        env = SandboxedEnvironment()\n        pytest.raises(\n            TemplateSyntaxError,\n            env.from_string,\n            \"{% for item.attribute in seq %}...{% endfor %}\",\n        )\n        pytest.raises(\n            TemplateSyntaxError,\n            env.from_string,\n            \"{% for foo, bar.baz in seq %}...{% endfor %}\",\n        )\n\n    def test_template_data(self, env):\n        env = Environment(autoescape=True)\n        t = env.from_string(\n            \"{% macro say_hello(name) %}\"\n            \"<p>Hello {{ name }}!</p>{% endmacro %}\"\n            '{{ say_hello(\"<blink>foo</blink>\") }}'\n        )\n        escaped_out = \"<p>Hello &lt;blink&gt;foo&lt;/blink&gt;!</p>\"\n        assert t.render() == escaped_out\n        assert str(t.module) == escaped_out\n        assert escape(t.module) == escaped_out\n        assert t.module.say_hello(\"<blink>foo</blink>\") == escaped_out\n        assert (\n            escape(t.module.say_hello(EvalContext(env), \"<blink>foo</blink>\"))\n            == escaped_out\n        )\n        assert escape(t.module.say_hello(\"<blink>foo</blink>\")) == escaped_out\n\n    def test_attr_filter(self, env):\n        env = SandboxedEnvironment()\n        tmpl = env.from_string('{{ cls|attr(\"__subclasses__\")() }}')\n        pytest.raises(SecurityError, tmpl.render, cls=int)\n\n    def test_binary_operator_intercepting(self, env):\n        def disable_op(left, right):\n            raise TemplateRuntimeError(\"that operator so does not work\")\n\n        for expr, ctx, rv in (\"1 + 2\", {}, \"3\"), (\"a + 2\", {\"a\": 2}, \"4\"):\n            env = SandboxedEnvironment()\n            env.binop_table[\"+\"] = disable_op\n            t = env.from_string(f\"{{{{ {expr} }}}}\")\n            assert t.render(ctx) == rv\n            env.intercepted_binops = frozenset([\"+\"])\n            t = env.from_string(f\"{{{{ {expr} }}}}\")\n            with pytest.raises(TemplateRuntimeError):\n                t.render(ctx)\n\n    def test_unary_operator_intercepting(self, env):\n        def disable_op(arg):\n            raise TemplateRuntimeError(\"that operator so does not work\")\n\n        for expr, ctx, rv in (\"-1\", {}, \"-1\"), (\"-a\", {\"a\": 2}, \"-2\"):\n            env = SandboxedEnvironment()\n            env.unop_table[\"-\"] = disable_op\n            t = env.from_string(f\"{{{{ {expr} }}}}\")\n            assert t.render(ctx) == rv\n            env.intercepted_unops = frozenset([\"-\"])\n            t = env.from_string(f\"{{{{ {expr} }}}}\")\n            with pytest.raises(TemplateRuntimeError):\n                t.render(ctx)\n\n\nclass TestStringFormat:\n    def test_basic_format_safety(self):\n        env = SandboxedEnvironment()\n        t = env.from_string('{{ \"a{0.__class__}b\".format(42) }}')\n        assert t.render() == \"ab\"\n\n    def test_basic_format_all_okay(self):\n        env = SandboxedEnvironment()\n        t = env.from_string('{{ \"a{0.foo}b\".format({\"foo\": 42}) }}')\n        assert t.render() == \"a42b\"\n\n    def test_safe_format_safety(self):\n        env = SandboxedEnvironment()\n        t = env.from_string('{{ (\"a{0.__class__}b{1}\"|safe).format(42, \"<foo>\") }}')\n        assert t.render() == \"ab&lt;foo&gt;\"\n\n    def test_safe_format_all_okay(self):\n        env = SandboxedEnvironment()\n        t = env.from_string('{{ (\"a{0.foo}b{1}\"|safe).format({\"foo\": 42}, \"<foo>\") }}')\n        assert t.render() == \"a42b&lt;foo&gt;\"\n\n    def test_empty_braces_format(self):\n        env = SandboxedEnvironment()\n        t1 = env.from_string('{{ (\"a{}b{}\").format(\"foo\", \"42\")}}')\n        t2 = env.from_string('{{ (\"a{}b{}\"|safe).format(42, \"<foo>\") }}')\n        assert t1.render() == \"afoob42\"\n        assert t2.render() == \"a42b&lt;foo&gt;\"\n\n\nclass TestStringFormatMap:\n    def test_basic_format_safety(self):\n        env = SandboxedEnvironment()\n        t = env.from_string('{{ \"a{x.__class__}b\".format_map({\"x\":42}) }}')\n        assert t.render() == \"ab\"\n\n    def test_basic_format_all_okay(self):\n        env = SandboxedEnvironment()\n        t = env.from_string('{{ \"a{x.foo}b\".format_map({\"x\":{\"foo\": 42}}) }}')\n        assert t.render() == \"a42b\"\n\n    def test_safe_format_all_okay(self):\n        env = SandboxedEnvironment()\n        t = env.from_string(\n            '{{ (\"a{x.foo}b{y}\"|safe).format_map({\"x\":{\"foo\": 42}, \"y\":\"<foo>\"}) }}'\n        )\n        assert t.render() == \"a42b&lt;foo&gt;\"\n\n    def test_indirect_call(self):\n        def run(value, arg):\n            return value.run(arg)\n\n        env = SandboxedEnvironment()\n        env.filters[\"run\"] = run\n        t = env.from_string(\n            \"\"\"{% set\n                ns = namespace(run=\"{0.__call__.__builtins__[__import__]}\".format)\n            %}\n            {{ ns | run(not_here) }}\n            \"\"\"\n        )\n\n        with pytest.raises(SecurityError):\n            t.render()\n\n    def test_attr_filter(self) -> None:\n        env = SandboxedEnvironment()\n        t = env.from_string(\n            \"\"\"{{ \"{0.__call__.__builtins__[__import__]}\"\n                  | attr(\"format\")(not_here) }}\"\"\"\n        )\n\n        with pytest.raises(SecurityError):\n            t.render()\n"
  },
  {
    "path": "tests/test_tests.py",
    "content": "import pytest\nfrom markupsafe import Markup\n\nfrom jinja2 import Environment\nfrom jinja2 import TemplateAssertionError\nfrom jinja2 import TemplateRuntimeError\n\n\nclass MyDict(dict):\n    pass\n\n\nclass TestTestsCase:\n    def test_defined(self, env):\n        tmpl = env.from_string(\"{{ missing is defined }}|{{ true is defined }}\")\n        assert tmpl.render() == \"False|True\"\n\n    def test_even(self, env):\n        tmpl = env.from_string(\"\"\"{{ 1 is even }}|{{ 2 is even }}\"\"\")\n        assert tmpl.render() == \"False|True\"\n\n    def test_odd(self, env):\n        tmpl = env.from_string(\"\"\"{{ 1 is odd }}|{{ 2 is odd }}\"\"\")\n        assert tmpl.render() == \"True|False\"\n\n    def test_lower(self, env):\n        tmpl = env.from_string(\"\"\"{{ \"foo\" is lower }}|{{ \"FOO\" is lower }}\"\"\")\n        assert tmpl.render() == \"True|False\"\n\n    # Test type checks\n    @pytest.mark.parametrize(\n        \"op,expect\",\n        (\n            (\"none is none\", True),\n            (\"false is none\", False),\n            (\"true is none\", False),\n            (\"42 is none\", False),\n            (\"none is true\", False),\n            (\"false is true\", False),\n            (\"true is true\", True),\n            (\"0 is true\", False),\n            (\"1 is true\", False),\n            (\"42 is true\", False),\n            (\"none is false\", False),\n            (\"false is false\", True),\n            (\"true is false\", False),\n            (\"0 is false\", False),\n            (\"1 is false\", False),\n            (\"42 is false\", False),\n            (\"none is boolean\", False),\n            (\"false is boolean\", True),\n            (\"true is boolean\", True),\n            (\"0 is boolean\", False),\n            (\"1 is boolean\", False),\n            (\"42 is boolean\", False),\n            (\"0.0 is boolean\", False),\n            (\"1.0 is boolean\", False),\n            (\"3.14159 is boolean\", False),\n            (\"none is integer\", False),\n            (\"false is integer\", False),\n            (\"true is integer\", False),\n            (\"42 is integer\", True),\n            (\"3.14159 is integer\", False),\n            (\"(10 ** 100) is integer\", True),\n            (\"none is float\", False),\n            (\"false is float\", False),\n            (\"true is float\", False),\n            (\"42 is float\", False),\n            (\"4.2 is float\", True),\n            (\"(10 ** 100) is float\", False),\n            (\"none is number\", False),\n            (\"false is number\", True),\n            (\"true is number\", True),\n            (\"42 is number\", True),\n            (\"3.14159 is number\", True),\n            (\"complex is number\", True),\n            (\"(10 ** 100) is number\", True),\n            (\"none is string\", False),\n            (\"false is string\", False),\n            (\"true is string\", False),\n            (\"42 is string\", False),\n            ('\"foo\" is string', True),\n            (\"none is sequence\", False),\n            (\"false is sequence\", False),\n            (\"42 is sequence\", False),\n            ('\"foo\" is sequence', True),\n            (\"[] is sequence\", True),\n            (\"[1, 2, 3] is sequence\", True),\n            (\"{} is sequence\", True),\n            (\"none is mapping\", False),\n            (\"false is mapping\", False),\n            (\"42 is mapping\", False),\n            ('\"foo\" is mapping', False),\n            (\"[] is mapping\", False),\n            (\"{} is mapping\", True),\n            (\"mydict is mapping\", True),\n            (\"none is iterable\", False),\n            (\"false is iterable\", False),\n            (\"42 is iterable\", False),\n            ('\"foo\" is iterable', True),\n            (\"[] is iterable\", True),\n            (\"{} is iterable\", True),\n            (\"range(5) is iterable\", True),\n            (\"none is callable\", False),\n            (\"false is callable\", False),\n            (\"42 is callable\", False),\n            ('\"foo\" is callable', False),\n            (\"[] is callable\", False),\n            (\"{} is callable\", False),\n            (\"range is callable\", True),\n        ),\n    )\n    def test_types(self, env, op, expect):\n        t = env.from_string(f\"{{{{ {op} }}}}\")\n        assert t.render(mydict=MyDict(), complex=complex(1, 2)) == str(expect)\n\n    def test_upper(self, env):\n        tmpl = env.from_string('{{ \"FOO\" is upper }}|{{ \"foo\" is upper }}')\n        assert tmpl.render() == \"True|False\"\n\n    def test_equalto(self, env):\n        tmpl = env.from_string(\n            \"{{ foo is eq 12 }}|\"\n            \"{{ foo is eq 0 }}|\"\n            \"{{ foo is eq (3 * 4) }}|\"\n            '{{ bar is eq \"baz\" }}|'\n            '{{ bar is eq \"zab\" }}|'\n            '{{ bar is eq (\"ba\" + \"z\") }}|'\n            \"{{ bar is eq bar }}|\"\n            \"{{ bar is eq foo }}\"\n        )\n        assert (\n            tmpl.render(foo=12, bar=\"baz\")\n            == \"True|False|True|True|False|True|True|False\"\n        )\n\n    @pytest.mark.parametrize(\n        \"op,expect\",\n        (\n            (\"eq 2\", True),\n            (\"eq 3\", False),\n            (\"ne 3\", True),\n            (\"ne 2\", False),\n            (\"lt 3\", True),\n            (\"lt 2\", False),\n            (\"le 2\", True),\n            (\"le 1\", False),\n            (\"gt 1\", True),\n            (\"gt 2\", False),\n            (\"ge 2\", True),\n            (\"ge 3\", False),\n        ),\n    )\n    def test_compare_aliases(self, env, op, expect):\n        t = env.from_string(f\"{{{{ 2 is {op} }}}}\")\n        assert t.render() == str(expect)\n\n    def test_sameas(self, env):\n        tmpl = env.from_string(\"{{ foo is sameas false }}|{{ 0 is sameas false }}\")\n        assert tmpl.render(foo=False) == \"True|False\"\n\n    def test_no_paren_for_arg1(self, env):\n        tmpl = env.from_string(\"{{ foo is sameas none }}\")\n        assert tmpl.render(foo=None) == \"True\"\n\n    def test_escaped(self, env):\n        env = Environment(autoescape=True)\n        tmpl = env.from_string(\"{{ x is escaped }}|{{ y is escaped }}\")\n        assert tmpl.render(x=\"foo\", y=Markup(\"foo\")) == \"False|True\"\n\n    def test_greaterthan(self, env):\n        tmpl = env.from_string(\"{{ 1 is greaterthan 0 }}|{{ 0 is greaterthan 1 }}\")\n        assert tmpl.render() == \"True|False\"\n\n    def test_lessthan(self, env):\n        tmpl = env.from_string(\"{{ 0 is lessthan 1 }}|{{ 1 is lessthan 0 }}\")\n        assert tmpl.render() == \"True|False\"\n\n    def test_multiple_tests(self):\n        items = []\n\n        def matching(x, y):\n            items.append((x, y))\n            return False\n\n        env = Environment()\n        env.tests[\"matching\"] = matching\n        tmpl = env.from_string(\n            \"{{ 'us-west-1' is matching '(us-east-1|ap-northeast-1)'\"\n            \" or 'stage' is matching '(dev|stage)' }}\"\n        )\n        assert tmpl.render() == \"False\"\n        assert items == [\n            (\"us-west-1\", \"(us-east-1|ap-northeast-1)\"),\n            (\"stage\", \"(dev|stage)\"),\n        ]\n\n    def test_in(self, env):\n        tmpl = env.from_string(\n            '{{ \"o\" is in \"foo\" }}|'\n            '{{ \"foo\" is in \"foo\" }}|'\n            '{{ \"b\" is in \"foo\" }}|'\n            \"{{ 1 is in ((1, 2)) }}|\"\n            \"{{ 3 is in ((1, 2)) }}|\"\n            \"{{ 1 is in [1, 2] }}|\"\n            \"{{ 3 is in [1, 2] }}|\"\n            '{{ \"foo\" is in {\"foo\": 1}}}|'\n            '{{ \"baz\" is in {\"bar\": 1}}}'\n        )\n        assert tmpl.render() == \"True|True|False|True|False|True|False|True|False\"\n\n\ndef test_name_undefined(env):\n    with pytest.raises(TemplateAssertionError, match=\"No test named 'f'\"):\n        env.from_string(\"{{ x is f }}\")\n\n\ndef test_name_undefined_in_if(env):\n    t = env.from_string(\"{% if x is defined %}{{ x is f }}{% endif %}\")\n    assert t.render() == \"\"\n\n    with pytest.raises(TemplateRuntimeError, match=\"No test named 'f'\"):\n        t.render(x=1)\n\n\ndef test_is_filter(env):\n    assert env.call_test(\"filter\", \"title\")\n    assert not env.call_test(\"filter\", \"bad-name\")\n\n\ndef test_is_test(env):\n    assert env.call_test(\"test\", \"number\")\n    assert not env.call_test(\"test\", \"bad-name\")\n"
  },
  {
    "path": "tests/test_utils.py",
    "content": "import copy\nimport pickle\nimport random\nfrom collections import deque\nfrom copy import copy as shallow_copy\n\nimport pytest\nfrom markupsafe import Markup\n\nfrom jinja2.utils import consume\nfrom jinja2.utils import generate_lorem_ipsum\nfrom jinja2.utils import LRUCache\nfrom jinja2.utils import missing\nfrom jinja2.utils import object_type_repr\nfrom jinja2.utils import select_autoescape\nfrom jinja2.utils import urlize\n\n\nclass TestLRUCache:\n    def test_simple(self):\n        d = LRUCache(3)\n        d[\"a\"] = 1\n        d[\"b\"] = 2\n        d[\"c\"] = 3\n        d[\"a\"]\n        d[\"d\"] = 4\n        assert d.keys() == [\"d\", \"a\", \"c\"]\n\n    def test_values(self):\n        cache = LRUCache(3)\n        cache[\"b\"] = 1\n        cache[\"a\"] = 2\n        assert cache.values() == [2, 1]\n\n    def test_values_empty(self):\n        cache = LRUCache(2)\n        assert cache.values() == []\n\n    def test_pickleable(self):\n        cache = LRUCache(2)\n        cache[\"foo\"] = 42\n        cache[\"bar\"] = 23\n        cache[\"foo\"]\n\n        for protocol in range(3):\n            copy = pickle.loads(pickle.dumps(cache, protocol))\n            assert copy.capacity == cache.capacity\n            assert copy._mapping == cache._mapping\n            assert copy._queue == cache._queue\n\n    @pytest.mark.parametrize(\"copy_func\", [LRUCache.copy, shallow_copy])\n    def test_copy(self, copy_func):\n        cache = LRUCache(2)\n        cache[\"a\"] = 1\n        cache[\"b\"] = 2\n        copy = copy_func(cache)\n        assert copy._queue == cache._queue\n        copy[\"c\"] = 3\n        assert copy._queue != cache._queue\n        assert copy.keys() == [\"c\", \"b\"]\n\n    def test_clear(self):\n        d = LRUCache(3)\n        d[\"a\"] = 1\n        d[\"b\"] = 2\n        d[\"c\"] = 3\n        d.clear()\n        assert d.__getstate__() == {\"capacity\": 3, \"_mapping\": {}, \"_queue\": deque([])}\n\n    def test_repr(self):\n        d = LRUCache(3)\n        d[\"a\"] = 1\n        d[\"b\"] = 2\n        d[\"c\"] = 3\n        # Sort the strings - mapping is unordered\n        assert sorted(repr(d)) == sorted(\"<LRUCache {'a': 1, 'b': 2, 'c': 3}>\")\n\n    def test_items(self):\n        \"\"\"Test various items, keys, values and iterators of LRUCache.\"\"\"\n        d = LRUCache(3)\n        d[\"a\"] = 1\n        d[\"b\"] = 2\n        d[\"c\"] = 3\n        assert d.items() == [(\"c\", 3), (\"b\", 2), (\"a\", 1)]\n        assert d.keys() == [\"c\", \"b\", \"a\"]\n        assert d.values() == [3, 2, 1]\n        assert list(reversed(d)) == [\"a\", \"b\", \"c\"]\n\n        # Change the cache a little\n        d[\"b\"]\n        d[\"a\"] = 4\n        assert d.items() == [(\"a\", 4), (\"b\", 2), (\"c\", 3)]\n        assert d.keys() == [\"a\", \"b\", \"c\"]\n        assert d.values() == [4, 2, 3]\n        assert list(reversed(d)) == [\"c\", \"b\", \"a\"]\n\n    def test_setdefault(self):\n        d = LRUCache(3)\n        assert len(d) == 0\n        assert d.setdefault(\"a\") is None\n        assert d.setdefault(\"a\", 1) is None\n        assert len(d) == 1\n        assert d.setdefault(\"b\", 2) == 2\n        assert len(d) == 2\n\n\nclass TestHelpers:\n    def test_object_type_repr(self):\n        class X:\n            pass\n\n        assert object_type_repr(42) == \"int object\"\n        assert object_type_repr([]) == \"list object\"\n        assert object_type_repr(X()) == \"test_utils.X object\"\n        assert object_type_repr(None) == \"None\"\n        assert object_type_repr(Ellipsis) == \"Ellipsis\"\n\n    def test_autoescape_select(self):\n        func = select_autoescape(\n            enabled_extensions=(\"html\", \".htm\"),\n            disabled_extensions=(\"txt\",),\n            default_for_string=\"STRING\",\n            default=\"NONE\",\n        )\n\n        assert func(None) == \"STRING\"\n        assert func(\"unknown.foo\") == \"NONE\"\n        assert func(\"foo.html\")\n        assert func(\"foo.htm\")\n        assert not func(\"foo.txt\")\n        assert func(\"FOO.HTML\")\n        assert not func(\"FOO.TXT\")\n\n\nclass TestEscapeUrlizeTarget:\n    def test_escape_urlize_target(self):\n        url = \"http://example.org\"\n        target = \"<script>\"\n        assert urlize(url, target=target) == (\n            '<a href=\"http://example.org\"'\n            ' target=\"&lt;script&gt;\">'\n            \"http://example.org</a>\"\n        )\n\n    def test_urlize_mail_mastodon(self):\n        fr = \"nabijaczleweli@nabijaczleweli.xyz\\n@eater@cijber.social\\n\"\n        to = (\n            '<a href=\"mailto:nabijaczleweli@nabijaczleweli.xyz\">'\n            \"nabijaczleweli@nabijaczleweli.xyz</a>\\n@eater@cijber.social\\n\"\n        )\n        assert urlize(fr) == to\n\n\nclass TestLoremIpsum:\n    def test_lorem_ipsum_markup(self):\n        \"\"\"Test that output of lorem_ipsum is Markup by default.\"\"\"\n        assert isinstance(generate_lorem_ipsum(), Markup)\n\n    def test_lorem_ipsum_html(self):\n        \"\"\"Test that output of lorem_ipsum is a string_type when not html.\"\"\"\n        assert isinstance(generate_lorem_ipsum(html=False), str)\n\n    def test_lorem_ipsum_n(self):\n        \"\"\"Test that the n (number of lines) works as expected.\"\"\"\n        assert generate_lorem_ipsum(n=0, html=False) == \"\"\n        for n in range(1, 50):\n            assert generate_lorem_ipsum(n=n, html=False).count(\"\\n\") == (n - 1) * 2\n\n    def test_lorem_ipsum_min(self):\n        \"\"\"Test that at least min words are in the output of each line\"\"\"\n        for _ in range(5):\n            m = random.randrange(20, 99)\n            for _ in range(10):\n                assert generate_lorem_ipsum(n=1, min=m, html=False).count(\" \") >= m - 1\n\n    def test_lorem_ipsum_max(self):\n        \"\"\"Test that at least max words are in the output of each line\"\"\"\n        for _ in range(5):\n            m = random.randrange(21, 100)\n            for _ in range(10):\n                assert generate_lorem_ipsum(n=1, max=m, html=False).count(\" \") < m - 1\n\n\ndef test_missing():\n    \"\"\"Test the repr of missing.\"\"\"\n    assert repr(missing) == \"missing\"\n\n\ndef test_consume():\n    \"\"\"Test that consume consumes an iterator.\"\"\"\n    x = iter([1, 2, 3, 4, 5])\n    consume(x)\n    with pytest.raises(StopIteration):\n        next(x)\n\n\n@pytest.mark.parametrize(\"protocol\", range(pickle.HIGHEST_PROTOCOL + 1))\ndef test_pickle_missing(protocol: int) -> None:\n    \"\"\"Test that missing can be pickled while remaining a singleton.\"\"\"\n    assert pickle.loads(pickle.dumps(missing, protocol)) is missing\n\n\ndef test_copy_missing() -> None:\n    \"\"\"Test that missing can be copied while remaining a singleton.\"\"\"\n    assert copy.copy(missing) is missing\n"
  }
]