# HG changeset patch # Parent 76170e33f25183101000668be79754654f9070a6 diff -r 76170e33f251 Doc/library/urllib.parse.rst --- a/Doc/library/urllib.parse.rst Mon Feb 09 21:00:00 2015 -0500 +++ b/Doc/library/urllib.parse.rst Tue Feb 10 05:04:17 2015 +0000 @@ -350,7 +350,11 @@ :func:`urldefrag` functions are subclasses of the :class:`tuple` type. These subclasses add the attributes listed in the documentation for those functions, the encoding and decoding support described in the -previous section, as well as an additional method: +previous section, as well as an additional method. + +.. versionchanged:: 3.5 + Constructor arguments now default to empty strings. Previously, + all constructor arguments were mandatory. .. method:: urllib.parse.SplitResult.geturl() @@ -379,7 +383,7 @@ The following classes provide the implementations of the structured parse results when operating on :class:`str` objects: -.. class:: DefragResult(url, fragment) +.. class:: DefragResult(url='', fragment='') Concrete class for :func:`urldefrag` results containing :class:`str` data. The :meth:`encode` method returns a :class:`DefragResultBytes` @@ -387,13 +391,13 @@ .. versionadded:: 3.2 -.. class:: ParseResult(scheme, netloc, path, params, query, fragment) +.. class:: ParseResult(scheme='', netloc='', path='', params='', query='', fragment='') Concrete class for :func:`urlparse` results containing :class:`str` data. The :meth:`encode` method returns a :class:`ParseResultBytes` instance. -.. class:: SplitResult(scheme, netloc, path, query, fragment) +.. class:: SplitResult(scheme='', netloc='', path='', query='', fragment='') Concrete class for :func:`urlsplit` results containing :class:`str` data. The :meth:`encode` method returns a :class:`SplitResultBytes` @@ -403,7 +407,7 @@ The following classes provide the implementations of the parse results when operating on :class:`bytes` or :class:`bytearray` objects: -.. class:: DefragResultBytes(url, fragment) +.. class:: DefragResultBytes(url=b'', fragment=b'') Concrete class for :func:`urldefrag` results containing :class:`bytes` data. The :meth:`decode` method returns a :class:`DefragResult` @@ -411,7 +415,7 @@ .. versionadded:: 3.2 -.. class:: ParseResultBytes(scheme, netloc, path, params, query, fragment) +.. class:: ParseResultBytes(scheme=b'', netloc=b'', path=b'', params=b'', query=b'', fragment=b'') Concrete class for :func:`urlparse` results containing :class:`bytes` data. The :meth:`decode` method returns a :class:`ParseResult` @@ -419,7 +423,7 @@ .. versionadded:: 3.2 -.. class:: SplitResultBytes(scheme, netloc, path, query, fragment) +.. class:: SplitResultBytes(scheme=b'', netloc=b'', path=b'', query=b'', fragment=b'') Concrete class for :func:`urlsplit` results containing :class:`bytes` data. The :meth:`decode` method returns a :class:`SplitResult` diff -r 76170e33f251 Lib/test/test_urlparse.py --- a/Lib/test/test_urlparse.py Mon Feb 09 21:00:00 2015 -0500 +++ b/Lib/test/test_urlparse.py Tue Feb 10 05:04:17 2015 +0000 @@ -743,6 +743,31 @@ for result_type in result_types: self._check_result_type(result_type) + def test_result_default(self): + # Test default argument handling of -Result object constructors + tests = ( + ('Defrag', (), dict(), ''), + ('Defrag', (), dict(fragment='f'), '#f'), + ('Defrag', ('file:///path',), dict(), 'file:///path'), + ('Split', (), dict(), ''), + ('Split', ('rtp', 'localhost:5004'), dict(query='rtcpport=5005'), + 'rtp://localhost:5004?rtcpport=5005'), + ('Split', (), dict(netloc='[::1]:0'), '//[::1]:0'), + ('Parse', (), dict(), ''), + ('Parse', ('', 'localhost'), dict(params='a=1;b=2', fragment=''), + '//localhost/;a=1;b=2'), + ) + for type, pos, kw, url in tests: + with self.subTest(type, pos=pos, **kw): + constructor = getattr(urllib.parse, type + 'Result') + self.assertEqual(constructor(*pos, **kw).geturl(), url) + pos = (arg.encode('ascii') for arg in pos) + kw = dict((name, value.encode('ascii')) for + [name, value] in kw.items()) + constructor = getattr(urllib.parse, type + 'ResultBytes') + url = url.encode('ascii') + self.assertEqual(constructor(*pos, **kw).geturl(), url) + def test_parse_qs_encoding(self): result = urllib.parse.parse_qs("key=\u0141%E9", encoding="latin-1") self.assertEqual(result, {'key': ['\u0141\xE9']}) diff -r 76170e33f251 Lib/urllib/parse.py --- a/Lib/urllib/parse.py Mon Feb 09 21:00:00 2015 -0500 +++ b/Lib/urllib/parse.py Tue Feb 10 05:04:17 2015 +0000 @@ -234,6 +234,9 @@ # Structured result objects for string data class DefragResult(_DefragResultBase, _ResultMixinStr): __slots__ = () + def __new__(type, url='', fragment=''): + return _DefragResultBase.__new__(type, url, fragment) + def geturl(self): if self.fragment: return self.url + '#' + self.fragment @@ -242,17 +245,29 @@ class SplitResult(_SplitResultBase, _NetlocResultMixinStr): __slots__ = () + def __new__(type, scheme='', netloc='', path='', query='', fragment=''): + return _SplitResultBase.__new__(type, + scheme, netloc, path, query, fragment) + def geturl(self): return urlunsplit(self) class ParseResult(_ParseResultBase, _NetlocResultMixinStr): __slots__ = () + def __new__(type, + scheme='', netloc='', path='', params='', query='', fragment=''): + return _ParseResultBase.__new__(type, + scheme, netloc, path, params, query, fragment) + def geturl(self): return urlunparse(self) # Structured result objects for bytes data class DefragResultBytes(_DefragResultBase, _ResultMixinBytes): __slots__ = () + def __new__(type, url=b'', fragment=b''): + return _DefragResultBase.__new__(type, url, fragment) + def geturl(self): if self.fragment: return self.url + b'#' + self.fragment @@ -261,11 +276,21 @@ class SplitResultBytes(_SplitResultBase, _NetlocResultMixinBytes): __slots__ = () + def __new__(type, + scheme=b'', netloc=b'', path=b'', query=b'',fragment=b''): + return _SplitResultBase.__new__(type, + scheme, netloc, path, query, fragment) + def geturl(self): return urlunsplit(self) class ParseResultBytes(_ParseResultBase, _NetlocResultMixinBytes): __slots__ = () + def __new__(type, scheme=b'', netloc=b'', path=b'', + params=b'', query=b'', fragment=b''): + return _ParseResultBase.__new__(type, + scheme, netloc, path, params, query, fragment) + def geturl(self): return urlunparse(self)