Skip to content

Commit fd7f546

Browse files
committed
Remove usage of IO internals.
1 parent 6f5903d commit fd7f546

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

ext/readline/extconf.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: false
22
require "mkmf"
33

4+
have_func("rb_io_descriptor")
5+
46
readline = Struct.new(:headers, :extra_check).new(["stdio.h"])
57

68
def readline.have_header(header)

ext/readline/readline.c

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@
4646
#include <sys/stat.h>
4747
#endif
4848

49+
#ifndef HAVE_RB_IO_DESCRIPTOR
50+
int io_descriptor_fallback(VALUE io) {
51+
rb_io_t *fptr;
52+
GetOpenFile(io, fptr);
53+
return fptr->fd;
54+
}
55+
#define rb_io_descriptor io_descriptor_fallback
56+
#endif
57+
4958
static VALUE mReadline;
5059

5160
#define EDIT_LINE_LIBRARY_VERSION "EditLine wrapper"
@@ -373,30 +382,36 @@ clear_rl_outstream(void)
373382
readline_outstream = Qfalse;
374383
}
375384

385+
VALUE io_descriptor(VALUE io) {
386+
return RB_INT2NUM(rb_io_descriptor(io));
387+
}
388+
376389
static void
377390
prepare_readline(void)
378391
{
379392
static int initialized = 0;
393+
int state;
394+
380395
if (!initialized) {
381396
rl_initialize();
382397
initialized = 1;
383398
}
384399

385400
if (readline_instream) {
386-
rb_io_t *ifp;
387-
rb_io_check_initialized(ifp = RFILE(rb_io_taint_check(readline_instream))->fptr);
388-
if (ifp->fd < 0) {
401+
rb_protect(io_descriptor, readline_instream, &state);
402+
403+
if (state) {
389404
clear_rl_instream();
390-
rb_raise(rb_eIOError, "closed readline input");
405+
rb_jump_tag(state);
391406
}
392407
}
393408

394409
if (readline_outstream) {
395-
rb_io_t *ofp;
396-
rb_io_check_initialized(ofp = RFILE(rb_io_taint_check(readline_outstream))->fptr);
397-
if (ofp->fd < 0) {
410+
rb_protect(io_descriptor, readline_outstream, &state);
411+
412+
if (state) {
398413
clear_rl_outstream();
399-
rb_raise(rb_eIOError, "closed readline output");
414+
rb_jump_tag(state);
400415
}
401416
}
402417
}
@@ -553,7 +568,6 @@ readline_readline(int argc, VALUE *argv, VALUE self)
553568
static VALUE
554569
readline_s_set_input(VALUE self, VALUE input)
555570
{
556-
rb_io_t *ifp;
557571
int fd;
558572
FILE *f;
559573

@@ -562,9 +576,9 @@ readline_s_set_input(VALUE self, VALUE input)
562576
}
563577
else {
564578
Check_Type(input, T_FILE);
565-
GetOpenFile(input, ifp);
579+
fd = rb_io_descriptor(input);g
566580
clear_rl_instream();
567-
fd = rb_cloexec_dup(ifp->fd);
581+
fd = rb_cloexec_dup();
568582
if (fd == -1)
569583
rb_sys_fail("dup");
570584
f = fdopen(fd, "r");
@@ -589,7 +603,6 @@ readline_s_set_input(VALUE self, VALUE input)
589603
static VALUE
590604
readline_s_set_output(VALUE self, VALUE output)
591605
{
592-
rb_io_t *ofp;
593606
int fd;
594607
FILE *f;
595608

@@ -598,9 +611,9 @@ readline_s_set_output(VALUE self, VALUE output)
598611
}
599612
else {
600613
Check_Type(output, T_FILE);
601-
GetOpenFile(output, ofp);
614+
fd = rb_io_descriptor(output);
602615
clear_rl_outstream();
603-
fd = rb_cloexec_dup(ofp->fd);
616+
fd = rb_cloexec_dup(fd);
604617
if (fd == -1)
605618
rb_sys_fail("dup");
606619
f = fdopen(fd, "w");

0 commit comments

Comments
 (0)