Skip to content

Commit 5d34153

Browse files
committed
(wip) Keep instance class created by new method
1 parent 0b8b678 commit 5d34153

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

lib/pycall/pytypeobject_wrapper.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ def inherited(subclass)
2020
subclass.instance_variable_set(:@__pyptr__, __pyptr__)
2121
end
2222

23-
def new(*args)
24-
wrap_pyptr(LibPython::Helpers.call_object(__pyptr__, *args))
23+
def new(*args, &b)
24+
wrap_pyptr(LibPython::Helpers.call_object(__pyptr__, *args)).tap do |obj|
25+
obj.instance_eval { initialize(&b) }
26+
end
2527
end
2628

2729
def wrap_pyptr(pyptr)
28-
return pyptr if pyptr.kind_of? self
30+
return pyptr if pyptr.class <= self
2931
pyptr = pyptr.__pyptr__ if pyptr.kind_of? PyObjectWrapper
3032
unless pyptr.kind_of? PyPtr
3133
raise TypeError, "unexpected argument type #{pyptr.class} (expected PyCall::PyPtr)"

spec/pycall/subclass_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
Class.new(superclass_wrapper)
1414
end
1515

16+
it 'is an instance of subclass' do
17+
expect(subclass.new.class).to eq(subclass)
18+
end
19+
1620
it 'calls __init__ of superclass' do
1721
a = subclass.new(1, 2, 3)
1822
expect(a.init_args.to_a).to eq([1, 2, 3])

0 commit comments

Comments
 (0)