|
1 | 1 | use crate::{ |
2 | | - builtins::{PyIntRef, PyTupleRef}, |
| 2 | + builtins::{PyIntRef, PyTuple}, |
3 | 3 | cformat::cformat_string, |
| 4 | + convert::TryFromBorrowedObject, |
4 | 5 | function::OptionalOption, |
5 | | - PyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine, |
| 6 | + Py, PyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine, |
6 | 7 | }; |
7 | 8 | use num_traits::{cast::ToPrimitive, sign::Signed}; |
8 | 9 |
|
@@ -213,21 +214,21 @@ pub trait AnyStr { |
213 | 214 | F: Fn(&Self) -> PyObjectRef; |
214 | 215 |
|
215 | 216 | #[inline] |
216 | | - fn py_startsendswith<T, F>( |
| 217 | + fn py_startsendswith<'a, T, F>( |
217 | 218 | &self, |
218 | | - affix: PyObjectRef, |
| 219 | + affix: &'a PyObject, |
219 | 220 | func_name: &str, |
220 | 221 | py_type_name: &str, |
221 | 222 | func: F, |
222 | 223 | vm: &VirtualMachine, |
223 | 224 | ) -> PyResult<bool> |
224 | 225 | where |
225 | | - T: TryFromObject, |
226 | | - F: Fn(&Self, &T) -> bool, |
| 226 | + T: TryFromBorrowedObject<'a>, |
| 227 | + F: Fn(&Self, T) -> bool, |
227 | 228 | { |
228 | 229 | single_or_tuple_any( |
229 | 230 | affix, |
230 | | - &|s: &T| Ok(func(self, s)), |
| 231 | + &|s: T| Ok(func(self, s)), |
231 | 232 | &|o| { |
232 | 233 | format!( |
233 | 234 | "{} first arg must be {} or a tuple of {}, not {}", |
@@ -448,24 +449,25 @@ pub trait AnyStr { |
448 | 449 | /// test that any of the values contained within the tuples satisfies the predicate. Type parameter |
449 | 450 | /// T specifies the type that is expected, if the input value is not of that type or a tuple of |
450 | 451 | /// values of that type, then a TypeError is raised. |
451 | | -pub fn single_or_tuple_any<T, F, M>( |
452 | | - obj: PyObjectRef, |
| 452 | +pub fn single_or_tuple_any<'a, T, F, M>( |
| 453 | + obj: &'a PyObject, |
453 | 454 | predicate: &F, |
454 | 455 | message: &M, |
455 | 456 | vm: &VirtualMachine, |
456 | 457 | ) -> PyResult<bool> |
457 | 458 | where |
458 | | - T: TryFromObject, |
459 | | - F: Fn(&T) -> PyResult<bool>, |
| 459 | + T: TryFromBorrowedObject<'a>, |
| 460 | + F: Fn(T) -> PyResult<bool>, |
460 | 461 | M: Fn(&PyObject) -> String, |
461 | 462 | { |
462 | | - match T::try_from_object(vm, obj.clone()) { |
463 | | - Ok(single) => (predicate)(&single), |
| 463 | + match obj.try_to_value::<T>(vm) { |
| 464 | + Ok(single) => (predicate)(single), |
464 | 465 | Err(_) => { |
465 | | - let tuple = PyTupleRef::try_from_object(vm, obj.clone()) |
466 | | - .map_err(|_| vm.new_type_error((message)(&obj)))?; |
467 | | - for obj in &tuple { |
468 | | - if single_or_tuple_any(obj.clone(), predicate, message, vm)? { |
| 466 | + let tuple: &Py<PyTuple> = obj |
| 467 | + .try_to_value(vm) |
| 468 | + .map_err(|_| vm.new_type_error((message)(obj)))?; |
| 469 | + for obj in tuple { |
| 470 | + if single_or_tuple_any(obj, predicate, message, vm)? { |
469 | 471 | return Ok(true); |
470 | 472 | } |
471 | 473 | } |
|
0 commit comments