summaryrefslogtreecommitdiff
path: root/numpy/compat
diff options
context:
space:
mode:
authorEric Wieser <wieser.eric@gmail.com>2018-02-08 21:40:21 -0800
committerEric Wieser <wieser.eric@gmail.com>2018-09-23 09:39:53 -0700
commit8805080acf9e87a10881443c32b57d69fe44aec4 (patch)
tree3b24a4ef155bea93b1a1483d23460cd278dc490b /numpy/compat
parent17a6221cb20e1f07b672d03bf69e3b526a11a03c (diff)
downloadnumpy-8805080acf9e87a10881443c32b57d69fe44aec4.tar.gz
MAINT: Close the file if any unexpected errors occur within memmap
Diffstat (limited to 'numpy/compat')
-rw-r--r--numpy/compat/py3k.py25
1 files changed, 24 insertions, 1 deletions
diff --git a/numpy/compat/py3k.py b/numpy/compat/py3k.py
index d5bb2e4c7..ce4543bc3 100644
--- a/numpy/compat/py3k.py
+++ b/numpy/compat/py3k.py
@@ -7,7 +7,8 @@ from __future__ import division, absolute_import, print_function
__all__ = ['bytes', 'asbytes', 'isfileobj', 'getexception', 'strchar',
'unicode', 'asunicode', 'asbytes_nested', 'asunicode_nested',
'asstr', 'open_latin1', 'long', 'basestring', 'sixu',
- 'integer_types', 'is_pathlib_path', 'npy_load_module', 'Path']
+ 'integer_types', 'is_pathlib_path', 'npy_load_module', 'Path',
+ 'contextlib_nullcontext']
import sys
try:
@@ -97,6 +98,28 @@ def is_pathlib_path(obj):
"""
return Path is not None and isinstance(obj, Path)
+# from Python 3.7
+class contextlib_nullcontext(object):
+ """Context manager that does no additional processing.
+
+ Used as a stand-in for a normal context manager, when a particular
+ block of code is only sometimes used with a normal context manager:
+
+ cm = optional_cm if condition else nullcontext()
+ with cm:
+ # Perform operation, using optional_cm if condition is True
+ """
+
+ def __init__(self, enter_result=None):
+ self.enter_result = enter_result
+
+ def __enter__(self):
+ return self.enter_result
+
+ def __exit__(self, *excinfo):
+ pass
+
+
if sys.version_info[0] >= 3 and sys.version_info[1] >= 4:
def npy_load_module(name, fn, info=None):
"""