#12820 closed (fixed)
for param in request.REQUEST fails with (a misleading) KeyError exception
Reported by: | Tamas Szabo | Owned by: | nobody |
---|---|---|---|
Component: | Core (Other) | Version: | dev |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
If you do a "for param in" on request.POST or request.GET it works as expected, but if you do it on request.REQUEST it fails.
The following simple code
for param in request.REQUEST: print param
will throw a KeyError exception! A few WTFs later I realized that the problem is in django.util.datastructures.MergeDict, that doesn't implement iter, but it does implement getitem so the for calls into getitem and passes in 0 (index of first element in a sequence).
0 won't be a valid key in request.GET or request.POST so a KeyError is thrown by the MergeDict.getitem implementation.
I've attached a patch that adds the missing iter method.
Attachments (3)
Change History (10)
by , 15 years ago
Attachment: | mergedict_patch.diff added |
---|
comment:1 by , 15 years ago
milestone: | → 1.2 |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 15 years ago
Component: | Uncategorized → Core framework |
---|
comment:3 by , 15 years ago
Patch needs improvement: | set |
---|
by , 15 years ago
Attachment: | 0001-Ticket-12820-fix-iteration-over-MergeDict.patch added |
---|
Updated patch that also fixes MergeDict.items()
comment:4 by , 15 years ago
Patch needs improvement: | unset |
---|
by , 15 years ago
Attachment: | 0001-Ticket-12820-fix-iteration-over-MergeDict.2.patch added |
---|
Revised patch based on IRC feedback from Alex_Gaynor (provides iter{iterms,keys,values}) and use tuple unpacking instead of indexing)
comment:5 by , 15 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
I get a test failure when applying this patch: "AttributeError: type object 'itertools.chain' has no attribute 'from_iterable'". Using python 2.5.4.