diff options
| author | Senthil Kumaran <orsenthil@gmail.com> | 2010-07-22 01:47:30 (GMT) |
|---|---|---|
| committer | Senthil Kumaran <orsenthil@gmail.com> | 2010-07-22 01:47:30 (GMT) |
| commit | 880685f69894d3d267ac41abe0c06db3ba9a322d (patch) | |
| tree | a9d0b2de88cb208a40d98e88faad37cf783c1a77 /Lib | |
| parent | 7a7013e8304a5e54782330fe91e96e510d63e72e (diff) | |
| download | cpython-880685f69894d3d267ac41abe0c06db3ba9a322d.zip cpython-880685f69894d3d267ac41abe0c06db3ba9a322d.tar.gz cpython-880685f69894d3d267ac41abe0c06db3ba9a322d.tar.bz2 | |
Reverting the checkin made in revision 82940, as it was adding new parameters to quote function in a bugfix release.
Discussed in issue1712522
Diffstat (limited to 'Lib')
| -rw-r--r-- | Lib/test/test_urllib.py | 107 | ||||
| -rw-r--r-- | Lib/urllib.py | 26 |
2 files changed, 4 insertions, 129 deletions
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index 16febae..7119f55 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -355,38 +355,6 @@ class QuotingTests(unittest.TestCase): self.assertEqual(quote_by_default, result, "using quote_plus(): %s != %s" % (quote_by_default, result)) - # Safe expressed as unicode rather than str - result = urllib.quote(quote_by_default, safe=u"<>") - self.assertEqual(quote_by_default, result, - "using quote(): %r != %r" % (quote_by_default, result)) - # "Safe" non-ASCII bytes should still work - # (Technically disallowed by the URI standard, but allowed for - # backwards compatibility with previous versions of Python) - result = urllib.quote(b"a\xfcb", safe=b"\xfc") - expect = b"a\xfcb" - self.assertEqual(expect, result, - "using quote(): %r != %r" % - (expect, result)) - # Same as above, but with 'safe' as a unicode rather than str - # "Safe" non-ASCII unicode characters should have no effect - # (Since URIs are not allowed to have non-ASCII characters) - result = urllib.quote(b"a\xfcb", safe=u"\xfc") - expect = urllib.quote(b"a\xfcb", safe="") - self.assertEqual(expect, result, - "using quote(): %r != %r" % - (expect, result)) - # Same as above, but quoting a unicode rather than a str - result = urllib.quote(u"a\xfcb", encoding="latin-1", safe=b"\xfc") - expect = b"a\xfcb" - self.assertEqual(expect, result, - "using quote(): %r != %r" % - (expect, result)) - # Same as above, but with both the quoted value and 'safe' as unicode - result = urllib.quote(u"a\xfcb", encoding="latin-1", safe=u"\xfc") - expect = urllib.quote(u"a\xfcb", encoding="latin-1", safe="") - self.assertEqual(expect, result, - "using quote(): %r != %r" % - (expect, result)) def test_default_quoting(self): # Make sure all characters that should be quoted are by default sans @@ -439,81 +407,6 @@ class QuotingTests(unittest.TestCase): 'alpha%2Bbeta+gamma') self.assertEqual(urllib.quote_plus('alpha+beta gamma', '+'), 'alpha+beta+gamma') - # Test with unicode - self.assertEqual(urllib.quote_plus(u'alpha+beta gamma'), - 'alpha%2Bbeta+gamma') - # Test with safe unicode - self.assertEqual(urllib.quote_plus('alpha+beta gamma', u'+'), - 'alpha+beta+gamma') - - def test_quote_bytes(self): - # Non-ASCII bytes should quote directly to percent-encoded values - given = b"\xa2\xd8ab\xff" - expect = "%A2%D8ab%FF" - result = urllib.quote(given) - self.assertEqual(expect, result, - "using quote(): %r != %r" % (expect, result)) - # Encoding argument should raise UnicodeDecodeError on bytes input - # with non-ASCII characters (just as with str.encode). - self.assertRaises(UnicodeDecodeError, urllib.quote, given, - encoding="latin-1") - - def test_quote_with_unicode(self): - # Characters in Latin-1 range, encoded by default in UTF-8 - given = u"\xa2\xd8ab\xff" - expect = "%C2%A2%C3%98ab%C3%BF" - result = urllib.quote(given) - self.assertEqual(expect, result, - "using quote(): %r != %r" % (expect, result)) - # Characters in Latin-1 range, encoded by with None (default) - result = urllib.quote(given, encoding=None, errors=None) - self.assertEqual(expect, result, - "using quote(): %r != %r" % (expect, result)) - # Characters in Latin-1 range, encoded with Latin-1 - given = u"\xa2\xd8ab\xff" - expect = "%A2%D8ab%FF" - result = urllib.quote(given, encoding="latin-1") - self.assertEqual(expect, result, - "using quote(): %r != %r" % (expect, result)) - # Characters in BMP, encoded by default in UTF-8 - given = u"\u6f22\u5b57" # "Kanji" - expect = "%E6%BC%A2%E5%AD%97" - result = urllib.quote(given) - self.assertEqual(expect, result, - "using quote(): %r != %r" % (expect, result)) - # Characters in BMP, encoded with Latin-1 - given = u"\u6f22\u5b57" - self.assertRaises(UnicodeEncodeError, urllib.quote, given, - encoding="latin-1") - # Characters in BMP, encoded with Latin-1, with replace error handling - given = u"\u6f22\u5b57" - expect = "%3F%3F" # "??" - result = urllib.quote(given, encoding="latin-1", - errors="replace") - self.assertEqual(expect, result, - "using quote(): %r != %r" % (expect, result)) - # Characters in BMP, Latin-1, with xmlcharref error handling - given = u"\u6f22\u5b57" - expect = "%26%2328450%3B%26%2323383%3B" # "漢字" - result = urllib.quote(given, encoding="latin-1", - errors="xmlcharrefreplace") - self.assertEqual(expect, result, - "using quote(): %r != %r" % (expect, result)) - - def test_quote_plus_with_unicode(self): - # Encoding (latin-1) test for quote_plus - given = u"\xa2\xd8 \xff" - expect = "%A2%D8+%FF" - result = urllib.quote_plus(given, encoding="latin-1") - self.assertEqual(expect, result, - "using quote_plus(): %r != %r" % (expect, result)) - # Errors test for quote_plus - given = u"ab\u6f22\u5b57 cd" - expect = "ab%3F%3F+cd" - result = urllib.quote_plus(given, encoding="latin-1", - errors="replace") - self.assertEqual(expect, result, - "using quote_plus(): %r != %r" % (expect, result)) class UnquotingTests(unittest.TestCase): """Tests for unquote() and unquote_plus() diff --git a/Lib/urllib.py b/Lib/urllib.py index 3460a56..9c58923 100644 --- a/Lib/urllib.py +++ b/Lib/urllib.py @@ -1193,7 +1193,7 @@ for i, c in zip(xrange(256), str(bytearray(xrange(256)))): _safe_map[c] = c if (i < 128 and c in always_safe) else '%{:02X}'.format(i) _safe_quoters = {} -def quote(s, safe='/', encoding=None, errors=None): +def quote(s, safe='/'): """quote('abc def') -> 'abc%20def' Each part of a URL, e.g. the path info, the query, etc., has a @@ -1213,30 +1213,12 @@ def quote(s, safe='/', encoding=None, errors=None): is reserved, but in typical usage the quote function is being called on a path where the existing slash characters are used as reserved characters. - - string and safe may be either str or unicode objects. - - The optional encoding and errors parameters specify how to deal with the - non-ASCII characters, as accepted by the unicode.encode method. - By default, encoding='utf-8' (characters are encoded with UTF-8), and - errors='strict' (unsupported characters raise a UnicodeEncodeError). """ # fastpath if not s: if s is None: raise TypeError('None object cannot be quoted') return s - - if encoding is not None or isinstance(s, unicode): - if encoding is None: - encoding = 'utf-8' - if errors is None: - errors = 'strict' - s = s.encode(encoding, errors) - if isinstance(safe, unicode): - # Normalize 'safe' by converting to str and removing non-ASCII chars - safe = safe.encode('ascii', 'ignore') - cachekey = (safe, always_safe) try: (quoter, safe) = _safe_quoters[cachekey] @@ -1250,12 +1232,12 @@ def quote(s, safe='/', encoding=None, errors=None): return s return ''.join(map(quoter, s)) -def quote_plus(s, safe='', encoding=None, errors=None): +def quote_plus(s, safe=''): """Quote the query fragment of a URL; replacing ' ' with '+'""" if ' ' in s: - s = quote(s, safe + ' ', encoding, errors) + s = quote(s, safe + ' ') return s.replace(' ', '+') - return quote(s, safe, encoding, errors) + return quote(s, safe) def urlencode(query, doseq=0): """Encode a sequence of two-element tuples or dictionary into a URL query string. |
