Skip to content

Commit ce8d7f9

Browse files
committed
fix nullcontext on 3.6
1 parent 033a3b8 commit ce8d7f9

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/datasets/builder.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
has_sufficient_disk_space,
6666
map_nested,
6767
memoize,
68+
nullcontext,
6869
size_str,
6970
temporary_assignment,
7071
)
@@ -341,7 +342,7 @@ def __init__(
341342
if is_local:
342343
os.makedirs(self._cache_dir_root, exist_ok=True)
343344
lock_path = os.path.join(self._cache_dir_root, self._cache_dir.replace(os.sep, "_") + ".lock")
344-
with FileLock(lock_path) if is_local else contextlib.nullcontext():
345+
with FileLock(lock_path) if is_local else nullcontext():
345346
if self._fs.exists(self._cache_dir): # check if data exist
346347
if len(self._fs.listdir(self._cache_dir)) > 0:
347348
logger.info("Overwrite dataset info from restored data version.")
@@ -646,7 +647,7 @@ def download_and_prepare(
646647
if is_local:
647648
lock_path = os.path.join(self._cache_dir_root, self._cache_dir.replace(os.sep, "_") + ".lock")
648649
# File locking only with local paths; no file locking on GCS or S3
649-
with FileLock(lock_path) if is_local else contextlib.nullcontext():
650+
with FileLock(lock_path) if is_local else nullcontext():
650651
data_exists = self._fs.exists(self._cache_dir)
651652
if data_exists and download_mode == DownloadMode.REUSE_DATASET_IF_EXISTS:
652653
logger.warning(f"Found cached dataset {self.name} ({self._cache_dir})")
@@ -855,14 +856,14 @@ def _save_info(self):
855856
is_local = not is_remote_filesystem(self._fs)
856857
if is_local:
857858
lock_path = os.path.join(self._cache_dir_root, self._cache_dir.replace(os.sep, "_") + ".lock")
858-
with FileLock(lock_path) if is_local else contextlib.nullcontext():
859+
with FileLock(lock_path) if is_local else nullcontext():
859860
self.info.write_to_directory(self._cache_dir, fs=self._fs)
860861

861862
def _save_infos(self):
862863
is_local = not is_remote_filesystem(self._fs)
863864
if is_local:
864865
lock_path = os.path.join(self._cache_dir_root, self._cache_dir.replace(os.sep, "_") + ".lock")
865-
with FileLock(lock_path) if is_local else contextlib.nullcontext():
866+
with FileLock(lock_path) if is_local else nullcontext():
866867
DatasetInfosDict(**{self.config.name: self.info}).write_to_directory(self.get_imported_module_dir())
867868

868869
def _make_split_generators_kwargs(self, prepare_split_kwargs):

src/datasets/utils/py_utils.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
1818
"""
1919

20-
import contextlib
2120
import functools
2221
import itertools
2322
import os
@@ -151,7 +150,19 @@ def string_to_dict(string: str, pattern: str) -> Dict[str, str]:
151150
return _dict
152151

153152

154-
@contextlib.contextmanager
153+
@contextmanager
154+
def nullcontext():
155+
"""Context manager that does no additional processing.
156+
Used as a stand-in for a normal context manager, when a particular
157+
block of code is only sometimes used with a normal context manager:
158+
cm = optional_cm if condition else nullcontext()
159+
with cm:
160+
# Perform operation, using optional_cm if condition is True
161+
"""
162+
yield
163+
164+
165+
@contextmanager
155166
def temporary_assignment(obj, attr, value):
156167
"""Temporarily assign obj.attr to value."""
157168
original = getattr(obj, attr, None)
@@ -540,7 +551,7 @@ def dump(obj, file):
540551
return
541552

542553

543-
@contextlib.contextmanager
554+
@contextmanager
544555
def _no_cache_fields(obj):
545556
try:
546557
if (

0 commit comments

Comments
 (0)