[Bug #21625] Allow io/wait methods with IO#ungetc in text mode

This commit is contained in:
Nobuyoshi Nakada 2025-10-28 21:55:18 +09:00
parent 6e618a2c79
commit 12350eb9e0
No known key found for this signature in database
GPG Key ID: 3582D74E1FEE4465
Notes: git 2025-10-29 08:58:23 +00:00
3 changed files with 36 additions and 6 deletions

View File

@ -84,7 +84,7 @@ io_nread(VALUE io)
ioctl_arg n;
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
rb_io_check_char_readable(fptr);
len = rb_io_read_pending(fptr);
if (len > 0) return INT2FIX(len);
@ -143,7 +143,7 @@ io_ready_p(VALUE io)
#endif
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
rb_io_check_char_readable(fptr);
if (rb_io_read_pending(fptr)) return Qtrue;
#ifndef HAVE_RB_IO_WAIT
@ -178,7 +178,7 @@ io_wait_readable(int argc, VALUE *argv, VALUE io)
#endif
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
rb_io_check_char_readable(fptr);
#ifndef HAVE_RB_IO_WAIT
tv = get_timeout(argc, argv, &timerec);
@ -252,7 +252,7 @@ io_wait_priority(int argc, VALUE *argv, VALUE io)
rb_io_t *fptr = NULL;
RB_IO_POINTER(io, fptr);
rb_io_check_readable(fptr);
rb_io_check_char_readable(fptr);
if (rb_io_read_pending(fptr)) return Qtrue;

4
io.c
View File

@ -9832,7 +9832,7 @@ io_wait_readable(int argc, VALUE *argv, VALUE io)
rb_io_t *fptr;
RB_IO_POINTER(io, fptr);
rb_io_check_readable(fptr);
rb_io_check_char_readable(fptr);
if (rb_io_read_pending(fptr)) return Qtrue;
@ -9879,7 +9879,7 @@ io_wait_priority(int argc, VALUE *argv, VALUE io)
rb_io_t *fptr = NULL;
RB_IO_POINTER(io, fptr);
rb_io_check_readable(fptr);
rb_io_check_char_readable(fptr);
if (rb_io_read_pending(fptr)) return Qtrue;

View File

@ -1,5 +1,6 @@
# frozen_string_literal: true
require 'test/unit'
require 'io/wait'
# test uncommon device types to check portability problems
# We may optimize IO#wait_*able for non-Linux kernels in the future
@ -74,4 +75,33 @@ class TestIOWaitUncommon < Test::Unit::TestCase
def test_wait_writable_null
check_dev(IO::NULL, :wait_writable)
end
def test_after_ungetc_ready?
check_dev(IO::NULL, mode: "r") {|fp|
assert_respond_to fp, :ready?
fp.ungetc(?a)
assert_predicate fp, :ready?
}
end
def test_after_ungetc_wait_readable
check_dev(IO::NULL, mode: "r") {|fp|
fp.ungetc(?a)
assert_predicate fp, :wait_readable
}
end
def test_after_ungetc_in_text_ready?
check_dev(IO::NULL, mode: "rt") {|fp|
fp.ungetc(?a)
assert_predicate fp, :ready?
}
end
def test_after_ungetc_in_text_wait_readable
check_dev(IO::NULL, mode: "rt") {|fp|
fp.ungetc(?a)
assert_predicate fp, :wait_readable
}
end
end