Issue #7148 has been updated by sowieso (So Wieso).


I guess this is related:

``` ruby
tf = Tempfile.new
tf.unlink
tf2 = tf.dup
puts tf.fileno
# => 10
puts tf2.fileno
# => 11
tf2.close

print tf2.fileno
# => 11
print tf.fileno
IOError: closed stream
from /usr/local/rvm/rubies/ruby-2.6.0/lib/ruby/2.6.0/delegate.rb:349:in `fileno'
```

It closes the wrong fd. Using normal `File` instead of `Tmpfile` makes it work like usual.

Libraries that accept an IO object and duplicate it internally don't work correctly with `Tempfile` (in my case rubyzip). This really needs a fix.

----------------------------------------
Feature #7148: Improved Tempfile w/o DelegateClass
https://bugs.ruby-lang.org/issues/7148#change-77337

* Author: Glass_saga (Masaki Matsushita)
* Status: Assigned
* Priority: Normal
* Assignee: Glass_saga (Masaki Matsushita)
* Target version: 
----------------------------------------
I propose improved `Tempfile` without `DelegateClass()`.
Present `Tempfile` has following problems.

1.  confusing inspect

    ~~~ruby
    t = Tempfile.new("foo") #=> #<File:/tmp/foo20121012-6762-12w11to>
    t.is_a? File #=> false
    ~~~

2.  `#dup` doesn't duplicate `IO`

    ~~~ruby
    t = Tempfile.new("foo")
    t.dup.close
    t.read #=> IOError: closed stream
    ~~~

3.  finalizer performs unlink even when it has been duplicated

    ~~~ruby
    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 `DelegateClass()`.
Therefore, I made a patch to resolve the problems.
The patched Tempfile class is a subclass of File.


---Files--------------------------------
patch.diff (3.52 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>