toastdriven/django-haystack — pull request #999 by yorkedork (2014-05-09 23:04:10+00:00)
Title: '__len__' may never return a value < 0.

There have been a few issues (#445, #704) and a referenced commit cd06d3be concerning the use of `__len__` in the `haystack.query` module.

In particular, the line:

`if len(self) <= 0`

will always raise a ValueError when `(self._result_count - self._ignored_result_count) < 0`. Although it appears that the referenced commit attempts to treat symptoms of this state, it seems that it is still quite possible that the implementation of `SearchQuerySet.__len__` will produce a negative value.

The attached pull request resolves this issue by ensuring that this method always returns a nonnegative integer.

Alternatively, the calling convention could be altered to avoid being beholden to the semantics bound to the `__len__` builtin:

`if self.__len__() <= 0`

Without more thoroughly reviewing the code paths, I'm unsure which method is a more practical resolution.