Skip to content

io: fd-based IO.new/IO.open/IO.for_fd mode+encoding resolution - #576

Merged
tannevaled merged 1 commit into
mainfrom
wave15/io
Sep 6, 2026
Merged

tannevaled merged 1 commit into
mainfrom
wave15/io

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Summary

Reworks the fd-based IO constructors (IO.new / IO.open / IO.for_fd) to follow MRI's rb_io_extract_modeenc / rb_io_extract_encoding_option (io.c, ruby v3_4_0), raising core/io conformance substantially.

Before this change IO.open(fd, …) resolved to a private Kernel#open (the File.open path form), so IO.open(fd) raised no implicit conversion of Integer into String; IO.new's mode/encoding handling was a thin subset.

What changed (all in the wave15/io owned files)

  • IO.open registered as a public IO singleton: IO.new's fd wrapping plus the block yield + ensure-close contract (io.c rb_io_s_open). The ensure runs the Ruby-level #close so an overridden #close runs; a #close exception propagates except an IOError "closed stream" (swallowed), and a block exception takes precedence over a #close one.
  • mode/encoding decoding into an ioModeSpec: a String mode (via #to_str) or Integer open-flags (via #to_int); an overriding :mode option ("mode specified twice"); :binmode / :textmode with the "specified twice" / "both … specified" conflicts; :encoding / :external_encoding / :internal_encoding ("encoding specified twice", the "-"/nil no-transcode forms, the ext==int collapse, and the "Ignoring encoding parameter" override warned only when $VERBOSE is set).
  • descriptor wrapping: writes reach the store the fd names. An explicit mode incompatible with the fd's access half raises Errno::EINVAL; with no explicit mode the wrapper inherits the fd's half (MRI reads it via fcntl). A closed fd raises IOError.
  • #binmode / #binmode? reflect real binmode state (and raise IOError on a closed stream); #autoclose? / #autoclose= reflect the :autoclose option.

Measured impact (ruby/spec core/io)

  • shared io_new specs: open_spec 1→62, new_spec 36→56, for_fd_spec 36→56, binmode_spec 3→7
  • core/io total pass 742 → 849 (fail 279 → 230, error 238 → 180); no regressions.

Every change verified byte-for-byte against MRI Ruby 4.0.6. New whitebox tests in io_modeenc_internal_test.go drive every added branch (100% coverage on the changed code). go build ./..., go test ./internal/vm/, gofmt -l, and the wasip1 wasm build are all green.

References read: io.c rb_io_s_open, rb_io_extract_modeenc, rb_io_extract_encoding_option, extract_binmode; core/io/{open,new,for_fd,binmode}_spec and core/io/shared/new.

🤖 Generated with Claude Code

IO.open was resolving to a private Kernel#open (the File.open path form), so
IO.open(fd) raised "no implicit conversion of Integer into String", and IO.new's
mode/encoding handling was a thin subset. Rework the fd-based constructors to
follow MRI's rb_io_extract_modeenc / rb_io_extract_encoding_option (io.c v3_4_0):

- Register IO.open as a public IO singleton: IO.new's fd wrapping plus the block
  yield + ensure-close contract (io.c rb_io_s_open). The ensure runs the
  Ruby-level #close so an overridden #close runs; a #close exception propagates
  except an IOError "closed stream" (swallowed), and a block exception takes
  precedence over a #close one.
- Decode the (fd, mode, **opts) arguments into an ioModeSpec: a String mode (via
  #to_str) or Integer open-flags (via #to_int); an overriding :mode option
  ("mode specified twice" when the positional mode is also given); :binmode /
  :textmode with the "specified twice" / "both … specified" conflicts
  (extract_binmode); and :encoding / :external_encoding / :internal_encoding
  ("encoding specified twice" when the mode string already named one, the "-"
  and nil no-transcode forms, the ext==int collapse, and the "Ignoring encoding
  parameter" override warned only when $VERBOSE is set).
- Wrap the descriptor's backing buffer/path so writes reach the store the fd
  names. An explicit mode incompatible with the fd's half raises Errno::EINVAL;
  with no explicit mode the wrapper inherits the fd's half (MRI reads it via
  fcntl). A closed fd raises IOError.
- #binmode / #binmode? now reflect real binmode state (and raise IOError on a
  closed stream); #autoclose? / #autoclose= reflect the :autoclose option.

Verified byte-for-byte against MRI Ruby 4.0.6. Whitebox tests in
io_modeenc_internal_test.go drive every new branch (100% coverage).
core/io shared io_new specs: open_spec 1->62, new_spec 36->56, for_fd 36->56,
binmode_spec 3->7; core/io total pass 742->849 (fail 279->230, error 238->180).

References: io.c rb_io_s_open, rb_io_extract_modeenc, rb_io_extract_encoding_option,
extract_binmode; core/io/{open,new,for_fd,binmode}_spec and shared/new.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit e9b995b into main Sep 6, 2026
7 checks passed
@tannevaled
tannevaled deleted the wave15/io branch September 6, 2026 13:18
tannevaled added a commit that referenced this pull request Sep 6, 2026
…578)

Wave-15 merged three disjoint-file, bibliography-first conformance PRs:
- #576 core/io: fd-based IO constructors (IO.open/new/for_fd + mode/encoding/
  binmode/autoclose resolution per io.c) — core/io +107.
- #575 core/kernel: Kernel#caller_locations (unblocks the raise_spec collection
  cascade), caller start/length/range args, raise #exception protocol + empty
  bare-raise message per MRI — core/kernel +29.
- #577 core/regexp+enumerator: Regexp.linear_time?, quote/escape result
  encoding, \u-escape fixed_encoding, named_captures dup names, Lazy value
  packing — core/regexp +17, enumerator +2.

Measured total on merged main: 19179 passing (files loaded 2171, +7 that
previously failed to load). 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