ruby-changes:65184
From: Nobuyoshi <ko1@a...>
Date: Mon, 8 Feb 2021 11:38:31 +0900 (JST)
Subject: [ruby-changes:65184] bf627e518d (master): Forward keyword arguments to ARGF from global functions
https://git.ruby-lang.org/ruby.git/commit/?id=bf627e518d From bf627e518d4575fd8ebfc28992b2177392b1dd20 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Mon, 8 Feb 2021 11:33:53 +0900 Subject: Forward keyword arguments to ARGF from global functions --- io.c | 31 +++++++++++++++++++------------ test/ruby/test_argf.rb | 9 +++++++++ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/io.c b/io.c index 2466cea..056bf16 100644 --- a/io.c +++ b/io.c @@ -7803,6 +7803,13 @@ rb_io_putc(VALUE io, VALUE ch) https://github.com/ruby/ruby/blob/trunk/io.c#L7803 return ch; } +#define forward(obj, id, argc, argv) \ + rb_funcallv_kw(obj, id, argc, argv, RB_PASS_CALLED_KEYWORDS) +#define forward_public(obj, id, argc, argv) \ + rb_funcallv_public_kw(obj, id, argc, argv, RB_PASS_CALLED_KEYWORDS) +#define forward_current(id, argc, argv) \ + forward_public(ARGF.current_file, id, argc, argv) + /* * call-seq: * putc(int) -> int @@ -7822,7 +7829,7 @@ rb_f_putc(VALUE recv, VALUE ch) https://github.com/ruby/ruby/blob/trunk/io.c#L7829 if (recv == r_stdout) { return rb_io_putc(recv, ch); } - return rb_funcallv(r_stdout, rb_intern("putc"), 1, &ch); + return forward(r_stdout, rb_intern("putc"), 1, &ch); } @@ -7937,7 +7944,7 @@ rb_f_puts(int argc, VALUE *argv, VALUE recv) https://github.com/ruby/ruby/blob/trunk/io.c#L7944 if (recv == r_stdout) { return rb_io_puts(argc, argv, recv); } - return rb_funcallv(r_stdout, rb_intern("puts"), argc, argv); + return forward(r_stdout, rb_intern("puts"), argc, argv); } static VALUE @@ -8772,7 +8779,7 @@ argf_lineno(VALUE argf) https://github.com/ruby/ruby/blob/trunk/io.c#L8779 static VALUE argf_forward(int argc, VALUE *argv, VALUE argf) { - return rb_funcall3(ARGF.current_file, rb_frame_this_func(), argc, argv); + return forward_current(rb_frame_this_func(), argc, argv); } #define next_argv() argf_next_argv(argf) @@ -8987,7 +8994,7 @@ argf_getline(int argc, VALUE *argv, VALUE argf) https://github.com/ruby/ruby/blob/trunk/io.c#L8994 retry: if (!next_argv()) return Qnil; if (ARGF_GENERIC_INPUT_P()) { - line = rb_funcall3(ARGF.current_file, idGets, argc, argv); + line = forward_current(idGets, argc, argv); } else { if (argc == 0 && rb_rs == rb_default_rs) { @@ -9065,7 +9072,7 @@ rb_f_gets(int argc, VALUE *argv, VALUE recv) https://github.com/ruby/ruby/blob/trunk/io.c#L9072 if (recv == argf) { return argf_gets(argc, argv, argf); } - return rb_funcallv(argf, idGets, argc, argv); + return forward(argf, idGets, argc, argv); } /* @@ -9141,7 +9148,7 @@ rb_f_readline(int argc, VALUE *argv, VALUE recv) https://github.com/ruby/ruby/blob/trunk/io.c#L9148 if (recv == argf) { return argf_readline(argc, argv, argf); } - return rb_funcallv(argf, rb_intern("readline"), argc, argv); + return forward(argf, rb_intern("readline"), argc, argv); } @@ -9195,7 +9202,7 @@ rb_f_readlines(int argc, VALUE *argv, VALUE recv) https://github.com/ruby/ruby/blob/trunk/io.c#L9202 if (recv == argf) { return argf_readlines(argc, argv, argf); } - return rb_funcallv(argf, rb_intern("readlines"), argc, argv); + return forward(argf, rb_intern("readlines"), argc, argv); } /* @@ -9223,7 +9230,7 @@ argf_readlines(int argc, VALUE *argv, VALUE argf) https://github.com/ruby/ruby/blob/trunk/io.c#L9230 ary = rb_ary_new(); while (next_argv()) { if (ARGF_GENERIC_INPUT_P()) { - lines = rb_funcall3(ARGF.current_file, rb_intern("readlines"), argc, argv); + lines = forward_current(rb_intern("readlines"), argc, argv); } else { lines = rb_io_readlines(argc, argv, ARGF.current_file); @@ -11979,7 +11986,7 @@ rb_io_set_encoding(int argc, VALUE *argv, VALUE io) https://github.com/ruby/ruby/blob/trunk/io.c#L11986 VALUE v1, v2, opt; if (!RB_TYPE_P(io, T_FILE)) { - return rb_funcallv(io, id_set_encoding, argc, argv); + return forward(io, id_set_encoding, argc, argv); } argc = rb_scan_args(argc, argv, "11:", &v1, &v2, &opt); @@ -12495,7 +12502,7 @@ argf_getc(VALUE argf) https://github.com/ruby/ruby/blob/trunk/io.c#L12502 retry: if (!next_argv()) return Qnil; if (ARGF_GENERIC_INPUT_P()) { - ch = rb_funcall3(ARGF.current_file, rb_intern("getc"), 0, 0); + ch = forward_current(rb_intern("getc"), 0, 0); } else { ch = rb_io_getc(ARGF.current_file); @@ -12535,7 +12542,7 @@ argf_getbyte(VALUE argf) https://github.com/ruby/ruby/blob/trunk/io.c#L12542 retry: if (!next_argv()) return Qnil; if (!RB_TYPE_P(ARGF.current_file, T_FILE)) { - ch = rb_funcall3(ARGF.current_file, rb_intern("getbyte"), 0, 0); + ch = forward_current(rb_intern("getbyte"), 0, 0); } else { ch = rb_io_getbyte(ARGF.current_file); @@ -12575,7 +12582,7 @@ argf_readchar(VALUE argf) https://github.com/ruby/ruby/blob/trunk/io.c#L12582 retry: if (!next_argv()) rb_eof_error(); if (!RB_TYPE_P(ARGF.current_file, T_FILE)) { - ch = rb_funcall3(ARGF.current_file, rb_intern("getc"), 0, 0); + ch = forward_current(rb_intern("getc"), 0, 0); } else { ch = rb_io_getc(ARGF.current_file); diff --git a/test/ruby/test_argf.rb b/test/ruby/test_argf.rb index 874f084..7922e5a 100644 --- a/test/ruby/test_argf.rb +++ b/test/ruby/test_argf.rb @@ -1013,6 +1013,9 @@ class TestArgf < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_argf.rb#L1013 ensure argf.close end + + assert_in_out_err(['-e', 'p readlines(chomp: true)'], "a\nb\n", + ["[\"a\", \"b\"]"], []) end def test_readline_chomp @@ -1023,6 +1026,9 @@ class TestArgf < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_argf.rb#L1026 ensure argf.close end + + assert_in_out_err(['-e', 'p readline(chomp: true)'], "a\nb\n", + ["\"a\""], []) end def test_gets_chomp @@ -1033,6 +1039,9 @@ class TestArgf < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_argf.rb#L1039 ensure argf.close end + + assert_in_out_err(['-e', 'p gets(chomp: true)'], "a\nb\n", + ["\"a\""], []) end def test_readlines_twice -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/