Fix file handle leaks (Fixes #35)

This commit is contained in:
2026-07-12 10:54:06 +02:00
parent ef6bd32d68
commit 5cd8df89be
2 changed files with 54 additions and 50 deletions
+25 -26
View File
@@ -220,26 +220,25 @@ class ADC_Result(Resultable, Drawable, DamarisFFT, Signalpath):
destination can be a file or a filename destination can be a file or a filename
suitable for further processing suitable for further processing
""" """
# write sorted if isinstance(destination, str):
the_destination=destination with open(destination, "w") as f:
if type(destination) in (str,): self._write_to_csv_dest(f, delimiter)
the_destination=open(destination, "w") else:
self._write_to_csv_dest(destination, delimiter)
the_destination.write("# adc_result\n") def _write_to_csv_dest(self, dest, delimiter):
the_destination.write("# t y0 y1 ...\n") dest.write("# adc_result\n")
dest.write("# t y0 y1 ...\n")
self.lock.acquire() self.lock.acquire()
try: try:
xdata=self.get_xdata() xdata=self.get_xdata()
ch_no=self.get_number_of_channels() ch_no=self.get_number_of_channels()
ydata=list(map(self.get_ydata, range(ch_no))) ydata=list(map(self.get_ydata, range(ch_no)))
#yerr=map(self.get_yerr, xrange(ch_no))
for i in range(len(xdata)): for i in range(len(xdata)):
the_destination.write("%e"%xdata[i]) dest.write("%e"%xdata[i])
for j in range(ch_no): for j in range(ch_no):
the_destination.write("%s%e"%(delimiter, ydata[j][i])) dest.write("%s%e"%(delimiter, ydata[j][i]))
the_destination.write("\n") dest.write("\n")
the_destination=None
xdata=ydata=None
finally: finally:
self.lock.release() self.lock.release()
@@ -249,28 +248,28 @@ class ADC_Result(Resultable, Drawable, DamarisFFT, Signalpath):
for further processing with the NMRnotebook software; for further processing with the NMRnotebook software;
destination can be a file or a filename destination can be a file or a filename
""" """
# write sorted if isinstance(destination, str):
the_destination=destination with open(destination, "w") as f:
if type(destination) in (str,): self._write_to_simpson_dest(f, delimiter)
the_destination=open(destination, "w") else:
self._write_to_simpson_dest(destination, delimiter)
def _write_to_simpson_dest(self, dest, delimiter):
self.lock.acquire() self.lock.acquire()
try: try:
xdata=self.get_xdata() xdata=self.get_xdata()
the_destination.write("SIMP\n") dest.write("SIMP\n")
the_destination.write("%s%i%s"%("NP=", len(xdata), "\n")) dest.write("%s%i%s"%("NP=", len(xdata), "\n"))
the_destination.write("%s%i%s"%("SW=", self.get_sampling_rate(), "\n")) dest.write("%s%i%s"%("SW=", self.get_sampling_rate(), "\n"))
the_destination.write("TYPE=FID\n") dest.write("TYPE=FID\n")
the_destination.write("DATA\n") dest.write("DATA\n")
ch_no=self.get_number_of_channels() ch_no=self.get_number_of_channels()
ydata=list(map(self.get_ydata, range(ch_no))) ydata=list(map(self.get_ydata, range(ch_no)))
for i in range(len(xdata)): for i in range(len(xdata)):
for j in range(ch_no): for j in range(ch_no):
the_destination.write("%g%s"%(ydata[j][i], delimiter)) dest.write("%g%s"%(ydata[j][i], delimiter))
the_destination.write("\n") dest.write("\n")
the_destination.write("END\n") dest.write("END\n")
the_destination=None
xdata=ydata=None
finally: finally:
self.lock.release() self.lock.release()
+29 -24
View File
@@ -248,38 +248,41 @@ class Accumulation(Errorable, Drawable, DamarisFFT, Signalpath):
writes the data to a file. writes the data to a file.
destination can be a filehandle or a filename, default sys.stdout destination can be a filehandle or a filename, default sys.stdout
""" """
the_destination=destination
if isinstance(destination, str): if isinstance(destination, str):
the_destination=open(destination, "w") with open(destination, "w") as f:
self._write_to_csv_dest(f, delimiter)
else:
self._write_to_csv_dest(destination, delimiter)
the_destination.write("# accumulation %d\n"%self.n) def _write_to_csv_dest(self, dest, delimiter):
dest.write("# accumulation %d\n"%self.n)
self.lock.acquire() self.lock.acquire()
try: try:
if self.common_descriptions is not None: if self.common_descriptions is not None:
for (key,value) in self.common_descriptions.items(): for (key,value) in self.common_descriptions.items():
the_destination.write("# %s : %s\n"%(key, str(value))) dest.write("# %s : %s\n"%(key, str(value)))
the_destination.write("# t") dest.write("# t")
ch_no=self.get_number_of_channels() ch_no=self.get_number_of_channels()
if self.use_error: if self.use_error:
for i in range(ch_no): for i in range(ch_no):
the_destination.write(" ch%d_mean ch%d_err"%(i,i)) dest.write(" ch%d_mean ch%d_err"%(i,i))
else: else:
for i in range(ch_no): for i in range(ch_no):
the_destination.write(" ch%d_mean"%i) dest.write(" ch%d_mean"%i)
the_destination.write("\n") dest.write("\n")
xdata=self.get_xdata() xdata=self.get_xdata()
ydata=list(map(self.get_ydata, range(ch_no))) ydata=list(map(self.get_ydata, range(ch_no)))
yerr=None yerr=None
if self.use_error: if self.use_error:
yerr=list(map(self.get_yerr, range(ch_no))) yerr=list(map(self.get_yerr, range(ch_no)))
for i in range(len(xdata)): for i in range(len(xdata)):
the_destination.write("%e"%xdata[i]) dest.write("%e"%xdata[i])
for j in range(ch_no): for j in range(ch_no):
if self.use_error: if self.use_error:
the_destination.write("%s%e%s%e"%(delimiter, ydata[j][i], delimiter, yerr[j][i])) dest.write("%s%e%s%e"%(delimiter, ydata[j][i], delimiter, yerr[j][i]))
else: else:
the_destination.write("%s%e"%(delimiter,ydata[j][i])) dest.write("%s%e"%(delimiter,ydata[j][i]))
the_destination.write("\n") dest.write("\n")
finally: finally:
self.lock.release() self.lock.release()
@@ -290,27 +293,29 @@ class Accumulation(Errorable, Drawable, DamarisFFT, Signalpath):
for further processing with the NMRnotebook software; for further processing with the NMRnotebook software;
destination can be a file or a filename destination can be a file or a filename
""" """
the_destination=destination
if isinstance(destination, str): if isinstance(destination, str):
the_destination=open(destination, "w") with open(destination, "w") as f:
self._write_to_simpson_dest(f, delimiter, frequency)
else:
self._write_to_simpson_dest(destination, delimiter, frequency)
def _write_to_simpson_dest(self, dest, delimiter, frequency):
self.lock.acquire() self.lock.acquire()
try: try:
xdata=self.get_xdata() xdata=self.get_xdata()
the_destination.write("SIMP\n") dest.write("SIMP\n")
the_destination.write("%s%i%s"%("NP=", len(xdata), "\n")) dest.write("%s%i%s"%("NP=", len(xdata), "\n"))
the_destination.write("%s%i%s"%("SW=", self.get_sampling_rate(), "\n")) dest.write("%s%i%s"%("SW=", self.get_sampling_rate(), "\n"))
the_destination.write("%s%i%s"%("REF=", frequency, "\n")) dest.write("%s%i%s"%("REF=", frequency, "\n"))
the_destination.write("TYPE=FID\n") dest.write("TYPE=FID\n")
the_destination.write("DATA\n") dest.write("DATA\n")
ch_no=self.get_number_of_channels() ch_no=self.get_number_of_channels()
ydata=list(map(self.get_ydata, range(ch_no))) ydata=list(map(self.get_ydata, range(ch_no)))
for i in range(len(xdata)): for i in range(len(xdata)):
for j in range(ch_no): for j in range(ch_no):
the_destination.write("%g%s"%(ydata[j][i], delimiter)) dest.write("%g%s"%(ydata[j][i], delimiter))
the_destination.write("\n") dest.write("\n")
the_destination.write("END\n") dest.write("END\n")
the_destination.close()
finally: finally:
self.lock.release() self.lock.release()