From 31ef3124a9db534abcc3e323f5d3cb696eda3bf5 Mon Sep 17 00:00:00 2001 From: nobu Date: Wed, 22 Feb 2017 02:02:11 +0000 Subject: [PATCH] numeric.c: Numeric#clone and #dup * numeric.c (num_clone, num_dup): no longer raises TypeError, returns the receiver instead as well as Integer and Float. [ruby-core:79636] [Bug #13237] * object.c (rb_immutable_obj_clone): immutable object clone with freeze optional keyword argument. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57682 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- internal.h | 1 + numeric.c | 35 ++++++++++++++++++++++++++++------- object.c | 10 ++++++++++ test/ruby/test_numeric.rb | 14 ++++++++++---- 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/internal.h b/internal.h index 7b13cd5484..f74852369a 100644 --- a/internal.h +++ b/internal.h @@ -1363,6 +1363,7 @@ VALUE rb_class_search_ancestor(VALUE klass, VALUE super); NORETURN(void rb_undefined_alloc(VALUE klass)); double rb_num_to_dbl(VALUE val); VALUE rb_obj_dig(int argc, VALUE *argv, VALUE self, VALUE notfound); +VALUE rb_immutable_obj_clone(int, VALUE *, VALUE); struct RBasicRaw { VALUE flags; diff --git a/numeric.c b/numeric.c index 1a685ed252..65edfec205 100644 --- a/numeric.c +++ b/numeric.c @@ -552,18 +552,38 @@ num_sadded(VALUE x, VALUE name) UNREACHABLE; } +#if 0 /* - * Numerics are immutable values, which should not be copied. + * call-seq: + * num.clone(freeze: true) -> num * - * Any attempt to use this method on a Numeric will raise a TypeError. + * Returns the receiver. + * _freeze_ cannot be +false+. */ static VALUE -num_init_copy(VALUE x, VALUE y) +num_clone(int argc, VALUE *argv, VALUE x) { - rb_raise(rb_eTypeError, "can't copy %"PRIsVALUE, rb_obj_class(x)); - - UNREACHABLE; + return rb_immutable_obj_clone(argc, argv, x); } +#else +# define num_clone rb_immutable_obj_clone +#endif + +#if 0 +/* + * call-seq: + * num.dup -> num + * + * Returns the receiver. + */ +static VALUE +num_dup(VALUE x) +{ + return x; +} +#else +# define num_dup num_uplus +#endif /* * call-seq: @@ -5222,8 +5242,9 @@ Init_Numeric(void) rb_define_method(rb_cNumeric, "singleton_method_added", num_sadded, 1); rb_include_module(rb_cNumeric, rb_mComparable); - rb_define_method(rb_cNumeric, "initialize_copy", num_init_copy, 1); rb_define_method(rb_cNumeric, "coerce", num_coerce, 1); + rb_define_method(rb_cNumeric, "clone", num_clone, -1); + rb_define_method(rb_cNumeric, "dup", num_dup, 0); rb_define_method(rb_cNumeric, "i", num_imaginary, 0); rb_define_method(rb_cNumeric, "+@", num_uplus, 0); diff --git a/object.c b/object.c index 4dad9b7491..9ec96687e9 100644 --- a/object.c +++ b/object.c @@ -309,6 +309,9 @@ special_object_p(VALUE obj) case T_BIGNUM: case T_FLOAT: case T_SYMBOL: + case T_RATIONAL: + case T_COMPLEX: + /* not a comprehensive list */ return TRUE; default: return FALSE; @@ -349,6 +352,13 @@ rb_obj_clone2(int argc, VALUE *argv, VALUE obj) return immutable_obj_clone(obj, kwfreeze); } +VALUE +rb_immutable_obj_clone(int argc, VALUE *argv, VALUE obj) +{ + int kwfreeze = freeze_opt(argc, argv); + return immutable_obj_clone(obj, kwfreeze); +} + static int freeze_opt(int argc, VALUE *argv) { diff --git a/test/ruby/test_numeric.rb b/test/ruby/test_numeric.rb index eb056f61c3..163d65334c 100644 --- a/test/ruby/test_numeric.rb +++ b/test/ruby/test_numeric.rb @@ -76,12 +76,18 @@ class TestNumeric < Test::Unit::TestCase def test_dup a = Numeric.new - assert_raise(TypeError) { a.dup } + assert_same a, a.dup + end - c = Module.new do - break eval("class C\u{3042} < Numeric; self; end") + def test_clone + a = Numeric.new + assert_same a, a.clone + assert_raise(ArgumentError) {a.clone(freeze: false)} + + c = EnvUtil.labeled_class("\u{1f4a9}", Numeric) + assert_raise_with_message(ArgumentError, /\u{1f4a9}/) do + c.new.clone(freeze: false) end - assert_raise_with_message(TypeError, /C\u3042/) {c.new.dup} end def test_quo