ruby-changes:50607
From: mrkn <ko1@a...>
Date: Thu, 15 Mar 2018 16:19:48 +0900 (JST)
Subject: [ruby-changes:50607] mrkn:r62757 (trunk): Add `exception:` keyword in Kernel#Integer()
mrkn 2018-03-15 16:19:43 +0900 (Thu, 15 Mar 2018) New Revision: 62757 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=62757 Log: Add `exception:` keyword in Kernel#Integer() Support `exception:` keyword argument in Kernel#Integer(). If `exception:` is `false`, `Kernel#Integer()` returns `nil` if the given value cannot be interpreted as an integer value. The default value of `exception:` is `true`. This is part of [Feature #12732]. Modified files: trunk/bignum.c trunk/internal.h trunk/object.c trunk/test/ruby/test_integer.rb Index: internal.h =================================================================== --- internal.h (revision 62756) +++ internal.h (revision 62757) @@ -1101,6 +1101,7 @@ size_t rb_big_size(VALUE); https://github.com/ruby/ruby/blob/trunk/internal.h#L1101 VALUE rb_integer_float_cmp(VALUE x, VALUE y); VALUE rb_integer_float_eq(VALUE x, VALUE y); VALUE rb_cstr_parse_inum(const char *str, ssize_t len, char **endp, int base); +VALUE rb_str_convert_to_inum(VALUE str, int base, int badcheck, int raise_exception); VALUE rb_big_comp(VALUE x); VALUE rb_big_aref(VALUE x, VALUE y); VALUE rb_big_abs(VALUE x); Index: bignum.c =================================================================== --- bignum.c (revision 62756) +++ bignum.c (revision 62757) @@ -4230,7 +4230,7 @@ rb_cstr_parse_inum(const char *str, ssiz https://github.com/ruby/ruby/blob/trunk/bignum.c#L4230 } VALUE -rb_str_to_inum(VALUE str, int base, int badcheck) +rb_str_convert_to_inum(VALUE str, int base, int badcheck, int raise_exception) { VALUE ret; const char *s; @@ -4242,13 +4242,22 @@ rb_str_to_inum(VALUE str, int base, int https://github.com/ruby/ruby/blob/trunk/bignum.c#L4242 RSTRING_GETMEM(str, s, len); ret = rb_cstr_parse_inum(s, len, (badcheck ? NULL : &end), base); if (NIL_P(ret)) { - if (badcheck) invalid_integer(str); - ret = INT2FIX(0); + if (badcheck) { + if (!raise_exception) return Qnil; + invalid_integer(str); + } + ret = INT2FIX(0); } return ret; } VALUE +rb_str_to_inum(VALUE str, int base, int badcheck) +{ + return rb_str_convert_to_inum(str, base, badcheck, TRUE); +} + +VALUE rb_str2big_poweroftwo(VALUE arg, int base, int badcheck) { int positive_p = 1; Index: object.c =================================================================== --- object.c (revision 62756) +++ object.c (revision 62757) @@ -3108,40 +3108,57 @@ rb_check_to_int(VALUE val) https://github.com/ruby/ruby/blob/trunk/object.c#L3108 } static VALUE -rb_convert_to_integer(VALUE val, int base) +rb_check_to_i(VALUE val) +{ + if (RB_INTEGER_TYPE_P(val)) return val; + val = try_to_int(val, idTo_i, FALSE); + if (RB_INTEGER_TYPE_P(val)) return val; + return Qnil; +} + +static VALUE +rb_convert_to_integer(VALUE val, int base, int raise_exception) { VALUE tmp; if (RB_FLOAT_TYPE_P(val)) { - double f; - if (base != 0) goto arg_error; - f = RFLOAT_VALUE(val); - if (FIXABLE(f)) return LONG2FIX((long)f); - return rb_dbl2big(f); + double f; + if (base != 0) goto arg_error; + f = RFLOAT_VALUE(val); + if (FIXABLE(f)) return LONG2FIX((long)f); + return rb_dbl2big(f); } else if (RB_INTEGER_TYPE_P(val)) { - if (base != 0) goto arg_error; - return val; + if (base != 0) goto arg_error; + return val; } else if (RB_TYPE_P(val, T_STRING)) { - return rb_str_to_inum(val, base, TRUE); + return rb_str_convert_to_inum(val, base, TRUE, raise_exception); } else if (NIL_P(val)) { - if (base != 0) goto arg_error; - rb_raise(rb_eTypeError, "can't convert nil into Integer"); + if (base != 0) goto arg_error; + if (!raise_exception) return Qnil; + rb_raise(rb_eTypeError, "can't convert nil into Integer"); } if (base != 0) { - tmp = rb_check_string_type(val); - if (!NIL_P(tmp)) return rb_str_to_inum(tmp, base, TRUE); + tmp = rb_check_string_type(val); + if (!NIL_P(tmp)) return rb_str_convert_to_inum(tmp, base, TRUE, raise_exception); arg_error: - rb_raise(rb_eArgError, "base specified for non string value"); + if (!raise_exception) return Qnil; + rb_raise(rb_eArgError, "base specified for non string value"); } - tmp = convert_type_with_id(val, "Integer", idTo_int, FALSE, -1); - if (!RB_INTEGER_TYPE_P(tmp)) { - return rb_to_integer(val, "to_i", idTo_i); + + tmp = rb_protect(rb_check_to_int, val, NULL); + if (RB_INTEGER_TYPE_P(tmp)) return tmp; + rb_set_errinfo(Qnil); + + if (!raise_exception) { + VALUE result = rb_protect(rb_check_to_i, val, NULL); + rb_set_errinfo(Qnil); + return result; } - return tmp; + return rb_to_integer(val, "to_i", idTo_i); } /** @@ -3153,7 +3170,19 @@ rb_convert_to_integer(VALUE val, int bas https://github.com/ruby/ruby/blob/trunk/object.c#L3170 VALUE rb_Integer(VALUE val) { - return rb_convert_to_integer(val, 0); + return rb_convert_to_integer(val, 0, TRUE); +} + +static int +opts_exception_p(VALUE opts) +{ + static ID kwds[1]; + VALUE exception; + if (!kwds[0]) { + kwds[0] = rb_intern_const("exception"); + } + rb_get_kwargs(opts, kwds, 0, 1, &exception); + return exception != Qfalse; } /* @@ -3183,20 +3212,20 @@ rb_Integer(VALUE val) https://github.com/ruby/ruby/blob/trunk/object.c#L3212 static VALUE rb_f_integer(int argc, VALUE *argv, VALUE obj) { - VALUE arg = Qnil; + VALUE arg = Qnil, opts = Qnil; int base = 0; - switch (argc) { + switch (rb_scan_args(argc, argv, "11:", NULL, NULL, &opts)) { case 2: - base = NUM2INT(argv[1]); + base = NUM2INT(argv[1]); case 1: - arg = argv[0]; - break; + arg = argv[0]; + break; default: - /* should cause ArgumentError */ - rb_scan_args(argc, argv, "11", NULL, NULL); + UNREACHABLE; } - return rb_convert_to_integer(arg, base); + + return rb_convert_to_integer(arg, base, opts_exception_p(opts)); } /*! Index: test/ruby/test_integer.rb =================================================================== --- test/ruby/test_integer.rb (revision 62756) +++ test/ruby/test_integer.rb (revision 62757) @@ -127,6 +127,40 @@ class TestInteger < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_integer.rb#L127 end; end + def test_Integer_with_exception_keyword + assert_nothing_raised(ArgumentError) { + assert_equal(nil, Integer("1z", exception: false)) + } + assert_nothing_raised(ArgumentError) { + assert_equal(nil, Integer(Object.new, exception: false)) + } + assert_nothing_raised(ArgumentError) { + o = Object.new + def o.to_i; 42.5; end + assert_equal(nil, Integer(o, exception: false)) + } + assert_nothing_raised(ArgumentError) { + o = Object.new + def o.to_i; raise; end + assert_equal(nil, Integer(o, exception: false)) + } + assert_nothing_raised(ArgumentError) { + o = Object.new + def o.to_int; raise; end + assert_equal(nil, Integer(o, exception: false)) + } + + assert_raise(ArgumentError) { + Integer("1z", exception: true) + } + assert_raise(TypeError) { + Integer(nil, exception: true) + } + assert_nothing_raised(TypeError) { + assert_equal(nil, Integer(nil, exception: false)) + } + end + def test_int_p assert_not_predicate(1.0, :integer?) assert_predicate(1, :integer?) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/