Skip to content

Commit

Permalink
CLN EnvironmentError, IOError, WindowsError → OSError (scikit-learn#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitriPapadopoulos committed Jun 2, 2023
1 parent 2998f30 commit 4509e58
Show file tree
Hide file tree
Showing 17 changed files with 36 additions and 41 deletions.
2 changes: 1 addition & 1 deletion doc/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ size limit of Windows if Python is installed in a nested location such as the
Collecting scikit-learn
...
Installing collected packages: scikit-learn
ERROR: Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory: 'C:\\Users\\username\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python37\\site-packages\\sklearn\\datasets\\tests\\data\\openml\\292\\api-v1-json-data-list-data_name-australian-limit-2-data_version-1-status-deactivated.json.gz'
ERROR: Could not install packages due to an OSError: [Errno 2] No such file or directory: 'C:\\Users\\username\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python37\\site-packages\\sklearn\\datasets\\tests\\data\\openml\\292\\api-v1-json-data-list-data_name-australian-limit-2-data_version-1-status-deactivated.json.gz'

In this case it is possible to lift that limit in the Windows registry by
using the ``regedit`` tool:
Expand Down
7 changes: 7 additions & 0 deletions doc/whats_new/v1.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,13 @@ Changelog
accepts sparse metrics.
:pr:`19664` by :user:`Kaushik Amar Das <cozek>`.

Miscellaneous
.............

- |Enhancement| Replace obsolete exceptions EnvironmentError, IOError and
WindowsError.
:pr:`26466` by :user:`Dimitri Papadopoulos ORfanos <DimitriPapadopoulos>`.

Code and Documentation Contributors
-----------------------------------

Expand Down
2 changes: 1 addition & 1 deletion sklearn/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def wrapped(*args, **kwargs):
kwargs["download_if_missing"] = download_if_missing
try:
return f(*args, **kwargs)
except IOError as e:
except OSError as e:
if str(e) != "Data not found and `download_if_missing` is False":
raise
pytest.skip("test is enabled when SKLEARN_SKIP_NETWORK_TESTS=0")
Expand Down
2 changes: 1 addition & 1 deletion sklearn/datasets/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1377,7 +1377,7 @@ def _fetch_remote(remote, dirname=None):
urlretrieve(remote.url, file_path)
checksum = _sha256(file_path)
if remote.checksum != checksum:
raise IOError(
raise OSError(
"{} has an SHA256 checksum ({}) "
"differing from expected ({}), "
"file may be corrupted.".format(file_path, checksum, remote.checksum)
Expand Down
4 changes: 2 additions & 2 deletions sklearn/datasets/_california_housing.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def fetch_california_housing(
all scikit-learn data is stored in '~/scikit_learn_data' subfolders.
download_if_missing : bool, default=True
If False, raise a IOError if the data is not locally available
If False, raise an OSError if the data is not locally available
instead of trying to download the data from the source site.
return_X_y : bool, default=False
Expand Down Expand Up @@ -138,7 +138,7 @@ def fetch_california_housing(
filepath = _pkl_filepath(data_home, "cal_housing.pkz")
if not exists(filepath):
if not download_if_missing:
raise IOError("Data not found and `download_if_missing` is False")
raise OSError("Data not found and `download_if_missing` is False")

logger.info(
"Downloading Cal. housing from {} to {}".format(ARCHIVE.url, data_home)
Expand Down
4 changes: 2 additions & 2 deletions sklearn/datasets/_covtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def fetch_covtype(
all scikit-learn data is stored in '~/scikit_learn_data' subfolders.
download_if_missing : bool, default=True
If False, raise a IOError if the data is not locally available
If False, raise an OSError if the data is not locally available
instead of trying to download the data from the source site.
random_state : int, RandomState instance or None, default=None
Expand Down Expand Up @@ -184,7 +184,7 @@ def fetch_covtype(
os.rename(targets_tmp_path, targets_path)

elif not available and not download_if_missing:
raise IOError("Data not found and `download_if_missing` is False")
raise OSError("Data not found and `download_if_missing` is False")
try:
X, y
except NameError:
Expand Down
8 changes: 4 additions & 4 deletions sklearn/datasets/_kddcup99.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def fetch_kddcup99(
Whether to load only 10 percent of the data.
download_if_missing : bool, default=True
If False, raise a IOError if the data is not locally available
If False, raise an OSError if the data is not locally available
instead of trying to download the data from the source site.
return_X_y : bool, default=False
Expand Down Expand Up @@ -252,7 +252,7 @@ def _fetch_brute_kddcup99(data_home=None, download_if_missing=True, percent10=Tr
all scikit-learn data is stored in '~/scikit_learn_data' subfolders.
download_if_missing : bool, default=True
If False, raise a IOError if the data is not locally available
If False, raise an OSError if the data is not locally available
instead of trying to download the data from the source site.
percent10 : bool, default=True
Expand Down Expand Up @@ -345,7 +345,7 @@ def _fetch_brute_kddcup99(data_home=None, download_if_missing=True, percent10=Tr
X = joblib.load(samples_path)
y = joblib.load(targets_path)
except Exception as e:
raise IOError(
raise OSError(
"The cache for fetch_kddcup99 is invalid, please delete "
f"{str(kddcup_dir)} and run the fetch_kddcup99 again"
) from e
Expand Down Expand Up @@ -379,7 +379,7 @@ def _fetch_brute_kddcup99(data_home=None, download_if_missing=True, percent10=Tr
joblib.dump(X, samples_path, compress=0)
joblib.dump(y, targets_path, compress=0)
else:
raise IOError("Data not found and `download_if_missing` is False")
raise OSError("Data not found and `download_if_missing` is False")

return Bunch(
data=X,
Expand Down
8 changes: 4 additions & 4 deletions sklearn/datasets/_lfw.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def _check_fetch_lfw(data_home=None, funneled=True, download_if_missing=True):
logger.info("Downloading LFW metadata: %s", target.url)
_fetch_remote(target, dirname=lfw_home)
else:
raise IOError("%s is missing" % target_filepath)
raise OSError("%s is missing" % target_filepath)

if funneled:
data_folder_path = join(lfw_home, "lfw_funneled")
Expand All @@ -104,7 +104,7 @@ def _check_fetch_lfw(data_home=None, funneled=True, download_if_missing=True):
logger.info("Downloading LFW data (~200MB): %s", archive.url)
_fetch_remote(archive, dirname=lfw_home)
else:
raise IOError("%s is missing" % archive_path)
raise OSError("%s is missing" % archive_path)

import tarfile

Expand Down Expand Up @@ -297,7 +297,7 @@ def fetch_lfw_people(
correlation from the background.
download_if_missing : bool, default=True
If False, raise a IOError if the data is not locally available
If False, raise an OSError if the data is not locally available
instead of trying to download the data from the source site.
return_X_y : bool, default=False
Expand Down Expand Up @@ -500,7 +500,7 @@ def fetch_lfw_pairs(
correlation from the background.
download_if_missing : bool, default=True
If False, raise a IOError if the data is not locally available
If False, raise an OSError if the data is not locally available
instead of trying to download the data from the source site.
Returns
Expand Down
4 changes: 2 additions & 2 deletions sklearn/datasets/_olivetti_faces.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def fetch_olivetti_faces(
See :term:`Glossary <random_state>`.
download_if_missing : bool, default=True
If False, raise a IOError if the data is not locally available
If False, raise an OSError if the data is not locally available
instead of trying to download the data from the source site.
return_X_y : bool, default=False
Expand Down Expand Up @@ -121,7 +121,7 @@ def fetch_olivetti_faces(
filepath = _pkl_filepath(data_home, "olivetti.pkz")
if not exists(filepath):
if not download_if_missing:
raise IOError("Data not found and `download_if_missing` is False")
raise OSError("Data not found and `download_if_missing` is False")

print("downloading Olivetti faces from %s to %s" % (FACES.url, data_home))
mat_path = _fetch_remote(FACES, dirname=data_home)
Expand Down
2 changes: 1 addition & 1 deletion sklearn/datasets/_rcv1.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def fetch_rcv1(
This follows the official LYRL2004 chronological split.
download_if_missing : bool, default=True
If False, raise a IOError if the data is not locally available
If False, raise an OSError if the data is not locally available
instead of trying to download the data from the source site.
random_state : int, RandomState instance or None, default=None
Expand Down
4 changes: 2 additions & 2 deletions sklearn/datasets/_species_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def fetch_species_distributions(*, data_home=None, download_if_missing=True):
all scikit-learn data is stored in '~/scikit_learn_data' subfolders.
download_if_missing : bool, default=True
If False, raise a IOError if the data is not locally available
If False, raise an OSError if the data is not locally available
instead of trying to download the data from the source site.
Returns
Expand Down Expand Up @@ -228,7 +228,7 @@ def fetch_species_distributions(*, data_home=None, download_if_missing=True):

if not exists(archive_path):
if not download_if_missing:
raise IOError("Data not found and `download_if_missing` is False")
raise OSError("Data not found and `download_if_missing` is False")
logger.info("Downloading species data from %s to %s" % (SAMPLES.url, data_home))
samples_path = _fetch_remote(SAMPLES, dirname=data_home)
with np.load(samples_path) as X: # samples.zip is a valid npz
Expand Down
6 changes: 3 additions & 3 deletions sklearn/datasets/_twenty_newsgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def fetch_20newsgroups(
correct.
download_if_missing : bool, default=True
If False, raise an IOError if the data is not locally available
If False, raise an OSError if the data is not locally available
instead of trying to download the data from the source site.
return_X_y : bool, default=False
Expand Down Expand Up @@ -283,7 +283,7 @@ def fetch_20newsgroups(
target_dir=twenty_home, cache_path=cache_path
)
else:
raise IOError("20Newsgroups dataset not found")
raise OSError("20Newsgroups dataset not found")

if subset in ("train", "test"):
data = cache[subset]
Expand Down Expand Up @@ -413,7 +413,7 @@ def fetch_20newsgroups_vectorized(
all scikit-learn data is stored in '~/scikit_learn_data' subfolders.
download_if_missing : bool, default=True
If False, raise an IOError if the data is not locally available
If False, raise an OSError if the data is not locally available
instead of trying to download the data from the source site.
return_X_y : bool, default=False
Expand Down
2 changes: 1 addition & 1 deletion sklearn/datasets/tests/test_kddcup99.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,5 @@ def test_corrupted_file_error_message(fetch_kddcup99_fxt, tmp_path):
f"delete {str(kddcup99_dir)} and run the fetch_kddcup99 again"
)

with pytest.raises(IOError, match=msg):
with pytest.raises(OSError, match=msg):
fetch_kddcup99_fxt(data_home=str(tmp_path))
4 changes: 2 additions & 2 deletions sklearn/datasets/tests/test_lfw.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def teardown_module():


def test_load_empty_lfw_people():
with pytest.raises(IOError):
with pytest.raises(OSError):
fetch_lfw_people(data_home=SCIKIT_LEARN_EMPTY_DATA, download_if_missing=False)


Expand Down Expand Up @@ -181,7 +181,7 @@ def test_load_fake_lfw_people_too_restrictive():


def test_load_empty_lfw_pairs():
with pytest.raises(IOError):
with pytest.raises(OSError):
fetch_lfw_pairs(data_home=SCIKIT_LEARN_EMPTY_DATA, download_if_missing=False)


Expand Down
2 changes: 1 addition & 1 deletion sklearn/datasets/tests/test_svmlight_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def test_not_a_filename():


def test_invalid_filename():
with pytest.raises(IOError):
with pytest.raises(OSError):
load_svmlight_file("trou pic nic douille")


Expand Down
8 changes: 1 addition & 7 deletions sklearn/model_selection/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,6 @@
from sklearn.model_selection import GridSearchCV


try:
WindowsError # type: ignore
except NameError:
WindowsError = None


class MockImprovingEstimator(BaseEstimator):
"""Dummy classifier to test the learning curve"""

Expand Down Expand Up @@ -2064,7 +2058,7 @@ def test_score_memmap():
try:
os.unlink(tf.name)
break
except WindowsError:
except OSError:
sleep(1.0)


Expand Down
8 changes: 1 addition & 7 deletions sklearn/utils/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@
import unittest
from unittest import TestCase

# WindowsError only exist on Windows
try:
WindowsError # type: ignore
except NameError:
WindowsError = None

from numpy.testing import assert_allclose as np_assert_allclose
from numpy.testing import assert_almost_equal
from numpy.testing import assert_approx_equal
Expand Down Expand Up @@ -453,7 +447,7 @@ def _delete_folder(folder_path, warn=False):
# This can fail under windows,
# but will succeed when called by atexit
shutil.rmtree(folder_path)
except WindowsError:
except OSError:
if warn:
warnings.warn("Could not delete temporary folder %s" % folder_path)

Expand Down

0 comments on commit 4509e58

Please sign in to comment.