# Tempfile.unlink $B8e$K(B tempfile.close

**URL:** <https://www.ruby-forum.com/t/tempfile-unlink-b8e-k-b-tempfile-close/132447>\
**Category:** Development\
**Created:** [March 20, 2008, 7:48am UTC](https://www.ruby-forum.com/t/tempfile-unlink-b8e-k-b-tempfile-close/132447 "2008-03-20T07:48:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SATOH\_Fumiyasu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@SATOH\_Fumiyasu](https://www.ruby-forum.com/u/SATOH_Fumiyasu)\
**Post date:** [March 20, 2008, 7:48am UTC](https://www.ruby-forum.com/t/tempfile-unlink-b8e-k-b-tempfile-close/132447/1 "2008-03-20T07:48:54Z")

</div>

e$B$5$H$&$U$\_$d$9e(B @ OSS e$B%F%/%N%m%8$G$9!#e(B

Tempfile e$B%\*%V%8%’%/%H$re(B unlink e$B$7$?8e$Ke(B close e$B$9$k$H!"e(B  
e$B0J2\<$N$h$&$KNc30$,H/@8$7$F$7$^$$$^$9!#e(B

$ ruby -v  
ruby 1.8.6 (2008-03-03 patchlevel 114) [x86\_64-linux]  
e$B$A$J$\_$Ke(B Ruby 1.9.0-1 e$B$G$OH/@8$7$^$;$s$G$7$?!#e(B

$ cat ~/test.rb  
require ‘tempfile’  
temp = Tempfile.new(‘test’)  
temp.unlink  
temp.close

$ ruby test.rb  
/usr/lib/ruby/1.8/tempfile.rb:84:in `_close': undefined method`[]=’ for  
nil:NilClass (NoMethodError)  
from /usr/lib/ruby/1.8/tempfile.rb:97:in `close’  
from /home/fumiyas/test.rb:5

e$B\<B:]$KLdBj$H$J$C$?$N$O!"0J2\<$N$h$&$J%3!\<%I$G$9e(B:

```
input_tmp = Tempfile.new('chimera-filter')
input_tmp.unlink
super(input, input_tmp)
input_tmp.rewind

stdout_reader, stdout_writer = IO.pipe
stderr_reader, stderr_writer = IO.pipe
pid = Process.fork do
  begin
    $stdin.reopen(input_tmp)
    input_tmp.close e$B"+e(B e$B$3$3$GMn$A$ke(B
    $stdout.reopen(stdout_writer)
    stdout_writer.close
    stdout_reader.close
    $stderr.reopen(stderr_writer)
    stderr_writer.close
    stderr_reader.close
    ENV['PATH'] = @path if (@path)
    Kernel.exec([@command, @arg0], *@args)
  rescue Exception => e
    $stderr.puts("#{$0}: ERROR: #{ __FILE__ }: exec failed: 

```

#{@command}: #{e}")  
exit!(1)  
end  
end

---

<div class="post-metadata">

**Author:** ![SATOH\_Fumiyasu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@SATOH\_Fumiyasu](https://www.ruby-forum.com/u/SATOH_Fumiyasu)\
**Post date:** [April 16, 2008, 4:24am UTC](https://www.ruby-forum.com/t/tempfile-unlink-b8e-k-b-tempfile-close/132447/2 "2008-04-16T04:24:57Z")

</div>

ã•ã¨ã†ãµã¿ã‚„ã™ @ OSS ãƒ†ã‚¯ãƒŽãƒ­ã‚¸ã§ã™ã€‚

At Thu, 20 Mar 2008 15:47:56 +0900,  
SATOH Fumiyasu wrote:

> Tempfile ã‚ªãƒ–ã‚¸ã‚§ã‚¯ãƒˆã‚’ unlink ã—ãŸå¾Œã« close ã™ã‚‹ã¨ã€  
> ä»¥ä¸‹ã®ã‚ˆã†ã«ä¾‹å¤–ãŒç™ºç”Ÿã—ã¦ã—ã¾ã„ã¾ã™ã€‚

åå¿œãªã—ã§ã™ãŒã€BTS ã«ç™»éŒ²ã—ãŸã»ã†ãŒã„ã„ã®ã§ã—ã‚‡ã†ã‹?

ã‚ã¾ã‚Šç¾Žã—ããªã„æ„Ÿã˜ã§ã™ãŒã€ã¨ã‚Šã‚ãˆãšãƒ‘ãƒƒãƒã‚’  
ä½œã£ã¦ã¿ã¾ã—ãŸ(æ·»ä»˜)ã€‚Ruby 1.8.7-preview1 ã§ä½œæˆ  
ã—ã¾ã—ãŸãŒã€ãŸã¶ã‚“ Ruby 1.8.6 ç³»çµ±ã«ã‚‚ã‚ãŸã‚Šã¾ã™ã€‚

unlink å¾Œã® open ã§ã‚‚åŒæ§˜ã®å•é¡ŒãŒã‚ã‚‹ã®ã§ã™ãŒã€  
ä¿®æ­£ã™ã‚‹ãªã‚‰ä»¥ä¸‹ã®ã‚ˆã†ãªæ„Ÿã˜?

— lib/tempfile.rb 2008-04-16 11:08:19.000000000 +0900  
+++ lib/tempfile.rb 2008-04-16 11:19:25.000000000 +0900  
@@ -131,6 +131,9 @@  
@@cleanlist.delete(@tmpname)  
@data = @tmpname = nil  
ObjectSpace.undefine\_finalizer(self)

- 

```
 def self.open

```

- raise “cannot open unlinked tempfile”
- 

```
 end

```

rescue Errno::EACCES  
# may not be able to unlink on Windows; just ignore  
end

---

<div class="post-metadata">

**Author:** ![SATOH\_Fumiyasu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@SATOH\_Fumiyasu](https://www.ruby-forum.com/u/SATOH_Fumiyasu)\
**Post date:** [April 16, 2008, 7:28am UTC](https://www.ruby-forum.com/t/tempfile-unlink-b8e-k-b-tempfile-close/132447/3 "2008-04-16T07:28:04Z")

</div>

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:34094] tempfile.unlink e$B8e$Ke(B  
tempfile.close”  
on Thu, 20 Mar 2008 15:47:56 +0900, SATOH Fumiyasu  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|Tempfile e$B%\*%V%8%'%/%H$re(B unlink e$B$7$?8e$Ke(B close e$B$9$k$H!"e(B  
|e$B0J2\<$N$h$&$KNc30$,H/@8$7$F$7$^$$$^$9!#e(B  
|  
|$ ruby -v  
|ruby 1.8.6 (2008-03-03 patchlevel 114) [x86\_64-linux]  
|e$B$A$J$\_$Ke(B Ruby 1.9.0-1 e$B$G$OH/@8$7$^$;$s$G$7$?!#e(B

1.9e$B$G$OBP1~$5$l$F$^$9$M!#0J2\<$N$h$&$J%Q%C%A$,8z$/$\_$?$$$G$9!#e(B  
1.8.7e$B$KF~$l$k$+$I$&$+$O$\*G$$;$7$^$9e(B \> knue$B$5$s!#e(B

diff --git a/ChangeLog b/ChangeLog  
index 9994dae…797d766 100644  
— a/ChangeLog  
+++ b/ChangeLog  
@@ -1,3 +1,10 @@  
+Wed Apr 16 14:22:42 2008 Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
+

- 
  - lib/tempfile.rb (Tempfile::\_close): check @data before modifying

- it backported from 1.9. [ruby-dev:34094]
- 
- 
  - lib/tempfile.rb (Tempfile::close): clear @data and @tmpname.

- 

Wed Apr 16 06:03:45 2008 Akinori MUSHA [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

- test/ruby/test\_settracefunc.rb (TestSetTraceFunc#test\_event):  
diff --git a/lib/tempfile.rb b/lib/tempfile.rb  
index c728dbc…703e781 100644  
— a/lib/tempfile.rb  
+++ b/lib/tempfile.rb  
@@ -95,7 +95,8 @@ class Tempfile \< DelegateClass(File)

def \_close # :nodoc:  
@tmpfile.close if @tmpfile

- @data[1] = @tmpfile = nil

- @tmpfile = nil
- @data[1] = nil if @data  
end  
protected :\_close

@@ -117,6 +118,7 @@ class Tempfile \< DelegateClass(File)  
\_close  
@clean\_proc.call  
ObjectSpace.undefine\_finalizer(self)

- @data = @tmpname = nil  
end

# Unlinks the file. On UNIX-like systems, it is often a good idea
