Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Unreleased
- Update minimum Babel version to >= 2.17.
- Deprecate the ``__version__`` attribute. Use feature detection or
``importlib.metadata.version("jinja2")`` instead.
- Support copying ``Template`` objects. :issue:`758`
- Use modern packaging metadata with ``pyproject.toml`` instead of ``setup.cfg``.
:pr:`1793`
- Use ``flit_core`` instead of ``setuptools`` as build backend.
Expand Down
6 changes: 6 additions & 0 deletions src/jinja2/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,12 @@ def __new__(
)
return env.from_string(source, template_class=cls)

def __copy__(self) -> "Template":
return self

def __deepcopy__(self, memo: dict[int, t.Any]) -> "Template":
return self

@classmethod
def from_code(
cls,
Expand Down
9 changes: 9 additions & 0 deletions tests/test_regression.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import copy

import pytest

from jinja2 import DictLoader
Expand All @@ -11,6 +13,13 @@


class TestCorner:
def test_template_copy(self):
template = Template("Hello {{ name }}")

assert copy.copy(template) is template
assert copy.deepcopy(template) is template
assert template.render(name="world") == "Hello world"

def test_assigned_scoping(self, env):
t = env.from_string(
"""
Expand Down
Loading