Improved Tempfile w/o DelegateClass
I propose improved
Tempfile has following problems.
t = Tempfile.new("foo") #=> #<File:/tmp/foo20121012-6762-12w11to> t.is_a? File #=> false
t = Tempfile.new("foo") t.dup.close t.read #=> IOError: closed stream
finalizer performs unlink even when it has been duplicated
t = Tempfile.new("foo") path = t.path #=> "/tmp/foo20121012-7533-1q537gq" File.exist? path #=> true tt = t.dup t = nil GC.start File.exist? path #=> false
I think these problems caused by using
Therefore, I made a patch to resolve the problems.
The patched Tempfile class is a subclass of File.
#1 [ruby-core:47932] Updated by Anonymous over 3 years ago
I propose improved
Tempfilehas following problems.
- confusing inspect
- finalizer performs unlink even when it has been duplicated
I have no objection about (1), but what we expect when we call
Tempfile#dup might differ, for example, I'd expect it to copy the
underlying temporary file. So making
Tempfile a subclass of
may not be the ideal solution.
#2 [ruby-core:48015] Updated by Masaki Matsushita over 3 years ago
Yukihiro Matsumoto wrote:
I'd expect it to copy the underlying temporary file.
Is the behavior of #dup you expect like the following?
def dup dupe = self.class.new(@basename) IO.copy_stream(self, dupe, 0) dupe end
I think the reason why
DelegateClass is that to implement
Tempfile#open without it used to be difficult.
To implement it as subclass of
self must be reopened with full configuration, mode and opts.
IO#reopen used not to accept them, but now it accepts after r37071.
#6 [ruby-core:69385] Updated by Eric Wong 11 months ago
"Glass_saga (Masaki Matsushita)" firstname.lastname@example.org wrote:
I would still like this for 2.3.0, just hit a snag with
Also, Charles hit a similar problem not long ago, too:
#11 [ruby-core:69529] Updated by Yukihiro Matsumoto 11 months ago
It's OK for me to implement Tempfile without using DelegateClass. If JRuby does similar thing already, it might be a good idea to share implementation. Besides that, we might need to think about killing/redefining some unnecessary/invalid methods of File class, e.g. reopen. I am afraid dup is one of them.