[Python-Dev] Should urlencode() sort the query parameters (if they come from a dict)?

Christian Heimes lists at cheimes.de
Sat Aug 18 15:28:03 CEST 2012


Am 17.08.2012 21:27, schrieb Guido van Rossum:
> I wonder if it wouldn't make sense to change urlencode() to generate
> URLs that don't depend on the hash order, for all versions of Python
> that support PYTHONHASHSEED? It seems a one-line fix:
> 
>         query = query.items()
> 
> with this:
> 
>         query = sorted(query.items())
> 
> This would not prevent breakage of unit tests, but it would make a
> much simpler fix possible: simply sort the parameters in the URL.

I vote -0. The issue can also be addressed with a small and simple
helper function that wraps urlparse and compares the query parameter. Or
you cann urlencode() with `sorted(qs.items)` instead of `qs` in the
application.

The order of query string parameter is actually important for some
applications, for example Zope, colander+deform and other form
frameworks use the parameter order to group parameters.

Therefore I propose that the query string is only sorted when the query
is exactly a dict and not some subclass or class that has an items() method.

    if type(query) is dict:
        query = sorted(query.items())
    else:
        query = query.items()

Christian



More information about the Python-Dev mailing list