Fix some arguments for common functions in objfloat and objstr

This commit is contained in:
Noah
2020-04-14 13:16:32 -05:00
parent ad111b029f
commit 5712857092
2 changed files with 19 additions and 24 deletions

View File

@@ -651,7 +651,14 @@ fn to_float(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<f64> {
}
}
} else {
return Err(vm.new_type_error(format!("can't convert {} to float", obj.class().name)));
let method = vm.get_method_or_type_error(obj.clone(), "__float__", || {
format!(
"float() argument must be a string or a number, not '{}'",
obj.class().name
)
})?;
let result = vm.invoke(&method, vec![])?;
PyFloatRef::try_from_object(vm, result)?.to_f64()
};
Ok(value)
}
@@ -712,21 +719,6 @@ pub fn get_value(obj: &PyObjectRef) -> f64 {
obj.payload::<PyFloat>().unwrap().value
}
fn make_float(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<f64> {
if let Some(float) = obj.payload_if_subclass::<PyFloat>(vm) {
Ok(float.value)
} else {
let method = vm.get_method_or_type_error(obj.clone(), "__float__", || {
format!(
"float() argument must be a string or a number, not '{}'",
obj.class().name
)
})?;
let result = vm.invoke(&method, vec![])?;
Ok(get_value(&result))
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct IntoPyFloat {
value: f64,
@@ -741,7 +733,7 @@ impl IntoPyFloat {
impl TryFromObject for IntoPyFloat {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
Ok(IntoPyFloat {
value: make_float(vm, &obj)?,
value: to_float(vm, &obj)?,
})
}
}

View File

@@ -1192,13 +1192,8 @@ impl PyString {
}
#[pymethod]
fn encode(
zelf: PyRef<Self>,
encoding: OptionalArg<PyStringRef>,
errors: OptionalArg<PyStringRef>,
vm: &VirtualMachine,
) -> PyResult<PyBytesRef> {
encode_string(zelf, encoding.into_option(), errors.into_option(), vm)
fn encode(zelf: PyRef<Self>, args: EncodeArgs, vm: &VirtualMachine) -> PyResult<PyBytesRef> {
encode_string(zelf, args.encoding, args.errors, vm)
}
#[pymethod(magic)]
@@ -1220,6 +1215,14 @@ impl PyString {
}
}
#[derive(FromArgs)]
struct EncodeArgs {
#[pyarg(positional_or_keyword, default = "None")]
encoding: Option<PyStringRef>,
#[pyarg(positional_or_keyword, default = "None")]
errors: Option<PyStringRef>,
}
pub(crate) fn encode_string(
s: PyStringRef,
encoding: Option<PyStringRef>,