@@ -51,10 +51,10 @@ public static object __new__(CodeContext/*!*/ context, PythonType cls, object x)
5151 // TODO: should also raise DeprecationWarning
5252 value = ( ( Extensible < double > ) d ) . Value ;
5353 } else {
54- throw PythonOps . TypeError ( "__float__ returned non-float (type {0})" , PythonTypeOps . GetName ( d ) ) ;
54+ throw PythonOps . TypeErrorForBadInstance ( "__float__ returned non-float (type {0})" , d ) ;
5555 }
5656 } else {
57- throw PythonOps . TypeError ( "float() argument must be a string or a number, not '{0}'" , PythonTypeOps . GetName ( x ) ) ;
57+ throw PythonOps . TypeErrorForBadInstance ( "float() argument must be a string or a number, not '{0}'" , x ) ;
5858 }
5959 }
6060
@@ -66,13 +66,15 @@ public static object __new__(CodeContext/*!*/ context, PythonType cls, object x)
6666 }
6767
6868 [ StaticExtensionMethod ]
69- public static object __new__ ( CodeContext /*!*/ context , PythonType cls , [ BytesLike , NotNull ] IList < byte > s ) {
69+ public static object __new__ ( CodeContext /*!*/ context , PythonType cls , [ NotNull ] IBufferProtocol x ) {
7070 // First, check for subclasses of bytearray/bytes
7171 object value ;
72- if ( ! ( s is IPythonObject po ) ||
72+ if ( ! ( x is IPythonObject po ) ||
7373 ! PythonTypeOps . TryInvokeUnaryOperator ( DefaultContext . Default , po , "__float__" , out value ) ) {
7474 // If __float__ does not exist, just parse the string normally
75- value = ParseFloat ( s . MakeString ( ) ) ;
75+ using IPythonBuffer buf = x . GetBufferNoThrow ( )
76+ ?? throw PythonOps . TypeErrorForBadInstance ( "float() argument must be a string or a number, not '{0}'" , x ) ;
77+ value = ParseFloat ( buf . AsReadOnlySpan ( ) . MakeString ( ) ) ;
7678 }
7779
7880 if ( cls == TypeCache . Double ) {
@@ -1172,23 +1174,24 @@ public static object __new__(CodeContext/*!*/ context, PythonType cls, object x)
11721174
11731175 object d = PythonOps . CallWithContext ( context , PythonOps . GetBoundAttr ( context , x , "__float__" ) ) ;
11741176 if ( d is double ) return ( float ) ( double ) d ;
1175- throw PythonOps . TypeError ( "__float__ returned non-float (type %s )" , DynamicHelpers . GetPythonType ( d ) ) ;
1177+ throw PythonOps . TypeErrorForBadInstance ( "__float__ returned non-float (type {0} )" , d ) ;
11761178 }
11771179
11781180 [ StaticExtensionMethod ]
1179- public static object __new__ ( CodeContext /*!*/ context , PythonType cls , IList < byte > s ) {
1181+ public static object __new__ ( CodeContext /*!*/ context , PythonType cls , [ NotNull ] IBufferProtocol x ) {
11801182 // First, check for subclasses of bytearray/bytes
11811183 object value ;
1182- IPythonObject po = s as IPythonObject ;
1183- if ( po == null ||
1184+ if ( ! ( x is IPythonObject po ) ||
11841185 ! PythonTypeOps . TryInvokeUnaryOperator ( DefaultContext . Default , po , "__float__" , out value ) ) {
11851186 // If __float__ does not exist, just parse the string normally
1186- value = ParseFloat ( s . MakeString ( ) ) ;
1187+ using IPythonBuffer buf = x . GetBufferNoThrow ( )
1188+ ?? throw PythonOps . TypeErrorForBadInstance ( "float() argument must be a string or a number, not '{0}'" , x ) ;
1189+ value = ParseFloat ( buf . AsReadOnlySpan ( ) . MakeString ( ) ) ;
11871190 }
11881191
11891192 if ( ! ( value is double ) ) {
11901193 // The check for double is correct, because that's all Python types should be using
1191- throw PythonOps . TypeError ( "__float__ returned non-float (type %s )" , DynamicHelpers . GetPythonType ( value ) ) ;
1194+ throw PythonOps . TypeErrorForBadInstance ( "__float__ returned non-float (type {0} )" , value ) ;
11921195 }
11931196
11941197 if ( cls == TypeCache . Single ) {
0 commit comments