Repository navigation
io: fd-based IO.new/IO.open/IO.for_fd mode+encoding resolution - #576
Merged
Merged
Conversation
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
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Reworks the fd-based IO constructors (
IO.new/IO.open/IO.for_fd) to follow MRI'srb_io_extract_modeenc/rb_io_extract_encoding_option(io.c, rubyv3_4_0), raising core/io conformance substantially.Before this change
IO.open(fd, …)resolved to a privateKernel#open(theFile.openpath form), soIO.open(fd)raisedno 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.openregistered 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#closeso an overridden#closeruns; a#closeexception propagates except anIOError "closed stream"(swallowed), and a block exception takes precedence over a#closeone.ioModeSpec: a String mode (via#to_str) or Integer open-flags (via#to_int); an overriding:modeoption ("mode specified twice");:binmode/:textmodewith the"specified twice"/"both … specified"conflicts;:encoding/:external_encoding/:internal_encoding("encoding specified twice", the"-"/nilno-transcode forms, the ext==int collapse, and the"Ignoring encoding parameter"override warned only when$VERBOSEis set).Errno::EINVAL; with no explicit mode the wrapper inherits the fd's half (MRI reads it viafcntl). A closed fd raisesIOError.#binmode/#binmode?reflect real binmode state (and raiseIOErroron a closed stream);#autoclose?/#autoclose=reflect the:autocloseoption.Measured impact (ruby/spec core/io)
io_newspecs:open_spec1→62,new_spec36→56,for_fd_spec36→56,binmode_spec3→7Every change verified byte-for-byte against MRI Ruby 4.0.6. New whitebox tests in
io_modeenc_internal_test.godrive 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.crb_io_s_open,rb_io_extract_modeenc,rb_io_extract_encoding_option,extract_binmode;core/io/{open,new,for_fd,binmode}_specandcore/io/shared/new.🤖 Generated with Claude Code