Skip to content

Commit

Permalink
(wip) Keep instance class created by new method
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkn committed Sep 10, 2017
1 parent 0b8b678 commit 5d34153
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/pycall/pytypeobject_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down
4 changes: 4 additions & 0 deletions spec/pycall/subclass_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit 5d34153

Please sign in to comment.