[macruby-changes] [4905] MacRuby/trunk/io.c

source_changes at macosforge.org source_changes at macosforge.org
Mon Nov 15 04:49:29 PST 2010


Revision: 4905
          http://trac.macosforge.org/projects/ruby/changeset/4905
Author:   watson1978 at gmail.com
Date:     2010-11-15 04:49:27 -0800 (Mon, 15 Nov 2010)
Log Message:
-----------
IO.read will close resource surely with rb_ensure.

Modified Paths:
--------------
    MacRuby/trunk/io.c

Modified: MacRuby/trunk/io.c
===================================================================
--- MacRuby/trunk/io.c	2010-11-15 08:12:19 UTC (rev 4904)
+++ MacRuby/trunk/io.c	2010-11-15 12:49:27 UTC (rev 4905)
@@ -3989,6 +3989,12 @@
     return Qnil;
 }
 
+static VALUE
+io_s_read(struct foreach_arg *arg)
+{
+    return io_read(arg->io, 0, arg->argc, arg->argv);
+}
+
 /*
  *  call-seq:
  *     IO.read(name, [length [, offset]] )   => string
@@ -4031,14 +4037,15 @@
     rb_scan_args(argc, argv, "13", &fname, &length, &offset, NULL);
 
     SafeStringValue(fname);
-    VALUE io = rb_file_open(io_alloc(recv, 0), 1, &fname);
+    struct foreach_arg arg;
+    arg.io = rb_file_open(io_alloc(recv, 0), 1, &fname);
+    arg.argc = 1;
+    arg.argv = &length;
 
     if (!NIL_P(offset)) {
-	rb_io_seek(io, offset, 0);
+	rb_io_seek(arg.io, offset, 0);
     }
-    VALUE result = io_read(io, 0, 1, &length);
-    rb_io_close(io);
-    return result;
+    return rb_ensure(io_s_read, (VALUE)&arg, rb_io_close, arg.io);
 }
 
 static VALUE
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101115/786b99c2/attachment.html>


More information about the macruby-changes mailing list