Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------------------

Expand Down
5 changes: 1 addition & 4 deletions src/commoncode/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
23 changes: 23 additions & 0 deletions tests/commoncode/test_fetch.py
Original file line number Diff line number Diff line change
@@ -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
Loading