From 7951d1e09103cef18fa763f985a3e5115c9ba769 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 6 Sep 2026 14:32:05 +0200 Subject: [PATCH] io: fd-based IO.new/IO.open/IO.for_fd mode+encoding resolution MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- internal/vm/io.go | 50 ++-- internal/vm/io_class.go | 346 +++++++++++++++++++++--- internal/vm/io_descriptors.go | 15 +- internal/vm/io_modeenc_internal_test.go | 227 ++++++++++++++++ 4 files changed, 582 insertions(+), 56 deletions(-) create mode 100644 internal/vm/io_modeenc_internal_test.go diff --git a/internal/vm/io.go b/internal/vm/io.go index ffb8e568..69c41046 100644 --- a/internal/vm/io.go +++ b/internal/vm/io.go @@ -14,23 +14,25 @@ import ( // in-memory byte buffer with a read/write cursor. The two share the write path // so puts/print/printf/<< work uniformly, and StringIO adds the read methods. type IOObj struct { - cls *RClass // IO, StringIO or File — so classOf/is_a? are exact - w io.Writer - buf []byte // StringIO / File content (buffered in memory) - pos int // read/write cursor - isStr bool // buffer-backed (StringIO / File) vs writer-backed (real IO) - sync bool - closed bool - label string // "STDOUT"/"STDERR"/"STDIN" for inspect - path string // backing file path for a File stream (else "") - writable bool // a File opened for writing — flush the buffer on flush/close - lineno int // #lineno — advanced by each successful line read (gets/readline) - rdClosed bool // #close_read (or a write-only mode) — reads raise "not opened for reading" - wrClosed bool // #close_write (or a read-only mode) — writes raise "not opened for writing" - appendMode bool // opened in append mode ("a"/"a+") — every write lands at end-of-buffer - extEnc string // external encoding name, "" ⇒ Encoding.default_external - intEnc string // internal encoding name, "" ⇒ none (nil) - fd int // synthetic file descriptor for #fileno (0 ⇒ not yet assigned) + cls *RClass // IO, StringIO or File — so classOf/is_a? are exact + w io.Writer + buf []byte // StringIO / File content (buffered in memory) + pos int // read/write cursor + isStr bool // buffer-backed (StringIO / File) vs writer-backed (real IO) + sync bool + closed bool + label string // "STDOUT"/"STDERR"/"STDIN" for inspect + path string // backing file path for a File stream (else "") + writable bool // a File opened for writing — flush the buffer on flush/close + lineno int // #lineno — advanced by each successful line read (gets/readline) + rdClosed bool // #close_read (or a write-only mode) — reads raise "not opened for reading" + wrClosed bool // #close_write (or a read-only mode) — writes raise "not opened for writing" + appendMode bool // opened in append mode ("a"/"a+") — every write lands at end-of-buffer + extEnc string // external encoding name, "" ⇒ Encoding.default_external + intEnc string // internal encoding name, "" ⇒ none (nil) + fd int // synthetic file descriptor for #fileno (0 ⇒ not yet assigned) + binmode bool // opened in binary mode ("b"/binmode:) — #binmode? is true + noAutoclose bool // autoclose: false was requested — #autoclose? is false // strObj is the live String object backing a StringIO — MRI's StringIO holds // (and mutates in place) the very String passed to it, so #string returns that @@ -647,7 +649,18 @@ func defIOWrite(cls *RClass) { cls.define("tty?", func(_ *VM, _ object.Value, _ []object.Value, _ *Proc) object.Value { return object.Bool(false) }) // #isatty is a true alias of #tty?, as in MRI. cls.methods["isatty"] = cls.methods["tty?"] - cls.define("binmode", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { return self }) + // IO#binmode marks the stream binary (#binmode? true) and, as MRI's + // rb_io_ascii8bit_binmode does, sets the external encoding to ASCII-8BIT and + // clears the internal encoding. + cls.define("binmode", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + o := self.(*IOObj) + if o.closed { + raise("IOError", "closed stream") + } + o.binmode = true + o.extEnc, o.intEnc = "ASCII-8BIT", "" + return self + }) // external_encoding: the stream's external encoding — the one set explicitly // (at creation or via #set_encoding), else Encoding.default_external. @@ -1087,6 +1100,7 @@ func defStringIOExtra(vm *VM, cls *RClass) { // any internal encoding, and returns self. cls.define("binmode", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { o := self.(*IOObj) + o.binmode = true o.extEnc, o.intEnc = "ASCII-8BIT", "" return self }) diff --git a/internal/vm/io_class.go b/internal/vm/io_class.go index d1971ffc..5aa1c164 100644 --- a/internal/vm/io_class.go +++ b/internal/vm/io_class.go @@ -5,6 +5,7 @@ package vm import ( + "fmt" "os" "strings" @@ -31,24 +32,68 @@ func (vm *VM) registerIOClassMethods(cIO, cFile *RClass) { raise("ArgumentError", "wrong number of arguments (given %d, expected 1..2)", len(pos)) } fd := int(vm.repeatLong(pos[0])) - if _, ok := vm.fdTable[fd]; !ok { + src, ok := vm.fdTable[fd] + if !ok { raise("Errno::EBADF", "Bad file descriptor - fd %d", fd) } - mode := "" - if len(pos) >= 2 && !object.IsNil(pos[1]) { - mode = strArg(pos[1]) + if src.closed { + raise("IOError", "closed stream") } - if opts != nil { - if m, ok := opts.Get(object.Symbol("mode")); ok && !object.IsNil(m) { - mode = strArg(m) + ms := vm.ioResolveModeEnc(pos, opts) + // Wrap the underlying descriptor's buffer/path so writes reach the same + // backing store the fd names (MRI shares the OS fd). The mode governs the + // read/write intent and encoding of the fresh wrapper. + res := &IOObj{cls: cIO, isStr: true, buf: src.buf, path: src.path, + binmode: ms.binmode, noAutoclose: ms.noAutoclose, + extEnc: ms.extEnc, intEnc: ms.intEnc} + if ms.explicit { + // An explicit mode must be compatible with the descriptor's current mode + // (io.c io_reopen / rb_update_max_fd path: EINVAL when e.g. a write-only fd + // is opened for reading). + if (ms.readable && src.rdClosed) || (ms.writable && src.wrClosed) { + raise("Errno::EINVAL", "Invalid argument") } + res.writable, res.appendMode = ms.writable, ms.appendMode + res.rdClosed, res.wrClosed = !ms.readable, !ms.writable + } else { + // No explicit mode: the wrapper inherits the descriptor's actual access + // half (MRI derives it from the fd via fcntl(F_GETFL)). + res.writable, res.appendMode = src.writable, src.appendMode + res.rdClosed, res.wrClosed = src.rdClosed, src.wrClosed + } + if res.appendMode { + res.pos = len(res.buf) } - res := &IOObj{cls: cIO, isStr: true, writable: strings.ContainsAny(mode, "wa+")} - vm.applyIOMode(res, mode, opts) return res } cIO.smethods["for_fd"] = &Method{name: "for_fd", owner: cIO, native: forFd} cIO.smethods["new"] = &Method{name: "new", owner: cIO, native: forFd} + // IO.open(fd, mode = "r", **opts) is IO.new plus a block: it yields the wrapped + // stream then, in an ensure, closes it — via the Ruby-level #close so an + // overridden #close runs (io.c rb_io_s_open + io_close). The block's value is + // returned. A #close that raises propagates, EXCEPT an IOError whose message is + // "closed stream", which is swallowed (and leaves no last error); when both the + // block and #close raise, the block's exception is the one that propagates. + cIO.smethods["open"] = &Method{name: "open", owner: cIO, native: func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { + oVal := forFd(vm, self, args, nil) + o, ok := oVal.(*IOObj) + if !ok || blk == nil { + return oVal + } + var ret object.Value + blockRec := recoverAny(func() { ret = vm.callBlock(blk, []object.Value{o}) }) + closeRec := recoverAny(func() { vm.send(o, "close", nil, nil) }) + if re, isRE := closeRec.(RubyError); isRE && re.Class == "IOError" && re.Message == "closed stream" { + closeRec = nil // MRI ignores a "closed stream" IOError from the ensure close + } + if blockRec != nil { + panic(blockRec) // the block's exception (or break/return) is primary + } + if closeRec != nil { + panic(closeRec) + } + return ret + }} def("read", func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { return vm.ioReadFile(args, false) }) @@ -394,40 +439,275 @@ func encOpt(opts *object.Hash) (object.Value, bool) { return nil, false } -// applyIOMode sets the external/internal encoding of a stream from a mode string -// ("w:ext[:int]") and/or the :external_encoding / :internal_encoding / :encoding -// options, canonicalising each name. -func (vm *VM) applyIOMode(o *IOObj, mode string, opts *object.Hash) { - if i := strings.IndexByte(mode, ':'); i >= 0 { - enc := mode[i+1:] - if j := strings.IndexByte(enc, ':'); j >= 0 { - o.extEnc = vm.lookupEncodingName(enc[:j]).name - o.intEnc = vm.lookupEncodingName(enc[j+1:]).name - } else if enc != "" { - o.extEnc = vm.lookupEncodingName(enc).name +// ioModeSpec is the decoded access mode + encoding of an IO.new/IO.open/IO.for_fd +// request: the read/write/append/binmode intent plus the resolved external and +// internal encoding names ("" ⇒ default/none). It mirrors MRI's +// rb_io_extract_modeenc bookkeeping (io.c). +type ioModeSpec struct { + readable, writable, appendMode, binmode, textmode, noAutoclose bool + extEnc, intEnc string + hasEnc bool // the mode STRING carried a ":enc" suffix + explicit bool // a mode argument (or :mode option) was given +} + +// ioResolveModeEnc decodes the (fd, mode, **opts) arguments of IO.new / IO.open +// into an ioModeSpec, following io.c rb_io_extract_modeenc: a String or Integer +// mode argument, an overriding :mode option (an error if the positional mode is +// also given), :binmode / :textmode with their "specified twice" / "both …" +// conflicts, and the :encoding / :external_encoding / :internal_encoding options +// (an error when the mode string already named an encoding). +func (vm *VM) ioResolveModeEnc(pos []object.Value, opts *object.Hash) ioModeSpec { + var ms ioModeSpec + vmodeGiven := false + if len(pos) >= 2 && !object.IsNil(pos[1]) { + vm.resolveVmode(pos[1], &ms) + vmodeGiven = true + } + if opts != nil { + if m, ok := opts.Get(object.Symbol("mode")); ok && !object.IsNil(m) { + if vmodeGiven { + raise("ArgumentError", "mode specified twice") + } + vm.resolveVmode(m, &ms) + vmodeGiven = true } } - if opts == nil { + ms.explicit = vmodeGiven + if !vmodeGiven { + ms.readable = true // no explicit mode ⇒ the wrapper inherits the fd's mode + } + if opts != nil { + extractBinmode(opts, &ms) + } + // A binary stream defaults to ASCII-8BIT external encoding unless the mode + // string already named one (a later :encoding option may still override it). + if ms.binmode && !ms.hasEnc && ms.extEnc == "" { + ms.extEnc = "ASCII-8BIT" + } + if opts != nil { + if vm.extractEncodingOption(opts, &ms) && ms.hasEnc { + raise("ArgumentError", "encoding specified twice") + } + if v, ok := opts.Get(object.Symbol("autoclose")); ok { + ms.noAutoclose = !v.Truthy() + } + } + return ms +} + +// resolveVmode decodes a single mode argument (the positional mode or a :mode +// option) into ms: an Integer (via #to_int) is an open-flag set; otherwise the +// value is coerced with #to_str and parsed as an fopen-style mode string. +func (vm *VM) resolveVmode(v object.Value, ms *ioModeSpec) { + if i, ok := v.(object.Integer); ok { + flagsModeSpec(int64(i), ms) + return + } + if vm.respondsToDynamic(v, "to_int") { + flagsModeSpec(vm.repeatLong(v), ms) return } - if v, ok := opts.Get(object.Symbol("external_encoding")); ok && !object.IsNil(v) { - o.extEnc = vm.encodingArg(v).name + s, ok := v.(*object.String) + if !ok && vm.respondsToDynamic(v, "to_str") { + s, ok = vm.send(v, "to_str", nil, nil).(*object.String) + } + if !ok { + raise("TypeError", "no implicit conversion of %s into String", classNameOf(v)) + } + vm.parseModeString(s.Str(), ms) +} + +// flagsModeSpec fills the read/write/append intent of ms from an integer open-flag +// set (a bitwise OR of File::RDONLY/WRONLY/RDWR/APPEND). +func flagsModeSpec(flags int64, ms *ioModeSpec) { + switch flags & 0x3 { + case fO_RDONLY: + ms.readable = true + case fO_WRONLY: + ms.writable = true + default: // RDWR + ms.readable, ms.writable = true, true + } + if flags&fO_APPEND != 0 { + ms.writable, ms.appendMode = true, true + } +} + +// parseModeString fills ms from an fopen-style mode string ("r"/"w"/"a" with an +// optional "+"/"b"/"t" and a trailing ":ext[:int]" encoding), matching MRI's +// rb_io_modestr_fmode + parse_mode_enc. An empty or malformed base raises +// ArgumentError. +func (vm *VM) parseModeString(mode string, ms *ioModeSpec) { + base := mode + if i := strings.IndexByte(mode, ':'); i >= 0 { + base, ms.hasEnc = mode[:i], true + vm.parseEncPart(mode[i+1:], ms) + } + if base == "" { + raise("ArgumentError", "invalid access mode %s", mode) + } + switch base[0] { + case 'r': + ms.readable = true + case 'w': + ms.writable = true + case 'a': + ms.writable, ms.appendMode = true, true + default: + raise("ArgumentError", "invalid access mode %s", mode) + } + for _, c := range base[1:] { + switch c { + case '+': + ms.readable, ms.writable = true, true + case 'b': + if ms.textmode { + raise("ArgumentError", "invalid access mode %s", mode) + } + ms.binmode = true + case 't': + if ms.binmode { + raise("ArgumentError", "invalid access mode %s", mode) + } + ms.textmode = true + case 'x': // exclusive-create flag; no effect on the fmode intent + default: + raise("ArgumentError", "invalid access mode %s", mode) + } + } +} + +// parseEncPart resolves the "ext[:int]" encoding suffix of a mode string into +// ms.extEnc / ms.intEnc. An "-" internal encoding (or one equal to the external) +// leaves the internal encoding unset (no transcoding). +func (vm *VM) parseEncPart(enc string, ms *ioModeSpec) { + ext, intn := enc, "" + if j := strings.IndexByte(enc, ':'); j >= 0 { + ext, intn = enc[:j], enc[j+1:] + } + if ext != "" { + ms.extEnc = vm.lookupEncodingName(ext).name + } + if intn != "" && intn != "-" { + ms.intEnc = vm.lookupEncodingName(intn).name + if ms.intEnc == ms.extEnc { + ms.intEnc = "" + } } - if v, ok := opts.Get(object.Symbol("internal_encoding")); ok && !object.IsNil(v) { - o.intEnc = vm.encodingArg(v).name +} + +// extractBinmode applies the :textmode / :binmode options to ms, raising the +// ArgumentError conflicts MRI's extract_binmode does ("textmode specified twice", +// "binmode specified twice", "both textmode and binmode specified"). +func extractBinmode(opts *object.Hash, ms *ioModeSpec) { + if v, ok := opts.Get(object.Symbol("textmode")); ok && !object.IsNil(v) { + if ms.textmode { + raise("ArgumentError", "textmode specified twice") + } + if ms.binmode { + raise("ArgumentError", "both textmode and binmode specified") + } + if v.Truthy() { + ms.textmode = true + } + } + if v, ok := opts.Get(object.Symbol("binmode")); ok && !object.IsNil(v) { + if ms.binmode { + raise("ArgumentError", "binmode specified twice") + } + if ms.textmode { + raise("ArgumentError", "both textmode and binmode specified") + } + if v.Truthy() { + ms.binmode = true + } } - if v, ok := opts.Get(object.Symbol("encoding")); ok && !object.IsNil(v) { - // A String :encoding may be a combined "external:internal"; an Encoding is a - // single external encoding. - if s, isStr := v.(*object.String); isStr { + // (MRI's extract_binmode has a final "both … specified" guard here; it is + // unreachable in rbgo because the two branches above already reject every mode + // string / option combination that could set both flags.) +} + +// extractEncodingOption applies the :encoding / :external_encoding / +// :internal_encoding options to ms, following io.c rb_io_extract_encoding_option: +// a present :external_encoding or :internal_encoding makes a sibling :encoding be +// ignored (with a warning when $VERBOSE is set); an internal encoding of nil or +// "-" — or one equal to the external — leaves the internal encoding unset. It +// returns whether any encoding was extracted. +func (vm *VM) extractEncodingOption(opts *object.Hash, ms *ioModeSpec) bool { + encV, hasEnc := opts.Get(object.Symbol("encoding")) + if hasEnc && object.IsNil(encV) { + hasEnc = false + } + extV, hasExt := opts.Get(object.Symbol("external_encoding")) + if hasExt && object.IsNil(extV) { + hasExt = false + } + intV, hasInt := opts.Get(object.Symbol("internal_encoding")) + if (hasExt || hasInt) && hasEnc { + which := "external" + if !hasExt { + which = "internal" + } + vm.rbWarn("Ignoring encoding parameter '%s': %s_encoding is used", vm.displayStr(encV), which) + hasEnc = false + } + if hasExt { + ms.extEnc = vm.encodingArg(extV).name + } + if hasInt { + switch { + case object.IsNil(intV), isDashString(intV): + ms.intEnc = "" + default: + ms.intEnc = vm.encodingArg(intV).name + } + if ms.intEnc != "" && ms.intEnc == ms.extEnc { + ms.intEnc = "" + } + } + if hasEnc { + if s, ok := encV.(*object.String); ok { if j := strings.IndexByte(s.Str(), ':'); j >= 0 { - o.extEnc = vm.lookupEncodingName(s.Str()[:j]).name - o.intEnc = vm.lookupEncodingName(s.Str()[j+1:]).name - return + ms.extEnc = vm.lookupEncodingName(s.Str()[:j]).name + ms.intEnc = vm.lookupEncodingName(s.Str()[j+1:]).name + if ms.intEnc == ms.extEnc { + ms.intEnc = "" + } + return true } } - o.extEnc = vm.encodingArg(v).name + ms.extEnc = vm.encodingArg(encV).name + return true + } + return hasExt || hasInt +} + +// recoverAny runs fn and returns whatever it panics with (a RubyError, or a +// break/return/throw control signal), or nil when it returns normally. It lets a +// caller run an ensure step (IO.open's close) before re-raising the first panic. +func recoverAny(fn func()) (rec any) { + defer func() { rec = recover() }() + fn() + return nil +} + +// isDashString reports whether v is the String "-" (the internal_encoding value +// MRI treats as "no transcoding"). +func isDashString(v object.Value) bool { + s, ok := v.(*object.String) + return ok && s.Str() == "-" +} + +// rbWarn emits an MRI rb_warn-style warning line to the current $stderr, but only +// when $VERBOSE is non-nil (false or true) — the gate rb_warn applies. Writing to +// curStderr (rather than Kernel#warn) keeps the warning off stdout while still +// honouring a $stderr reassigned to a StringIO (mspec's `complain` matcher). Used +// for the encoding options that IO.new silently overrides. +func (vm *VM) rbWarn(format string, a ...any) { + if object.IsNil(vm.globals["$VERBOSE"]) { + return } + vm.curStderr().writeStr(fmt.Sprintf(format, a...) + "\n") } // ioOptEncoding returns the canonical encoding name selected by the :encoding / diff --git a/internal/vm/io_descriptors.go b/internal/vm/io_descriptors.go index 2f5c6348..dd6555db 100644 --- a/internal/vm/io_descriptors.go +++ b/internal/vm/io_descriptors.go @@ -228,13 +228,18 @@ func defIOSeekable(cls *RClass) { } return object.IntValue(int64(o.pos)) }) - cls.define("binmode?", func(_ *VM, _ object.Value, _ []object.Value, _ *Proc) object.Value { - return object.Bool(false) + cls.define("binmode?", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + o := self.(*IOObj) + if o.closed { + raise("IOError", "closed stream") + } + return object.Bool(o.binmode) }) - cls.define("autoclose?", func(_ *VM, _ object.Value, _ []object.Value, _ *Proc) object.Value { - return object.Bool(true) + cls.define("autoclose?", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + return object.Bool(!self.(*IOObj).noAutoclose) }) - cls.define("autoclose=", func(_ *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { + cls.define("autoclose=", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + self.(*IOObj).noAutoclose = !args[0].Truthy() return args[0] }) cls.define("fdatasync", func(_ *VM, _ object.Value, _ []object.Value, _ *Proc) object.Value { diff --git a/internal/vm/io_modeenc_internal_test.go b/internal/vm/io_modeenc_internal_test.go new file mode 100644 index 00000000..a656b2c5 --- /dev/null +++ b/internal/vm/io_modeenc_internal_test.go @@ -0,0 +1,227 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm + +import ( + "fmt" + "testing" +) + +// ioFdProg wraps a Ruby body with a tmpdir that exposes three live synthetic +// descriptors: `wo` (write-only), `ro` (read-only, pre-filled), and `rw` +// (read+write). Used to drive IO.new / IO.open / IO.for_fd mode-and-encoding +// resolution against real fds. +func ioFdProg(body string) string { + return `require "tmpdir" +Dir.mktmpdir do |d| + File.write(File.join(d, "ro"), "read-data") + wo = File.open(File.join(d, "wo"), "w").fileno + ro = File.open(File.join(d, "ro"), "r").fileno + rw = File.open(File.join(d, "rw"), "w+").fileno + ` + body + ` +end` +} + +// TestIOResolveModeEnc covers ioResolveModeEnc and its helpers (resolveVmode, +// flagsModeSpec, parseModeString, parseEncPart) via IO.new: integer/string mode +// coercion, the fopen mode grammar, and the ":ext:int" encoding suffix. Asserted +// against MRI Ruby 4.0.6. +func TestIOResolveModeEnc(t *testing.T) { + cases := []struct{ body, want string }{ + // Integer open-flag modes (flagsModeSpec): WRONLY, RDONLY, RDWR, APPEND. + {`p IO.new(wo, File::WRONLY).instance_of?(IO)`, "true\n"}, + {`p IO.new(ro, File::RDONLY).instance_of?(IO)`, "true\n"}, + {`p IO.new(rw, File::RDWR).instance_of?(IO)`, "true\n"}, + {`p IO.new(wo, File::WRONLY | File::APPEND).instance_of?(IO)`, "true\n"}, + // #to_int coercion of the mode argument. + {`m = Object.new; def m.to_int; File::WRONLY; end; p IO.new(wo, m).instance_of?(IO)`, "true\n"}, + // #to_str coercion of the mode argument. + {`m = Object.new; def m.to_str; "w"; end; p IO.new(wo, m).instance_of?(IO)`, "true\n"}, + // :mode option alone (no positional mode). + {`p IO.new(wo, mode: "w").instance_of?(IO)`, "true\n"}, + // The fopen mode grammar: "+", "x" (exclusive flag, no fmode effect), append. + {`p IO.new(rw, "w+").instance_of?(IO)`, "true\n"}, + {`p IO.new(wo, "wx").instance_of?(IO)`, "true\n"}, + {`p IO.new(wo, "a").instance_of?(IO)`, "true\n"}, + // Encoding suffix: ext only, ext:int, "-" internal, and int==ext collapse. + {`io = IO.new(wo, "w:utf-8"); p io.external_encoding.to_s`, "\"UTF-8\"\n"}, + {`io = IO.new(wo, "w:utf-8:-"); p io.internal_encoding.to_s`, "\"\"\n"}, + {`io = IO.new(wo, "w:utf-8:utf-8"); p io.internal_encoding.to_s`, "\"\"\n"}, + } + for _, c := range cases { + if got := eval(t, ioFdProg(c.body)); got != c.want { + t.Errorf("body=%q got=%q want=%q", c.body, got, c.want) + } + } + // TypeError for a mode that is neither Integer- nor String-coercible. + if cls, _ := evalErr(t, ioFdProg(`IO.new(wo, Object.new)`)); cls != "TypeError" { + t.Errorf("non-coercible mode: got %q want TypeError", cls) + } + // Malformed mode strings raise ArgumentError (empty base, bad first char, bad + // flag char). + for _, body := range []string{`IO.new(wo, "")`, `IO.new(wo, "z")`, `IO.new(wo, "wq")`} { + if cls, _ := evalErr(t, ioFdProg(body)); cls != "ArgumentError" { + t.Errorf("body=%q: got %q want ArgumentError", body, cls) + } + } +} + +// TestIOModeEncErrors covers the ArgumentError conflicts ioResolveModeEnc raises: +// mode/encoding specified twice and the binmode/textmode clashes (extractBinmode), +// plus the b/t clash inside a single mode string. +func TestIOModeEncErrors(t *testing.T) { + cases := []struct{ body, msg string }{ + {`IO.new(wo, "w", mode: "w")`, "mode specified twice"}, + {`IO.new(wo, "w:utf-8", encoding: "utf-8")`, "encoding specified twice"}, + {`IO.new(wo, "wt", textmode: true)`, "textmode specified twice"}, + {`IO.new(wo, "wb", binmode: true)`, "binmode specified twice"}, + {`IO.new(wo, "wb", textmode: true)`, "both textmode and binmode specified"}, + {`IO.new(wo, "wt", binmode: true)`, "both textmode and binmode specified"}, + {`IO.new(wo, "w", textmode: true, binmode: true)`, "both textmode and binmode specified"}, + {`IO.new(wo, "wbt")`, "invalid access mode wbt"}, + {`IO.new(wo, "wtb")`, "invalid access mode wtb"}, + } + for _, c := range cases { + cls, msg := evalErr(t, ioFdProg(c.body)) + if cls != "ArgumentError" || msg != c.msg { + t.Errorf("body=%q: got %s/%q want ArgumentError/%q", c.body, cls, msg, c.msg) + } + } +} + +// TestIOBinmodeTextmodeAutoclose covers the :binmode / :textmode / :autoclose +// options and the #binmode / #binmode? / #autoclose? descriptor state they drive. +func TestIOBinmodeTextmodeAutoclose(t *testing.T) { + cases := []struct{ body, want string }{ + // binmode from a mode string and from the :binmode option; the default false. + {`io = IO.new(wo, "wb"); p [io.binmode?, io.external_encoding.to_s]`, "[true, \"ASCII-8BIT\"]\n"}, + {`io = IO.new(wo, "w", binmode: true); p io.binmode?`, "true\n"}, + {`p IO.new(wo, "w").binmode?`, "false\n"}, + // A present-but-falsy :binmode / :textmode does not set the flag. + {`p IO.new(wo, "w", binmode: false).binmode?`, "false\n"}, + {`p IO.new(wo, "w", textmode: false).instance_of?(IO)`, "true\n"}, + {`p IO.new(wo, "w", textmode: true).instance_of?(IO)`, "true\n"}, + // #binmode marks the stream binary and sets ASCII-8BIT. + {`io = IO.new(wo, "w"); io.binmode; p [io.binmode?, io.external_encoding.to_s]`, "[true, \"ASCII-8BIT\"]\n"}, + // :autoclose option and the #autoclose? / #autoclose= accessors. + {`p IO.new(wo, "w").autoclose?`, "true\n"}, + {`p IO.new(wo, "w", autoclose: false).autoclose?`, "false\n"}, + {`p IO.new(wo, "w", autoclose: 42).autoclose?`, "true\n"}, + {`io = IO.new(wo, "w"); io.autoclose = false; a = io.autoclose?; io.autoclose = true; p [a, io.autoclose?]`, "[false, true]\n"}, + // StringIO#binmode sets the binary flag too (and returns self). + {`io = StringIO.new("x"); p io.binmode.class`, "StringIO\n"}, + } + for _, c := range cases { + if got := eval(t, ioFdProg(c.body)); got != c.want { + t.Errorf("body=%q got=%q want=%q", c.body, got, c.want) + } + } + // #binmode and #binmode? raise IOError on a closed stream. + for _, body := range []string{ + `io = IO.new(wo, "w"); io.close; io.binmode`, + `io = IO.new(wo, "w"); io.close; io.binmode?`, + } { + cls, msg := evalErr(t, ioFdProg(body)) + if cls != "IOError" || msg != "closed stream" { + t.Errorf("body=%q: got %s/%q want IOError/closed stream", body, cls, msg) + } + } +} + +// TestIOEncodingOptions covers extractEncodingOption / isDashString / rbWarn: the +// :external_encoding / :internal_encoding / :encoding options, the nil and "-" +// internal-encoding forms, the int==ext collapse, and the "Ignoring encoding +// parameter" override (whose external/internal wording is exercised both with +// $VERBOSE set, so the warning fires, and unset, so rbWarn early-returns). +func TestIOEncodingOptions(t *testing.T) { + cases := []struct{ body, want string }{ + {`io = IO.new(wo, "w", external_encoding: "utf-8", internal_encoding: "ibm866"); p [io.external_encoding.to_s, io.internal_encoding.to_s]`, "[\"UTF-8\", \"IBM866\"]\n"}, + {`io = IO.new(wo, "w", internal_encoding: "-"); p io.internal_encoding.to_s`, "\"\"\n"}, + {`io = IO.new(wo, "w", internal_encoding: nil); p io.internal_encoding.to_s`, "\"\"\n"}, + // A nil :encoding / :external_encoding is treated as absent (not an error). + {`p IO.new(wo, "w", encoding: nil).instance_of?(IO)`, "true\n"}, + {`p IO.new(wo, "w", external_encoding: nil).instance_of?(IO)`, "true\n"}, + {`io = IO.new(wo, "w", external_encoding: "utf-8", internal_encoding: "utf-8"); p io.internal_encoding.to_s`, "\"\"\n"}, + {`io = IO.new(wo, "w", encoding: "utf-8:utf-8"); p io.internal_encoding.to_s`, "\"\"\n"}, + // :encoding is ignored when :external_encoding is present (external wording). + {`io = IO.new(wo, "w", external_encoding: "ibm866", encoding: "utf-8"); p io.external_encoding.to_s`, "\"IBM866\"\n"}, + {`$VERBOSE = true; io = IO.new(wo, "w", external_encoding: "ibm866", encoding: "utf-8"); p io.external_encoding.to_s`, "Ignoring encoding parameter 'utf-8': external_encoding is used\n\"IBM866\"\n"}, + // :encoding is ignored when :internal_encoding is present (internal wording). + {`$VERBOSE = true; io = IO.new(wo, "w", internal_encoding: "ibm866", encoding: "utf-8"); p io.internal_encoding.to_s`, "Ignoring encoding parameter 'utf-8': internal_encoding is used\n\"IBM866\"\n"}, + } + for _, c := range cases { + if got := eval(t, ioFdProg(c.body)); got != c.want { + t.Errorf("body=%q got=%q want=%q", c.body, got, c.want) + } + } +} + +// TestIOForFdModeCompat covers the descriptor-compatibility logic in forFd: an +// explicit mode incompatible with the fd's current access half raises +// Errno::EINVAL; no explicit mode inherits the fd's half; and a closed fd raises +// IOError. +func TestIOForFdModeCompat(t *testing.T) { + // Explicit read on a write-only fd, and explicit write on a read-only fd, EINVAL. + for _, body := range []string{`IO.new(wo, "r")`, `IO.new(ro, "w")`} { + if cls, _ := evalErr(t, ioFdProg(body)); cls != "Errno::EINVAL" { + t.Errorf("body=%q: got %q want Errno::EINVAL", body, cls) + } + } + // No explicit mode inherits the descriptor's half: a wrapper of a write-only fd + // writes but is not readable. + if got := eval(t, ioFdProg(`io = IO.new(wo); n = io.write("hi"); r = (io.read rescue $!.message); p [n, r]`)); got != "[2, \"not opened for reading\"]\n" { + t.Errorf("inherit write-only: got %q", got) + } + // A closed descriptor raises IOError when re-wrapped. + cls, msg := evalErr(t, `require "tmpdir" +Dir.mktmpdir do |d| + f = File.open(File.join(d, "c"), "w") + fd = f.fileno + f.close + IO.new(fd, "w") +end`) + if cls != "IOError" || msg != "closed stream" { + t.Errorf("closed fd: got %s/%q want IOError/closed stream", cls, msg) + } +} + +// TestIOOpenBlock covers IO.open's block form and its ensure-close propagation +// (recoverAny): the block value is returned, #close runs even when the block +// raises, a #close exception propagates, a block exception takes precedence over +// a #close exception, and an IOError "closed stream" from #close is swallowed. +func TestIOOpenBlock(t *testing.T) { + cases := []struct{ body, want string }{ + // Block value is returned; no-block form returns the IO. + {`p IO.open(wo, "w") { |io| 42 }`, "42\n"}, + {`p IO.open(wo, "w").instance_of?(IO)`, "true\n"}, + // #close runs even when the block raises. + {`$ran = false; (IO.open(wo, "w") { |io| def io.close; $ran = true; end; raise "blk" } rescue nil); p $ran`, "true\n"}, + // A "closed stream" IOError from #close is swallowed; the block value returns. + {`p(IO.open(wo, "w") { |io| def io.close; raise IOError, "closed stream"; end; 7 })`, "7\n"}, + } + for _, c := range cases { + if got := eval(t, ioFdProg(c.body)); got != c.want { + t.Errorf("body=%q got=%q want=%q", c.body, got, c.want) + } + } + // A non-"closed stream" #close exception propagates. + if cls, msg := evalErr(t, ioFdProg(`IO.open(wo, "w") { |io| def io.close; raise "boom"; end }`)); cls != "RuntimeError" || msg != "boom" { + t.Errorf("close raise: got %s/%q want RuntimeError/boom", cls, msg) + } + // The block's exception takes precedence over a #close exception. + if cls, msg := evalErr(t, ioFdProg(`IO.open(wo, "w") { |io| def io.close; raise "fromclose"; end; raise "fromblock" }`)); cls != "RuntimeError" || msg != "fromblock" { + t.Errorf("block precedence: got %s/%q want RuntimeError/fromblock", cls, msg) + } +} + +// TestIORbWarnSilentByDefault confirms rbWarn's $VERBOSE gate: with $VERBOSE nil +// (the default) the ignored-encoding override is silent, yet still resolves the +// external encoding to the winning option. +func TestIORbWarnSilentByDefault(t *testing.T) { + got := eval(t, ioFdProg(`io = IO.new(wo, "w", external_encoding: "ibm866", encoding: "utf-8"); p [$VERBOSE, io.external_encoding.to_s]`)) + if want := fmt.Sprintf("[nil, %q]\n", "IBM866"); got != want { + t.Errorf("silent warn: got %q want %q", got, want) + } +}