[New-bugs-announce] [issue15719] Sort dict items in urlencode()

Guido van Rossum report at bugs.python.org
Fri Aug 17 22:44:53 CEST 2012

New submission from Guido van Rossum:

>From http://mail.python.org/pipermail/python-dev/2012-August/121364.html :

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

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.

MvL's response (http://mail.python.org/pipermail/python-dev/2012-August/121366.html) seems to suggest that this can go in as a bugfix in 3.2 and later if augmented with a check for type(query) is dict and a check for whether dict hashing is enabled.

Does anyone want to turn this idea into a patch?

keywords: easy
messages: 168477
nosy: gvanrossum
priority: normal
severity: normal
status: open
title: Sort dict items in urlencode()
versions: Python 3.2, Python 3.3, Python 3.4

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list