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

Guido van Rossum guido at python.org
Fri Aug 17 21:27:04 CEST 2012


I just fixed a unittest for some code used at Google that was
comparing a url generated by urllib.encode() to a fixed string. The
problem was caused by turning on PYTHONHASHSEED=1. Because of this,
the code under test would generate a textually different URL each time
the test was run, but the intention of the test was just to check that
all the query parameters were present and equal to the expected
values.

The solution was somewhat painful, I had to parse the url, split the
query parameters, and compare them to a known dict.

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?

-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-Dev mailing list