Skip to content

Commit abb994d

Browse files
committed
Fix class method
1 parent f1e1046 commit abb994d

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

Lib/test/test_float.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,8 +1525,6 @@ def roundtrip(x):
15251525
else:
15261526
self.identical(x, fromHex(toHex(x)))
15271527

1528-
# TODO: RUSTPYTHON
1529-
@unittest.expectedFailure
15301528
def test_subclass(self):
15311529
class F(float):
15321530
def __new__(cls, value):

vm/src/builtins/float.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
PyComparisonValue,
1414
},
1515
protocol::{PyNumber, PyNumberMethods},
16-
types::{AsNumber, Comparable, Constructor, Hashable, PyComparisonOp},
16+
types::{AsNumber, Callable, Comparable, Constructor, Hashable, PyComparisonOp},
1717
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
1818
TryFromBorrowedObject, TryFromObject, VirtualMachine,
1919
};
@@ -481,11 +481,13 @@ impl PyFloat {
481481
Ok((numer, denom))
482482
}
483483

484-
#[pymethod]
485-
fn fromhex(repr: PyStrRef, vm: &VirtualMachine) -> PyResult<f64> {
486-
float_ops::from_hex(repr.as_str().trim()).ok_or_else(|| {
487-
vm.new_value_error("invalid hexadecimal floating-point string".to_owned())
488-
})
484+
#[pyclassmethod]
485+
fn fromhex(cls: PyTypeRef, string: PyStrRef, vm: &VirtualMachine) -> PyResult {
486+
let result = float_ops::from_hex(string.as_str().trim());
487+
match result {
488+
Some(value) => PyType::call(&cls, vec![vm.ctx.new_float(value).into()].into(), vm),
489+
None => Err(vm.new_value_error("invalid hexadecimal floating-point string".to_owned())),
490+
}
489491
}
490492

491493
#[pymethod]

0 commit comments

Comments
 (0)