diff --git a/test/common_utils.py b/test/common_utils.py index 9a4b41e606f..9acbd9f9ab8 100644 --- a/test/common_utils.py +++ b/test/common_utils.py @@ -34,14 +34,12 @@ @contextlib.contextmanager def get_tmp_dir(src=None, **kwargs): - tmp_dir = tempfile.mkdtemp(**kwargs) - if src is not None: - os.rmdir(tmp_dir) - shutil.copytree(src, tmp_dir) - try: + with tempfile.TemporaryDirectory( + **kwargs, + ) as tmp_dir: + if src is not None: + shutil.copytree(src, tmp_dir) yield tmp_dir - finally: - shutil.rmtree(tmp_dir) def set_rng_seed(seed): diff --git a/test/test_datasets.py b/test/test_datasets.py index feaabd7acd2..84f243388aa 100644 --- a/test/test_datasets.py +++ b/test/test_datasets.py @@ -11,7 +11,6 @@ import re import shutil import string -import sys import unittest import xml.etree.ElementTree as ET import zipfile @@ -1904,7 +1903,6 @@ def test_class_to_idx(self): assert dataset.class_to_idx == class_to_idx -@pytest.mark.skipif(sys.platform in ("win32", "cygwin"), reason="temporarily disabled on Windows") class INaturalistTestCase(datasets_utils.ImageDatasetTestCase): DATASET_CLASS = datasets.INaturalist FEATURE_TYPES = (PIL.Image.Image, (int, tuple)) diff --git a/torchvision/datasets/inaturalist.py b/torchvision/datasets/inaturalist.py index 8713bc041db..973f90fc32b 100644 --- a/torchvision/datasets/inaturalist.py +++ b/torchvision/datasets/inaturalist.py @@ -113,7 +113,7 @@ def __init__( for fname in files: self.index.append((dir_index, fname)) - self.loader = loader or Image.open + self.loader = loader def _init_2021(self) -> None: """Initialize based on 2021 layout""" @@ -184,7 +184,8 @@ def __getitem__(self, index: int) -> Tuple[Any, Any]: """ cat_id, fname = self.index[index] - img = self.loader(os.path.join(self.root, self.all_categories[cat_id], fname)) + image_path = os.path.join(self.root, self.all_categories[cat_id], fname) + img = self.loader(image_path) if self.loader is not None else Image.open(image_path) target: Any = [] for t in self.target_type: