diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-02-13 03:20:52 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-02-13 03:20:52 +0000 |
commit | d6fb854ecbf48111a746009f559d642446b8d1c1 (patch) | |
tree | 1ac02d7c709f031404a23a9b1528fc844f6b5520 /lib/tempfile.rb | |
parent | 3f9bc1fd88554a7ebacba2246ed51a147103c3b7 (diff) |
* lib/tempfile.rb (Tempfile::Remover): new class to replace
Tempfile.callback. port r24902 from Ruby 1.8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26656 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/tempfile.rb')
-rwxr-xr-x | lib/tempfile.rb | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/lib/tempfile.rb b/lib/tempfile.rb index b327acb62b..e1bccf0a90 100755 --- a/lib/tempfile.rb +++ b/lib/tempfile.rb @@ -128,7 +128,7 @@ class Tempfile < DelegateClass(File) # number of tries, then it will raise an exception. def initialize(basename, *rest) @data = [] - @clean_proc = self.class.callback(@data) + @clean_proc = Remover.new(@data) ObjectSpace.define_finalizer(self, @clean_proc) create(basename, *rest) do |tmpname, n, opts| @@ -230,7 +230,7 @@ class Tempfile < DelegateClass(File) if File.exist?(@tmpname) File.unlink(@tmpname) end - # remove tmpname from callback + # remove tmpname from remover @data[0] = @data[2] = nil @data = @tmpname = nil rescue Errno::EACCES @@ -257,27 +257,33 @@ class Tempfile < DelegateClass(File) end alias length size - class << self - def callback(data) # :nodoc: - pid = $$ - Proc.new { - if pid == $$ - path, tmpfile = *data + # :stopdoc: + class Remover + def initialize(data) + @pid = $$ + @data = data + end - STDERR.print "removing ", path, "..." if $DEBUG + def call(*args) + if @pid == $$ + path, tmpfile = *@data - tmpfile.close if tmpfile + STDERR.print "removing ", path, "..." if $DEBUG - # keep this order for thread safeness - if path - File.unlink(path) if File.exist?(path) - end + tmpfile.close if tmpfile - STDERR.print "done\n" if $DEBUG - end - } + # keep this order for thread safeness + if path + File.unlink(path) if File.exist?(path) + end + + STDERR.print "done\n" if $DEBUG + end end + end + # :startdoc: + class << self # Creates a new Tempfile. # # If no block is given, this is a synonym for Tempfile.new. |