Opened 6 years ago

Closed 6 years ago

Last modified 4 years ago

#12820 closed (fixed)

for param in request.REQUEST fails with (a misleading) KeyError exception

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

comment:1 Changed 6 years ago by Alex

  • milestone set to 1.2
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 6 years ago by russellm

  • Component changed from Uncategorized to Core framework

comment:3 Changed 6 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 6 years ago by gisle

Updated patch that also fixes MergeDict.items()

comment:4 Changed 6 years ago by gisle

  • Patch needs improvement unset

Changed 6 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 6 years ago by jkocherhans

  • Resolution set to fixed
  • Status changed from new to closed

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

comment:6 Changed 6 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 4 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

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