Skip to content

Commit

Permalink
Fixed #35395 -- slice filter crashes on an empty dict with Python 3.12.
Browse files Browse the repository at this point in the history
Keep consistent behaviour of slice() filter between python 3.12 and prior
versions in the case of a dict passed to the filter (catch the new to python
3.12 KeyError exception).
  • Loading branch information
Tim Richardson authored and sarahboyce committed Apr 24, 2024
1 parent 16d0542 commit e64d42e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion django/template/defaultfilters.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ def slice_filter(value, arg):
bits.append(int(x))
return value[slice(*bits)]

except (ValueError, TypeError):
except (ValueError, TypeError, KeyError):
return value # Fail silently.


Expand Down
3 changes: 3 additions & 0 deletions tests/template_tests/filter_tests/test_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,6 @@ def test_range_step(self):
def test_fail_silently(self):
obj = object()
self.assertEqual(slice_filter(obj, "0::2"), obj)

def test_empty_dict(self):
self.assertEqual(slice_filter({}, "1"), {})

0 comments on commit e64d42e

Please sign in to comment.