diff --git a/lib/pycall/pytypeobject_wrapper.rb b/lib/pycall/pytypeobject_wrapper.rb index cb88a082..3518930c 100644 --- a/lib/pycall/pytypeobject_wrapper.rb +++ b/lib/pycall/pytypeobject_wrapper.rb @@ -20,12 +20,14 @@ def inherited(subclass) subclass.instance_variable_set(:@__pyptr__, __pyptr__) end - def new(*args) - wrap_pyptr(LibPython::Helpers.call_object(__pyptr__, *args)) + def new(*args, &b) + wrap_pyptr(LibPython::Helpers.call_object(__pyptr__, *args)).tap do |obj| + obj.instance_eval { initialize(&b) } + end end def wrap_pyptr(pyptr) - return pyptr if pyptr.kind_of? self + return pyptr if pyptr.class <= self pyptr = pyptr.__pyptr__ if pyptr.kind_of? PyObjectWrapper unless pyptr.kind_of? PyPtr raise TypeError, "unexpected argument type #{pyptr.class} (expected PyCall::PyPtr)" diff --git a/spec/pycall/subclass_spec.rb b/spec/pycall/subclass_spec.rb index e9973853..e91f56e4 100644 --- a/spec/pycall/subclass_spec.rb +++ b/spec/pycall/subclass_spec.rb @@ -13,6 +13,10 @@ Class.new(superclass_wrapper) end + it 'is an instance of subclass' do + expect(subclass.new.class).to eq(subclass) + end + it 'calls __init__ of superclass' do a = subclass.new(1, 2, 3) expect(a.init_args.to_a).to eq([1, 2, 3])