@@ -6,16 +6,16 @@ use num_integer::Integer;
66use num_traits:: { One , Pow , Signed , ToPrimitive , Zero } ;
77
88use crate :: format:: FormatSpec ;
9- use crate :: function:: { OptionalArg , PyFuncArgs } ;
9+ use crate :: function:: { KwArgs , OptionalArg , PyFuncArgs } ;
1010use crate :: pyobject:: {
1111 IntoPyObject , PyClassImpl , PyContext , PyObjectRef , PyRef , PyResult , PyValue , TryFromObject ,
1212 TypeProtocol ,
1313} ;
1414use crate :: vm:: VirtualMachine ;
1515
16+ use super :: objbyteinner:: PyByteInner ;
1617use super :: objstr:: { PyString , PyStringRef } ;
1718use super :: objtype;
18- use super :: objbyteinner:: { PyByteInner } ;
1919use crate :: obj:: objtype:: PyClassRef ;
2020
2121/// int(x=0) -> integer
@@ -491,24 +491,37 @@ impl PyInt {
491491 }
492492
493493 #[ pymethod]
494- fn from_bytes ( bytes : PyByteInner , byteorder : PyStringRef , signed : OptionalArg < bool > , vm : & VirtualMachine ) -> PyResult < BigInt > {
494+ fn from_bytes (
495+ bytes : PyByteInner ,
496+ byteorder : PyStringRef ,
497+ kwargs : KwArgs ,
498+ vm : & VirtualMachine ,
499+ ) -> PyResult < BigInt > {
500+ let mut signed = false ;
501+ for ( key, value) in kwargs. into_iter ( ) {
502+ if key == "signed" {
503+ signed = match_class ! ( value,
504+
505+ b @ PyInt => !b. as_bigint( ) . is_zero( ) ,
506+ _ => false ,
507+ ) ;
508+ }
509+ }
495510 let x;
496511 if byteorder. value == "big" {
497512 x = match signed {
498- OptionalArg :: Present ( true ) => BigInt :: from_signed_bytes_be ( & bytes. elements ) ,
499- _ => BigInt :: from_bytes_be ( Sign :: Plus , & bytes. elements ) ,
513+ true => BigInt :: from_signed_bytes_be ( & bytes. elements ) ,
514+ false => BigInt :: from_bytes_be ( Sign :: Plus , & bytes. elements ) ,
500515 }
501- }
502- else if byteorder. value == "little" {
516+ } else if byteorder. value == "little" {
503517 x = match signed {
504- OptionalArg :: Present ( true ) => BigInt :: from_signed_bytes_le ( & bytes. elements ) ,
505- _ => BigInt :: from_bytes_le ( Sign :: Plus , & bytes. elements ) ,
518+ true => BigInt :: from_signed_bytes_le ( & bytes. elements ) ,
519+ false => BigInt :: from_bytes_le ( Sign :: Plus , & bytes. elements ) ,
506520 }
507- }
508- else {
509- return Err ( vm. new_value_error (
510- "byteorder must be either 'little' or 'big'" . to_string ( ) ,
511- ) ) ;
521+ } else {
522+ return Err (
523+ vm. new_value_error ( "byteorder must be either 'little' or 'big'" . to_string ( ) )
524+ ) ;
512525 }
513526 Ok ( x)
514527 }
0 commit comments