Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ee613f8

Browse files
committed
Add rb_io_mode to get IO mode.
1 parent 4a5fb36 commit ee613f8

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

include/ruby/io.h

+6
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,12 @@ VALUE rb_io_path(VALUE io);
623623
*/
624624
int rb_io_descriptor(VALUE io);
625625

626+
/**
627+
* Get the mode of the IO.
628+
*
629+
*/
630+
int rb_io_mode(VALUE io);
631+
626632
/**
627633
* This function breaks down the option hash that `IO#initialize` takes into
628634
* components. This is an implementation detail of rb_io_extract_modeenc()

io.c

+7
Original file line numberDiff line numberDiff line change
@@ -2869,6 +2869,13 @@ rb_io_descriptor(VALUE io)
28692869
}
28702870
}
28712871

2872+
int rb_io_mode(VALUE io)
2873+
{
2874+
rb_io_t *fptr;
2875+
GetOpenFile(io, fptr);
2876+
return fptr->mode;
2877+
}
2878+
28722879
/*
28732880
* call-seq:
28742881
* pid -> integer or nil

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ VALUE io_spec_rb_io_set_nonblock(VALUE self, VALUE io) {
307307
GetOpenFile(io, fp);
308308
rb_io_set_nonblock(fp);
309309
#ifdef F_GETFL
310-
flags = fcntl(fp->fd, F_GETFL, 0);
310+
flags = fcntl(io_spec_get_fd(io), F_GETFL, 0);
311311
return flags & O_NONBLOCK ? Qtrue : Qfalse;
312312
#else
313313
return Qfalse;
@@ -326,9 +326,13 @@ static VALUE io_spec_errno_set(VALUE self, VALUE val) {
326326
}
327327

328328
VALUE io_spec_mode_sync_flag(VALUE self, VALUE io) {
329+
#ifdef RUBY_VERSION_IS_3_3
330+
if (rb_io_mode(io) & FMODE_SYNC) {
331+
#else
329332
rb_io_t *fp;
330333
GetOpenFile(io, fp);
331334
if (fp->mode & FMODE_SYNC) {
335+
#endif
332336
return Qtrue;
333337
} else {
334338
return Qfalse;

0 commit comments

Comments
 (0)