Project

General

Profile

Actions

Feature #3418

closed

IO#putc Clobbers Multi-byte Characters

Added by runpaint (Run Paint Run Run) over 14 years ago. Updated over 13 years ago.

Status:
Closed
Target version:
[ruby-core:30697]

Description

=begin
IO#putc claims to write a "character", when in fact it writes a byte. I assume this is for backward compatibility reasons, but as this could lead to data loss, the documentation needs clarifying. Currently, #putc doesn't require the stream to be in binmode, provide any warning of the truncation, or agree with IO#getc on the definition of "character".

open('/tmp/putc', 'w+') {|f| f.putc "\u1234"; f.rewind; f.read}
#=> "\xE1

open('/tmp/getc', 'w+'){|f| f.print "\u1234"; f.rewind; f.getc}
#=> "ሴ"

If the IO stream explicitly specifies a non-BINARY encoding, the first example fails with an Encoding::UndefinedConversionError, which is reasonable.

open('/tmp/putc', 'w+:UTF-8'){|f| f.putc "\u1234"; f.rewind; f.read}
#=> Encoding::UndefinedConversionError: "\xE1" from ASCII-8BIT to UTF-8
=end


Files

io.c-putc.patch (1.25 KB) io.c-putc.patch runpaint (Run Paint Run Run), 06/10/2010 07:15 AM
io.c-putc.patch (1.1 KB) io.c-putc.patch runpaint (Run Paint Run Run), 06/10/2010 07:18 AM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0