1
- use super :: { int , PyBytes , PyInt , PyIntRef , PyStr , PyStrRef , PyTypeRef } ;
1
+ use super :: { try_bigint_to_f64 , PyBytes , PyInt , PyIntRef , PyStr , PyStrRef , PyTypeRef } ;
2
2
use crate :: common:: { float_ops, hash} ;
3
3
use crate :: format:: FormatSpec ;
4
4
use crate :: function:: { OptionalArg , OptionalOption } ;
@@ -51,29 +51,31 @@ impl From<f64> for PyFloat {
51
51
}
52
52
}
53
53
54
- pub fn try_float_opt ( obj : & PyObjectRef , vm : & VirtualMachine ) -> PyResult < Option < f64 > > {
55
- if let Some ( float) = obj. payload_if_exact :: < PyFloat > ( vm) {
56
- return Ok ( Some ( float. value ) ) ;
57
- }
58
- if let Some ( method) = vm. get_method ( obj. clone ( ) , "__float__" ) {
59
- let result = vm. invoke ( & method?, ( ) ) ?;
60
- // TODO: returning strict subclasses of float in __float__ is deprecated
61
- return match result. payload :: < PyFloat > ( ) {
62
- Some ( float_obj) => Ok ( Some ( float_obj. value ) ) ,
63
- None => Err ( vm. new_type_error ( format ! (
64
- "__float__ returned non-float (type '{}')" ,
65
- result. class( ) . name( )
66
- ) ) ) ,
67
- } ;
68
- }
69
- if let Some ( r) = vm. to_index_opt ( obj. clone ( ) ) . transpose ( ) ? {
70
- return Ok ( Some ( int:: to_float ( r. as_bigint ( ) , vm) ?) ) ;
54
+ impl PyObjectRef {
55
+ pub fn try_to_f64 ( & self , vm : & VirtualMachine ) -> PyResult < Option < f64 > > {
56
+ if let Some ( float) = self . payload_if_exact :: < PyFloat > ( vm) {
57
+ return Ok ( Some ( float. value ) ) ;
58
+ }
59
+ if let Some ( method) = vm. get_method ( self . clone ( ) , "__float__" ) {
60
+ let result = vm. invoke ( & method?, ( ) ) ?;
61
+ // TODO: returning strict subclasses of float in __float__ is deprecated
62
+ return match result. payload :: < PyFloat > ( ) {
63
+ Some ( float_obj) => Ok ( Some ( float_obj. value ) ) ,
64
+ None => Err ( vm. new_type_error ( format ! (
65
+ "__float__ returned non-float (type '{}')" ,
66
+ result. class( ) . name( )
67
+ ) ) ) ,
68
+ } ;
69
+ }
70
+ if let Some ( r) = vm. to_index_opt ( self . clone ( ) ) . transpose ( ) ? {
71
+ return Ok ( Some ( try_bigint_to_f64 ( r. as_bigint ( ) , vm) ?) ) ;
72
+ }
73
+ Ok ( None )
71
74
}
72
- Ok ( None )
73
75
}
74
76
75
77
pub fn try_float ( obj : & PyObjectRef , vm : & VirtualMachine ) -> PyResult < f64 > {
76
- try_float_opt ( obj, vm) ?. ok_or_else ( || {
78
+ obj. try_to_f64 ( vm) ?. ok_or_else ( || {
77
79
vm. new_type_error ( format ! ( "must be real number, not {}" , obj. class( ) . name( ) ) )
78
80
} )
79
81
}
@@ -82,7 +84,7 @@ pub(crate) fn to_op_float(obj: &PyObjectRef, vm: &VirtualMachine) -> PyResult<Op
82
84
let v = if let Some ( float) = obj. payload_if_subclass :: < PyFloat > ( vm) {
83
85
Some ( float. value )
84
86
} else if let Some ( int) = obj. payload_if_subclass :: < PyInt > ( vm) {
85
- Some ( int :: to_float ( int. as_bigint ( ) , vm) ?)
87
+ Some ( try_bigint_to_f64 ( int. as_bigint ( ) , vm) ?)
86
88
} else {
87
89
None
88
90
} ;
@@ -111,7 +113,7 @@ fn inner_mod(v1: f64, v2: f64, vm: &VirtualMachine) -> PyResult<f64> {
111
113
. ok_or_else ( || vm. new_zero_division_error ( "float mod by zero" . to_owned ( ) ) )
112
114
}
113
115
114
- pub fn try_bigint ( value : f64 , vm : & VirtualMachine ) -> PyResult < BigInt > {
116
+ pub fn try_to_bigint ( value : f64 , vm : & VirtualMachine ) -> PyResult < BigInt > {
115
117
match value. to_bigint ( ) {
116
118
Some ( int) => Ok ( int) ,
117
119
None => {
@@ -171,7 +173,7 @@ impl SlotConstructor for PyFloat {
171
173
val
172
174
} ;
173
175
174
- if let Some ( f) = try_float_opt ( & val, vm) ? {
176
+ if let Some ( f) = val. try_to_f64 ( vm) ? {
175
177
f
176
178
} else if let Some ( s) = val. payload_if_subclass :: < PyStr > ( vm) {
177
179
float_ops:: parse_str ( s. as_str ( ) . trim ( ) ) . ok_or_else ( || {
@@ -379,17 +381,17 @@ impl PyFloat {
379
381
380
382
#[ pymethod( magic) ]
381
383
fn trunc ( & self , vm : & VirtualMachine ) -> PyResult < BigInt > {
382
- try_bigint ( self . value , vm)
384
+ try_to_bigint ( self . value , vm)
383
385
}
384
386
385
387
#[ pymethod( magic) ]
386
388
fn floor ( & self , vm : & VirtualMachine ) -> PyResult < BigInt > {
387
- try_bigint ( self . value . floor ( ) , vm)
389
+ try_to_bigint ( self . value . floor ( ) , vm)
388
390
}
389
391
390
392
#[ pymethod( magic) ]
391
393
fn ceil ( & self , vm : & VirtualMachine ) -> PyResult < BigInt > {
392
- try_bigint ( self . value . ceil ( ) , vm)
394
+ try_to_bigint ( self . value . ceil ( ) , vm)
393
395
}
394
396
395
397
#[ pymethod( magic) ]
@@ -417,7 +419,7 @@ impl PyFloat {
417
419
} else {
418
420
self . value . round ( )
419
421
} ;
420
- let int = try_bigint ( value, vm) ?;
422
+ let int = try_to_bigint ( value, vm) ?;
421
423
vm. ctx . new_int ( int)
422
424
} ;
423
425
Ok ( value)
0 commit comments