Opened 7 years ago

Closed 7 years ago

Last modified 5 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: master
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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 7 years ago.
0001-Ticket-12820-fix-iteration-over-MergeDict.patch (2.1 KB) - added by gisle 7 years ago.
Updated patch that also fixes MergeDict.items()
0001-Ticket-12820-fix-iteration-over-MergeDict.2.patch (2.5 KB) - added by gisle 7 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)

Changed 7 years ago by Tamas Szabo

Attachment: mergedict_patch.diff added

comment:1 Changed 7 years ago by Alex Gaynor

milestone: 1.2
Triage Stage: UnreviewedAccepted

comment:2 Changed 7 years ago by Russell Keith-Magee

Component: UncategorizedCore framework

comment:3 Changed 7 years ago by anonymous

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.

Changed 7 years ago by gisle

Updated patch that also fixes MergeDict.items()

comment:4 Changed 7 years ago by gisle

Patch needs improvement: unset

Changed 7 years ago by gisle

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

comment:5 Changed 7 years ago by jkocherhans

Resolution: fixed
Status: newclosed

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

comment:6 Changed 7 years ago by jkocherhans

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

comment:7 Changed 5 years ago by Jacob

milestone: 1.2

Milestone 1.2 deleted

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