Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Ticket #732 (defect)

Opened 1 year ago

Last modified 1 year ago

tools.decode and non str params

Status: closed (fixed)

Reported by: d.rothe@semantics.de Assigned to: fumanchu
Priority: normal Milestone: 3.1
Component: CherryPy code Keywords:
Cc:

tools.decode does not handle non str params well.

using cp.3.0.2

After handling all the request-parameter decoding by myself (never sure I got it right), I checked out tools.decode.

It seems like this tool expects as parameters only strings and lists of strings. But I'm using the routes-dispatcher wich defaults some parameters to "None" for example. The tool tries to decode that "None" - bang.

I have modified the subroutine decode_params of the decode tool which reflects this.

def decode_params(encoding):
    decoded_params = {}
    for key, value in cherrypy.request.params.items():
        if hasattr(value, 'file'):
            # This is a file being uploaded: skip it
            decoded_params[key] = value
        elif isinstance(value, list):
            # value is a list: decode each element
            decoded_params[key] = [v.decode(encoding) for v in value]
        elif isinstance(value, unicode):
            #dirk: keep unicode param (move this to else:)
            decoded_params[key] = value
        elif isinstance(value, str):
            # value is a regular string: decode it
            decoded_params[key] = value.decode(encoding)
        else:
            # dirk: value is something else (maybe modified by a custom dispatcher)
            # keep it
            decoded_params[key] = value

    # Decode all or nothing, so we can try again on error.
    cherrypy.request.params = decoded_params

Change History

09/20/07 17:26:06: Modified by fumanchu

  • owner changed from rdelon to fumanchu.
  • status changed from new to assigned.
  • description changed.

09/20/07 17:31:13: Modified by fumanchu

  • status changed from assigned to closed.
  • resolution set to fixed.

Fixed in [1722]. I'm not 100% sure I like having non-string params in the first place, but allowing them might make the exposed function more readable, especially if someone is doing some form validation and type coercion before the handler is called. Yet Another Example of the fine line between HTTP-ness and Object-ness.

Hosted by WebFaction

Log in as guest/cpguest to create tickets