@@ -132,27 +132,6 @@ 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-
156135# Result objects are more helpful than simple tuples
157136class _ResultMixinStr (object ):
158137 """Standard approach to encoding parsed results from str to bytes"""
@@ -197,7 +176,12 @@ def hostname(self):
197176 def port (self ):
198177 port = self ._hostinfo [1 ]
199178 if port is not None :
200- port = _validate_port (port )
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" )
201185 return port
202186
203187 __class_getitem__ = classmethod (types .GenericAlias )
@@ -221,9 +205,16 @@ def _userinfo(self):
221205 @property
222206 def _hostinfo (self ):
223207 netloc = self .netloc
224- if netloc is None :
225- return None , None
226- return _parse_hostinfo (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
227218
228219
229220class _NetlocResultMixinBytes (_NetlocResultMixinBase , _ResultMixinBytes ):
@@ -432,7 +423,9 @@ def _splitnetloc(url, start=0):
432423 delim = min (delim , wdelim ) # use earliest delim position
433424 return url [start :delim ], url [delim :] # return (domain, rest)
434425
435- def _checknetloc_nfkc (netloc ):
426+ def _checknetloc (netloc ):
427+ if not netloc or netloc .isascii ():
428+ return
436429 # looking for characters like \u2100 that expand to 'a/c'
437430 # IDNA uses NFKC equivalence, so normalize for this check
438431 import unicodedata
@@ -448,16 +441,6 @@ def _checknetloc_nfkc(netloc):
448441 raise ValueError ("netloc '" + netloc + "' contains invalid " +
449442 "characters under NFKC normalization" )
450443
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-
461444def _check_bracketed_netloc (netloc ):
462445 # Note that this function must mirror the splitting
463446 # done in NetlocResultMixins._hostinfo().
0 commit comments