Opened 15 years ago

Closed 15 years ago

Last modified 13 years ago

#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)

mergedict_patch.diff (1.3 KB ) - added by Tamas Szabo 15 years ago.
0001-Ticket-12820-fix-iteration-over-MergeDict.patch (2.1 KB ) - added by gisle 15 years ago.
Updated patch that also fixes MergeDict.items()
0001-Ticket-12820-fix-iteration-over-MergeDict.2.patch (2.5 KB ) - added by gisle 15 years ago.
Revised patch based on IRC feedback from Alex_Gaynor (provides iter{iterms,keys,values}) and use tuple unpacking instead of indexing)

Download all attachments as: .zip

Change History (10)

by Tamas Szabo, 15 years ago

Attachment: mergedict_patch.diff added

comment:1 by Alex Gaynor, 15 years ago

milestone: 1.2
Triage Stage: UnreviewedAccepted

comment:2 by Russell Keith-Magee, 15 years ago

Component: UncategorizedCore framework

comment:3 by anonymous, 15 years ago

Patch needs improvement: set

I get a test failure when applying this patch: "AttributeError: type object 'itertools.chain' has no attribute 'from_iterable'". Using python 2.5.4.

by gisle, 15 years ago

Updated patch that also fixes MergeDict.items()

comment:4 by gisle, 15 years ago

Patch needs improvement: unset

by gisle, 15 years ago

Revised patch based on IRC feedback from Alex_Gaynor (provides iter{iterms,keys,values}) and use tuple unpacking instead of indexing)

comment:5 by jkocherhans, 15 years ago

Resolution: fixed
Status: newclosed

(In [12498]) Fixed #12820. Implemented other dict methods for MergeDict. Thanks, Gisle Aas.

comment:6 by jkocherhans, 15 years ago

(In [12501]) [1.1.X] Fixed #12820. Implemented other dict methods for MergeDict. Backport of [12498] from trunk.

comment:7 by Jacob, 13 years ago

milestone: 1.2

Milestone 1.2 deleted

Note: See TracTickets for help on using tickets.
Back to Top