Skip to content

io: pread/pwrite, sysopen, dup, initialize, encodings & positioning conformance (core/io +56) - #593

Merged
tannevaled merged 4 commits into
mainfrom
wave19/io
Sep 7, 2026
Merged

tannevaled merged 4 commits into
mainfrom
wave19/io

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Advances MRI-Ruby-4.0.x conformance of the core/io surface, reading MRI io.c (v3_4_0) and the ruby/spec expectations for each method and verifying every change byte-for-byte against MRI Ruby 4.0.5. core/io ruby/spec examples: 898 → 954 passing (+56).

Methods fixed (with the reference read for each)

Descriptors & construction

  • IO#pread / IO#pwrite (rb_io_pread/rb_io_pwrite): coerce maxlen/offset with #to_int (NUM2SIZET/NUM2OFFT) and the buffer with #to_str (io_setstrbuf→StringValue); pwrite coerces its object with #to_s (rb_obj_as_string); the maxlen==0 early return leaves a given buffer's bytes untouched. pread 14→22, pwrite 5→9
  • IO.sysopen (rb_io_s_sysopen): open the path into a buffered stream, register a synthetic descriptor (so IO.for_fd can wrap it), return it; #to_path coerces the path. 0→5
  • IO#dup (rb_io_dup): an independent IO on a fresh descriptor with its own open/close state and the autoclose + close-on-exec flags always set, plus IO#close_on_exec? / #close_on_exec=. 1→7
  • IO#initialize (rb_io_initialize): reassociate the receiver with a descriptor, coercing the fd with #to_int and raising EBADF/IOError/TypeError as MRI does; the fd-adoption logic is shared with IO.for_fd/new via a new ioAdoptDescriptor helper. 1→7
  • IO#to_io (returns self), IO#path / IO#to_path (rb_io_path, incl. the <STDOUT> pseudo-paths and IO.new's path: option), IO#inspect (rb_io_inspect: #<Class:fd N> / path / (closed)). to_io 0→2, path 0→1, to_path 0→1, inspect 0→3

Encodings

  • IO#set_encoding (io_encoding_set): enforce the 1..2 arity (a trailing econv-options Hash is separated first), coerce a non-String/non-Encoding first argument with #to_str before the ext:int split, and drop an internal encoding equal to the external one (enc == enc2 ⇒ enc2 = NULL). 21→26
  • IO#ungetbyte (rb_io_ungetbyte): reduce an Integer/Bignum modulo 256 to one byte (never RangeError) and coerce any other value with #to_str. 3→6

Positioning & line numbers

  • IO#lineno / #lineno= (rb_io_lineno/rb_io_set_lineno): a real IO must be char-readable (closed/write-only raises; a StringIO does not); #lineno= is a C int, so it range-checks (RangeError for a Bignum, or an in-range Integer that overflows the int). 7→12
  • IO#pos= / IO#seek (NUM2OFFT): a Bignum too large for the off_t position raises RangeError rather than a TypeError. pos 7→8, seek 14→15
  • IO#sysread (rb_io_sysread): #to_str-coerce the buffer, return it untouched for length 0, and empty it before an EOFError. 13→16

Standard streams & misc

  • $stdout/$stderr are write-only and $stdin read-only, so a read of $stdout / a write to $stdin raises IOError, matching MRI's fd modes (this also gives ungetbyte its "not opened for reading" error).
  • IO#to_i is a true alias of IO#fileno (equal UnboundMethods); IO mixes in Enumerable. to_i 0→1, io 1→2

One pinned test (StringIO#ungetbyte([])) is updated to the MRI-verified "into String" message, since ungetbyte now coerces with StringValue.

Verification

  • go build ./..., full go test ./... (only the pre-existing 127.0.0.1:1 network tests fail), wasip1 build — all green.
  • 100% coverage on internal/vm (new whitebox io_wave19_test.go), gofmt clean.

🤖 Generated with Claude Code

tannevaled and others added 4 commits September 7, 2026 23:33
…ct (core/io)

Advance MRI-Ruby-4.0.x core/io conformance, reading MRI io.c (v3_4_0) and the
ruby/spec expectations for each method:

- IO#pread / IO#pwrite (io.c rb_io_pread/rb_io_pwrite): coerce maxlen/offset
  with #to_int (NUM2SIZET/NUM2OFFT) and the buffer with #to_str
  (io_setstrbuf → StringValue), pwrite coerces the object with #to_s
  (rb_obj_as_string); the maxlen==0 early return leaves a given buffer's bytes
  untouched. pread 14->22, pwrite 5->9.
- IO.sysopen (io.c rb_io_s_sysopen): open the path into a buffered stream,
  register a synthetic descriptor (so IO.for_fd can wrap it) and return it;
  #to_path coerces the path. sysopen 0->5.
- IO#dup (io.c rb_io_dup): an independent IO on a fresh descriptor with its own
  open/close state and the autoclose + close-on-exec flags always set, plus
  IO#close_on_exec? / #close_on_exec=. dup 1->7.
- IO#initialize (io.c rb_io_initialize): reassociate the receiver with a
  descriptor, coercing the fd with #to_int and raising EBADF/IOError/TypeError
  as MRI does; the fd-adoption logic is shared with IO.for_fd/new via a new
  ioAdoptDescriptor helper. initialize 1->7.
- IO#to_io (returns self), IO#path / IO#to_path (rb_io_path, incl. the
  "<STDOUT>" pseudo-paths and IO.new's path: option) and IO#inspect
  (rb_io_inspect, "#<Class:fd N>" / path / "(closed)"). to_io 0->2, path 0->1,
  to_path 0->1, inspect 0->3.

All changes verified byte-for-byte against MRI Ruby 4.0.5.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…re/io)

