Bug #12989
closedPassing `binmode: true` to `IO.pipe` makes `binmode?` return `true` but encoding is not binary
Description
Here is a sample program:
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
reader, writer = IO.pipe(binmode: true)
reader.binmode? # => true
reader.external_encoding # => #<Encoding:UTF-8>
writer.binmode? # => true
writer.external_encoding # => #<Encoding:UTF-8>
reader, writer = IO.pipe
reader.binmode
writer.binmode
reader.binmode? # => true
reader.external_encoding # => #<Encoding:ASCII-8BIT>
writer.binmode? # => true
writer.external_encoding # => #<Encoding:ASCII-8BIT>
I think that passing binmode: true
to IO.pipe
should behave the same way as calling binmode
on each file. Today, passing binmode: true
to IO.pipe puts the files in a strange state: they are binary and not binary.
I've attached a patch to fix the problem.
Files
Updated by nobu (Nobuyoshi Nakada) over 8 years ago
- Description updated (diff)
Encoding argument should be prior to binmode
option.
open(IO::NULL, "r", binmode: true){|f| p [f.binmode?, f.external_encoding]} #=> [true, #<Encoding:ASCII-8BIT>]
open(IO::NULL, "r:utf-8", binmode: true){|f| p [f.binmode?, f.external_encoding]} #=> [true, #<Encoding:UTF-8>]
Your patch always makes external_encoding
ASCII-8BIT
when binmode
is set, even if an encoding is given.
Updated by jeremyevans0 (Jeremy Evans) almost 6 years ago
- File io-pipe-binmode.patch io-pipe-binmode.patch added
nobu (Nobuyoshi Nakada) wrote:
Encoding argument should be prior to
binmode
option.open(IO::NULL, "r", binmode: true){|f| p [f.binmode?, f.external_encoding]} #=> [true, #<Encoding:ASCII-8BIT>] open(IO::NULL, "r:utf-8", binmode: true){|f| p [f.binmode?, f.external_encoding]} #=> [true, #<Encoding:UTF-8>]
Your patch always makes
external_encoding
ASCII-8BIT
whenbinmode
is set, even if an encoding is given.
Here's an updated patch based on tenderlove's original patch that does not change the encoding of the returned IO objects if encoding arguments are given in addition to the binmode
option. Is this acceptable?
Updated by ko1 (Koichi Sasada) almost 6 years ago
- Status changed from Open to Assigned
- Assignee set to nobu (Nobuyoshi Nakada)
Updated by nobu (Nobuyoshi Nakada) almost 6 years ago
jeremyevans0 (Jeremy Evans) wrote:
Here's an updated patch based on tenderlove's original patch that does not change the encoding of the returned IO objects if encoding arguments are given in addition to the
binmode
option. Is this acceptable?
I think so.
In general, dedicated assertion methods e.g.,assert_predicate
would be preferable to mere assert
, I think.
Updated by jeremyevans (Jeremy Evans) almost 6 years ago
- Status changed from Assigned to Closed
Applied in changeset git|ebc99e026d0ae770b297a93d1f1c1ceeffd13bfc.
Do not change IO.pipe encodings if encodings explicitly given
This commit makes it so that if the binmode option is given with
any encoding arguments, the reader and writer IO objects are
not set to binary encoding.
Fixes [Bug #12989]