Skip to content

Return a String from ARGF.getc and ARGF.readchar when reading files - #10033

Merged
headius merged 1 commit into
jruby:jruby-10.0from
taxor03:fix-argf-getc
Nov 11, 2026
Merged

headius merged 1 commit into
jruby:jruby-10.0from
taxor03:fix-argf-getc

Conversation

@taxor03

@taxor03 taxor03 commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Fixes #10032.

When the current file was a File, RubyArgsFile#getc called getbyte on it, so ARGF.getc and ARGF.readchar returned Integers (one per byte) instead of one-character Strings. Other inputs, like $stdin, already went through getc. CRuby's argf_getc and argf_readchar call rb_io_getc on the file, so this now calls RubyIO#getc.

each_byte and each_char were built on the same getc, so they change too:

  • each_byte now calls getbyte. It yielded one-character Strings, not Integers, when reading $stdin.
  • each_char now yields what getc returns. It joined bytes into characters using the runtime's KCode setting (binary by default), not the stream's encoding. For a file it split é into "\xC3" and "\xA9", and for $stdin it dropped bytes.

With a file or $stdin containing "hé\n":

before after (same as CRuby 4.0.9 and 3.4.12)
ARGF.getc (file) 104 "h"
ARGF.each_char.to_a (file) ["h", "\xC3", "\xA9", "\n"] ["h", "é", "\n"]
ARGF.each_char.to_a ($stdin) ["h", "\xC3", "\n"] ["h", "é", "\n"]
ARGF.each_byte.to_a ($stdin) ["h", "é", "\n"] [104, 195, 169, 10]

ARGF.each_byte on files and ARGF.getc on $stdin already worked and are unchanged. CRuby's argf_each_byte and argf_each_char call each_byte and each_char on each file instead. I kept the loops over getbyte and getc, because they already handle ARGF.skip inside the block the way CRuby does. With this change TestArgf#test_skip_in_each_char passes, so its exclude is removed.

The existing specs only used ASCII files and built the result with String#<<, which also accepts an Integer. This adds a small UTF-8 fixture and examples for getc and readchar (shared getc.rb) and each_char, plus $stdin examples for each_byte and each_char. The multibyte examples call set_encoding so they don't depend on the default external encoding. The 5 new examples fail before this change and pass after it. spec/ruby/core/argf passes on CRuby 4.0.9, 3.4.12 and 3.3.13, with LANG=C.UTF-8 and LANG=C.

Tested with spec/ruby/core/argf (also with -Xjit.threshold=0), spec/ruby/core/io, test/mri/ruby/test_argf.rb and the fast RubySpec suite. No new failures.

@taxor03
taxor03 changed the base branch from master to jruby-10.0 November 11, 2026 06:33

@headius headius left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likely never updated since Ruby 1.8 returned Fixnum from these methods.

@headius headius added this to the JRuby 10.1.4.0 milestone Nov 11, 2026
@headius
headius merged commit 5ae8631 into jruby:jruby-10.0 Nov 11, 2026
127 checks passed
@headius

headius commented Nov 12, 2026

Copy link
Copy Markdown
Member

Looks like I botched the merge to master. looking into it.

headius added a commit that referenced this pull request Nov 12, 2026
This was merged prematurely in #10033 and needs
additional work to pass. Removing here temporarily.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ARGF.getc and ARGF.readchar return byte Integers instead of one-character Strings

2 participants