Fixed #35395 -- slice filter crashes on an empty dict with Python 3.12.

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).
This commit is contained in:
Tim Richardson 2024-04-23 15:31:59 +10:00 committed by Sarah Boyce
parent 16d0542bb6
commit e64d42e753
2 changed files with 4 additions and 1 deletions

View File

@ -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.

View File

@ -53,3 +53,6 @@ class FunctionTests(SimpleTestCase):
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"), {})