From ba7a870a8924fdd0fe1612ef4b74024ae0e3fe32 Mon Sep 17 00:00:00 2001 From: nobu Date: Wed, 26 Aug 2009 05:34:47 +0000 Subject: [PATCH] * lib/tempfile.rb (Tempfile#close!, Tempfile#path): added side notes from Hongli Lai's fork. * lib/tempfile.rb (Tempfile#unlink, Tempfile.callback): do nothing any more once unlinked. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24663 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 8 ++++++++ lib/tempfile.rb | 19 ++++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index b1c0438572..97a45b3478 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Wed Aug 26 14:34:39 2009 Nobuyoshi Nakada + + * lib/tempfile.rb (Tempfile#close!, Tempfile#path): added side + notes from Hongli Lai's fork. + + * lib/tempfile.rb (Tempfile#unlink, Tempfile.callback): do nothing + any more once unlinked. + Wed Aug 26 13:48:33 2009 Nobuyoshi Nakada * lib/tempfile.rb (Tempfile#unlink): reverted r23494, since the diff --git a/lib/tempfile.rb b/lib/tempfile.rb index 1bee9e1015..57127c7214 100644 --- a/lib/tempfile.rb +++ b/lib/tempfile.rb @@ -11,7 +11,7 @@ require 'thread' # A class for managing temporary files. This library is written to be # thread safe. class Tempfile < DelegateClass(File) - MAX_TRY = 10 + MAX_TRY = 10 # :nodoc: @@cleanlist = [] @@lock = Mutex.new @@ -53,7 +53,7 @@ class Tempfile < DelegateClass(File) rescue failure += 1 retry if failure < MAX_TRY - raise "cannot generate tempfile `%s'" % tmpname + raise "cannot generate tempfile `#{tmpname}'" end } @@ -122,11 +122,10 @@ class Tempfile < DelegateClass(File) end # Closes and unlinks the file. + # Has the same effect as called close(true). def close! _close - @clean_proc.call - ObjectSpace.undefine_finalizer(self) - @data = @tmpname = nil + unlink end # Unlinks the file. On UNIX-like systems, it is often a good idea @@ -135,11 +134,14 @@ class Tempfile < DelegateClass(File) # file. def unlink # keep this order for thread safeness + return unless @tmpname begin if File.exist?(@tmpname) File.unlink(@tmpname) end @@cleanlist.delete(@tmpname) + # remove tmpname and cleanlist from callback + @data[0] = @data[2] = nil @data = @tmpname = nil ObjectSpace.undefine_finalizer(self) rescue Errno::EACCES @@ -149,6 +151,7 @@ class Tempfile < DelegateClass(File) alias delete unlink # Returns the full path name of the temporary file. + # This will be nil if #unlink has been called. def path @tmpname end @@ -177,8 +180,10 @@ class Tempfile < DelegateClass(File) tmpfile.close if tmpfile # keep this order for thread safeness - File.unlink(path) if File.exist?(path) - cleanlist.delete(path) if cleanlist + if path + File.unlink(path) if File.exist?(path) + cleanlist.delete(path) if cleanlist + end print "done\n" if $DEBUG end