[ruby-core:112115] [Ruby master Misc#19391] IO #write/#read behaviour when binmode and encoding are explicitly specified

Issue #19391 has been reported by andrykonchin (Andrew Konchin). ---------------------------------------- Misc #19391: IO #write/#read behaviour when binmode and encoding are explicitly specified https://bugs.ruby-lang.org/issues/19391 * Author: andrykonchin (Andrew Konchin) * Status: Open * Priority: Normal ---------------------------------------- I have a question about how `IO` operations are supposed to behave when file is opened in binary mode but encoding is specified too. Documentation of `b` option of `IO.new` method and of `IO#binmode` method is a bit not synchronised and confusing: `IO.open`: ``` "b" Binary file mode Suppresses EOL <-> CRLF conversion on Windows. And sets external encoding to ASCII-8BIT unless explicitly specified. ``` `IO#binmode`: ``` Puts ios into binary mode. Once a stream is in binary mode, it cannot be reset to nonbinary mode. newline conversion disabled encoding conversion disabled content is treated as ASCII-8BIT ``` So `unless explicitly specified` and `encoding conversion disabled` are a bit contradictory. Right now I observe that `IO.write` still encodes an input string in explicitly specified encoding (with `encoding` option) even if binary mode is set (with `binmode` option) ```ruby File.open(@filename, "w", encoding: Encoding::UTF_32LE, binmode: true) do |file| file.write("hi") end File.binread(@filename) # => "h\u0000\u0000\u0000i\u0000\u0000\u0000" ``` -- https://bugs.ruby-lang.org/
participants (1)
-
andrykonchin (Andrew Konchin)