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
This commit is contained in:
nobu 2017-02-22 02:02:11 +00:00
Родитель 4096f39dcf
Коммит 31ef3124a9
4 изменённых файлов: 49 добавлений и 11 удалений

Просмотреть файл

@ -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;

Просмотреть файл

@ -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));
UNREACHABLE;
return rb_immutable_obj_clone(argc, argv, x);
}
#else
# define num_clone rb_immutable_obj_clone
#endif
#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);

Просмотреть файл

@ -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)
{

Просмотреть файл

@ -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
c = Module.new do
break eval("class C\u{3042} < Numeric; self; end")
def test_clone
a = Numeric.new
assert_same a, a.clone
assert_raise(ArgumentError) {a.clone(freeze: false)}
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