[前][次][番号順一覧][スレッド一覧]

ruby-changes:44856

From: nobu <ko1@a...>
Date: Wed, 30 Nov 2016 00:14:10 +0900 (JST)
Subject: [ruby-changes:44856] nobu:r56929 (trunk): object.c: no TypeError at special const clone

nobu	2016-11-30 00:14:05 +0900 (Wed, 30 Nov 2016)

  New Revision: 56929

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56929

  Log:
    object.c: no TypeError at special const clone
    
    * object.c (rb_obj_clone2): no longer raise a TypeError for
      special constants, and return itself instead.  however, if
      freeze option is false, raise an ArgumentError.  [Feature#12979]

  Modified files:
    trunk/object.c
    trunk/test/ruby/test_object.rb
Index: test/ruby/test_object.rb
===================================================================
--- test/ruby/test_object.rb	(revision 56928)
+++ test/ruby/test_object.rb	(revision 56929)
@@ -22,6 +22,7 @@ class TestObject < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_object.rb#L22
     assert_equal 1, 1.dup
     assert_equal true, true.dup
     assert_equal nil, nil.dup
+    assert_equal false, false.dup
 
     assert_raise(TypeError) do
       Object.new.instance_eval { initialize_copy(1) }
@@ -37,11 +38,21 @@ class TestObject < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_object.rb#L38
     assert_equal(true, c.frozen?)
     assert_equal(2, c.b)
 
+    assert_raise(ArgumentError) {a.clone(freeze: [])}
     d = a.clone(freeze: false)
     def d.e; 3; end
     assert_equal(false, d.frozen?)
     assert_equal(2, d.b)
     assert_equal(3, d.e)
+
+    assert_equal 1, 1.clone
+    assert_equal true, true.clone
+    assert_equal nil, nil.clone
+    assert_equal false, false.clone
+    assert_raise(ArgumentError) {1.clone(freeze: false)}
+    assert_raise(ArgumentError) {true.clone(freeze: false)}
+    assert_raise(ArgumentError) {nil.clone(freeze: false)}
+    assert_raise(ArgumentError) {false.clone(freeze: false)}
   end
 
   def test_init_dupclone
Index: object.c
===================================================================
--- object.c	(revision 56928)
+++ object.c	(revision 56929)
@@ -346,7 +346,9 @@ rb_obj_clone2(int argc, VALUE *argv, VAL https://github.com/ruby/ruby/blob/trunk/object.c#L346
     }
 
     if (rb_special_const_p(obj)) {
-        rb_raise(rb_eTypeError, "can't clone %s", rb_obj_classname(obj));
+	if (kwfreeze == Qfalse)
+	    rb_raise(rb_eArgError, "can't unfreeze %s", rb_obj_classname(obj));
+	return obj;
     }
     clone = rb_obj_alloc(rb_obj_class(obj));
     RBASIC(clone)->flags &= (FL_TAINT|FL_PROMOTED0|FL_PROMOTED1);

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]