diff options
author | Nobuyoshi Nakada <[email protected]> | 2024-08-24 00:37:24 +0900 |
---|---|---|
committer | git <[email protected]> | 2024-08-26 02:49:02 +0000 |
commit | 136cbf04419acf993357701e54712f921a294355 (patch) | |
tree | 7d6526aa2653bb8ee315156b60d728d22cfbbd24 /lib | |
parent | 2e07c13049a887c22849e77648838f040b0759b0 (diff) |
[ruby/tempfile] Support anonymous tempfile on earlier than Ruby 3.2
https://github.com/ruby/tempfile/commit/7052805029
Diffstat (limited to 'lib')
-rw-r--r-- | lib/tempfile.rb | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/lib/tempfile.rb b/lib/tempfile.rb index bb05080cec..2f8a39cfe5 100644 --- a/lib/tempfile.rb +++ b/lib/tempfile.rb @@ -556,7 +556,7 @@ end # Related: Tempfile.new. # def Tempfile.create(basename="", tmpdir=nil, mode: 0, anonymous: false, **options, &block) - if anonymous && RUBY_VERSION >= '3.2' + if anonymous create_anonymous(basename, tmpdir, mode: mode, **options, &block) else create_with_filename(basename, tmpdir, mode: mode, **options, &block) @@ -593,6 +593,18 @@ private def create_with_filename(basename="", tmpdir=nil, mode: 0, **options) end end +File.open(IO::NULL) do |f| + File.new(f.fileno, autoclose: false, path: "").path +rescue IOError + module PathAttr # :nodoc: + attr_reader :path + + def self.set_path(file, path) + file.extend(self).instance_variable_set(:@path, path) + end + end +end + private def create_anonymous(basename="", tmpdir=nil, mode: 0, **options, &block) tmpfile = nil tmpdir = Dir.tmpdir() if tmpdir.nil? @@ -608,12 +620,14 @@ private def create_anonymous(basename="", tmpdir=nil, mode: 0, **options, &block mode |= File::SHARE_DELETE | File::BINARY # Windows needs them to unlink the opened file. tmpfile = create_with_filename(basename, tmpdir, mode: mode, **options) File.unlink(tmpfile.path) + tmppath = tmpfile.path end path = File.join(tmpdir, '') - if tmpfile.path != path + unless tmppath == path # clear path. tmpfile.autoclose = false tmpfile = File.new(tmpfile.fileno, mode: File::RDWR, path: path) + PathAttr.set_path(tmpfile, path) if defined?(PathAttr) end if block begin |