8000 Merge pull request #4225 from joohongpark/fix-float-fromhex · RustPython/RustPython@1f9d852 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1f9d852

Browse files
authored
Merge pull request #4225 from joohongpark/fix-float-fromhex
Fix class method of a float subclass
2 parents cf099ff + 8c18208 commit 1f9d852

File tree

3 files changed

+6
-9
lines changed

3 files changed

+6
-9
lines changed

Lib/test/test_enum.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,8 +720,6 @@ def test_intenum_from_bytes(self):
720720
with self.assertRaises(ValueError):
721721
IntStooges.from_bytes(b'\x00\x05', 'big')
722722

723-
# TODO: RUSTPYTHON
724-
@unittest.expectedFailure
725723
def test_floatenum_fromhex(self):
726724
h = float.hex(FloatStooges.MOE.value)
727725
self.assertIs(FloatStooges.fromhex(h), FloatStooges.MOE)

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: 6 additions & 5 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
};
@@ -480,11 +480,12 @@ impl PyFloat {
480480
Ok((numer, denom))
481481
}
482482

483-
#[pymethod]
484-
fn fromhex(repr: PyStrRef, vm: &VirtualMachine) -> PyResult<f64> {
485-
float_ops::from_hex(repr.as_str().trim()).ok_or_else(|| {
483+
#[pyclassmethod]
484+
fn fromhex(cls: PyTypeRef, string: PyStrRef, vm: &VirtualMachine) -> PyResult {
485+
let result = float_ops::from_hex(string.as_str().trim()).ok_or_else(|| {
486486
vm.new_value_error("invalid hexadecimal floating-point string".to_owned())
487-
})
487+
})?;
488+
PyType::call(&cls, vec![vm.ctx.new_float(result).into()].into(), vm)
488489
}
489490

490491
#[pymethod]

0 commit comments

Comments
 (0)
0