From aea2bbcbe50b64b9f4294002e9822e0444e2b13a Mon Sep 17 00:00:00 2001 From: Asmod4n <791770+Asmod4n@users.noreply.github.com> Date: Thu, 14 May 2026 09:53:18 +0200 Subject: [PATCH 1/2] add IO#autoclose= and IO#autoclose? --- mrbgems/mruby-io/README.md | 4 ++-- mrbgems/mruby-io/src/io.c | 47 +++++++++++++++++++++++++++++++++++++ mrbgems/mruby-io/test/io.rb | 10 ++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/mrbgems/mruby-io/README.md b/mrbgems/mruby-io/README.md index 597aeee98..390289e29 100644 --- a/mrbgems/mruby-io/README.md +++ b/mrbgems/mruby-io/README.md @@ -33,8 +33,8 @@ Add the line below to your build configuration. | IO.write | | | | IO#<< | | | | IO#advise | | | -| IO#autoclose= | | | -| IO#autoclose? | | | +| IO#autoclose= | o | | +| IO#autoclose? | o | | | IO#binmode | | | | IO#binmode? | | | | IO#bytes | | obsolete | diff --git a/mrbgems/mruby-io/src/io.c b/mrbgems/mruby-io/src/io.c index 196ab1a90..8c01c8de6 100644 --- a/mrbgems/mruby-io/src/io.c +++ b/mrbgems/mruby-io/src/io.c @@ -2205,6 +2205,51 @@ io_flush(mrb_state *mrb, mrb_value io) return io; } +/* + * call-seq: + * ios.autoclose = bool -> bool + * + * Sets the autoclose flag. + * + * If the autoclose flag is set, the underlying file descriptor(s) of +ios+ + * will be closed when +ios+ is closed (explicitly via +#close+, or implicitly + * when the +IO+ object is garbage-collected). When unset, the file + * descriptor(s) are left open. + * + * f = File.open("testfile") + * IO.for_fd(f.fileno).autoclose = false + */ +static mrb_value +io_set_autoclose(mrb_state *mrb, mrb_value io) +{ + struct mrb_io *fptr = io_get_open_fptr(mrb, io); + mrb_bool b; + + mrb_get_args(mrb, "b", &b); + fptr->close_fd = b; + fptr->close_fd2 = b; + return mrb_bool_value(b); +} + +/* + * call-seq: + * ios.autoclose? -> true or false + * + * Returns +true+ if the underlying file descriptor of +ios+ will be closed + * when +ios+ is closed ( via finalization), otherwise +false+. + * + * f = File.open("testfile") + * f.autoclose? #=> true + * f.autoclose = false + * f.autoclose? #=> false + */ +static mrb_value +io_autoclose_p(mrb_state *mrb, mrb_value io) +{ + struct mrb_io *fptr = io_get_open_fptr(mrb, io); + return mrb_bool_value(fptr->close_fd); +} + /* ---------------------------*/ static const mrb_mt_entry io_rom_entries[] = { MRB_MT_ENTRY(io_init, MRB_SYM(initialize), MRB_ARGS_ARG(1,2)), @@ -2243,6 +2288,8 @@ static const mrb_mt_entry io_rom_entries[] = { MRB_MT_ENTRY(io_pwrite, MRB_SYM(pwrite), MRB_ARGS_ANY()), /* Ruby 2.5 feature */ MRB_MT_ENTRY(io_getbyte, MRB_SYM(getbyte), MRB_ARGS_NONE()), MRB_MT_ENTRY(io_readbyte, MRB_SYM(readbyte), MRB_ARGS_NONE()), + MRB_MT_ENTRY(io_set_autoclose, MRB_SYM_E(autoclose), MRB_ARGS_REQ(1)), + MRB_MT_ENTRY(io_autoclose_p, MRB_SYM_Q(autoclose), MRB_ARGS_NONE()), }; void diff --git a/mrbgems/mruby-io/test/io.rb b/mrbgems/mruby-io/test/io.rb index 38af70e9a..2aa7480db 100644 --- a/mrbgems/mruby-io/test/io.rb +++ b/mrbgems/mruby-io/test/io.rb @@ -665,4 +665,14 @@ assert('`cmd`') do end end +assert('IO#autoclose?, IO#autoclose=') do + io = IO.new(IO.sysopen($mrbtest_io_rfname), "r") + assert_true io.autoclose? + io.autoclose = false + assert_false io.autoclose? + io.autoclose = true + assert_true io.autoclose? + io.close +end + MRubyIOTestUtil.io_test_cleanup From 9eb8865a8a35ea0209aafd5c04fa9980f667cb77 Mon Sep 17 00:00:00 2001 From: Hendrik Date: Thu, 14 May 2026 09:57:26 +0200 Subject: [PATCH 2/2] Fix documentation for autoclose? method --- mrbgems/mruby-io/src/io.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mrbgems/mruby-io/src/io.c b/mrbgems/mruby-io/src/io.c index 8c01c8de6..e38545c2c 100644 --- a/mrbgems/mruby-io/src/io.c +++ b/mrbgems/mruby-io/src/io.c @@ -2236,7 +2236,7 @@ io_set_autoclose(mrb_state *mrb, mrb_value io) * ios.autoclose? -> true or false * * Returns +true+ if the underlying file descriptor of +ios+ will be closed - * when +ios+ is closed ( via finalization), otherwise +false+. + * when +ios+ is closed, otherwise +false+. * * f = File.open("testfile") * f.autoclose? #=> true