Skip to content

Commit 81a318c

Browse files
committed
Add PyCall.same?
1 parent 7848e94 commit 81a318c

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

lib/pycall.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ def hasattr?(obj, name)
5858
LibPython::Helpers.hasattr?(obj.__pyptr__, name)
5959
end
6060

61+
def same?(left, right)
62+
case left
63+
when PyObjectWrapper
64+
case right
65+
when PyObjectWrapper
66+
return left.__pyptr__ == right.__pyptr__
67+
end
68+
end
69+
false
70+
end
71+
6172
def import_module(name)
6273
LibPython::Helpers.import_module(name)
6374
end

spec/pycall_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@
7070
end
7171
end
7272

73+
describe '.same?' do
74+
let(:simple_module) { PyCall.import_module('pycall.simple_module') }
75+
let(:simple_class) { PyCall.import_module('pycall.simple_class').SimpleClass }
76+
77+
specify do
78+
pyobj_1 = simple_class.new
79+
expect(PyCall.same?(pyobj_1, pyobj_1)).to eq(true)
80+
pyobj_2 = simple_module.identity(pyobj_1)
81+
expect(pyobj_2).not_to be_equal(pyobj_1)
82+
expect(PyCall.same?(pyobj_1, pyobj_2)).to eq(true)
83+
expect(PyCall.same?(pyobj_1, simple_class.new)).to eq(false)
84+
end
85+
end
86+
7387
describe '.import_module' do
7488
subject { PyCall.import_module('sys') }
7589

spec/python/pycall/simple_module.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
def identity(x):
2+
return x
3+
14
def double(x):
25
return 2 * x
36

0 commit comments

Comments
 (0)