@@ -6,6 +6,7 @@ use crate::{
66 convert:: ToPyObject ,
77 object:: { Traverse , TraverseFn } ,
88} ;
9+ use alloc:: borrow:: Cow ;
910use core:: ops:: { Deref , DerefMut , RangeInclusive } ;
1011use indexmap:: IndexMap ;
1112use itertools:: Itertools ;
@@ -305,7 +306,8 @@ impl FuncArgs {
305306 let instance = callee. instance_args ( ) ;
306307 let arity = T :: arity ( ) ;
307308 let arity = arity. start ( ) . saturating_sub ( instance) ..=arity. end ( ) . saturating_sub ( instance) ;
308- let num_given = self . args . len ( ) . saturating_sub ( instance) ;
309+ let nargs = self . args . len ( ) ;
310+ let num_given = nargs. saturating_sub ( instance) ;
309311
310312 // A function that takes no keywords refuses them before counting its
311313 // arguments.
@@ -316,13 +318,13 @@ impl FuncArgs {
316318 // Too many positional arguments are reported before any argument is
317319 // converted, so a conversion cannot run user code first.
318320 if num_given > * arity. end ( ) {
319- return Err ( ArgumentError :: TooManyArgs . into_exception ( & arity, num_given, callee, vm) ) ;
321+ return Err ( ArgumentError :: TooManyArgs . into_exception ( & arity, num_given, 0 , callee, vm) ) ;
320322 }
321323
322324 // No keyword can fill a missing parameter of such a function, so too few
323325 // arguments are also reported before any is converted.
324326 if T :: TAKES_NO_KEYWORDS && num_given < * arity. start ( ) {
325- return Err ( ArgumentError :: TooFewArgs . into_exception ( & arity, num_given, callee, vm) ) ;
327+ return Err ( ArgumentError :: TooFewArgs . into_exception ( & arity, num_given, 0 , callee, vm) ) ;
326328 }
327329
328330 // A keyword that fills a parameter already given by position is
@@ -334,11 +336,20 @@ impl FuncArgs {
334336 return Err ( callee. given_by_name_and_position ( name, pos, vm) ) ;
335337 }
336338
337- let bound = T :: from_args ( vm, & mut self )
338- . map_err ( |e| e. into_exception ( & arity, num_given, callee, vm) ) ?;
339+ let bound = match T :: from_args ( vm, & mut self ) {
340+ Ok ( bound) => bound,
341+ Err ( e) => {
342+ // take_positional has just removed the failed argument, so N is
343+ // nargs - remaining, minus the instance.
344+ let pos = nargs
345+ . saturating_sub ( self . args . len ( ) )
346+ . saturating_sub ( instance) ;
347+ return Err ( e. into_exception ( & arity, num_given, pos, callee, vm) ) ;
348+ }
349+ } ;
339350
340351 if !self . args . is_empty ( ) {
341- Err ( ArgumentError :: TooManyArgs . into_exception ( & arity, num_given, callee, vm) )
352+ Err ( ArgumentError :: TooManyArgs . into_exception ( & arity, num_given, 0 , callee, vm) )
342353 } else if let Some ( err) = self . check_kwargs_empty_for ( vm, callee) {
343354 Err ( err)
344355 } else {
@@ -462,19 +473,31 @@ impl Callee {
462473 /// _PyArg_BadArgument
463474 fn bad_argument (
464475 self ,
465- pos : usize ,
466- expected : & ' static str ,
476+ displayname : & str ,
477+ expected : & ' static Py < PyType > ,
478+ or_none : bool ,
467479 found : & PyObject ,
468480 vm : & VirtualMachine ,
469481 ) -> PyBaseExceptionRef {
482+ let expected_ty = expected. slot_name ( ) ;
483+ let expected = if or_none {
484+ Cow :: Owned ( format ! ( "{expected_ty} or None" ) )
485+ } else {
486+ Cow :: Borrowed ( & * expected_ty)
487+ } ;
470488 let class = found. class ( ) ;
471489 let found_ty = class. slot_name ( ) ;
472490 let found_name = if vm. is_none ( found) {
473491 "None"
474492 } else {
475493 & * found_ty
476494 } ;
477- vm. new_type_error ( bad_argument_message ( self . name , pos, expected, found_name) )
495+ vm. new_type_error ( bad_argument_message (
496+ self . name ,
497+ displayname,
498+ & expected,
499+ found_name,
500+ ) )
478501 }
479502}
480503
@@ -529,15 +552,26 @@ pub(crate) fn no_keywords_message(name: Option<&str>) -> String {
529552 format ! ( "{name}{parens} takes no keyword arguments" )
530553}
531554
555+ /// get_displayname
556+ fn get_displayname ( name : Option < & str > , pos : usize , meth_o : bool ) -> Cow < ' static , str > {
557+ if let Some ( name) = name {
558+ Cow :: Owned ( format ! ( "argument '{name}'" ) )
559+ } else if meth_o {
560+ Cow :: Borrowed ( "argument" )
561+ } else {
562+ Cow :: Owned ( format ! ( "argument {pos}" ) )
563+ }
564+ }
565+
532566/// _PyArg_BadArgument
533567pub ( crate ) fn bad_argument_message (
534568 name : Option < & str > ,
535- pos : usize ,
569+ displayname : & str ,
536570 expected : & str ,
537571 found : & str ,
538572) -> String {
539573 let ( name, parens) = call_form ( name, "this function" ) ;
540- format ! ( "{name}{parens} argument {pos } must be {expected}, not {found}" )
574+ format ! ( "{name}{parens} {displayname } must be {expected}, not {found}" )
541575}
542576
543577pub ( crate ) fn given_by_name_and_position_message (
@@ -604,12 +638,13 @@ pub enum ArgumentError {
604638 /// The function requires an argument for the named parameter, at the given
605639 /// 1-based position, but the call didn't pass one.
606640 MissingRequiredArgument { name : String , pos : usize } ,
607- /// The argument at the 1-based position is not the expected type.
608- /// `_PyArg_BadArgument` .
641+ /// The argument is not the expected type. `_PyArg_BadArgument` .
642+ /// `name` is `None` for a positional-only parameter .
609643 BadArgument {
610- pos : usize ,
611- expected : & ' static str ,
644+ expected : & ' static Py < PyType > ,
645+ or_none : bool ,
612646 found : PyObjectRef ,
647+ name : Option < & ' static str > ,
613648 } ,
614649 /// An exception was raised while binding arguments to the function
615650 /// parameters.
@@ -623,10 +658,49 @@ impl From<PyBaseExceptionRef> for ArgumentError {
623658}
624659
625660impl ArgumentError {
661+ /// Attach the parameter name for get_displayname (`argument 'name'`).
662+ #[ must_use]
663+ pub fn with_argument_name ( self , name : & ' static str ) -> Self {
664+ match self {
665+ Self :: BadArgument {
666+ expected,
667+ or_none,
668+ found,
669+ ..
670+ } => Self :: BadArgument {
671+ expected,
672+ or_none,
673+ found,
674+ name : Some ( name) ,
675+ } ,
676+ other => other,
677+ }
678+ }
679+
680+ /// Mark `{expected}` as `X or None`.
681+ #[ must_use]
682+ pub ( crate ) fn with_or_none ( self ) -> Self {
683+ match self {
684+ Self :: BadArgument {
685+ expected,
686+ found,
687+ name,
688+ ..
689+ } => Self :: BadArgument {
690+ expected,
691+ or_none : true ,
692+ found,
693+ name,
694+ } ,
695+ other => other,
696+ }
697+ }
698+
626699 fn into_exception (
627700 self ,
628701 arity : & RangeInclusive < usize > ,
629702 num_given : usize ,
703+ pos : usize ,
630704 callee : Callee ,
631705 vm : & VirtualMachine ,
632706 ) -> PyBaseExceptionRef {
@@ -636,10 +710,19 @@ impl ArgumentError {
636710 Self :: InvalidKeywordArgument ( name) => callee. unexpected_keyword ( & name, vm) ,
637711 Self :: MissingRequiredArgument { name, pos } => callee. missing_argument ( & name, pos, vm) ,
638712 Self :: BadArgument {
639- pos,
640713 expected,
714+ or_none,
641715 found,
642- } => callee. bad_argument ( pos, expected, & found, vm) ,
716+ name,
717+ } => {
718+ if callee. name . is_none ( ) || ( name. is_none ( ) && pos == 0 ) {
719+ vm. new_downcast_type_error ( expected, & found)
720+ } else {
721+ let meth_o = * arity. start ( ) == 1 && * arity. end ( ) == 1 ;
722+ let displayname = get_displayname ( name, pos, meth_o) ;
723+ callee. bad_argument ( & displayname, expected, or_none, & found, vm)
724+ }
725+ }
643726 Self :: Exception ( ex) => ex,
644727 }
645728 }
@@ -996,7 +1079,7 @@ where
9961079
9971080 fn from_args ( vm : & VirtualMachine , args : & mut FuncArgs ) -> Result < Self , ArgumentError > {
9981081 let value = args. take_positional ( ) . ok_or ( ArgumentError :: TooFewArgs ) ?;
999- Ok ( value . try_into_value ( vm ) ? )
1082+ T :: try_from_argument ( vm , value )
10001083 }
10011084}
10021085
@@ -1059,7 +1142,7 @@ where
10591142
10601143 fn from_args ( vm : & VirtualMachine , args : & mut FuncArgs ) -> Result < Self , ArgumentError > {
10611144 let r = if let Some ( value) = args. take_positional ( ) {
1062- Self :: Present ( value . try_into_value ( vm) ?)
1145+ Self :: Present ( T :: try_from_argument ( vm, value ) ?)
10631146 } else {
10641147 Self :: Missing
10651148 } ;
0 commit comments