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

ruby-changes:31354

From: nobu <ko1@a...>
Date: Sat, 26 Oct 2013 19:08:06 +0900 (JST)
Subject: [ruby-changes:31354] nobu:r43433 (trunk): range.c: modify check

nobu	2013-10-26 19:08:02 +0900 (Sat, 26 Oct 2013)

  New Revision: 43433

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

  Log:
    range.c: modify check
    
    * range.c (range_initialize_copy): disallow to modify after
      initialized.

  Modified files:
    trunk/ChangeLog
    trunk/range.c
    trunk/test/ruby/test_range.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 43432)
+++ ChangeLog	(revision 43433)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Oct 26 19:08:00 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* range.c (range_initialize_copy): disallow to modify after
+	  initialized.
+
 Sat Oct 26 17:48:54 2013  Tanaka Akira  <akr@f...>
 
 	* lib/open-uri.rb (meta_add_field): : Re-implemented.
Index: range.c
===================================================================
--- range.c	(revision 43432)
+++ range.c	(revision 43433)
@@ -82,6 +82,15 @@ rb_range_new(VALUE beg, VALUE end, int e https://github.com/ruby/ruby/blob/trunk/range.c#L82
     return range;
 }
 
+static void
+range_modify(VALUE range)
+{
+    /* Ranges are immutable, so that they should be initialized only once. */
+    if (RANGE_EXCL(range) != Qnil) {
+	rb_name_error(idInitialize, "`initialize' called twice");
+    }
+}
+
 /*
  *  call-seq:
  *     Range.new(begin, end, exclude_end=false)    -> rng
@@ -97,15 +106,19 @@ range_initialize(int argc, VALUE *argv, https://github.com/ruby/ruby/blob/trunk/range.c#L106
     VALUE beg, end, flags;
 
     rb_scan_args(argc, argv, "21", &beg, &end, &flags);
-    /* Ranges are immutable, so that they should be initialized only once. */
-    if (RANGE_EXCL(range) != Qnil) {
-	rb_name_error(idInitialize, "`initialize' called twice");
-    }
+    range_modify(range);
     range_init(range, beg, end, RBOOL(RTEST(flags)));
     return Qnil;
 }
 
-#define range_initialize_copy rb_struct_init_copy /* :nodoc: */
+/* :nodoc: */
+static VALUE
+range_initialize_copy(VALUE range, VALUE orig)
+{
+    range_modify(range);
+    rb_struct_init_copy(range, orig);
+    return range;
+}
 
 /*
  *  call-seq:
@@ -1244,6 +1257,7 @@ range_loader(VALUE range, VALUE obj) https://github.com/ruby/ruby/blob/trunk/range.c#L1257
         rb_raise(rb_eTypeError, "not a dumped range object");
     }
 
+    range_modify(range);
     RANGE_SET_BEG(range, rb_ivar_get(obj, id_beg));
     RANGE_SET_END(range, rb_ivar_get(obj, id_end));
     RANGE_SET_EXCL(range, rb_ivar_get(obj, id_excl));
Index: test/ruby/test_range.rb
===================================================================
--- test/ruby/test_range.rb	(revision 43432)
+++ test/ruby/test_range.rb	(revision 43433)
@@ -82,6 +82,7 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L82
   def test_initialize_twice
     r = eval("1..2")
     assert_raise(NameError) { r.instance_eval { initialize 3, 4 } }
+    assert_raise(NameError) { r.instance_eval { initialize_copy 3..4 } }
   end
 
   def test_uninitialized_range

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

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