Project

General

Profile

Actions

Bug #20416

closed

IO#read doesn't change buffer encoding unless `maxlen = nil`

Added by andrykonchin (Andrew Konchin) 7 months ago. Updated 5 months ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:117470]

Description

IO#read(nil, buffer) doesn't change buffer's encoding, but it does when maxlen parameter is specified:

buffer = "".encode(Encoding::ISO_8859_1)
p File.open("test.rb").read(1000, buffer).encoding # => #<Encoding:ISO-8859-1>

buffer = "".encode(Encoding::ISO_8859_1)
p File.open("test.rb").read(nil, buffer).encoding  # => #<Encoding:UTF-8>

I would expect changing buffer encoding in all the cases.

Actions #2

Updated by Eregon (Benoit Daloze) 7 months ago

  • Description updated (diff)

Updated by byroot (Jean Boussier) 7 months ago

I git blamed this behavior all the way down to 2007, by @matz (Yukihiro Matsumoto): https://github.com/ruby/ruby/commit/0ca7036682dad1caea4e7a4fe8fd908d94c878e8 and it seems deliberate

* io.c (read_all): should associate default external encoding.
* io.c (io_read): should NOT associate default external encoding.

Updated by nobu (Nobuyoshi Nakada) 5 months ago

The subject should be "IO#read doesn't change buffer encoding unless maxlen = nil"?

Since IO#read with maxlen is byte-oriented operation, there is not an encoding to be set from that IO.

Updated by byroot (Jean Boussier) 5 months ago

Yeah, I don't think it's a bug. @nobu (Nobuyoshi Nakada) should we close?

Actions #6

Updated by andrykonchin (Andrew Konchin) 5 months ago

  • Subject changed from IO#read doesn't preserve buffer encoding if `maxlen = nil` to IO#read doesn't change buffer encoding unless `maxlen = nil`

Updated by mame (Yusuke Endoh) 5 months ago

  • Status changed from Open to Closed

Discussed at the dev meeting, and @matz (Yukihiro Matsumoto) agreed with @nobu (Nobuyoshi Nakada). Closing

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0