ruby-changes:38480
From: nagachika <ko1@a...>
Date: Thu, 21 May 2015 00:51:44 +0900 (JST)
Subject: [ruby-changes:38480] nagachika:r50561 (ruby_2_2): merge revision(s) 50372, 50373: [Backport #11086]
nagachika 2015-05-21 00:51:27 +0900 (Thu, 21 May 2015) New Revision: 50561 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=50561 Log: merge revision(s) 50372,50373: [Backport #11086] * vm_eval.c (rb_obj_instance_eval, rb_obj_instance_exec): allow symbols to just instance_eval/exec, except for definition of singletons. [ruby-core:68961] [Bug #11086] Modified directories: branches/ruby_2_2/ Modified files: branches/ruby_2_2/ChangeLog branches/ruby_2_2/class.c branches/ruby_2_2/test/ruby/test_symbol.rb branches/ruby_2_2/version.h branches/ruby_2_2/vm_eval.c Index: ruby_2_2/ChangeLog =================================================================== --- ruby_2_2/ChangeLog (revision 50560) +++ ruby_2_2/ChangeLog (revision 50561) @@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_2/ChangeLog#L1 +Thu May 21 00:07:54 2015 Nobuyoshi Nakada <nobu@r...> + + * vm_eval.c (rb_obj_instance_eval, rb_obj_instance_exec): allow + symbols to just instance_eval/exec, except for definition of + singletons. [ruby-core:68961] [Bug #11086] + Wed May 20 04:33:50 2015 Nobuyoshi Nakada <nobu@r...> * string.c (STR_SET_EMBED): clear NOFREE flag at embedding as Index: ruby_2_2/vm_eval.c =================================================================== --- ruby_2_2/vm_eval.c (revision 50560) +++ ruby_2_2/vm_eval.c (revision 50561) @@ -1629,6 +1629,20 @@ specific_eval(int argc, const VALUE *arg https://github.com/ruby/ruby/blob/trunk/ruby_2_2/vm_eval.c#L1629 } } +static VALUE +singleton_class_for_eval(VALUE self) +{ + if (SPECIAL_CONST_P(self)) { + return rb_special_singleton_class(self); + } + switch (BUILTIN_TYPE(self)) { + case T_FLOAT: case T_BIGNUM: case T_SYMBOL: + return Qnil; + default: + return rb_singleton_class(self); + } +} + /* * call-seq: * obj.instance_eval(string [, filename [, lineno]] ) -> obj @@ -1665,14 +1679,7 @@ specific_eval(int argc, const VALUE *arg https://github.com/ruby/ruby/blob/trunk/ruby_2_2/vm_eval.c#L1679 VALUE rb_obj_instance_eval(int argc, const VALUE *argv, VALUE self) { - VALUE klass; - - if (SPECIAL_CONST_P(self)) { - klass = rb_special_singleton_class(self); - } - else { - klass = rb_singleton_class(self); - } + VALUE klass = singleton_class_for_eval(self); return specific_eval(argc, argv, klass, self); } @@ -1697,14 +1704,7 @@ rb_obj_instance_eval(int argc, const VAL https://github.com/ruby/ruby/blob/trunk/ruby_2_2/vm_eval.c#L1704 VALUE rb_obj_instance_exec(int argc, const VALUE *argv, VALUE self) { - VALUE klass; - - if (SPECIAL_CONST_P(self)) { - klass = rb_special_singleton_class(self); - } - else { - klass = rb_singleton_class(self); - } + VALUE klass = singleton_class_for_eval(self); return yield_under(klass, self, rb_ary_new4(argc, argv)); } Index: ruby_2_2/class.c =================================================================== --- ruby_2_2/class.c (revision 50560) +++ ruby_2_2/class.c (revision 50561) @@ -1531,7 +1531,8 @@ singleton_class_of(VALUE obj) https://github.com/ruby/ruby/blob/trunk/ruby_2_2/class.c#L1531 { VALUE klass; - if (FIXNUM_P(obj) || FLONUM_P(obj) || SYMBOL_P(obj)) { + if (FIXNUM_P(obj) || FLONUM_P(obj) || STATIC_SYM_P(obj)) { + no_singleton: rb_raise(rb_eTypeError, "can't define singleton"); } if (SPECIAL_CONST_P(obj)) { @@ -1541,9 +1542,9 @@ singleton_class_of(VALUE obj) https://github.com/ruby/ruby/blob/trunk/ruby_2_2/class.c#L1542 return klass; } else { - enum ruby_value_type type = BUILTIN_TYPE(obj); - if (type == T_FLOAT || type == T_BIGNUM) { - rb_raise(rb_eTypeError, "can't define singleton"); + switch (BUILTIN_TYPE(obj)) { + case T_FLOAT: case T_BIGNUM: case T_SYMBOL: + goto no_singleton; } } Index: ruby_2_2/version.h =================================================================== --- ruby_2_2/version.h (revision 50560) +++ ruby_2_2/version.h (revision 50561) @@ -1,10 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_2/version.h#L1 #define RUBY_VERSION "2.2.3" -#define RUBY_RELEASE_DATE "2015-05-20" -#define RUBY_PATCHLEVEL 110 +#define RUBY_RELEASE_DATE "2015-05-21" +#define RUBY_PATCHLEVEL 111 #define RUBY_RELEASE_YEAR 2015 #define RUBY_RELEASE_MONTH 5 -#define RUBY_RELEASE_DAY 20 +#define RUBY_RELEASE_DAY 21 #include "ruby/version.h" Index: ruby_2_2/test/ruby/test_symbol.rb =================================================================== --- ruby_2_2/test/ruby/test_symbol.rb (revision 50560) +++ ruby_2_2/test/ruby/test_symbol.rb (revision 50561) @@ -199,6 +199,35 @@ class TestSymbol < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_2_2/test/ruby/test_symbol.rb#L199 assert_raise(TypeError) { a = :foo; def a.foo; end } end + SymbolsForEval = [ + :foo, + "dynsym_#{Random.rand(10000)}_#{Time.now}".to_sym + ] + + def test_instance_eval + bug11086 = '[ruby-core:68961] [Bug #11086]' + SymbolsForEval.each do |sym| + assert_nothing_raised(TypeError, sym, bug11086) { + sym.instance_eval {} + } + assert_raise(TypeError, sym, bug11086) { + sym.instance_eval {def foo; end} + } + end + end + + def test_instance_exec + bug11086 = '[ruby-core:68961] [Bug #11086]' + SymbolsForEval.each do |sym| + assert_nothing_raised(TypeError, sym, bug11086) { + sym.instance_exec {} + } + assert_raise(TypeError, sym, bug11086) { + sym.instance_exec {def foo; end} + } + end + end + def test_frozen_symbol assert_equal(true, :foo.frozen?) assert_equal(true, :each.frozen?) Property changes on: ruby_2_2 ___________________________________________________________________ Modified: svn:mergeinfo Merged /trunk:r50372-50373 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/