Project

General

Profile

Actions

Bug #13198

closed

Tempfile#size is nil when nothing is written, expected 0

Added by kyledrake (Kyle Drake) about 7 years ago. Updated about 7 years ago.

Status:
Closed
Target version:
-
[ruby-core:79459]

Description

require 'tempfile'

tmp = Tempfile.new
tmp.write ''
tmp.close
tmp.size # => nil

File.size(tmp.path) # => 0

I'm not sure if this is actually a bug, but this behavior really surprised me. I think it would be better to return 0 in this scenario instead of nil, which would match the behavior of File.

Updated by nobu (Nobuyoshi Nakada) about 7 years ago

  • Status changed from Open to Assigned
  • Assignee set to Glass_saga (Masaki Matsushita)
  • Backport changed from 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN to 2.2: DONTNEED, 2.3: REQUIRED, 2.4: REQUIRED

It is because Fie.size? is used instead of File.size, since r50682.
I suspect that it would be a typo and unintentional.

diff --git i/lib/tempfile.rb w/lib/tempfile.rb
index d068dd603c..b36c6638b6 100644
--- i/lib/tempfile.rb
+++ w/lib/tempfile.rb
@@ -227,7 +227,7 @@
     if !@tmpfile.closed?
       @tmpfile.size # File#size calls rb_io_flush_raw()
     else
-      File.size?(@tmpfile.path)
+      File.size(@tmpfile.path)
     end
   end
   alias length size
diff --git i/test/test_tempfile.rb w/test/test_tempfile.rb
index 7f7f75c7db..a2b272747e 100644
--- i/test/test_tempfile.rb
+++ w/test/test_tempfile.rb
@@ -247,6 +247,13 @@
     assert_equal 5, t.size
   end
 
+  def test_size_on_empty_file
+    t = tempfile("foo")
+    t.write("")
+    t.close
+    assert_equal 0, t.size
+  end
+
   def test_concurrency
     threads = []
     tempfiles = []
Actions #2

Updated by Anonymous about 7 years ago

  • Status changed from Assigned to Closed

Applied in changeset r57972.


Fix bug of Tempfile#size if nothing is written [Bug #13198]

  • lib/tempfile.rb (Tempfile#size): Fix its behavior when nothing
    is written. Tempfile#size should return 0 in this case.
    The patch is from nobu .

Updated by naruse (Yui NARUSE) about 7 years ago

  • Backport changed from 2.2: DONTNEED, 2.3: REQUIRED, 2.4: REQUIRED to 2.2: DONTNEED, 2.3: REQUIRED, 2.4: DONE

ruby_2_4 r58013 merged revision(s) 57972.

Updated by nagachika (Tomoyuki Chikanaga) about 7 years ago

  • Backport changed from 2.2: DONTNEED, 2.3: REQUIRED, 2.4: DONE to 2.2: DONTNEED, 2.3: DONE, 2.4: DONE

ruby_2_3 r58154 merged revision(s) 57972.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0