@@ -556,7 +556,7 @@ def open(*args, **kw)
556556# Related: Tempfile.new.
557557#
558558def Tempfile . create ( basename = "" , tmpdir = nil , mode : 0 , anonymous : false , **options , &block )
559- if anonymous && RUBY_VERSION >= '3.2'
559+ if anonymous
560560 create_anonymous ( basename , tmpdir , mode : mode , **options , &block )
561561 else
562562 create_with_filename ( basename , tmpdir , mode : mode , **options , &block )
@@ -593,6 +593,20 @@ class << Tempfile
593593 end
594594end
595595
596+ File . open ( IO ::NULL ) do |f |
597+ File . new ( f . fileno , autoclose : false , path : "" ) . path
598+ rescue IOError
599+ #---
600+ module PathAttr
601+ attr_reader :path
602+
603+ def self . set_path ( file , path )
604+ file . extend ( PathAttr ) . instance_variable_set ( :@path , path )
605+ end
606+ end
607+ #+++
608+ end
609+
596610private def create_anonymous ( basename = "" , tmpdir = nil , mode : 0 , **options , &block )
597611 tmpfile = nil
598612 tmpdir = Dir . tmpdir ( ) if tmpdir . nil?
@@ -608,12 +622,14 @@ class << Tempfile
608622 mode |= File ::SHARE_DELETE | File ::BINARY # Windows needs them to unlink the opened file.
609623 tmpfile = create_with_filename ( basename , tmpdir , mode : mode , **options )
610624 File . unlink ( tmpfile . path )
625+ tmppath = tmpfile . path
611626 end
612627 path = File . join ( tmpdir , '' )
613- if tmpfile . path ! = path
628+ unless tmppath = = path
614629 # clear path.
615630 tmpfile . autoclose = false
616631 tmpfile = File . new ( tmpfile . fileno , mode : File ::RDWR , path : path )
632+ PathAttr . set_path ( tmpfile , path ) if defined? ( PathAttr )
617633 end
618634 if block
619635 begin
0 commit comments