Skip to content

Commit e6b07ba

Browse files
committed
Implement rb_io_descriptor()
* Avoids an issue if the fallback is declared with the same name: ruby/io-console@d1d9aef * Specs change from https://github.com/ruby/ruby/pull/6511/files
1 parent d52835e commit e6b07ba

File tree

6 files changed

+24
-2
lines changed

6 files changed

+24
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ Compatibility:
145145
* Add `rb_io_maybe_wait_readable`, `rb_io_maybe_wait_writable` and `rb_io_maybe_wait` functions (#2733, @andrykonchin).
146146
* `StringIO#set_encoding` should coerce the argument to an Encoding (#2954, @eregon).
147147
* Implement changes of Ruby 3.0 to `IO#wait` (#2953, @larskanis).
148+
* Implement `rb_io_descriptor()` (@eregon).
148149

149150
Performance:
150151

lib/cext/ABI_version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6
1+
7

lib/truffle/truffle/cext.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,10 @@ def rb_io_puts(out, args)
12541254
Truffle::IOOperations.puts out, args
12551255
end
12561256

1257+
def rb_io_descriptor(io)
1258+
Primitive.io_fd(io)
1259+
end
1260+
12571261
def rb_equal(a, b)
12581262
Primitive.same_or_equal?(a, b)
12591263
end

spec/ruby/optional/capi/ext/io_spec.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ static int set_non_blocking(int fd) {
2828
}
2929

3030
static int io_spec_get_fd(VALUE io) {
31+
#ifdef RUBY_VERSION_IS_3_1
32+
return rb_io_descriptor(io);
33+
#else
3134
rb_io_t* fp;
3235
GetOpenFile(io, fp);
3336
return fp->fd;
37+
#endif
3438
}
3539

3640
VALUE io_spec_GetOpenFile_fd(VALUE self, VALUE io) {

spec/ruby/optional/capi/io_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,18 @@
175175
end
176176
end
177177

178-
describe "GetOpenFile" do
178+
describe "rb_io_descriptor or GetOpenFile" do
179179
it "allows access to the system fileno" do
180180
@o.GetOpenFile_fd($stdin).should == 0
181181
@o.GetOpenFile_fd($stdout).should == 1
182182
@o.GetOpenFile_fd($stderr).should == 2
183183
@o.GetOpenFile_fd(@io).should == @io.fileno
184184
end
185+
186+
it "raises IOError if the IO is closed" do
187+
@io.close
188+
-> { @o.GetOpenFile_fd(@io) }.should raise_error(IOError, "closed stream")
189+
end
185190
end
186191

187192
describe "rb_io_binmode" do

src/main/c/cext/io.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414

1515
// IO, rb_io_*
1616

17+
int rb_io_descriptor(VALUE io) {
18+
int fd = polyglot_as_i32(RUBY_CEXT_INVOKE_NO_WRAP("rb_io_descriptor", io));
19+
if (fd < 0) {
20+
rb_raise(rb_eIOError, "closed stream");
21+
}
22+
return fd;
23+
}
24+
1725
void rb_io_check_writable(rb_io_t *io) {
1826
if (!rb_tr_writable(io->mode)) {
1927
rb_raise(rb_eIOError, "not opened for writing");

0 commit comments

Comments
 (0)