Skip to content

Commit 0250de4

Browse files
csabellaambv
authored andcommitted
bpo-27485: Rename and deprecate undocumented functions in urllib.parse (GH-2205)
1 parent 57faf34 commit 0250de4

File tree

6 files changed

+244
-62
lines changed

6 files changed

+244
-62
lines changed

Lib/mimetypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def guess_type(self, url, strict=True):
113113
Optional `strict' argument when False adds a bunch of commonly found,
114114
but non-standard types.
115115
"""
116-
scheme, url = urllib.parse.splittype(url)
116+
scheme, url = urllib.parse._splittype(url)
117117
if scheme == 'data':
118118
# syntax of data URLs:
119119
# dataurl := "data:" [ mediatype ] [ ";base64" ] "," data

Lib/test/test_urlparse.py

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22
import urllib.parse
3+
import warnings
34

45
RFC1808_BASE = "http://a/b/c/d;p?q#f"
56
RFC2396_BASE = "http://a/b/c/d;p?q"
@@ -1129,13 +1130,98 @@ def test_splitvalue(self):
11291130
def test_to_bytes(self):
11301131
result = urllib.parse.to_bytes('http://www.python.org')
11311132
self.assertEqual(result, 'http://www.python.org')
1132-
self.assertRaises(UnicodeError, urllib.parse.to_bytes,
1133+
self.assertRaises(UnicodeError, urllib.parse._to_bytes,
11331134
'http://www.python.org/medi\u00e6val')
11341135

11351136
def test_unwrap(self):
11361137
url = urllib.parse.unwrap('<URL:type://host/path>')
11371138
self.assertEqual(url, 'type://host/path')
11381139

11391140

1141+
class DeprecationTest(unittest.TestCase):
1142+
1143+
def test_splittype_deprecation(self):
1144+
with self.assertWarns(DeprecationWarning) as cm:
1145+
urllib.parse.splittype('')
1146+
self.assertEqual(str(cm.warning),
1147+
'urllib.parse.splittype() is deprecated as of 3.8, '
1148+
'use urllib.parse.urlparse() instead')
1149+
1150+
def test_splithost_deprecation(self):
1151+
with self.assertWarns(DeprecationWarning) as cm:
1152+
urllib.parse.splithost('')
1153+
self.assertEqual(str(cm.warning),
1154+
'urllib.parse.splithost() is deprecated as of 3.8, '
1155+
'use urllib.parse.urlparse() instead')
1156+
1157+
def test_splituser_deprecation(self):
1158+
with self.assertWarns(DeprecationWarning) as cm:
1159+
urllib.parse.splituser('')
1160+
self.assertEqual(str(cm.warning),
1161+
'urllib.parse.splituser() is deprecated as of 3.8, '
1162+
'use urllib.parse.urlparse() instead')
1163+
1164+
def test_splitpasswd_deprecation(self):
1165+
with self.assertWarns(DeprecationWarning) as cm:
1166+
urllib.parse.splitpasswd('')
1167+
self.assertEqual(str(cm.warning),
1168+
'urllib.parse.splitpasswd() is deprecated as of 3.8, '
1169+
'use urllib.parse.urlparse() instead')
1170+
1171+
def test_splitport_deprecation(self):
1172+
with self.assertWarns(DeprecationWarning) as cm:
1173+
urllib.parse.splitport('')
1174+
self.assertEqual(str(cm.warning),
1175+
'urllib.parse.splitport() is deprecated as of 3.8, '
1176+
'use urllib.parse.urlparse() instead')
1177+
1178+
def test_splitnport_deprecation(self):
1179+
with self.assertWarns(DeprecationWarning) as cm:
1180+
urllib.parse.splitnport('')
1181+
self.assertEqual(str(cm.warning),
1182+
'urllib.parse.splitnport() is deprecated as of 3.8, '
1183+
'use urllib.parse.urlparse() instead')
1184+
1185+
def test_splitquery_deprecation(self):
1186+
with self.assertWarns(DeprecationWarning) as cm:
1187+
urllib.parse.splitquery('')
1188+
self.assertEqual(str(cm.warning),
1189+
'urllib.parse.splitquery() is deprecated as of 3.8, '
1190+
'use urllib.parse.urlparse() instead')
1191+
1192+
def test_splittag_deprecation(self):
1193+
with self.assertWarns(DeprecationWarning) as cm:
1194+
urllib.parse.splittag('')
1195+
self.assertEqual(str(cm.warning),
1196+
'urllib.parse.splittag() is deprecated as of 3.8, '
1197+
'use urllib.parse.urlparse() instead')
1198+
1199+
def test_splitattr_deprecation(self):
1200+
with self.assertWarns(DeprecationWarning) as cm:
1201+
urllib.parse.splitattr('')
1202+
self.assertEqual(str(cm.warning),
1203+
'urllib.parse.splitattr() is deprecated as of 3.8, '
1204+
'use urllib.parse.urlparse() instead')
1205+
1206+
def test_splitvalue_deprecation(self):
1207+
with self.assertWarns(DeprecationWarning) as cm:
1208+
urllib.parse.splitvalue('')
1209+
self.assertEqual(str(cm.warning),
1210+
'urllib.parse.splitvalue() is deprecated as of 3.8, '
1211+
'use urllib.parse.parse_qsl() instead')
1212+
1213+
def test_to_bytes_deprecation(self):
1214+
with self.assertWarns(DeprecationWarning) as cm:
1215+
urllib.parse.to_bytes('')
1216+
self.assertEqual(str(cm.warning),
1217+
'urllib.parse.to_bytes() is deprecated as of 3.8')
1218+
1219+
def test_unwrap(self):
1220+
with self.assertWarns(DeprecationWarning) as cm:
1221+
urllib.parse.unwrap('')
1222+
self.assertEqual(str(cm.warning),
1223+
'urllib.parse.unwrap() is deprecated as of 3.8')
1224+
1225+
11401226
if __name__ == "__main__":
11411227
unittest.main()

Lib/urllib/parse.py

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import re
3131
import sys
3232
import collections
33+
import warnings
3334

3435
__all__ = ["urlparse", "urlunparse", "urljoin", "urldefrag",
3536
"urlsplit", "urlunsplit", "urlencode", "parse_qs",
@@ -288,7 +289,7 @@ def _hostinfo(self):
288289
"""
289290

290291
_ParseResultBase.__doc__ = """
291-
ParseResult(scheme, netloc, path, params, query, fragment)
292+
ParseResult(scheme, netloc, path, params, query, fragment)
292293
293294
A 6-tuple that contains components of a parsed URL.
294295
"""
@@ -913,7 +914,14 @@ def urlencode(query, doseq=False, safe='', encoding=None, errors=None,
913914
l.append(k + '=' + elt)
914915
return '&'.join(l)
915916

917+
916918
def to_bytes(url):
919+
warnings.warn("urllib.parse.to_bytes() is deprecated as of 3.8",
920+
DeprecationWarning, stacklevel=2)
921+
return _to_bytes(url)
922+
923+
924+
def _to_bytes(url):
917925
"""to_bytes(u"URL") --> 'URL'."""
918926
# Most URL schemes require ASCII. If that changes, the conversion
919927
# can be relaxed.
@@ -926,16 +934,31 @@ def to_bytes(url):
926934
" contains non-ASCII characters")
927935
return url
928936

937+
929938
def unwrap(url):
939+
warnings.warn("urllib.parse.unwrap() is deprecated as of 3.8",
940+
DeprecationWarning, stacklevel=2)
941+
return _unwrap(url)
942+
943+
944+
def _unwrap(url):
930945
"""unwrap('<URL:type://host/path>') --> 'type://host/path'."""
931946
url = str(url).strip()
932947
if url[:1] == '<' and url[-1:] == '>':
933948
url = url[1:-1].strip()
934949
if url[:4] == 'URL:': url = url[4:].strip()
935950
return url
936951

937-
_typeprog = None
952+
938953
def splittype(url):
954+
warnings.warn("urllib.parse.splittype() is deprecated as of 3.8, "
955+
"use urllib.parse.urlparse() instead",
956+
DeprecationWarning, stacklevel=2)
957+
return _splittype(url)
958+
959+
960+
_typeprog = None
961+
def _splittype(url):
939962
"""splittype('type:opaquestring') --> 'type', 'opaquestring'."""
940963
global _typeprog
941964
if _typeprog is None:
@@ -947,8 +970,16 @@ def splittype(url):
947970
return scheme.lower(), data
948971
return None, url
949972

950-
_hostprog = None
973+
951974
def splithost(url):
975+
warnings.warn("urllib.parse.splithost() is deprecated as of 3.8, "
976+
"use urllib.parse.urlparse() instead",
977+
DeprecationWarning, stacklevel=2)
978+
return _splithost(url)
979+
980+
981+
_hostprog = None
982+
def _splithost(url):
952983
"""splithost('//host[:port]/path') --> 'host[:port]', '/path'."""
953984
global _hostprog
954985
if _hostprog is None:
@@ -962,19 +993,43 @@ def splithost(url):
962993
return host_port, path
963994
return None, url
964995

996+
965997
def splituser(host):
998+
warnings.warn("urllib.parse.splituser() is deprecated as of 3.8, "
999+
"use urllib.parse.urlparse() instead",
1000+
DeprecationWarning, stacklevel=2)
1001+
return _splituser(host)
1002+
1003+
1004+
def _splituser(host):
9661005
"""splituser('user[:passwd]@host[:port]') --> 'user[:passwd]', 'host[:port]'."""
9671006
user, delim, host = host.rpartition('@')
9681007
return (user if delim else None), host
9691008

1009+
9701010
def splitpasswd(user):
1011+
warnings.warn("urllib.parse.splitpasswd() is deprecated as of 3.8, "
1012+
"use urllib.parse.urlparse() instead",
1013+
DeprecationWarning, stacklevel=2)
1014+
return _splitpasswd(user)
1015+
1016+
1017+
def _splitpasswd(user):
9711018
"""splitpasswd('user:passwd') -> 'user', 'passwd'."""
9721019
user, delim, passwd = user.partition(':')
9731020
return user, (passwd if delim else None)
9741021

1022+
1023+
def splitport(host):
1024+
warnings.warn("urllib.parse.splitport() is deprecated as of 3.8, "
1025+
"use urllib.parse.urlparse() instead",
1026+
DeprecationWarning, stacklevel=2)
1027+
return _splitport(host)
1028+
1029+
9751030
# splittag('/path#tag') --> '/path', 'tag'
9761031
_portprog = None
977-
def splitport(host):
1032+
def _splitport(host):
9781033
"""splitport('host:port') --> 'host', 'port'."""
9791034
global _portprog
9801035
if _portprog is None:
@@ -987,7 +1042,15 @@ def splitport(host):
9871042
return host, port
9881043
return host, None
9891044

1045+
9901046
def splitnport(host, defport=-1):
1047+
warnings.warn("urllib.parse.splitnport() is deprecated as of 3.8, "
1048+
"use urllib.parse.urlparse() instead",
1049+
DeprecationWarning, stacklevel=2)
1050+
return _splitnport(host, defport)
1051+
1052+
1053+
def _splitnport(host, defport=-1):
9911054
"""Split host and port, returning numeric port.
9921055
Return given default port if no ':' found; defaults to -1.
9931056
Return numerical port if a valid number are found after ':'.
@@ -1003,27 +1066,59 @@ def splitnport(host, defport=-1):
10031066
return host, nport
10041067
return host, defport
10051068

1069+
10061070
def splitquery(url):
1071+
warnings.warn("urllib.parse.splitquery() is deprecated as of 3.8, "
1072+
"use urllib.parse.urlparse() instead",
1073+
DeprecationWarning, stacklevel=2)
1074+
return _splitquery(url)
1075+
1076+
1077+
def _splitquery(url):
10071078
"""splitquery('/path?query') --> '/path', 'query'."""
10081079
path, delim, query = url.rpartition('?')
10091080
if delim:
10101081
return path, query
10111082
return url, None
10121083

1084+
10131085
def splittag(url):
1086+
warnings.warn("urllib.parse.splittag() is deprecated as of 3.8, "
1087+
"use urllib.parse.urlparse() instead",
1088+
DeprecationWarning, stacklevel=2)
1089+
return _splittag(url)
1090+
1091+
1092+
def _splittag(url):
10141093
"""splittag('/path#tag') --> '/path', 'tag'."""
10151094
path, delim, tag = url.rpartition('#')
10161095
if delim:
10171096
return path, tag
10181097
return url, None
10191098

1099+
10201100
def splitattr(url):
1101+
warnings.warn("urllib.parse.splitattr() is deprecated as of 3.8, "
1102+
"use urllib.parse.urlparse() instead",
1103+
DeprecationWarning, stacklevel=2)
1104+
return _splitattr(url)
1105+
1106+
1107+
def _splitattr(url):
10211108
"""splitattr('/path;attr1=value1;attr2=value2;...') ->
10221109
'/path', ['attr1=value1', 'attr2=value2', ...]."""
10231110
words = url.split(';')
10241111
return words[0], words[1:]
10251112

1113+
10261114
def splitvalue(attr):
1115+
warnings.warn("urllib.parse.splitvalue() is deprecated as of 3.8, "
1116+
"use urllib.parse.parse_qsl() instead",
1117+
DeprecationWarning, stacklevel=2)
1118+
return _splitvalue(attr)
1119+
1120+
1121+
def _splitvalue(attr):
10271122
"""splitvalue('attr=value') --> 'attr', 'value'."""
10281123
attr, delim, value = attr.partition('=')
10291124
return attr, (value if delim else None)

0 commit comments

Comments
 (0)