From: "andrykonchin (Andrew Konchin) via ruby-core" <ruby-core@...>
Date: 2023-01-30T10:19:06+00:00
Subject: [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/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/