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

Peter Otten __peter__ at web.de
Sat Aug 18 09:29:39 CEST 2012


Guido van Rossum wrote:

> 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.
> 
> Thoughts?

There may be people who mix bytes and str or pass other non-str keys:

>>> query = {b"a":b"b", "c":"d", 5:6}
>>> urlencode(query)
'a=b&c=d&5=6'
>>> sorted(query.items())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() < bytes()

Not pretty, but a bugfix should not break such constructs.



More information about the Python-Dev mailing list