Opened 14 years ago
Closed 13 years ago
#18097 closed Bug (worksforme)
__contains__ on an incompletely evaluated QuerySet can incorrectly return False
| Reported by: | Kevin Michel | Owned by: | nobody |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 1.4 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | yes | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
The bug is in the function QuerySet.__contains__(self, val), which is executed when using the python operator in.
It can only happen if :
valis in theQuerySetvalis the last item of theQuerySet- the
QuerySethas not been completely evaluated
If all those conditions hold, then :
valis not yet inself._result_cacheself._iteris notNone
This makes us jump to the while True loop, which runs correctly until we fetch the last item using self._fill_cache(num=1).
When fetching the last item, inside the self._fill_cache function, self._iter is set to None.
Just after fetching the last item, but before comparing its value against val, self.__contains__ checks the value of self._iter, which is None, and incorrectly return False.
The value of the last item should have been checked before checking the value of self._iter.
Attachments (1)
Change History (4)
by , 14 years ago
| Attachment: | fix_queryset_contains.diff added |
|---|
comment:1 by , 14 years ago
| Needs tests: | set |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
comment:2 by , 13 years ago
comment:3 by , 13 years ago
| Resolution: | → worksforme |
|---|---|
| Status: | new → closed |
As indicated by kmichel_wgs. Please re-open if you can provide a test case. Thanks!
Actually, after trying to write a test for the bug, I think the bug does not really exist.
During the "self._fill_cache(num=1)", self._iter will be set to None only if no element has been added to the _result_cache, in which case there is no new element to compare against val.
Sorry for the false alarm.