mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Merge pull request #1511 from youknowone/pyresult
Result<T, PyObjectRef> -> PyResult<T>
This commit is contained in:
@@ -100,7 +100,7 @@ impl PyFuncArgs {
|
||||
key: &str,
|
||||
ty: PyClassRef,
|
||||
vm: &VirtualMachine,
|
||||
) -> Result<Option<PyObjectRef>, PyObjectRef> {
|
||||
) -> PyResult<Option<PyObjectRef>> {
|
||||
match self.get_optional_kwarg(key) {
|
||||
Some(kwarg) => {
|
||||
if isinstance(&kwarg, &ty) {
|
||||
|
||||
@@ -473,7 +473,7 @@ impl PyByteArrayRef {
|
||||
}
|
||||
|
||||
#[pymethod(name = "append")]
|
||||
fn append(self, x: PyIntRef, vm: &VirtualMachine) -> Result<(), PyObjectRef> {
|
||||
fn append(self, x: PyIntRef, vm: &VirtualMachine) -> PyResult<()> {
|
||||
self.inner
|
||||
.borrow_mut()
|
||||
.elements
|
||||
@@ -482,7 +482,7 @@ impl PyByteArrayRef {
|
||||
}
|
||||
|
||||
#[pymethod(name = "extend")]
|
||||
fn extend(self, iterable_of_ints: PyIterable, vm: &VirtualMachine) -> Result<(), PyObjectRef> {
|
||||
fn extend(self, iterable_of_ints: PyIterable, vm: &VirtualMachine) -> PyResult<()> {
|
||||
let mut inner = self.inner.borrow_mut();
|
||||
|
||||
for x in iterable_of_ints.iter(vm)? {
|
||||
|
||||
@@ -1161,7 +1161,7 @@ pub fn try_as_byte(obj: &PyObjectRef) -> Option<Vec<u8>> {
|
||||
}
|
||||
|
||||
pub trait ByteOr: ToPrimitive {
|
||||
fn byte_or(&self, vm: &VirtualMachine) -> Result<u8, PyObjectRef> {
|
||||
fn byte_or(&self, vm: &VirtualMachine) -> PyResult<u8> {
|
||||
match self.to_u8() {
|
||||
Some(value) => Ok(value),
|
||||
None => Err(vm.new_value_error("byte must be in range(0, 256)".to_string())),
|
||||
|
||||
@@ -65,11 +65,7 @@ pub trait PySliceableSequence {
|
||||
start..stop
|
||||
}
|
||||
|
||||
fn get_slice_items(
|
||||
&self,
|
||||
vm: &VirtualMachine,
|
||||
slice: &PyObjectRef,
|
||||
) -> Result<Self::Sliced, PyObjectRef>
|
||||
fn get_slice_items(&self, vm: &VirtualMachine, slice: &PyObjectRef) -> PyResult<Self::Sliced>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
@@ -410,7 +406,7 @@ pub fn get_mut_elements<'a>(obj: &'a PyObjectRef) -> impl DerefMut<Target = Vec<
|
||||
pub fn is_valid_slice_arg(
|
||||
arg: OptionalArg<PyObjectRef>,
|
||||
vm: &VirtualMachine,
|
||||
) -> Result<Option<BigInt>, PyObjectRef> {
|
||||
) -> PyResult<Option<BigInt>> {
|
||||
if let OptionalArg::Present(value) = arg {
|
||||
match_class!(match value {
|
||||
i @ PyInt => Ok(Some(i.as_bigint().clone())),
|
||||
|
||||
@@ -1260,7 +1260,7 @@ fn do_cformat_specifier(
|
||||
vm: &VirtualMachine,
|
||||
format_spec: &mut CFormatSpec,
|
||||
obj: PyObjectRef,
|
||||
) -> Result<String, PyObjectRef> {
|
||||
) -> PyResult<String> {
|
||||
use CNumberType::*;
|
||||
// do the formatting by type
|
||||
let format_type = &format_spec.format_type;
|
||||
|
||||
@@ -640,10 +640,7 @@ struct PyItertoolsTeeData {
|
||||
}
|
||||
|
||||
impl PyItertoolsTeeData {
|
||||
fn new(
|
||||
iterable: PyObjectRef,
|
||||
vm: &VirtualMachine,
|
||||
) -> Result<Rc<PyItertoolsTeeData>, PyObjectRef> {
|
||||
fn new(iterable: PyObjectRef, vm: &VirtualMachine) -> PyResult<Rc<PyItertoolsTeeData>> {
|
||||
Ok(Rc::new(PyItertoolsTeeData {
|
||||
iterable: get_iter(vm, &iterable)?,
|
||||
values: RefCell::new(vec![]),
|
||||
|
||||
@@ -467,7 +467,7 @@ impl VirtualMachine {
|
||||
TryFromObject::try_from_object(self, str)
|
||||
}
|
||||
|
||||
pub fn to_pystr<'a, T: Into<&'a PyObjectRef>>(&'a self, obj: T) -> Result<String, PyObjectRef> {
|
||||
pub fn to_pystr<'a, T: Into<&'a PyObjectRef>>(&'a self, obj: T) -> PyResult<String> {
|
||||
let py_str_obj = self.to_str(obj.into())?;
|
||||
Ok(py_str_obj.as_str().to_owned())
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ enum FetchResponseFormat {
|
||||
}
|
||||
|
||||
impl FetchResponseFormat {
|
||||
fn from_str(vm: &VirtualMachine, s: &str) -> Result<Self, PyObjectRef> {
|
||||
fn from_str(vm: &VirtualMachine, s: &str) -> PyResult<Self> {
|
||||
match s {
|
||||
"json" => Ok(FetchResponseFormat::Json),
|
||||
"text" => Ok(FetchResponseFormat::Text),
|
||||
|
||||
@@ -9,14 +9,14 @@ use web_sys::{self, console};
|
||||
|
||||
use rustpython_vm::function::PyFuncArgs;
|
||||
use rustpython_vm::obj::{objstr, objtype};
|
||||
use rustpython_vm::pyobject::{IdProtocol, PyObjectRef, PyResult, TypeProtocol};
|
||||
use rustpython_vm::pyobject::{IdProtocol, PyResult, TypeProtocol};
|
||||
use rustpython_vm::VirtualMachine;
|
||||
|
||||
pub(crate) fn window() -> web_sys::Window {
|
||||
web_sys::window().expect("Window to be available")
|
||||
}
|
||||
|
||||
pub fn format_print_args(vm: &VirtualMachine, args: PyFuncArgs) -> Result<String, PyObjectRef> {
|
||||
pub fn format_print_args(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult<String> {
|
||||
// Handle 'sep' kwarg:
|
||||
let sep_arg = args
|
||||
.get_optional_kwarg("sep")
|
||||
|
||||
Reference in New Issue
Block a user