Project

General

Profile

Actions

Bug #20067

open

IO.pipe `int_enc` and `ext_enc` not working as documented?

Added by byroot (Jean Boussier) 2 months ago. Updated 2 months ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:115751]

Description

From the documentation:

pipe(ext_enc, int_enc, **opts)

If argument int_enc is given, it must be an Encoding object or encoding name string that specifies the internal encoding to be used; if argument ext_enc is also given, it must be an Encoding object or encoding name string that specifies the external encoding to be used.

Test script:

ios = IO.pipe(Encoding::BINARY, Encoding::BINARY)

p [:internal, *ios.map(&:internal_encoding)]
p [:external, *ios.map(&:external_encoding)]

Encoding.default_internal = Encoding::UTF_8
Encoding.default_external = Encoding::UTF_8

ios = IO.pipe(Encoding::BINARY, Encoding::BINARY)

p [:internal, *ios.map(&:internal_encoding)]
p [:external, *ios.map(&:external_encoding)]

Actual output:

[:internal, nil, nil]
[:external, #<Encoding:ASCII-8BIT>, nil]
[:internal, nil, nil]
[:external, #<Encoding:ASCII-8BIT>, #<Encoding:UTF-8>]

Expected output:

[:internal, #<Encoding:ASCII-8BIT>, #<Encoding:ASCII-8BIT>]
[:external, #<Encoding:ASCII-8BIT>, #<Encoding:ASCII-8BIT>]
[:internal, #<Encoding:ASCII-8BIT>, #<Encoding:ASCII-8BIT>]
[:external, #<Encoding:ASCII-8BIT>, #<Encoding:ASCII-8BIT>]

Am I reading the documentation incorrectly, or is it just not working as claimed?

I tried on older versions, and it seems to have been like that all the way down to Ruby 1.9.

The only reliable way I found to have a "binary" pipe is to call IO#binmode.

Updated by nobu (Nobuyoshi Nakada) 2 months ago

If the same encoding is specified for the internal and external encodings, it means that no encoding conversion will occur.
That's what the internal encoding nil means.

$ ruby -e 'File.open(IO::NULL, "r:ASCII-8BIT:ASCII-8BIT") {|f| p [f.internal_encoding, f.external_encoding]}'
[nil, #<Encoding:ASCII-8BIT>]

Regarding the external encoding of write-io, it feels something wrong a little.

Actions

Also available in: Atom PDF

Like0
Like0