summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2018-11-15 08:01:31 -0600
committerGitHub <noreply@github.com>2018-11-15 08:01:31 -0600
commitff8b0b21e0f17dbcc94fc87ba1194dcbbbe15432 (patch)
tree971b304cd89292c0209f399caaf8f650fd418180
parentfcfadfe84058498c96408f0be487c70be8bba65e (diff)
parent470d53fc6bc8267fec7d7cf5c7116d5e7437d789 (diff)
downloadnumpy-ff8b0b21e0f17dbcc94fc87ba1194dcbbbe15432.tar.gz
Merge pull request #12381 from tylerjereddy/datasource_del_handling
BUG: graceful DataSource __del__ when __init__ fails
-rw-r--r--numpy/lib/_datasource.py2
-rw-r--r--numpy/lib/tests/test__datasource.py15
2 files changed, 16 insertions, 1 deletions
diff --git a/numpy/lib/_datasource.py b/numpy/lib/_datasource.py
index e34c376e5..30237b76f 100644
--- a/numpy/lib/_datasource.py
+++ b/numpy/lib/_datasource.py
@@ -328,7 +328,7 @@ class DataSource(object):
def __del__(self):
# Remove temp directories
- if self._istmpdest:
+ if hasattr(self, '_istmpdest') and self._istmpdest:
shutil.rmtree(self._destpath)
def _iszip(self, filename):
diff --git a/numpy/lib/tests/test__datasource.py b/numpy/lib/tests/test__datasource.py
index 1df8bebf6..8eac16b58 100644
--- a/numpy/lib/tests/test__datasource.py
+++ b/numpy/lib/tests/test__datasource.py
@@ -361,3 +361,18 @@ class TestOpenFunc(object):
fp = datasource.open(local_file)
assert_(fp)
fp.close()
+
+def test_del_attr_handling():
+ # DataSource __del__ can be called
+ # even if __init__ fails when the
+ # Exception object is caught by the
+ # caller as happens in refguide_check
+ # is_deprecated() function
+
+ ds = datasource.DataSource()
+ # simulate failed __init__ by removing key attribute
+ # produced within __init__ and expected by __del__
+ del ds._istmpdest
+ # should not raise an AttributeError if __del__
+ # gracefully handles failed __init__:
+ ds.__del__()