@@ -556,7 +556,7 @@ def open(*args, **kw)
556
556
# Related: Tempfile.new.
557
557
#
558
558
def Tempfile . create ( basename = "" , tmpdir = nil , mode : 0 , anonymous : false , **options , &block )
559
- if anonymous && RUBY_VERSION >= '3.2'
559
+ if anonymous
560
560
create_anonymous ( basename , tmpdir , mode : mode , **options , &block )
561
561
else
562
562
create_with_filename ( basename , tmpdir , mode : mode , **options , &block )
@@ -593,6 +593,18 @@ class << Tempfile
593
593
end
594
594
end
595
595
596
+ File . open ( IO ::NULL ) do |f |
597
+ File . new ( f . fileno , autoclose : false , path : "" ) . path
598
+ rescue IOError
599
+ module PathAttr # :nodoc:
600
+ attr_reader :path
601
+
602
+ def self . set_path ( file , path )
603
+ file . extend ( self ) . instance_variable_set ( :@path , path )
604
+ end
605
+ end
606
+ end
607
+
596
608
private def create_anonymous ( basename = "" , tmpdir = nil , mode : 0 , **options , &block )
597
609
tmpfile = nil
598
610
tmpdir = Dir . tmpdir ( ) if tmpdir . nil?
@@ -608,12 +620,14 @@ class << Tempfile
608
620
mode |= File ::SHARE_DELETE | File ::BINARY # Windows needs them to unlink the opened file.
609
621
tmpfile = create_with_filename ( basename , tmpdir , mode : mode , **options )
610
622
File . unlink ( tmpfile . path )
623
+ tmppath = tmpfile . path
611
624
end
612
625
path = File . join ( tmpdir , '' )
613
- if tmpfile . path ! = path
626
+ unless tmppath = = path
614
627
# clear path.
615
628
tmpfile . autoclose = false
616
629
tmpfile = File . new ( tmpfile . fileno , mode : File ::RDWR , path : path )
630
+ PathAttr . set_path ( tmpfile , path ) if defined? ( PathAttr )
617
631
end
618
632
if block
619
633
begin
0 commit comments