Feature #5707

temporary file creation without finalizer and delegate.

Added by Akira Tanaka over 2 years ago. Updated about 1 year ago.

[ruby-core:41478]
Status:Closed
Priority:Normal
Assignee:Akira Tanaka
Category:-
Target version:next minor

Description

Tempfile always uses finalizer and delegate.
Sometimes we don't need them.

For example, following test creates a temporary file
using Tempfile.open but the file is used only in the block.
The file is removed when the Tempfile object is collected but
it is possible to remove just after the block return.

test/ruby/testio.rb:
def test
fcntldupfd
Tempfile.open(self.class.name) do |f|
fd = f.fcntl(Fcntl::F
DUPFD, 63)
begin
assertoperator(fd, :>=, 63)
ensure
IO.for
fd(fd).close
end
end
end

So I propose a method, Tempfile.open2, to create temporary file without finalizer and delegate.

The arguments of Tempfile.open2 is same as Tempfile.open.
The return value is File object instead of Tempfile object.

If a block is given, the temporary file is removed after the block returns.

If a block is not given, the temporary file is not removed.
It should be removed using File.unlink.
Non-block form can be used to create non-temporary file.
If you need to change temporary file to non-temporary file, File.rename can be used.

I made a patch to implement Tempfile.open2.
I also made a patch to change tests to use Tempfile.open2 to show usage of it.

A problem of this proposal is the method name.
Tempfile.open2 is not a good name.
Better idea is welcome.

tempfile-open2.patch Magnifier (1.52 KB) Akira Tanaka, 12/04/2011 11:31 PM

tempfile-open2-tests.patch Magnifier (3.16 KB) Akira Tanaka, 12/04/2011 11:31 PM


Subtasks

Feature #7767: Tempfileで自動的にファイルを削除するClosedAkira Tanaka

Associated revisions

Revision 40393
Added by Akira Tanaka about 1 year ago

  • lib/tempfile.rb (Tempfile.create): New method. The method name is proposed by Shugo Maeda. [Feature #5707]

History

#1 Updated by Eric Wong over 2 years ago

Akira Tanaka akr@fsij.org wrote:

A problem of this proposal is the method name.
Tempfile.open2 is not a good name.

Since Tempfile.new already takes an optional hash,
how about:

Tempfile.open(..., finalizer: false)

#2 Updated by Akira Tanaka over 2 years ago

2011/12/5 Eric Wong normalperson@yhbt.net:

Since Tempfile.new already takes an optional hash,
how about:

Tempfile.open(..., finalizer: false)

It is not bad.

But I feel the description is too subtle to change
the class of return value and the responsibility or timing of file deletion.
--
Tanaka Akira

#3 Updated by Motohiro KOSAKI over 2 years ago

Since Tempfile.new already takes an optional hash,
how about:

 Tempfile.open(..., finalizer: false)

It is not bad.

But I feel the description is too subtle to change
the class of return value and the responsibility or timing of file deletion.

I'm incline with Eric and I prefer to don't add Non-block form. It's
no recommended
programming style and shouldn't be encouraged.

Because of, if people accidentally use Tempfile.open instead of Tempfile.open2,
no visible fault is happen on many case. So, they don't have big difference from
point of end user view.

#4 Updated by Yusuke Endoh about 2 years ago

  • Status changed from Open to Assigned
  • Assignee set to Akira Tanaka

What's the status? How can we move on?

The easiest way I think is to become the maintainer of tempfile
and to accept the ticket by yourself.

Yusuke Endoh mame@tsg.ne.jp

#5 Updated by Akira Tanaka about 2 years ago

2012/3/28 mame (Yusuke Endoh) mame@tsg.ne.jp:

Issue #5707 has been updated by mame (Yusuke Endoh).

Status changed from Open to Assigned
Assignee set to akr (Akira Tanaka)

What's the status? How can we move on?

The easiest way I think is to become the maintainer of tempfile
and to accept the ticket by yourself.

I hope we can find a good method name.

I don't accept this issue without a good method name anyway.
--
Tanaka Akira

#6 Updated by Yusuke Endoh over 1 year ago

  • Target version set to next minor

#7 Updated by Akira Tanaka about 1 year ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r40393.
Akira, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • lib/tempfile.rb (Tempfile.create): New method. The method name is proposed by Shugo Maeda. [Feature #5707]

Also available in: Atom PDF