Skip to content
Myr

Fix pickle/copy support for the `missing` singleton (#2029)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by GitHub · parents ba8847a, 7232b82 · Browse files · 3 files+25 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+25 added−2 removed

  • tests:+12 added−0 removed
  • other:+13 added−2 removed

3 files+25 added−2 removed

modified:src/jinja2/utils.py+11 added-2 removed
16 unmodified lines
17 import typing_extensions as te1819F = t.TypeVar("F", bound=t.Callable[..., t.Any])2021# special singleton representing missing values for the runtime22missing: t.Any = type("MissingType", (), {"__repr__": lambda x: "missing"})()2122class _MissingType:23 def __repr__(self) -> str:24 return "missing"2526 def __reduce__(self) -> str:27 return "missing"282930missing: t.Any = _MissingType()31"""Special singleton representing missing values for the runtime."""3233internal_code: t.MutableSet[CodeType] = set()3435concat = "".join36
728 unmodified lines
modified:tests/test_utils.py+12 added
1import copy2import pickle3import random4from collections import deque5from copy import copy as shallow_copy
177 unmodified lines
183 x = iter([1, 2, 3, 4, 5])184 consume(x)185 with pytest.raises(StopIteration):186 next(x)187188189@pytest.mark.parametrize("protocol", range(pickle.HIGHEST_PROTOCOL + 1))190def test_pickle_missing(protocol: int) -> None:191 """Test that missing can be pickled while remaining a singleton."""192 assert pickle.loads(pickle.dumps(missing, protocol)) is missing193194195def test_copy_missing() -> None:196 """Test that missing can be copied while remaining a singleton."""197 assert copy.copy(missing) is missing
modified:CHANGES.rst+2 added
22 unmodified lines
23- Make compiling deterministic for tuple unpacking in a ``{% set ... %}``24 call. :issue:`2021`25- Fix dunder protocol (`copy`/`pickle`/etc) interaction with ``Undefined``26 objects. :issue:`2025`27- Fix `copy`/`pickle` support for the internal ``missing`` object.28 :issue:`2027`293031Version 3.1.432-------------
994 unmodified lines