From 2ca9ca7bdbef4db8292c73a43efa5c34623da15f Mon Sep 17 00:00:00 2001 From: Ben Kallus Date: Tue, 31 Jan 2023 17:10:09 -0500 Subject: [PATCH 1/2] Fix non-numeric port number bug --- src/hyperlink/_url.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/hyperlink/_url.py b/src/hyperlink/_url.py index 8797b5c..f55596d 100644 --- a/src/hyperlink/_url.py +++ b/src/hyperlink/_url.py @@ -501,9 +501,9 @@ def register_scheme( """ text = text.lower() if default_port is not None: - try: + if isinstance(default_port, int) or (isinstance(default_port, str) and default_port.isdigit() and default_port.isascii()): default_port = int(default_port) - except (ValueError, TypeError): + else: raise ValueError( "default_port expected integer or None, not %r" % (default_port,) @@ -1407,9 +1407,9 @@ def from_text(cls, text): host = au_gs["ipv6_host"] or au_gs["plain_host"] port = au_gs["port"] if port is not None: - try: + if port.isdigit() and port.isascii(): port = int(port) # type: ignore[assignment] # FIXME, see below - except ValueError: + else: if not port: # TODO: excessive? raise URLParseError("port must not be empty: %r" % au_text) raise URLParseError("expected integer for port, not %r" % port) From edff5c0dca34bb838b067cafa91cd889eefd12c5 Mon Sep 17 00:00:00 2001 From: Ben Kallus Date: Thu, 9 Feb 2023 23:08:28 -0500 Subject: [PATCH 2/2] remove isdigit and isascii calls to improve compatibility with old python. changed an error message. fixed negative default port bug --- src/hyperlink/_url.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/hyperlink/_url.py b/src/hyperlink/_url.py index f55596d..d3df645 100644 --- a/src/hyperlink/_url.py +++ b/src/hyperlink/_url.py @@ -478,7 +478,7 @@ def _encode_userinfo_part(text, maximal=True): def register_scheme( text, uses_netloc=True, default_port=None, query_plus_is_space=True ): - # type: (Text, bool, Optional[int], bool) -> None + # type: (Text, bool, Optional[Union[int, str]], bool) -> None """Registers new scheme information, resulting in correct port and slash behavior from the URL object. There are dozens of standard schemes preregistered, so this function is mostly meant for @@ -501,11 +501,12 @@ def register_scheme( """ text = text.lower() if default_port is not None: - if isinstance(default_port, int) or (isinstance(default_port, str) and default_port.isdigit() and default_port.isascii()): + default_port = str(default_port) + if all(d in "0123456789" for d in default_port): default_port = int(default_port) else: raise ValueError( - "default_port expected integer or None, not %r" + "default_port expected nonnegative integer, numeric string, or None, not %r" % (default_port,) ) @@ -1407,7 +1408,7 @@ def from_text(cls, text): host = au_gs["ipv6_host"] or au_gs["plain_host"] port = au_gs["port"] if port is not None: - if port.isdigit() and port.isascii(): + if all(d in "0123456789" for d in default_port) port = int(port) # type: ignore[assignment] # FIXME, see below else: if not port: # TODO: excessive?