From 7f30d00b53bd5ce680c13f3bcd57e8e29039aaee Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 29 Nov 2016 15:34:31 +0000 Subject: [PATCH] object.c: no TypeError at special const * object.c (special_object_p): no longer raise a TypeError for Integer and Float, and return itself instead. [Feature#12979] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56933 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- object.c | 17 +++++++++++++++-- test/ruby/test_object.rb | 4 ++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/object.c b/object.c index a1dcf830e7..5f0055fb5a 100644 --- a/object.c +++ b/object.c @@ -297,6 +297,19 @@ init_copy(VALUE dest, VALUE obj) } } +static inline int +special_object_p(VALUE obj) +{ + if (SPECIAL_CONST_P(obj)) return TRUE; + switch (BUILTIN_TYPE(obj)) { + case T_BIGNUM: + case T_FLOAT: + return TRUE; + default: + return FALSE; + } +} + /* * call-seq: * obj.clone(freeze: true) -> an_object @@ -345,7 +358,7 @@ rb_obj_clone2(int argc, VALUE *argv, VALUE obj) } } - if (rb_special_const_p(obj)) { + if (special_object_p(obj)) { if (kwfreeze == Qfalse) rb_raise(rb_eArgError, "can't unfreeze %s", rb_obj_classname(obj)); return obj; @@ -424,7 +437,7 @@ rb_obj_dup(VALUE obj) { VALUE dup; - if (rb_special_const_p(obj)) { + if (special_object_p(obj)) { return obj; } dup = rb_obj_alloc(rb_obj_class(obj)); diff --git a/test/ruby/test_object.rb b/test/ruby/test_object.rb index dbe7e442ba..c1721de39c 100644 --- a/test/ruby/test_object.rb +++ b/test/ruby/test_object.rb @@ -23,6 +23,8 @@ class TestObject < Test::Unit::TestCase assert_equal true, true.dup assert_equal nil, nil.dup assert_equal false, false.dup + x = 1 << 64; assert_equal x, x.dup + x = 1.72723e-77; assert_equal x, x.dup assert_raise(TypeError) do Object.new.instance_eval { initialize_copy(1) } @@ -49,6 +51,8 @@ class TestObject < Test::Unit::TestCase assert_equal true, true.clone assert_equal nil, nil.clone assert_equal false, false.clone + x = 1 << 64; assert_equal x, x.clone + x = 1.72723e-77; assert_equal x, x.clone assert_raise(ArgumentError) {1.clone(freeze: false)} assert_raise(ArgumentError) {true.clone(freeze: false)} assert_raise(ArgumentError) {nil.clone(freeze: false)}