[ruby-core:109842] [Ruby master Bug#18995] IO#set_encoding sometimes set an IO's internal encoding to the default external encoding
From:
"javanthropus (Jeremy Bopp)" <noreply@...>
Date:
2022-09-04 23:06:54 UTC
List:
ruby-core #109842
Issue #18995 has been reported by javanthropus (Jeremy Bopp).
----------------------------------------
Bug #18995: IO#set_encoding sometimes set an IO's internal encoding to the default external encoding
https://bugs.ruby-lang.org/issues/18995
* Author: javanthropus (Jeremy Bopp)
* Status: Open
* Priority: Normal
* ruby -v: ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux]
* Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN
----------------------------------------
This script demonstrates the behavior:
```ruby
def show(io)
printf(
"external encoding: %-25p internal encoding: %-25p\n",
io.external_encoding,
io.internal_encoding
)
end
Encoding.default_external = 'iso-8859-1'
Encoding.default_internal = 'iso-8859-2'
File.open('/dev/null') do |f|
f.set_encoding('utf-8', nil)
show(f) # f.internal_encoding is iso-8859-2, as expected
f.set_encoding('utf-8', 'invalid')
show(f) # f.internal_encoding is now iso-8859-1!
Encoding.default_external = 'iso-8859-3'
Encoding.default_internal = 'iso-8859-4'
show(f) # f.internal_encoding is now iso-8859-3!
end
```
In the 1st case, we see that the IO's internal encoding is set to the current setting of Encoding.default_internal. In the 2nd case, the IO's internal encoding is set to Encoding.default_external instead. The 3rd case is more interesting because it shows that the IO's internal encoding is actually following the current setting of Encoding.default_external. It didn't just copy it when #set_encoding was called. It changes whenever Encoding.default_external changes.
What should the correct behavior be?
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>