@@ -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):
522517def 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