Skip to content
Merged
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
7 changes: 6 additions & 1 deletion copier/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,12 @@ def answers_relpath(self) -> Path:
"""
path = self.answers_file or self.template.answers_relpath
template = self.jinja_env.from_string(str(path))
return Path(template.render(self._render_context()))
# HACK: Override `_copier_conf.answers_file` in the render context to
# avoid infinite recursion when accessing it in a Jinja context hook via
# `copier-templates-extensions`.
context = self._render_context()
context["_copier_conf"]["answers_file"] = ""
return Path(template.render(**context))

@cached_property
def all_exclusions(self) -> Sequence[str]:
Expand Down
12 changes: 10 additions & 2 deletions copier/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,26 @@


# HACK https://github.com/copier-org/copier/pull/1880#discussion_r1887491497
class LazyDict(Mapping[_K, _V]):
class LazyDict(MutableMapping[_K, _V]):
"""A dict where values are functions that get evaluated only once when requested."""

def __init__(self, mapping: Mapping[_K, Callable[[], _V]] | None = None):
self._pending = mapping or {}
self._pending = dict(mapping or {})
self._done: dict[_K, _V] = {}

def __getitem__(self, key: _K) -> _V:
if key not in self._done:
self._done[key] = self._pending[key]()
return self._done[key]

def __setitem__(self, key: _K, value: _V) -> None:
self._pending[key] = lambda: value
self._done.pop(key, None)

def __delitem__(self, key: _K) -> None:
del self._pending[key]
del self._done[key]

Check warning on line 98 in copier/_types.py

View check run for this annotation

Codecov / codecov/patch

copier/_types.py#L97-L98

Added lines #L97 - L98 were not covered by tests

def __iter__(self) -> Iterator[_K]:
return iter(self._pending)

Expand Down
Loading