Skip to content

Commit 7738222

Browse files
committed
add auto-close destructor and test
1 parent b885868 commit 7738222

File tree

5 files changed

+53
-3
lines changed

5 files changed

+53
-3
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.3.0
1+
1.3.1

src/interface.f90

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ module nc4fortran
6464
nc_read_scalar, nc_read_1d, nc_read_2d, nc_read_3d, nc_read_4d, nc_read_5d, nc_read_6d, nc_read_7d, &
6565
def_dims
6666

67+
!> flush file to disk and close file if user forgets to do so.
68+
final :: destructor
69+
6770
end type netcdf_file
6871

6972
!> Submodules
@@ -539,6 +542,18 @@ subroutine nc_initialize(self,filename,ierr, status,action,comp_lvl,verbose,debu
539542
end subroutine nc_initialize
540543

541544

545+
subroutine destructor(self)
546+
!! Close file and handle if user forgets to do so
547+
548+
type(netcdf_file), intent(inout) :: self
549+
550+
print *, "auto-closing " // self%filename
551+
552+
call self%close()
553+
554+
end subroutine destructor
555+
556+
542557
subroutine nc_finalize(self, ierr)
543558
class(netcdf_file), intent(inout) :: self
544559
integer, intent(out), optional :: ierr

src/tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ set_tests_properties(nc4fortran:minimal PROPERTIES
77
FIXTURES_SETUP nclib
88
LABELS core)
99

10-
foreach(t array attributes deflate error exist scalar shape string)
10+
foreach(t array attributes deflate destructor error exist scalar shape string)
1111
add_executable(test_${t} test_${t}.f90)
1212
target_link_libraries(test_${t} PRIVATE nc4fortran::nc4fortran)
1313
set_target_properties(test_${t} PROPERTIES LABELS unit)

src/tests/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ test('minimal', test_minimal,
66
priority: 100,
77
timeout: 10)
88

9-
foreach t : ['array', 'attributes', 'deflate', 'error', 'exist', 'scalar', 'shape', 'string']
9+
foreach t : ['array', 'attributes', 'deflate', 'destructor', 'error', 'exist', 'scalar', 'shape', 'string']
1010

1111
e = executable('test_'+t, 'test_' + t + '.f90', dependencies: netcdf_interface, fortran_args: quiet)
1212
if t == 'shape'

src/tests/test_destructor.f90

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
program test_destruct
2+
!! test netcdf_file destructor, that should auto-flush and close file
3+
!! if user forgets to %close() file
4+
5+
use, intrinsic :: iso_fortran_env, only : stderr=>error_unit
6+
use nc4fortran, only: netcdf_file
7+
implicit none (type, external)
8+
9+
call test_destructor()
10+
print *, 'OK: destructor'
11+
12+
contains
13+
14+
15+
subroutine test_destructor()
16+
type(netcdf_file) :: h
17+
character(*), parameter :: fn = 'destructor.nc'
18+
integer :: i
19+
20+
block
21+
type(netcdf_file) :: h
22+
!! we use block to test destructor is invoked
23+
call h%open(fn, action="write", status="replace")
24+
call h%write('x', 42)
25+
end block
26+
27+
if(h%is_open) error stop "destructor did not close " // fn
28+
29+
call h%open(fn, action="read", status="old")
30+
call h%read("x", i)
31+
if(i/=42) error stop "destructor did not flush " // fn
32+
33+
end subroutine test_destructor
34+
35+
end program

0 commit comments

Comments
 (0)