From 8f2bf43c51482dad7c9a2f5252c9ef25a14589d2 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 28 Nov 2016 14:22:12 -0800 Subject: [PATCH] Passing `binmode: true` to `IO.pipe` should behave like `binmode` When passing `binmode: true` to `IO.pipe`, it should behave the same way as calling `binmode` on each of the file handles. It should set the file to binmode *and* set the encoding to binary on the file. Before this commit, passing `binmode: true` to `IO.pipe` would make `binmode?` return `true`, but the file's encoding would remain the same as the default encoding. Passing `binmode: true` should make `binmode?` return `true` *and* set the encoding to binary. --- io.c | 6 ++++++ test/ruby/test_io.rb | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/io.c b/io.c index 1f00f6c..83670d3 100644 --- a/io.c +++ b/io.c @@ -9692,6 +9692,12 @@ rb_io_s_pipe(int argc, VALUE *argv, VALUE klass) rb_io_synchronized(fptr2); extract_binmode(opt, &fmode); + + if (fmode & FMODE_BINMODE) { + rb_io_ascii8bit_binmode(r); + rb_io_ascii8bit_binmode(w); + } + #if DEFAULT_TEXTMODE if ((fptr->mode & FMODE_TEXTMODE) && (fmode & FMODE_BINMODE)) { fptr->mode &= ~FMODE_TEXTMODE; diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb index a5cdc7b..26f8dd5 100644 --- a/test/ruby/test_io.rb +++ b/test/ruby/test_io.rb @@ -108,6 +108,38 @@ def test_pipe ].each{|thr| thr.join} end + def test_binmode_writer + EnvUtil.with_default_internal(Encoding::UTF_8) do + reader, writer = IO.pipe + reader.binmode + writer.binmode + + reader2, writer2 = IO.pipe(binmode: true) + + assert writer.binmode? + assert writer2.binmode? + assert_equal writer.binmode?, writer2.binmode? + assert_equal writer.external_encoding, writer2.external_encoding + assert_equal writer.internal_encoding, writer2.internal_encoding + end + end + + def test_binmode_reader + EnvUtil.with_default_internal(Encoding::UTF_8) do + reader, writer = IO.pipe + reader.binmode + writer.binmode + + reader2, writer2 = IO.pipe(binmode: true) + + assert reader.binmode? + assert reader2.binmode? + assert_equal reader.binmode?, reader2.binmode? + assert_equal reader.external_encoding, reader2.external_encoding + assert_equal reader.internal_encoding, reader2.internal_encoding + end + end + def test_pipe_block x = nil ret = IO.pipe {|r, w| -- 2.10.1