/
tst_refcount.py
28 lines (22 loc) · 978 Bytes
/
tst_refcount.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import unittest, netCDF4, tempfile, os
file_name = tempfile.NamedTemporaryFile(suffix='.nc', delete=False).name
class RefCountTestCase(unittest.TestCase):
def setUp(self):
nc = netCDF4.Dataset(file_name, mode='w', keepweakref=True, format='NETCDF4')
d = nc.createDimension('fred', 2000)
v = nc.createVariable('frank','f',('fred',))
self.file = file_name
self.nc = nc
def tearDown(self):
# Remove the temporary files
os.remove(self.file)
def runTest(self):
"""testing garbage collection (issue 218)"""
# this should trigger garbage collection (__dealloc__ method)
del self.nc
# if __dealloc__ not called to close file, then this
# will fail with "Permission denied" error (since you can't
# open a file 'w' that is already open for writing).
nc = netCDF4.Dataset(self.file, mode='w', format='NETCDF4')
if __name__ == '__main__':
unittest.main()