Skip to content

Commit 90da29f

Browse files
committed
Address review comments.
1 parent 1981f24 commit 90da29f

File tree

2 files changed

+42
-60
lines changed

2 files changed

+42
-60
lines changed

Lib/test/test_bisect.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from collections import UserList
55

66
py_bisect = support.import_fresh_module('bisect', blocked=['_bisect'])
7-
rust_bisect = support.import_fresh_module('bisect', fresh=['bisect'])
7+
c_bisect = support.import_fresh_module('bisect', fresh=['bisect'])
88

99
class Range(object):
1010
"""A trivial range()-like object that has an insert() method."""
@@ -202,8 +202,8 @@ def test_keyword_args(self):
202202
class TestBisectPython(TestBisect, unittest.TestCase):
203203
module = py_bisect
204204

205-
class TestBisectRust(TestBisect, unittest.TestCase):
206-
module = rust_bisect
205+
class TestBisectC(TestBisect, unittest.TestCase):
206+
module = c_bisect
207207

208208
#==============================================================================
209209

@@ -237,8 +237,8 @@ def insert(self, index, item):
237237
class TestInsortPython(TestInsort, unittest.TestCase):
238238
module = py_bisect
239239

240-
class TestInsortRust(TestInsort, unittest.TestCase):
241-
module = rust_bisect
240+
class TestInsortC(TestInsort, unittest.TestCase):
241+
module = c_bisect
242242

243243
#==============================================================================
244244

@@ -292,8 +292,8 @@ def test_arg_parsing(self):
292292
class TestErrorHandlingPython(TestErrorHandling, unittest.TestCase):
293293
module = py_bisect
294294

295-
class TestErrorHandlingRust(TestErrorHandling, unittest.TestCase):
296-
module = rust_bisect
295+
class TestErrorHandlingC(TestErrorHandling, unittest.TestCase):
296+
module = c_bisect
297297

298298
#==============================================================================
299299

@@ -319,8 +319,8 @@ def test_colors(self):
319319
class TestDocExamplePython(TestDocExample, unittest.TestCase):
320320
module = py_bisect
321321

322-
class TestDocExampleRust(TestDocExample, unittest.TestCase):
323-
module = rust_bisect
322+
class TestDocExampleC(TestDocExample, unittest.TestCase):
323+
module = c_bisect
324324

325325
#------------------------------------------------------------------------------
326326

vm/src/stdlib/bisect.rs

+33-51
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,20 @@ mod _bisect {
1212

1313
#[derive(FromArgs)]
1414
struct BisectArgs {
15-
#[pyarg(any, name = "a")]
15+
#[pyarg(any)]
1616
a: PyObjectRef,
17-
#[pyarg(any, name = "x")]
17+
#[pyarg(any)]
1818
x: PyObjectRef,
19-
#[pyarg(any, optional, name = "lo")]
19+
#[pyarg(any, optional)]
2020
lo: OptionalArg<PyObjectRef>,
21-
#[pyarg(any, optional, name = "hi")]
21+
#[pyarg(any, optional)]
2222
hi: OptionalArg<PyObjectRef>,
2323
}
2424

2525
// Handles objects that implement __index__ and makes sure index fits in needed isize.
2626
#[inline]
2727
fn handle_default(
2828
arg: OptionalArg<PyObjectRef>,
29-
default: Option<isize>,
3029
vm: &VirtualMachine,
3130
) -> PyResult<Option<isize>> {
3231
Ok(match arg {
@@ -35,7 +34,7 @@ mod _bisect {
3534
vm.new_index_error("cannot fit 'int' into an index-sized integer".to_owned())
3635
})?)
3736
}
38-
OptionalArg::Missing => default,
37+
OptionalArg::Missing => None,
3938
})
4039
}
4140

@@ -55,29 +54,29 @@ mod _bisect {
5554
) -> PyResult<(usize, usize)> {
5655
// We only deal with positives for lo, try_from can't fail.
5756
// Default is always a Some so we can safely unwrap.
58-
let lo = handle_default(lo, Some(0), vm)?
57+
let lo = handle_default(lo, vm)?
5958
.map(|value| {
60-
if value < 0 {
61-
return Err(vm.new_value_error("lo must be non-negative".to_owned()));
62-
}
63-
Ok(usize::try_from(value).unwrap())
64-
})
65-
.unwrap()?;
66-
let hi = handle_default(hi, None, vm)?
67-
.map(|value| {
68-
if value < 0 {
69-
0
70-
} else {
71-
// Can't fail.
72-
usize::try_from(value).unwrap()
73-
}
59+
usize::try_from(value)
60+
.map_err(|_| vm.new_value_error("lo must be non-negative".to_owned()))
7461
})
62+
.unwrap_or(Ok(0))?;
63+
let hi = handle_default(hi, vm)?
64+
.map(|value| usize::try_from(value).unwrap_or(0))
7565
.unwrap_or(seq_len);
7666
Ok((lo, hi))
7767
}
7868

