Index: Lib/test/test_urllib.py =================================================================== --- Lib/test/test_urllib.py (revision 76700) +++ Lib/test/test_urllib.py (working copy) @@ -8,6 +8,7 @@ import unittest from test import support import os +import random import tempfile def hexescape(char): @@ -525,6 +526,13 @@ self.assertEqual(expect, result, "using quote_plus(): %r != %r" % (expect, result)) + def test_quote_leak(self): + # bug 5596 - highlight the refleak in the internal _safe_quoters cache + safe = ''.join(chr(random.randrange(128)) for i in '123456') + text = 'abcdefghijklmnopqrstuvwxyz' + result = urllib.parse.quote(text, safe=safe) + self.assertEqual(result, text) + class UnquotingTests(unittest.TestCase): """Tests for unquote() and unquote_plus() Index: Lib/urllib/parse.py =================================================================== --- Lib/urllib/parse.py (revision 76700) +++ Lib/urllib/parse.py (working copy) @@ -41,8 +41,9 @@ _parse_cache = {} def clear_cache(): - """Clear the parse cache.""" + """Clear the parse cache and the quoters cache.""" _parse_cache.clear() + _safe_quoters.clear() class ResultMixin(object):