From a51634745974fd682d491f669578658695d7d9bb Mon Sep 17 00:00:00 2001 From: Dan Nicholson Date: Thu, 15 Jun 2023 11:37:22 -0600 Subject: [PATCH] Handle urlparse ValueError on invalid IP addresses in python >= 3.11 See https://github.com/python/cpython/issues/103848. --- kolibri/core/discovery/utils/network/urls.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/kolibri/core/discovery/utils/network/urls.py b/kolibri/core/discovery/utils/network/urls.py index cc80a05ddf5..dc1cf071a76 100644 --- a/kolibri/core/discovery/utils/network/urls.py +++ b/kolibri/core/discovery/utils/network/urls.py @@ -1,5 +1,6 @@ import re +from six import raise_from from six.moves.urllib.parse import urlparse from . import errors @@ -117,8 +118,12 @@ def parse_address_into_components(address): # noqa C901 if "://" not in address: address = "http://" + address - # parse out the URL into its components - parsed = urlparse(address) + # parse out the URL into its components. On python >= 3.11, this + # will throw a ValueError on invalid IP addresses. + try: + parsed = urlparse(address) + except ValueError as e: + raise_from(errors.InvalidHostname(address), e) p_scheme = parsed.scheme p_hostname = parsed.hostname p_path = parsed.path.rstrip("/") + "/"