From b3803cc49ad382e23291d75ce57ffb2b74bb9577 Mon Sep 17 00:00:00 2001 From: tenderlove Date: Tue, 16 Oct 2012 21:33:59 +0000 Subject: [PATCH] * 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 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37232 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +++++++ hash.c | 17 ++++++++++++++++- test/ruby/test_hash.rb | 9 +++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 3ae1756137..3fee39c7f1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Wed Oct 17 06:25:56 2012 Aaron Patterson + + * 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 * vm_insnhelper.c (vm_call_iseq_setup_2): separate tailcall and normal diff --git a/hash.c b/hash.c index 06d0ce9e68..5172e13b57 100644 --- a/hash.c +++ b/hash.c @@ -1182,6 +1182,21 @@ replace_i(VALUE key, VALUE val, VALUE hash) 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 @@ Init_Hash(void) 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); diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb index 34d58a0354..1806746ef2 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -91,6 +91,15 @@ class TestHash < Test::Unit::TestCase $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'])