Super dynamic class definition causes infinite loop


#1

e$B?\F#$G$9!#e(B

e$B0J2<$N%9%/%j%W%H$Ne(B2e$B2sL$Ne(Bdefined_class.newe$B$GL58B%k!<%W$K$J$Ce(B
e$B$F$7$^$$$^$9!#e(B


def define_class(parent=nil)
Class.new(parent || Object) do
def initialize
super
end
end
end

defined_class = define_class
p defined_class.new

define_class(defined_class)
p defined_class.new

% /tmp/local/bin/ruby -v /tmp/a.rb
ruby 1.9.0 (2008-11-09 revision 20163) [x86_64-linux]
#<#Class:0x00000000ef6c30:0x00000000ef6640>
/tmp/a.rb:4:in initialize': stack level too deep (SystemStackError) from /tmp/a.rb:4:ininitialize’
from /tmp/a.rb:4:in initialize' from /tmp/a.rb:4:ininitialize’
from /tmp/a.rb:4:in initialize' from /tmp/a.rb:4:ininitialize’
from /tmp/a.rb:4:in initialize' from /tmp/a.rb:4:ininitialize’
from /tmp/a.rb:4:in initialize' ... 8716 levels... from /tmp/a.rb:4:ininitialize’
from /tmp/a.rb:4:in initialize' from /tmp/a.rb:13:innew’
from /tmp/a.rb:13:in `’


#2

e$B?\F#$G$9!#e(B

In removed_email_address@domain.invalid
“[ruby-dev:37045] super dynamic class definition causes infinite loop”
on Sun, 9 Nov 2008 13:37:39 +0900,
Kouhei S. removed_email_address@domain.invalid wrote:

end
#<#Class:0x00000000ef6c30:0x00000000ef6640>
from /tmp/a.rb:4:in initialize' from /tmp/a.rb:4:ininitialize’
from /tmp/a.rb:13:in new' from /tmp/a.rb:13:in

e$B%F%9%H$K$7$F$_$^$7$?!#e(B

Index: test/ruby/test_class.rb

— test/ruby/test_class.rb (revision 20332)
+++ test/ruby/test_class.rb (working copy)
@@ -144,4 +144,20 @@
assert_raise(TypeError) { Class.allocate.new }
assert_raise(TypeError) { Class.allocate.superclass }
end
+

  • def test_dynamic_defined_initialize
  • define_class = Proc.new do |parent|
  •  Class.new(parent) do
    
  •    def initialize
    
  •      super
    
  •    end
    
  •  end
    
  • end
  • defined_class = define_class.call(Object)
  • assert_nothing_raised {defined_class.new}
  • define_class.call(defined_class)
  • assert_nothing_raised {defined_class.new}
  • end
    end

e$B$3$N$h$&$K<:GT$7$^$9!#e(B

% ./ruby test/runner.rb -ntest_dynamic_defined_initialize
test/ruby/test_class.rb
Loaded suite test/runner
Started
F
Finished in 0.046818 seconds.

  1. Failure:
    test_dynamic_defined_initialize(TestClass)
    [/home/kou/work/ruby/ruby/test/ruby/test_class.rb:152]:
    Exception raised:
    <#<SystemStackError: stack level too deep>>.

1 tests, 0 assertions, 1 failures, 0 errors, 0 skips