[macruby-changes] [2482] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Fri Sep 4 21:49:25 PDT 2009


Revision: 2482
          http://trac.macosforge.org/projects/ruby/changeset/2482
Author:   lsansonetti at apple.com
Date:     2009-09-04 21:49:21 -0700 (Fri, 04 Sep 2009)
Log Message:
-----------
fixing a bunch of minor IO bugs

Modified Paths:
--------------
    MacRuby/trunk/enum.c
    MacRuby/trunk/ext/socket/socket.c
    MacRuby/trunk/file.c
    MacRuby/trunk/include/ruby/intern.h
    MacRuby/trunk/io.c
    MacRuby/trunk/ruby.c

Modified: MacRuby/trunk/enum.c
===================================================================
--- MacRuby/trunk/enum.c	2009-09-05 03:21:01 UTC (rev 2481)
+++ MacRuby/trunk/enum.c	2009-09-05 04:49:21 UTC (rev 2482)
@@ -625,9 +625,11 @@
 	if (n <= 0) {
 	    rb_iter_break();
 	}
-	rb_ary_push(ary[1], i);
-	n--;
-	ary[0] = INT2NUM(n);
+	else {
+	    rb_ary_push(ary[1], i);
+	    n--;
+	    ary[0] = INT2NUM(n);
+	}
     }
     return Qnil;
 }

Modified: MacRuby/trunk/ext/socket/socket.c
===================================================================
--- MacRuby/trunk/ext/socket/socket.c	2009-09-05 03:21:01 UTC (rev 2481)
+++ MacRuby/trunk/ext/socket/socket.c	2009-09-05 04:49:21 UTC (rev 2482)
@@ -300,7 +300,7 @@
     GetOpenFile(sock, fptr);
     shutdown(fptr->fd, 0);
     if (!(fptr->mode & FMODE_WRITABLE)) {
-	return rb_io_close(sock, 0);
+	return rb_io_close(sock);
     }
     fptr->mode &= ~FMODE_READABLE;
 
@@ -317,7 +317,7 @@
     }
     GetOpenFile(sock, fptr);
     if (!(fptr->mode & FMODE_READABLE)) {
-	return rb_io_close(sock, 0);
+	return rb_io_close(sock);
     }
     shutdown(fptr->fd, 1);
     fptr->mode &= ~FMODE_WRITABLE;

Modified: MacRuby/trunk/file.c
===================================================================
--- MacRuby/trunk/file.c	2009-09-05 03:21:01 UTC (rev 2481)
+++ MacRuby/trunk/file.c	2009-09-05 04:49:21 UTC (rev 2482)
@@ -777,8 +777,10 @@
 static VALUE
 rb_io_stat(VALUE obj, SEL sel)
 {
+    struct rb_io_t *io = ExtractIOStruct(obj);
+    rb_io_check_closed(io);
+    
     struct stat st;
-    struct rb_io_t *io = ExtractIOStruct(obj);
     if (fstat(io->fd, &st) == -1) {
 	rb_sys_fail(RSTRING_PTR(io->path));
     }

Modified: MacRuby/trunk/include/ruby/intern.h
===================================================================
--- MacRuby/trunk/include/ruby/intern.h	2009-09-05 03:21:01 UTC (rev 2481)
+++ MacRuby/trunk/include/ruby/intern.h	2009-09-05 04:49:21 UTC (rev 2482)
@@ -390,7 +390,7 @@
 VALUE rb_io_gets(VALUE, SEL);
 VALUE rb_io_getbyte(VALUE, SEL);
 VALUE rb_io_ungetc(VALUE, SEL, VALUE);
-VALUE rb_io_close(VALUE, SEL);
+VALUE rb_io_close(VALUE);
 VALUE rb_io_flush(VALUE, SEL);
 VALUE rb_io_eof(VALUE, SEL);
 VALUE rb_io_binmode(VALUE, SEL);

Modified: MacRuby/trunk/io.c
===================================================================
--- MacRuby/trunk/io.c	2009-09-05 03:21:01 UTC (rev 2481)
+++ MacRuby/trunk/io.c	2009-09-05 04:49:21 UTC (rev 2482)
@@ -325,8 +325,10 @@
 }
 
 static void
