@@ -132,6 +132,27 @@ def _coerce_args(*args):
132132 return args + (_noop ,)
133133 return _decode_args (args ) + (_encode_result ,)
134134
135+ def _parse_hostinfo (netloc ):
136+ _ , _ , hostinfo = netloc .rpartition ('@' )
137+ _ , have_open_br , bracketed = hostinfo .partition ('[' )
138+ if have_open_br :
139+ hostname , _ , port = bracketed .partition (']' )
140+ _ , _ , port = port .partition (':' )
141+ else :
142+ hostname , _ , port = hostinfo .partition (':' )
143+ if not port :
144+ port = None
145+ return hostname , port
146+
147+ def _validate_port (port ):
148+ if port .isdigit () and port .isascii ():
149+ port = int (port )
150+ else :
151+ raise ValueError (f"Port could not be cast to integer value as { port !r} " )
152+ if not (0 <= port <= 65535 ):
153+ raise ValueError ("Port out of range 0-65535" )
154+ return port
155+
135156# Result objects are more helpful than simple tuples
136157class _ResultMixinStr (object ):
137158 """Standard approach to encoding parsed results from str to bytes"""
@@ -176,12 +197,7 @@ def hostname(self):
176197 def port (self ):
177198 port = self ._hostinfo [1 ]
178199 if port is not None :
179- if port .isdigit () and port .isascii ():
180- port = int (port )
181- else :
182- raise ValueError (f"Port could not be cast to integer value as { port !r} " )
183- if not (0 <= port <= 65535 ):
184- raise ValueError ("Port out of range 0-65535" )
200+ port = _validate_port (port )
185201 return port
186202
187203 __class_getitem__ = classmethod (types .GenericAlias )
@@ -205,16 +221,9 @@ def _userinfo(self):
205221 @property
206222 def _hostinfo (self ):
207223 netloc = self .netloc
208- _ , _ , hostinfo = netloc .rpartition ('@' )
209- _ , have_open_br , bracketed = hostinfo .partition ('[' )
210- if have_open_br :
211- hostname , _ , port = bracketed .partition (']' )
212- _ , _ , port = port .partition (':' )
213- else :
214- hostname , _ , port = hostinfo .partition (':' )
215- if not port :
216- port = None
217- return hostname , port
224+ if netloc is None :
225+ return None , None
226+ return _parse_hostinfo (netloc )
218227
219228
220229class _NetlocResultMixinBytes (_NetlocResultMixinBase , _ResultMixinBytes ):
@@ -423,9 +432,7 @@ def _splitnetloc(url, start=0):
423432 delim = min (delim , wdelim ) # use earliest delim position
424433 return url [start :delim ], url [delim :] # return (domain, rest)
425434
426- def _checknetloc (netloc ):
427- if not netloc or netloc .isascii ():
428- return
435+ def _checknetloc_nfkc (netloc ):
429436 # looking for characters like \u2100 that expand to 'a/c'
430437 # IDNA uses NFKC equivalence, so normalize for this check
431438 import unicodedata
@@ -441,6 +448,16 @@ def _checknetloc(netloc):
441448 raise ValueError ("netloc '" + netloc + "' contains invalid " +
442449 "characters under NFKC normalization" )
443450
451+ def _checknetloc (netloc ):
452+ if not netloc :
453+ return
454+ if not netloc .isascii ():
455+ _checknetloc_nfkc (netloc )
456+
457+ _ , port = _parse_hostinfo (netloc )
458+ if port is not None :
459+ _validate_port (port )
460+
444461def _check_bracketed_netloc (netloc ):
445462 # Note that this function must mirror the splitting
446463 # done in NetlocResultMixins._hostinfo().
0 commit comments