Skip to content

Commit b83e4cf

Browse files
Move check to _coerce_args()
This extends the solution to urldefrag(), urlunparse(), urlunsplit(), and parse_qsl()
1 parent cbc95c6 commit b83e4cf

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

Lib/test/test_urlparse.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,22 @@ def test_forbidden_types(self):
872872
TypeError,
873873
"Expected a string or bytes object: got <class 'NoneType'>"):
874874
urllib.parse.urlsplit('http://www.python.org', None)
875+
with self.assertRaisesRegex(
876+
TypeError,
877+
"Expected a string or bytes object: got <class 'tuple'>"):
878+
urllib.parse.urldefrag(())
879+
with self.assertRaisesRegex(
880+
TypeError,
881+
"Expected a string or bytes object: got <class 'NoneType'>"):
882+
urllib.parse.urlunparse([None, './Python','x-newscheme://foo.com/stuff','x://y','x:/y','x:/','/',])
883+
with self.assertRaisesRegex(
884+
TypeError,
885+
"Expected a string or bytes object: got <class 'int'>"):
886+
urllib.parse.urlunsplit(['http', 0, '', '', ''])
887+
with self.assertRaisesRegex(
888+
TypeError,
889+
"Expected a string or bytes object: got <class 'NoneType'>"):
890+
urllib.parse.parse_qsl(None, encoding='latin-1')
875891

876892
def _check_result_type(self, str_type):
877893
num_args = len(str_type._fields)

Lib/urllib/parse.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ def _coerce_args(*args):
113113
# an appropriate result coercion function
114114
# - noop for str inputs
115115
# - encoding function otherwise
116+
for arg in args:
117+
if not isinstance(arg, (str, bytes, bytearray)):
118+
raise TypeError(f'Expected a string or bytes object: got {type(arg)}')
116119
str_input = isinstance(args[0], str)
117120
for arg in args[1:]:
118121
# We special-case the empty string to support the
@@ -383,10 +386,6 @@ def urlparse(url, scheme='', allow_fragments=True):
383386
384387
Note that % escapes are not expanded.
385388
"""
386-
if not isinstance(url, (str, bytes, bytearray)):
387-
raise TypeError(f'Expected a string or bytes object: got {type(url)}')
388-
if not isinstance(scheme, (str, bytes, bytearray)):
389-
raise TypeError(f'Expected a string or bytes object: got {type(scheme)}')
390389
url, scheme, _coerce_result = _coerce_args(url, scheme)
391390
splitresult = urlsplit(url, scheme, allow_fragments)
392391
scheme, netloc, url, query, fragment = splitresult
@@ -455,10 +454,6 @@ def urlsplit(url, scheme='', allow_fragments=True):
455454
456455
Note that % escapes are not expanded.
457456
"""
458-
if not isinstance(url, (str, bytes, bytearray)):
459-
raise TypeError(f'Expected a string or bytes object: got {type(url)}')
460-
if not isinstance(scheme, (str, bytes, bytearray)):
461-
raise TypeError(f'Expected a string or bytes object: got {type(scheme)}')
462457

463458
url, scheme, _coerce_result = _coerce_args(url, scheme)
464459

@@ -522,17 +517,12 @@ def urlunsplit(components):
522517
def urljoin(base, url, allow_fragments=True):
523518
"""Join a base URL and a possibly relative URL to form an absolute
524519
interpretation of the latter."""
525-
if not isinstance(base, (str, bytes, bytearray)):
526-
raise TypeError(f'Expected a string or bytes object: got {type(base)}')
527-
if not isinstance(url, (str, bytes, bytearray)):
528-
raise TypeError(f'Expected a string or bytes object: got {type(url)}')
529-
520+
base, url, _coerce_result = _coerce_args(base, url)
530521
if not base:
531-
return url
522+
return _coerce_result(url)
532523
if not url:
533-
return base
524+
return _coerce_result(base)
534525

535-
base, url, _coerce_result = _coerce_args(base, url)
536526
bscheme, bnetloc, bpath, bparams, bquery, bfragment = \
537527
urlparse(base, '', allow_fragments)
538528
scheme, netloc, path, params, query, fragment = \

0 commit comments

Comments
 (0)