From 1987cfeab6598cec1d080a08f524c46e4fad6946 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 30 Aug 2026 10:30:27 +0200 Subject: [PATCH] Add IO#readpartial (and ARGF#readpartial) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit IO#readpartial(maxlen, out = nil) reads up to maxlen bytes from the buffered stream — the leading maxlen bytes on the in-memory model — filling an optional output-buffer String and returning it. A zero maxlen yields ""; a negative one raises ArgumentError; reading at end of input raises EOFError; and a closed stream raises IOError. ARGF#readpartial delegates to the current file. Verified against ruby 4.0.6. Whole-package coverage stays at 100%; the ruby/spec floor rises to 16285 (core/argf/readpartial 0 -> 5). The IO pipe scenarios that depend on non-blocking read semantics remain a follow-up. Co-Authored-By: Claude Opus 4.8 --- internal/vm/argf.go | 1 + internal/vm/argf_test.go | 1 + internal/vm/io.go | 38 ++++++++++++++++++++++++++++++ internal/vm/io_internal_test.go | 21 +++++++++++++++++ scripts/conformance/rubyspec/FLOOR | 2 +- 5 files changed, 62 insertions(+), 1 deletion(-) diff --git a/internal/vm/argf.go b/internal/vm/argf.go index 21c61eec..51e73bfd 100644 --- a/internal/vm/argf.go +++ b/internal/vm/argf.go @@ -336,6 +336,7 @@ func (vm *VM) registerARGF() { d("tell", delegate("tell")) d("pos=", delegate("pos=")) d("seek", delegate("seek")) + d("readpartial", delegate("readpartial")) d("fileno", delegate("fileno")) d("to_i", delegate("fileno")) d("set_encoding", delegate("set_encoding")) diff --git a/internal/vm/argf_test.go b/internal/vm/argf_test.go index 13d024f0..5de21d1a 100644 --- a/internal/vm/argf_test.go +++ b/internal/vm/argf_test.go @@ -80,6 +80,7 @@ end` {with(`a = ARGF.class.new(f1); a.gets; p [a.pos, a.tell]`), "[3, 3]\n"}, {with(`a = ARGF.class.new(f1); a.gets; a.rewind; p [a.gets, a.lineno]`), "[\"l1\\n\", 1]\n"}, {with(`a = ARGF.class.new(f2); a.seek(1); p a.read`), "\"3\\n\"\n"}, + {with(`a = ARGF.class.new(f1); p a.readpartial(3)`), "\"l1\\n\"\n"}, {with(`a = ARGF.class.new(f2); a.pos = 2; p a.read`), "\"\\n\"\n"}, {with(`a = ARGF.class.new(f1); p [a.fileno.is_a?(Integer), a.to_i.is_a?(Integer)]`), "[true, true]\n"}, {with(`a = ARGF.class.new(f1); a.set_encoding("UTF-8", "ISO-8859-1"); p [a.external_encoding.to_s, a.internal_encoding.to_s]`), "[\"UTF-8\", \"ISO-8859-1\"]\n"}, diff --git a/internal/vm/io.go b/internal/vm/io.go index 63edaee9..d56d0696 100644 --- a/internal/vm/io.go +++ b/internal/vm/io.go @@ -709,6 +709,44 @@ func defStringIORead(cls *RClass) { o.pos = len(o.buf) return result(data, false) }) + // readpartial(maxlen, out = nil): read up to maxlen bytes, blocking only until + // some are available. On the in-memory buffer that is the leading maxlen bytes; + // a zero maxlen yields ""; reading at end of input raises EOFError. + cls.define("readpartial", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + o := self.(*IOObj) + ioCheckReadable(o) + o.pipeRefresh() + n := int(toInt(args[0])) + if n < 0 { + raise("ArgumentError", "negative length %d given", n) + } + var buf *object.String + if len(args) > 1 { + if b, ok := args[1].(*object.String); ok { + buf = b + } + } + fill := func(data []byte) object.Value { + if buf != nil { + if buf.Frozen { + raise("FrozenError", "can't modify frozen String: %s", buf.Inspect()) + } + buf.SetBytes(append([]byte(nil), data...)) + return buf + } + return object.NewStringBytes(append([]byte(nil), data...)) + } + if n == 0 { + return fill(nil) + } + if o.pos >= len(o.buf) { + raise("EOFError", "end of file reached") + } + end := min(o.pos+n, len(o.buf)) + data := o.buf[o.pos:end] + o.pos = end + return fill(data) + }) cls.define("getc", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { o := self.(*IOObj) ioCheckReadable(o) diff --git a/internal/vm/io_internal_test.go b/internal/vm/io_internal_test.go index 41c1dccc..ceccbbb0 100644 --- a/internal/vm/io_internal_test.go +++ b/internal/vm/io_internal_test.go @@ -60,3 +60,24 @@ func TestIOFileno(t *testing.T) { } } } + +// TestIOReadpartial covers StringIO#readpartial: reading up to a length, the +// output-buffer form, the zero-length "" case, EOFError at end of input, a +// negative length ArgumentError and IOError on a closed stream. Asserted against +// MRI Ruby 4.0.6. +func TestIOReadpartial(t *testing.T) { + cases := []struct{ src, want string }{ + {`require "stringio"; s = StringIO.new("hello world"); p [s.readpartial(5), s.readpartial(100)]`, "[\"hello\", \" world\"]\n"}, + {`require "stringio"; s = StringIO.new("abc"); buf = "x"; r = s.readpartial(2, buf); p [r, buf.equal?(r)]`, "[\"ab\", true]\n"}, + {`require "stringio"; p StringIO.new("abc").readpartial(0)`, "\"\"\n"}, + {`require "stringio"; s = StringIO.new("hi"); s.readpartial(2); begin; s.readpartial(5); rescue EOFError => e; p e.class; end`, "EOFError\n"}, + {`require "stringio"; begin; StringIO.new("x").readpartial(-1); rescue ArgumentError => e; p e.class; end`, "ArgumentError\n"}, + {`require "stringio"; s = StringIO.new("x"); s.close; begin; s.readpartial(5); rescue IOError => e; p e.class; end`, "IOError\n"}, + {`require "stringio"; s = StringIO.new("hi"); begin; s.readpartial(2, "z".freeze); rescue FrozenError => e; p e.class; end`, "FrozenError\n"}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want) + } + } +} diff --git a/scripts/conformance/rubyspec/FLOOR b/scripts/conformance/rubyspec/FLOOR index 509c88ee..540bbc28 100644 --- a/scripts/conformance/rubyspec/FLOOR +++ b/scripts/conformance/rubyspec/FLOOR @@ -1 +1 @@ -16282 +16285