Continue MRI-Ruby-4.0.x core/io conformance, reading MRI io.c (v3_4_0) and the
ruby/spec expectations:

- IO#set_encoding (io.c io_encoding_set): enforce the 1..2 arity over the
  positional args (a trailing econv-options Hash is separated first), coerce a
  non-String/non-Encoding first argument with #to_str before the "ext:int"
  split, and drop an internal encoding equal to the external one (enc == enc2 =>
  enc2 = NULL). set_encoding 21->26.
- IO#ungetbyte (io.c rb_io_ungetbyte): reduce an Integer/Bignum modulo 256 to a
  single byte (never RangeError) and coerce any other value with #to_str
  (StringValue), so a non-coercible argument is an "into String" TypeError — the
  pinned StringIO#ungetbyte([]) test is updated to the MRI-verified message.
  ungetbyte 3->6.
- Standard streams carry their real access half: $stdout/$stderr are write-only
  and $stdin read-only, so a read of $stdout / a write to $stdin raises IOError
  ("not opened for reading"/"...writing"), matching MRI's fd modes. This also
  gives IO#ungetbyte its "not opened for reading" IOError.
- IO#to_i is a true alias of IO#fileno (equal UnboundMethods), and IO mixes in
  Enumerable. to_i 0->1, io 1->2.

100% coverage on internal/vm; all changes verified byte-for-byte against MRI
Ruby 4.0.5.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…re/io)

Continue MRI-Ruby-4.0.x core/io conformance (MRI io.c v3_4_0 + ruby/spec):

- IO#lineno / #lineno= (io.c rb_io_lineno / rb_io_set_lineno): a real IO must be
  char-readable, so a closed stream raises "closed stream" and a write-only one
  "not opened for reading"; a StringIO still tolerates both (strio has no such
  check). lineno 7->12.
- IO#pos= / IO#seek (NUM2OFFT): a Bignum too large for the off_t position raises
  RangeError "bignum too big to convert into 'long long'" rather than a
  TypeError. pos 7->8, seek 14->15.
- IO#lineno= (NUM2INT): the line number is a C int, so a Bignum raises
  "bignum too big to convert into 'long'" and an in-range Integer that still
  overflows the int raises "integer N too big to convert to 'int'".

100% coverage on internal/vm; all changes verified byte-for-byte against MRI
Ruby 4.0.5.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…ore/io)

IO#sysread (io.c rb_io_sysread), reading MRI io.c v3_4_0 + ruby/spec: coerce the
optional output buffer with #to_str (io_setstrbuf → StringValue); a zero-length
read returns the buffer untouched (or a fresh ""); and an end-of-file read
empties the given buffer (io_set_read_length to 0) before raising EOFError.
sysread 13->16 (the remaining fail needs read-buffer bookkeeping after a
buffered read).

100% coverage on internal/vm; verified byte-for-byte against MRI Ruby 4.0.5.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 1255fa4 into main Sep 7, 2026
7 checks passed
@tannevaled
tannevaled deleted the wave19/io branch September 7, 2026 23:18
tannevaled added a commit that referenced this pull request Sep 8, 2026
…594)

Wave-19 merged three bibliography-first conformance PRs:
- #593 core/io: pread/pwrite, sysopen, dup, initialize/reopen, to_io/path/
  inspect, set_encoding, ungetbyte, lineno, sysread, std-stream halves — +56.
- #591 core/file+dir: File::Stat surface, ftype, chmod/umask/truncate/rename
  errno + coercion, realpath ELOOP, Dir Enumerable + entries/children/foreach/
  each_child (fixing a nil-pointer crash) — core/file +22, core/dir +29.
- #592 core/kernel: respond_to?/eql?/remove_instance_variable mirrored on
  Kernel, then/yield_self blockless Enumerator — +8.

Measured total on merged main: 19773 passing. Local measure at TIMEOUT=25;
CI runs TIMEOUT=60 so its total is >= this floor.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant