Make SPK class close mmaps when exiting context manager (#25)
* Properly close mmaps; respect with context manager * Update test to use with context manager * Remove try/except in SPK.close() * Restore SPK mmap attributes to None on close()
This commit is contained in:
parent
c1b4c1882d
commit
79151ac7a4
|
@ -48,7 +48,9 @@ class SPK(object):
|
|||
self.daf.file.close()
|
||||
for segment in self.segments:
|
||||
if hasattr(segment, '_data'):
|
||||
del segment._data # TODO: explicitly close each memory map
|
||||
del segment._data
|
||||
self.daf._array = None
|
||||
self.daf._map = None
|
||||
|
||||
def __str__(self):
|
||||
daf = self.daf
|
||||
|
@ -65,6 +67,12 @@ class SPK(object):
|
|||
"""Return the file comments, as a string."""
|
||||
return self.daf.comments()
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||
self.close()
|
||||
|
||||
|
||||
class Segment(object):
|
||||
"""A single segment of an SPK file.
|
||||
|
|
|
@ -365,13 +365,12 @@ class LegacyTests(_CommonTests, TestCase):
|
|||
class NAIF_DAF_Tests(TestCase):
|
||||
|
||||
def test_single_position(self):
|
||||
kernel = SPK(NAIF_DAF(open('de405.bsp', 'rb')))
|
||||
x, y, z = kernel[0,4].compute(2457061.5)
|
||||
# Expect rough agreement with a DE430 position from our README:
|
||||
self.assertAlmostEqual(x, 2.05700211e+08, delta=2.0)
|
||||
self.assertAlmostEqual(y, 4.25141646e+07, delta=2.0)
|
||||
self.assertAlmostEqual(z, 1.39379183e+07, delta=2.0)
|
||||
kernel.close()
|
||||
with SPK(NAIF_DAF(open('de405.bsp', 'rb'))) as kernel:
|
||||
x, y, z = kernel[0,4].compute(2457061.5)
|
||||
# Expect rough agreement with a DE430 position from our README:
|
||||
self.assertAlmostEqual(x, 2.05700211e+08, delta=2.0)
|
||||
self.assertAlmostEqual(y, 4.25141646e+07, delta=2.0)
|
||||
self.assertAlmostEqual(z, 1.39379183e+07, delta=2.0)
|
||||
|
||||
|
||||
class CommandLineTests(TestCase):
|
||||
|
|
Loading…
Reference in New Issue