Project

General

Profile

Actions

Bug #21517

open

Behaviour of StringIO#readpartial with multibyte differs from IO#readpartial

Added by herwin (Herwin W) about 7 hours ago.

Status:
Open
Assignee:
-
Target version:
-
ruby -v:
ruby 3.4.5 (2025-07-16 revision 20cda200d3) +PRISM [x86_64-linux] (+ others)
[ruby-core:122817]

Description

The specs for IO#readpartial have the following test (https://github.com/ruby/spec/blob/ed254bae321221f5cd24280beb37e5bddaff6bb6/core/io/readpartial_spec.rb#L41-L47):

before :each do
  @rd, @wr = IO.pipe
  @rd.binmode
  @wr.binmode
end

it "reads after ungetc with multibyte characters in the buffer" do
  @wr.write("∂φ/∂x = gaîté")
  c = @rd.getc
  @rd.ungetc(c)
  @rd.readpartial(3).should == "\xE2\x88\x82"
  @rd.readpartial(3).should == "\xCF\x86/"
end

This behaviour is different in StringIO#readpartial. If I add this as a spec:

it "reads after ungetc with multibyte characters in the buffer" do
  @string = StringIO.new(+"∂φ/∂x = gaîté")
  c = @string.getc
  @string.ungetc(c)
  @string.readpartial(3).should == "\xE2\x88\x82"
  @string.readpartial(3).should == "\xCF\x86/"
end

it fails with every supported Ruby version, and even with 3.5-dev of today, with the message Expected "\xE2\x88\x82" == "∂"

The getc + ungetc can be removed, but this does not change the behaviour.

No data to display

Actions

Also available in: Atom PDF

Like0