69+
/// Return the index where to insert item x in list a, assuming a is sorted.
70+
///
71+
/// The return value i is such that all e in a[:i] have e < x, and all e in
72+
/// a[i:] have e >= x. So if x already appears in the list, a.insert(x) will
73+
/// insert just before the leftmost x already there.
74+
///
75+
/// Optional args lo (default 0) and hi (default len(a)) bound the
76+
/// slice of a to be searched.
7977
#[inline]
80-
fn bisect_left_impl(
78+
#[pyfunction]
79+
fn bisect_left(
8180
BisectArgs { a, x, lo, hi }: BisectArgs,
8281
vm: &VirtualMachine,
8382
) -> PyResult<usize> {
@@ -95,8 +94,17 @@ mod _bisect {
9594
Ok(lo)
9695
}
9796

97+
/// Return the index where to insert item x in list a, assuming a is sorted.
98+
///
99+
/// The return value i is such that all e in a[:i] have e <= x, and all e in
100+
/// a[i:] have e > x. So if x already appears in the list, a.insert(x) will
101+
/// insert just after the rightmost x already there.
102+
///
103+
/// Optional args lo (default 0) and hi (default len(a)) bound the
104+
/// slice of a to be searched.
98105
#[inline]
99-
fn bisect_right_impl(
106+
#[pyfunction]
107+
fn bisect_right(
100108
BisectArgs { a, x, lo, hi }: BisectArgs,
101109
vm: &VirtualMachine,
102110
) -> PyResult<usize> {
@@ -114,32 +122,6 @@ mod _bisect {
114122
Ok(lo)
115123
}
116124

117-
/// Return the index where to insert item x in list a, assuming a is sorted.
118-
///
119-
/// The return value i is such that all e in a[:i] have e < x, and all e in
120-
/// a[i:] have e >= x. So if x already appears in the list, a.insert(x) will
121-
/// insert just before the leftmost x already there.
122-
///
123-
/// Optional args lo (default 0) and hi (default len(a)) bound the
124-
/// slice of a to be searched.
125-
#[pyfunction]
126-
fn bisect_left(args: BisectArgs, vm: &VirtualMachine) -> PyResult<usize> {
127-
bisect_left_impl(args, vm)
128-
}
129-
130-
/// Return the index where to insert item x in list a, assuming a is sorted.
131-
///
132-
/// The return value i is such that all e in a[:i] have e <= x, and all e in
133-
/// a[i:] have e > x. So if x already appears in the list, a.insert(x) will
134-
/// insert just after the rightmost x already there.
135-
///
136-
/// Optional args lo (default 0) and hi (default len(a)) bound the
137-
/// slice of a to be searched.
138-
#[pyfunction]
139-
fn bisect_right(args: BisectArgs, vm: &VirtualMachine) -> PyResult<usize> {
140-
bisect_right_impl(args, vm)
141-
}
142-
143125
/// Insert item x in list a, and keep it sorted assuming a is sorted.
144126
///
145127
/// If x is already in a, insert it to the left of the leftmost x.
@@ -148,7 +130,7 @@ mod _bisect {
148130
/// slice of a to be searched.
149131
#[pyfunction]
150132
fn insort_left(BisectArgs { a, x, lo, hi }: BisectArgs, vm: &VirtualMachine) -> PyResult {
151-
let index = bisect_left_impl(
133+
let index = bisect_left(
152134
BisectArgs {
153135
a: a.clone(),
154136
x: x.clone(),
@@ -168,7 +150,7 @@ mod _bisect {
168150
/// slice of a to be searched
169151
#[pyfunction]
170152
fn insort_right(BisectArgs { a, x, lo, hi }: BisectArgs, vm: &VirtualMachine) -> PyResult {
171-
let index = bisect_right_impl(
153+
let index = bisect_right(
172154
BisectArgs {
173155
a: a.clone(),
174156
x: x.clone(),

0 commit comments

Comments
 (0)