-io_struct_close(rb_io_t *io_struct, bool close_read, bool close_write)
+io_close(rb_io_t *io_struct, bool close_read, bool close_write)
 {
+    // TODO we must check the return value of close(2) and appropriately call
+    // rb_sys_fail().
     if (close_read) {
 	if (io_struct->read_fd != io_struct->write_fd) {
 	    close(io_struct->read_fd);
@@ -1112,6 +1114,9 @@
     }
 
     const long size = FIX2LONG(len);
+    if (size < 0) {
+	rb_raise(rb_eArgError, "negative length %ld given", size);
+    }
     if (size == 0) {
 	return rb_str_new2("");
     }
@@ -1449,6 +1454,7 @@
     VALUE line = rb_io_gets_m(io, sel, argc, argv);
     while (!NIL_P(line)) {
 	rb_vm_yield(1, &line);
+	RETURN_IF_BROKEN();
 	line = rb_io_gets_m(io, sel, argc, argv);
     }
     return io;
@@ -1476,6 +1482,7 @@
     VALUE b = rb_io_getbyte(io, 0);
     while (!NIL_P(b)) {
 	rb_vm_yield(1, &b);
+	RETURN_IF_BROKEN();
 	b = rb_io_getbyte(io, 0);
     }
     return io;
@@ -1503,6 +1510,7 @@
     VALUE c = rb_io_getc(io, 0);
     while (!NIL_P(c)) {
 	rb_vm_yield(1, &c);
+	RETURN_IF_BROKEN();
 	c = rb_io_getc(io, 0);
     }
     return io;
@@ -1800,17 +1808,12 @@
     return arg;
 }
 
-static inline void
-io_close(VALUE io, bool close_read, bool close_write)
-{
-    rb_io_t *io_struct = ExtractIOStruct(io);
-    io_struct_close(io_struct, close_read, close_write);
-}
-
 static VALUE
 rb_io_close_m(VALUE io, SEL sel)
 {
-    io_close(io, true, true);
+    rb_io_t *io_s = ExtractIOStruct(io);
+    rb_io_check_closed(io_s);
+    io_close(io_s, true, true);
     return Qnil;
 }
 
@@ -1862,7 +1865,12 @@
 static VALUE
 rb_io_close_read(VALUE io, SEL sel)
 {
-    io_close(io, true, false);
+    rb_io_t *io_s = ExtractIOStruct(io);
+    rb_io_check_initialized(io_s);
+    if (io_s->read_fd == -1) {
+        rb_raise(rb_eIOError, "closed read stream");
+    }
+    io_close(io_s, true, false);
     return Qnil;
 }
 
@@ -1888,14 +1896,20 @@
 static VALUE
 rb_io_close_write(VALUE io, SEL sel)
 {
-    io_close(io, false, true);
+    rb_io_t *io_s = ExtractIOStruct(io);
+    rb_io_check_initialized(io_s);
+    if (io_s->write_fd == -1) {
+        rb_raise(rb_eIOError, "closed write stream");
+    }
+    io_close(io_s, false, true);
     return Qnil;
 }
 
 VALUE
-rb_io_close(VALUE io, SEL sel)
+rb_io_close(VALUE io)
 {
-    io_close(io, true, true);
+    rb_io_t *io_struct = ExtractIOStruct(io);
+    io_close(io_struct, true, true);
     return Qnil;
 }
 
@@ -2081,7 +2095,7 @@
     VALUE io = io_from_spawning_new_process(process_name, mode);
     if (rb_block_given_p()) {
 	VALUE ret = rb_vm_yield(1, &io);
-	rb_io_close_m(io, 0);
+	rb_io_close(io);
 	return ret;
     }
     return io;
@@ -2108,7 +2122,7 @@
     VALUE io = rb_io_s_new(klass, sel, argc, argv);
     if (rb_block_given_p()) {
         VALUE ret = rb_vm_yield(1, &io);
-        rb_io_close_m(io, 0);
+        rb_io_close(io);
         return ret;
     }
     return io;
@@ -2266,7 +2280,7 @@
 static VALUE
 f_open_ensure(VALUE io)
 {
-    rb_io_close_m(io, 0);
+    rb_io_close(io);
     return Qnil;
 }
 
@@ -2376,7 +2390,7 @@
 	}
 
 	int fd = io_s->fd;
-	io_struct_close(io_s, true, true);
+	io_close(io_s, true, true);
 	fd = dup2(other->fd, fd);
 	if (fd < 0) {
 	    rb_sys_fail("dup2() failed");
@@ -2819,7 +2833,7 @@
 static void
 rb_objc_io_finalize(void *rcv, SEL sel)
 {
-    rb_io_close((VALUE)rcv, 0);
+    rb_io_close((VALUE)rcv);
     if (rb_objc_io_finalize_super != NULL) {
 	((void(*)(void *, SEL))rb_objc_io_finalize_super)(rcv, sel);
     }
@@ -3019,7 +3033,7 @@
 		    VALUE str;
 		    int fw;
 		    if (TYPE(rb_stdout) == T_FILE && rb_stdout != orig_stdout) {
-			rb_io_close(rb_stdout, 0);
+			rb_io_close(rb_stdout);
 		    }
 		    fstat(fr, &st);
 		    if (*ARGF.inplace) {
@@ -3276,7 +3290,7 @@
 
     VALUE bstr = rb_bytestring_new();
     rb_io_read_all(ExtractIOStruct(io), bstr);
-    rb_io_close(io, 0);
+    rb_io_close(io);
 
     return bstr;
 }
@@ -3634,11 +3648,13 @@
 static VALUE
 rb_io_s_foreach(VALUE recv, SEL sel, int argc, VALUE *argv)
 {
+    RETURN_ENUMERATOR(recv, argc, argv);
     VALUE arr = rb_io_s_readlines(recv, sel, argc, argv);
     long i, count;
     for (i = 0, count = RARRAY_LEN(arr); i < count; i++) {
 	VALUE at = RARRAY_AT(arr, i);
 	rb_vm_yield(1, &at);
+	RETURN_IF_BROKEN();
     }
     return Qnil;
 }
@@ -3689,7 +3705,7 @@
 	rb_io_seek(io, offset, 0);
     }
     VALUE result = io_read(io, 0, 1, &length);
-    rb_io_close_m(io, 0);
+    rb_io_close(io);
     return result;
 }
 
@@ -4138,7 +4154,7 @@
 static VALUE
 argf_skip(VALUE argf, SEL sel)
 {
-    if (ARGF.next_p != -1) {
+    if (ARGF.init_p && ARGF.next_p == 0) {
 	argf_close(ARGF.current_file, 0);
 	ARGF.next_p = 1;
     }

Modified: MacRuby/trunk/ruby.c
===================================================================
--- MacRuby/trunk/ruby.c	2009-09-05 03:21:01 UTC (rev 2481)
+++ MacRuby/trunk/ruby.c	2009-09-05 04:49:21 UTC (rev 2482)
@@ -1164,7 +1164,7 @@
 	rb_define_global_const("DATA", f);
     }
     else if (f != rb_stdin) {
-	rb_io_close(f, 0);
+	rb_io_close(f);
     }
     return tree;
 }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090904/98b20f0a/attachment-0001.html>


More information about the macruby-changes mailing list