Commit 0250de48 authored by Cheryl Sabella's avatar Cheryl Sabella Committed by Łukasz Langa

bpo-27485: Rename and deprecate undocumented functions in urllib.parse (GH-2205)

parent 57faf348
......@@ -113,7 +113,7 @@ class MimeTypes:
Optional `strict' argument when False adds a bunch of commonly found,
but non-standard types.
"""
scheme, url = urllib.parse.splittype(url)
scheme, url = urllib.parse._splittype(url)
if scheme == 'data':
# syntax of data URLs:
# dataurl := "data:" [ mediatype ] [ ";base64" ] "," data
......
import unittest
import urllib.parse
import warnings
RFC1808_BASE = "http://a/b/c/d;p?q#f"
RFC2396_BASE = "http://a/b/c/d;p?q"
......@@ -1129,7 +1130,7 @@ class Utility_Tests(unittest.TestCase):
def test_to_bytes(self):
result = urllib.parse.to_bytes('http://www.python.org')
self.assertEqual(result, 'http://www.python.org')
self.assertRaises(UnicodeError, urllib.parse.to_bytes,
self.assertRaises(UnicodeError, urllib.parse._to_bytes,
'http://www.python.org/medi\u00e6val')
def test_unwrap(self):
......@@ -1137,5 +1138,90 @@ class Utility_Tests(unittest.TestCase):
self.assertEqual(url, 'type://host/path')
class DeprecationTest(unittest.TestCase):
def test_splittype_deprecation(self):
with self.assertWarns(DeprecationWarning) as cm:
urllib.parse.splittype('')
self.assertEqual(str(cm.warning),
'urllib.parse.splittype() is deprecated as of 3.8, '
'use urllib.parse.urlparse() instead')
def test_splithost_deprecation(self):
with self.assertWarns(DeprecationWarning) as cm:
urllib.parse.splithost('')
self.assertEqual(str(cm.warning),
'urllib.parse.splithost() is deprecated as of 3.8, '
'use urllib.parse.urlparse() instead')
def test_splituser_deprecation(self):
with self.assertWarns(DeprecationWarning) as cm:
urllib.parse.splituser('')
self.assertEqual(str(cm.warning),
'urllib.parse.splituser() is deprecated as of 3.8, '
'use urllib.parse.urlparse() instead')
def test_splitpasswd_deprecation(self):
with self.assertWarns(DeprecationWarning) as cm:
urllib.parse.splitpasswd('')
self.assertEqual(str(cm.warning),
'urllib.parse.splitpasswd() is deprecated as of 3.8, '
'use urllib.parse.urlparse() instead')
def test_splitport_deprecation(self):
with self.assertWarns(DeprecationWarning) as cm:
urllib.parse.splitport('')
self.assertEqual(str(cm.warning),
'urllib.parse.splitport() is deprecated as of 3.8, '
'use urllib.parse.urlparse() instead')
def test_splitnport_deprecation(self):
with self.assertWarns(DeprecationWarning) as cm:
urllib.parse.splitnport('')
self.assertEqual(str(cm.warning),
'urllib.parse.splitnport() is deprecated as of 3.8, '
'use urllib.parse.urlparse() instead')
def test_splitquery_deprecation(self):
with self.assertWarns(DeprecationWarning) as cm:
urllib.parse.splitquery('')
self.assertEqual(str(cm.warning),
'urllib.parse.splitquery() is deprecated as of 3.8, '
'use urllib.parse.urlparse() instead')
def test_splittag_deprecation(self):
with self.assertWarns(DeprecationWarning) as cm:
urllib.parse.splittag('')
self.assertEqual(str(cm.warning),
'urllib.parse.splittag() is deprecated as of 3.8, '
'use urllib.parse.urlparse() instead')
def test_splitattr_deprecation(self):
with self.assertWarns(DeprecationWarning) as cm:
urllib.parse.splitattr('')
self.assertEqual(str(cm.warning),
'urllib.parse.splitattr() is deprecated as of 3.8, '
'use urllib.parse.urlparse() instead')
def test_splitvalue_deprecation(self):
with self.assertWarns(DeprecationWarning) as cm:
urllib.parse.splitvalue('')
self.assertEqual(str(cm.warning),
'urllib.parse.splitvalue() is deprecated as of 3.8, '
'use urllib.parse.parse_qsl() instead')
def test_to_bytes_deprecation(self):
with self.assertWarns(DeprecationWarning) as cm:
urllib.parse.to_bytes('')
self.assertEqual(str(cm.warning),
'urllib.parse.to_bytes() is deprecated as of 3.8')
def test_unwrap(self):
with self.assertWarns(DeprecationWarning) as cm:
urllib.parse.unwrap('')
self.assertEqual(str(cm.warning),
'urllib.parse.unwrap() is deprecated as of 3.8')
if __name__ == "__main__":
unittest.main()
......@@ -30,6 +30,7 @@ test_urlparse.py provides a good indicator of parsing behavior.
import re
import sys
import collections
import warnings
__all__ = ["urlparse", "urlunparse", "urljoin", "urldefrag",
"urlsplit", "urlunsplit", "urlencode", "parse_qs",
......@@ -288,7 +289,7 @@ by reference to a primary resource and additional identifying information.
"""
_ParseResultBase.__doc__ = """
ParseResult(scheme, netloc, path, params, query, fragment)
ParseResult(scheme, netloc, path, params, query, fragment)
A 6-tuple that contains components of a parsed URL.
"""
......@@ -913,7 +914,14 @@ def urlencode(query, doseq=False, safe='', encoding=None, errors=None,
l.append(k + '=' + elt)
return '&'.join(l)
def to_bytes(url):
warnings.warn("urllib.parse.to_bytes() is deprecated as of 3.8",
DeprecationWarning, stacklevel=2)
return _to_bytes(url)
def _to_bytes(url):
"""to_bytes(u"URL") --> 'URL'."""
# Most URL schemes require ASCII. If that changes, the conversion
# can be relaxed.
......@@ -926,7 +934,14 @@ def to_bytes(url):
" contains non-ASCII characters")
return url
def unwrap(url):
warnings.warn("urllib.parse.unwrap() is deprecated as of 3.8",
DeprecationWarning, stacklevel=2)
return _unwrap(url)
def _unwrap(url):
"""unwrap('<URL:type://host/path>') --> 'type://host/path'."""
url = str(url).strip()
if url[:1] == '<' and url[-1:] == '>':
......@@ -934,8 +949,16 @@ def unwrap(url):
if url[:4] == 'URL:': url = url[4:].strip()
return url
_typeprog = None
def splittype(url):
warnings.warn("urllib.parse.splittype() is deprecated as of 3.8, "
"use urllib.parse.urlparse() instead",
DeprecationWarning, stacklevel=2)
return _splittype(url)
_typeprog = None
def _splittype(url):
"""splittype('type:opaquestring') --> 'type', 'opaquestring'."""
global _typeprog
if _typeprog is None:
......@@ -947,8 +970,16 @@ def splittype(url):
return scheme.lower(), data
return None, url
_hostprog = None
def splithost(url):
warnings.warn("urllib.parse.splithost() is deprecated as of 3.8, "
"use urllib.parse.urlparse() instead",
DeprecationWarning, stacklevel=2)
return _splithost(url)
_hostprog = None
def _splithost(url):
"""splithost('//host[:port]/path') --> 'host[:port]', '/path'."""
global _hostprog
if _hostprog is None:
......@@ -962,19 +993,43 @@ def splithost(url):
return host_port, path
return None, url
def splituser(host):
warnings.warn("urllib.parse.splituser() is deprecated as of 3.8, "
"use urllib.parse.urlparse() instead",
DeprecationWarning, stacklevel=2)
return _splituser(host)
def _splituser(host):
"""splituser('user[:passwd]@host[:port]') --> 'user[:passwd]', 'host[:port]'."""
user, delim, host = host.rpartition('@')
return (user if delim else None), host
def splitpasswd(user):
warnings.warn("urllib.parse.splitpasswd() is deprecated as of 3.8, "
"use urllib.parse.urlparse() instead",
DeprecationWarning, stacklevel=2)
return _splitpasswd(user)
def _splitpasswd(user):
"""splitpasswd('user:passwd') -> 'user', 'passwd'."""
user, delim, passwd = user.partition(':')
return user, (passwd if delim else None)
def splitport(host):
warnings.warn("urllib.parse.splitport() is deprecated as of 3.8, "
"use urllib.parse.urlparse() instead",
DeprecationWarning, stacklevel=2)
return _splitport(host)
# splittag('/path#tag') --> '/path', 'tag'
_portprog = None
def splitport(host):
def _splitport(host):
"""splitport('host:port') --> 'host', 'port'."""
global _portprog
if _portprog is None:
......@@ -987,7 +1042,15 @@ def splitport(host):
return host, port
return host, None
def splitnport(host, defport=-1):
warnings.warn("urllib.parse.splitnport() is deprecated as of 3.8, "
"use urllib.parse.urlparse() instead",
DeprecationWarning, stacklevel=2)
return _splitnport(host, defport)
def _splitnport(host, defport=-1):
"""Split host and port, returning numeric port.
Return given default port if no ':' found; defaults to -1.
Return numerical port if a valid number are found after ':'.
......@@ -1003,27 +1066,59 @@ def splitnport(host, defport=-1):
return host, nport
return host, defport
def splitquery(url):
warnings.warn("urllib.parse.splitquery() is deprecated as of 3.8, "
"use urllib.parse.urlparse() instead",
DeprecationWarning, stacklevel=2)
return _splitquery(url)
def _splitquery(url):
"""splitquery('/path?query') --> '/path', 'query'."""
path, delim, query = url.rpartition('?')
if delim:
return path, query
return url, None
def splittag(url):
warnings.warn("urllib.parse.splittag() is deprecated as of 3.8, "
"use urllib.parse.urlparse() instead",
DeprecationWarning, stacklevel=2)
return _splittag(url)
def _splittag(url):
"""splittag('/path#tag') --> '/path', 'tag'."""
path, delim, tag = url.rpartition('#')
if delim:
return path, tag
return url, None
def splitattr(url):
warnings.warn("urllib.parse.splitattr() is deprecated as of 3.8, "
"use urllib.parse.urlparse() instead",
DeprecationWarning, stacklevel=2)
return _splitattr(url)
def _splitattr(url):
"""splitattr('/path;attr1=value1;attr2=value2;...') ->
'/path', ['attr1=value1', 'attr2=value2', ...]."""
words = url.split(';')
return words[0], words[1:]
def splitvalue(attr):
warnings.warn("urllib.parse.splitvalue() is deprecated as of 3.8, "
"use urllib.parse.parse_qsl() instead",
DeprecationWarning, stacklevel=2)
return _splitvalue(attr)
def _splitvalue(attr):
"""splitvalue('attr=value') --> 'attr', 'value'."""
attr, delim, value = attr.partition('=')
return attr, (value if delim else None)
This diff is collapsed.
......@@ -1214,7 +1214,7 @@ class Transport:
if isinstance(host, tuple):
host, x509 = host
auth, host = urllib.parse.splituser(host)
auth, host = urllib.parse._splituser(host)
if auth:
auth = urllib.parse.unquote_to_bytes(auth)
......@@ -1413,10 +1413,10 @@ class ServerProxy:
# establish a "logical" server connection
# get the url
type, uri = urllib.parse.splittype(uri)
type, uri = urllib.parse._splittype(uri)
if type not in ("http", "https"):
raise OSError("unsupported XML-RPC protocol")
self.__host, self.__handler = urllib.parse.splithost(uri)
self.__host, self.__handler = urllib.parse._splithost(uri)
if not self.__handler:
self.__handler = "/RPC2"
......
Rename and deprecate undocumented functions in :func:`urllib.parse`.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment