зеркало из https://github.com/github/ruby.git
* 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
This commit is contained in:
Родитель
3f9bc1fd88
Коммит
d6fb854ecb
|
@ -1,3 +1,8 @@
|
||||||
|
Sat Feb 13 12:17:52 2010 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
|
* lib/tempfile.rb (Tempfile::Remover): new class to replace
|
||||||
|
Tempfile.callback. port r24902 from Ruby 1.8.
|
||||||
|
|
||||||
Fri Feb 12 17:55:21 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Fri Feb 12 17:55:21 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* vm.c (thread_free): fixed typo.
|
* vm.c (thread_free): fixed typo.
|
||||||
|
|
|
@ -128,7 +128,7 @@ class Tempfile < DelegateClass(File)
|
||||||
# number of tries, then it will raise an exception.
|
# number of tries, then it will raise an exception.
|
||||||
def initialize(basename, *rest)
|
def initialize(basename, *rest)
|
||||||
@data = []
|
@data = []
|
||||||
@clean_proc = self.class.callback(@data)
|
@clean_proc = Remover.new(@data)
|
||||||
ObjectSpace.define_finalizer(self, @clean_proc)
|
ObjectSpace.define_finalizer(self, @clean_proc)
|
||||||
|
|
||||||
create(basename, *rest) do |tmpname, n, opts|
|
create(basename, *rest) do |tmpname, n, opts|
|
||||||
|
@ -230,7 +230,7 @@ class Tempfile < DelegateClass(File)
|
||||||
if File.exist?(@tmpname)
|
if File.exist?(@tmpname)
|
||||||
File.unlink(@tmpname)
|
File.unlink(@tmpname)
|
||||||
end
|
end
|
||||||
# remove tmpname from callback
|
# remove tmpname from remover
|
||||||
@data[0] = @data[2] = nil
|
@data[0] = @data[2] = nil
|
||||||
@data = @tmpname = nil
|
@data = @tmpname = nil
|
||||||
rescue Errno::EACCES
|
rescue Errno::EACCES
|
||||||
|
@ -257,27 +257,33 @@ class Tempfile < DelegateClass(File)
|
||||||
end
|
end
|
||||||
alias length size
|
alias length size
|
||||||
|
|
||||||
class << self
|
# :stopdoc:
|
||||||
def callback(data) # :nodoc:
|
class Remover
|
||||||
pid = $$
|
def initialize(data)
|
||||||
Proc.new {
|
@pid = $$
|
||||||
if pid == $$
|
@data = data
|
||||||
path, tmpfile = *data
|
|
||||||
|
|
||||||
STDERR.print "removing ", path, "..." if $DEBUG
|
|
||||||
|
|
||||||
tmpfile.close if tmpfile
|
|
||||||
|
|
||||||
# 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
|
||||||
|
|
||||||
|
def call(*args)
|
||||||
|
if @pid == $$
|
||||||
|
path, tmpfile = *@data
|
||||||
|
|
||||||
|
STDERR.print "removing ", path, "..." if $DEBUG
|
||||||
|
|
||||||
|
tmpfile.close if tmpfile
|
||||||
|
|
||||||
|
# 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.
|
# Creates a new Tempfile.
|
||||||
#
|
#
|
||||||
# If no block is given, this is a synonym for Tempfile.new.
|
# If no block is given, this is a synonym for Tempfile.new.
|
||||||
|
|
Загрузка…
Ссылка в новой задаче