diff --git a/AUTHORS.rst b/AUTHORS.rst index 9a8224dc7d..2b21352c58 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -8,6 +8,7 @@ The following organizations or individuals have contributed to ScanCode: - Akanksha Garg @akugarg - Alex Blekhman @a-tinsmith - Alexander Gschrei @agschrei +- Anurag Tiwari @Anuragluck - Armijn Hemmel @armijnhemel - Armin Tänzer @armintaenzertng - Arnaud Jeansen @ajeans diff --git a/CHANGELOG.rst b/CHANGELOG.rst index abc3ae41ad..00a301857f 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -26,6 +26,11 @@ Next release ``ValueError: did not find any 'pre_scan' hooks``. https://github.com/aboutcode-org/scancode-toolkit/issues/5363 +- Fix the import typo in ``commoncode.fetch.ping_url``. It imported + ``urlib.request`` and fell back to ``urllib2``, so it always failed + on Python 3. + https://github.com/aboutcode-org/scancode-toolkit/issues/5423 + v33.0.0rc1 - 2026-05-14 ------------------------ diff --git a/src/commoncode/fetch.py b/src/commoncode/fetch.py index 7218b0159c..fecdd755ef 100644 --- a/src/commoncode/fetch.py +++ b/src/commoncode/fetch.py @@ -57,10 +57,7 @@ def ping_url(url): """ Return True is `url` is reachable. """ - try: - from urlib.request import urlopen - except ImportError: - from urllib2 import urlopen + from urllib.request import urlopen # FIXME: if there is no 200 HTTP status, then the ULR may not be reachable. try: diff --git a/tests/commoncode/test_fetch.py b/tests/commoncode/test_fetch.py new file mode 100644 index 0000000000..e3348ba228 --- /dev/null +++ b/tests/commoncode/test_fetch.py @@ -0,0 +1,23 @@ +# +# Copyright (c) nexB Inc. and others. All rights reserved. +# ScanCode is a trademark of nexB Inc. +# SPDX-License-Identifier: Apache-2.0 +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. +# See https://github.com/nexB/scancode-toolkit for support or download. +# See https://aboutcode.org for more information about nexB OSS projects. +# + +from unittest import mock + +from commoncode.fetch import ping_url + + +def test_ping_url_returns_true_when_url_is_reachable(): + with mock.patch("urllib.request.urlopen") as mock_urlopen: + assert ping_url("http://example.com") is True + mock_urlopen.assert_called_once_with("http://example.com") + + +def test_ping_url_returns_false_when_url_is_unreachable(): + with mock.patch("urllib.request.urlopen", side_effect=OSError): + assert ping_url("http://example.com") is False