Project

General

Profile

Actions

Bug #21517

closed

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

Added by herwin (Herwin W) 26 days ago. Updated 10 days ago.

Status:
Closed
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.

Updated by herwin (Herwin W) 10 days ago

Never mind, this was a mistake on my end. The file spec/core/io/readpartial_spec.rb has a # encoding: binary directive, once I added that to spec/library/stringio/readpartial_spec.rb too, the issue was fixed.

Please close this issue, I'll add it to the specs.

Actions #3

Updated by jeremyevans0 (Jeremy Evans) 10 days ago

  • Status changed from Open to Closed
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0