@@ -83,22 +83,37 @@ def make_fid(name, mode, userblock_size, fapl, fcpl=None):
83
83
fid = h5f .create (name , h5f .ACC_EXCL , fapl = fapl , fcpl = fcpl )
84
84
elif mode == 'w' :
85
85
fid = h5f .create (name , h5f .ACC_TRUNC , fapl = fapl , fcpl = fcpl )
86
- elif mode == 'a' or mode is None :
86
+ elif mode == 'a' :
87
+ # Open in append mode (read/write).
88
+ # If that fails, create a new file only if it won't clobber an
89
+ # existing one (ACC_EXCL)
87
90
try :
88
91
fid = h5f .open (name , h5f .ACC_RDWR , fapl = fapl )
89
- try :
90
- existing_fcpl = fid .get_create_plist ()
91
- if (userblock_size is not None and
92
- existing_fcpl .get_userblock () != userblock_size ):
93
- raise ValueError ("Requested userblock size (%d) does not match that of existing file (%d)" % (userblock_size , existing_fcpl .get_userblock ()))
94
- except :
95
- fid .close ()
96
- raise
97
92
except IOError :
98
93
fid = h5f .create (name , h5f .ACC_EXCL , fapl = fapl , fcpl = fcpl )
94
+ elif mode is None :
95
+ # Try to open in append mode (read/write).
96
+ # If that fails, try readonly, and finally create a new file only
97
+ # if it won't clobber an existing file (ACC_EXCL).
98
+ try :
99
+ fid = h5f .open (name , h5f .ACC_RDWR , fapl = fapl )
100
+ except IOError :
101
+ try :
102
+ fid = h5f .open (name , h5f .ACC_RDONLY , fapl = fapl )
103
+ except IOError :
104
+ fid = h5f .create (name , h5f .ACC_EXCL , fapl = fapl , fcpl = fcpl )
99
105
else :
100
106
raise ValueError ("Invalid mode; must be one of r, r+, w, w-, a" )
101
107
108
+ try :
109
+ if userblock_size is not None :
110
+ existing_fcpl = fid .get_create_plist ()
111
+ if existing_fcpl .get_userblock () != userblock_size :
112
+ raise ValueError ("Requested userblock size (%d) does not match that of existing file (%d)" % (userblock_size , existing_fcpl .get_userblock ()))
113
+ except :
114
+ fid .close ()
115
+ raise
116
+
102
117
return fid
103
118
104
119
0 commit comments