aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-02-22 02:02:11 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-02-22 02:02:11 +0000
commit77f850187106a0094a016b7ee52704b19f8532b0 (patch)
treeb1c6f0327fb570019586b007bcce785d6c495b99
parent9271c4510b8dc04a12f5c8dca7d3db1aa5fbb84c (diff)
downloadruby-77f850187106a0094a016b7ee52704b19f8532b0.tar.gz
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
-rw-r--r--internal.h1
-rw-r--r--numeric.c33
-rw-r--r--object.c10
-rw-r--r--test/ruby/test_numeric.rb14
4 files changed, 48 insertions, 10 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));
+ return rb_immutable_obj_clone(argc, argv, x);
+}
+#else
+# define num_clone rb_immutable_obj_clone
+#endif
- UNREACHABLE;
+#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
+
+ def test_clone
+ a = Numeric.new
+ assert_same a, a.clone
+ assert_raise(ArgumentError) {a.clone(freeze: false)}
- c = Module.new do
- break eval("class C\u{3042} < Numeric; self; end")
+ 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