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

ruby-changes:25180

From: tenderlove <ko1@a...>
Date: Wed, 17 Oct 2012 06:39:04 +0900 (JST)
Subject: [ruby-changes:25180] tenderlove:r37232 (trunk): * hash.c (initialize_copy): copy the underlying st_table on dup,

tenderlove	2012-10-17 06:33:59 +0900 (Wed, 17 Oct 2012)

  New Revision: 37232

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=37232

  Log:
    * hash.c (initialize_copy): copy the underlying st_table on dup,
      rather than copying the hash key by key. [ruby-core:48009]
    
    * test/ruby/test_hash.rb: relevant tests for initialize_copy

  Modified files:
    trunk/ChangeLog
    trunk/hash.c
    trunk/test/ruby/test_hash.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 37231)
+++ ChangeLog	(revision 37232)
@@ -1,3 +1,10 @@
+Wed Oct 17 06:25:56 2012  Aaron Patterson <aaron@t...>
+
+	* hash.c (initialize_copy): copy the underlying st_table on dup,
+	  rather than copying the hash key by key. [ruby-core:48009]
+
+	* test/ruby/test_hash.rb: relevant tests for initialize_copy
+
 Wed Oct 17 06:17:44 2012  Koichi Sasada  <ko1@a...>
 
 	* vm_insnhelper.c (vm_call_iseq_setup_2): separate tailcall and normal
Index: hash.c
===================================================================
--- hash.c	(revision 37231)
+++ hash.c	(revision 37232)
@@ -1182,6 +1182,21 @@
     return ST_CONTINUE;
 }
 
+static VALUE
+rb_hash_initialize_copy(VALUE hash, VALUE hash2)
+{
+    Check_Type(hash2, T_HASH);
+
+    if (!RHASH_EMPTY_P(hash2))
+        RHASH(hash)->ntbl = st_copy(RHASH(hash2)->ntbl);
+    if (FL_TEST(hash2, HASH_PROC_DEFAULT)) {
+        FL_SET(hash, HASH_PROC_DEFAULT);
+    }
+    RHASH_IFNONE(hash) = RHASH_IFNONE(hash2);
+
+    return hash;
+}
+
 /*
  *  call-seq:
  *     hsh.replace(other_hash) -> hsh
@@ -3359,7 +3374,7 @@
     rb_define_singleton_method(rb_cHash, "[]", rb_hash_s_create, -1);
     rb_define_singleton_method(rb_cHash, "try_convert", rb_hash_s_try_convert, 1);
     rb_define_method(rb_cHash,"initialize", rb_hash_initialize, -1);
-    rb_define_method(rb_cHash,"initialize_copy", rb_hash_replace, 1);
+    rb_define_method(rb_cHash,"initialize_copy", rb_hash_initialize_copy, 1);
     rb_define_method(rb_cHash,"rehash", rb_hash_rehash, 0);
 
     rb_define_method(rb_cHash,"to_hash", rb_hash_to_hash, 0);
Index: test/ruby/test_hash.rb
===================================================================
--- test/ruby/test_hash.rb	(revision 37231)
+++ test/ruby/test_hash.rb	(revision 37232)
@@ -91,6 +91,15 @@
     $VERBOSE = @verbose
   end
 
+  def test_bad_initialize_copy
+    h = Class.new(Hash) {
+      def initialize_copy(h)
+        super(Object.new)
+      end
+    }.new
+    assert_raises(TypeError) { h.dup }
+  end
+
   def test_s_AREF
     h = @cls["a" => 100, "b" => 200]
     assert_equal(100, h['a'])

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

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