Skip to content

Commit c8ba36d

Browse files
committed
Fix conversion from range to slice
1 parent 9d76062 commit c8ba36d

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

ext/pycall/pycall.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1778,7 +1778,16 @@ pycall_pyslice_from_ruby(VALUE obj)
17781778
}
17791779

17801780
if (!exclude_end) {
1781-
end = SSIZET2NUM(NUM2SSIZET(end) + 1); /* TODO: limit check */
1781+
ssize_t end_i = NUM2SSIZET(end);
1782+
switch (end_i) {
1783+
case -1:
1784+
end = Qnil;
1785+
break;
1786+
1787+
default:
1788+
end = SSIZET2NUM(end_i + 1); /* TODO: limit check */
1789+
break;
1790+
}
17821791
}
17831792

17841793
pystart = pycall_pyobject_from_ruby(begin);

spec/pycall/pyobject_wrapper_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ module PyCall
146146
context 'when the given index is a Range' do
147147
specify do
148148
list = PyCall::List.new([*1..10])
149+
expect(list[0..0]).to eq(PyCall::List.new([1]))
150+
expect(list[0...0]).to eq(PyCall::List.new([]))
151+
expect(list[0..-1]).to eq(PyCall::List.new([*1..10]))
152+
expect(list[0...-1]).to eq(PyCall::List.new([*1..9]))
149153
expect(list[1..-2]).to eq(PyCall::List.new([*2..9]))
150154
expect(list[1...-2]).to eq(PyCall::List.new([*2..8]))
151155
end
@@ -154,6 +158,7 @@ module PyCall
154158
context 'when the given index is an Enumerable that is created by Range#step' do
155159
specify do
156160
list = PyCall::List.new([*1..10])
161+
expect(list[(1..-1).step(2)]).to eq(PyCall::List.new([2, 4, 6, 8, 10]))
157162
expect(list[(1..-2).step(2)]).to eq(PyCall::List.new([2, 4, 6, 8]))
158163
end
159164
end

0 commit comments

Comments
 (0)