From f34f662f35075cd51c893979c2353ccf92e7c6a1 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Tue, 10 Jan 2017 02:34:21 -0500 Subject: [PATCH] Fix len() sometimes raising ValueError on empty node lists (fixes #174) --- CHANGELOG | 1 + docs/changelog.rst | 1 + mwparserfromhell/smart_list.py | 2 +- tests/test_smart_list.py | 1 + 4 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 05b64ef..f3728dd 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ v0.5 (unreleased): +- Fixed len() sometimes raising ValueError on empty node lists. - Fixed release script after changes to PyPI. v0.4.4 (released December 30, 2016): diff --git a/docs/changelog.rst b/docs/changelog.rst index ec12e6d..edf5ab9 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -7,6 +7,7 @@ v0.5 Unreleased (`changes `__): +- Fixed ``len()`` sometimes raising ``ValueError`` on empty node lists. - Fixed release script after changes to PyPI. v0.4.4 diff --git a/mwparserfromhell/smart_list.py b/mwparserfromhell/smart_list.py index c59a363..e7fa59f 100644 --- a/mwparserfromhell/smart_list.py +++ b/mwparserfromhell/smart_list.py @@ -271,7 +271,7 @@ class _ListProxy(_SliceNormalizerMixIn, list): return bool(self._render()) def __len__(self): - return (self._stop - self._start) // self._step + return max((self._stop - self._start) // self._step, 0) def __getitem__(self, key): if isinstance(key, slice): diff --git a/tests/test_smart_list.py b/tests/test_smart_list.py index 0330aed..3de7db7 100644 --- a/tests/test_smart_list.py +++ b/tests/test_smart_list.py @@ -398,6 +398,7 @@ class TestSmartList(unittest.TestCase): self.assertEqual([4, 3, 2, 1.9, 1.8, 5, 6], child1) self.assertEqual([4, 3, 2, 1.9, 1.8], child2) self.assertEqual([], child3) + self.assertEqual(0, len(child3)) del child1 self.assertEqual([1, 4, 3, 2, 1.9, 1.8, 5, 6], parent)