diff --git a/test/test_datasets_utils.py b/test/test_datasets_utils.py index 02ae23102c4..7fceb5f25e6 100644 --- a/test/test_datasets_utils.py +++ b/test/test_datasets_utils.py @@ -62,6 +62,13 @@ def test_download_url_retry_http(self): warnings.warn(msg, RuntimeWarning) raise unittest.SkipTest(msg) + @unittest.skipIf(sys.version_info < (3,), "Python2 doesn't raise error") + def test_download_url_dont_exist(self): + with get_tmp_dir() as temp_dir: + url = "http://github.com/pytorch/vision/archive/this_doesnt_exist.zip" + with self.assertRaises(URLError): + utils.download_url(url, temp_dir) + def test_extract_zip(self): with get_tmp_dir() as temp_dir: with tempfile.NamedTemporaryFile(suffix='.zip') as f: diff --git a/torchvision/datasets/utils.py b/torchvision/datasets/utils.py index 3e3c674ede9..27315e6e912 100644 --- a/torchvision/datasets/utils.py +++ b/torchvision/datasets/utils.py @@ -82,7 +82,7 @@ def download_url(url, root, filename=None, md5=None): url, fpath, reporthook=gen_bar_updater() ) - except OSError: + except urllib.error.URLError as e: if url[:5] == 'https': url = url.replace('https:', 'http:') print('Failed download. Trying https -> http instead.' @@ -91,6 +91,8 @@ def download_url(url, root, filename=None, md5=None): url, fpath, reporthook=gen_bar_updater() ) + else: + raise e def list_dir(root, prefix=False):