@@ -479,7 +479,7 @@ internal static bool ToManaged(BorrowedReference value, Type type,
479479 out object ? result , bool setError )
480480 {
481481 var usedImplicit = false ;
482- return ToManaged ( value , type , out result , setError , out usedImplicit ) ;
482+ return ToManaged ( value , type , out result , setError , out usedImplicit , out _ ) ;
483483 }
484484 /// <summary>
485485 /// Return a managed object for the given Python object, taking funny
@@ -488,29 +488,37 @@ internal static bool ToManaged(BorrowedReference value, Type type,
488488 /// <param name="value">A Python object</param>
489489 /// <param name="type">The desired managed type</param>
490490 /// <param name="result">Receives the managed object</param>
491- /// <param name="setError">If true, call <c>Exceptions.SetError</c> with the reason for failure.</param>
491+ /// <param name="setError">
492+ /// If true, call <c>Exceptions.SetError</c> with the reason for failure.
493+ /// If false, a failed conversion doesn't raise a Python error.
494+ /// </param>
495+ /// <param name="conversionError">
496+ /// Receives why the conversion failed when the value matched the type but converting
497+ /// it threw (e.g. a throwing implicit operator), whether or not <paramref name="setError"/> is set
498+ /// </param>
492499 /// <returns>True on success</returns>
493500 internal static bool ToManaged ( BorrowedReference value , Type type ,
494- out object ? result , bool setError , out bool usedImplicit )
501+ out object ? result , bool setError , out bool usedImplicit , out string ? conversionError )
495502 {
496503 if ( type . IsByRef )
497504 {
498505 type = type . GetElementType ( ) ;
499506 }
500- return Converter . ToManagedValue ( value , type , out result , setError , out usedImplicit ) ;
507+ return Converter . ToManagedValue ( value , type , out result , setError , out usedImplicit , out conversionError ) ;
501508 }
502509
503510 internal static bool ToManagedValue ( BorrowedReference value , Type obType ,
504511 out object result , bool setError )
505512 {
506513 var usedImplicit = false ;
507- return ToManagedValue ( value , obType , out result , setError , out usedImplicit ) ;
514+ return ToManagedValue ( value , obType , out result , setError , out usedImplicit , out _ ) ;
508515 }
509516
510517 internal static bool ToManagedValue ( BorrowedReference value , Type obType ,
511- out object ? result , bool setError , out bool usedImplicit )
518+ out object ? result , bool setError , out bool usedImplicit , out string ? conversionError )
512519 {
513520 usedImplicit = false ;
521+ conversionError = null ;
514522 if ( obType == typeof ( PyObject ) )
515523 {
516524 result = new PyObject ( value ) ;
@@ -562,9 +570,11 @@ internal static bool ToManagedValue(BorrowedReference value, Type obType,
562570 catch
563571 {
564572 // Failed to convert using implicit conversion, must catch the error to stop program from exploding on Linux
565- // Raised even when setError is false: MethodBinder.Invoke surfaces a pending
566- // error as the binding failure reason instead of its generic no-match message
567- Exceptions . RaiseTypeError ( $ "Failed to implicitly convert { type } to { obType } ") ;
573+ conversionError = $ "Failed to implicitly convert { type } to { obType } ";
574+ if ( setError )
575+ {
576+ Exceptions . RaiseTypeError ( conversionError ) ;
577+ }
568578 return false ;
569579 }
570580 }
@@ -587,7 +597,11 @@ internal static bool ToManagedValue(BorrowedReference value, Type obType,
587597
588598 if ( ! cb . type . Valid )
589599 {
590- Exceptions . SetError ( Exceptions . TypeError , cb . type . DeletedMessage ) ;
600+ conversionError = cb . type . DeletedMessage ;
601+ if ( setError )
602+ {
603+ Exceptions . SetError ( Exceptions . TypeError , conversionError ) ;
604+ }
591605 return false ;
592606 }
593607 result = cb . type . Value ;
@@ -737,7 +751,7 @@ internal static bool ToManagedValue(BorrowedReference value, Type obType,
737751 var underlyingType = Nullable . GetUnderlyingType ( obType ) ;
738752 if ( underlyingType != null )
739753 {
740- return ToManagedValue ( value , underlyingType , out result , setError , out usedImplicit ) ;
754+ return ToManagedValue ( value , underlyingType , out result , setError , out usedImplicit , out conversionError ) ;
741755 }
742756
743757 TypeCode typeCode = Type . GetTypeCode ( obType ) ;
@@ -758,7 +772,7 @@ internal static bool ToManagedValue(BorrowedReference value, Type obType,
758772 var opImplicit = obType . GetMethod ( "op_Implicit" , new [ ] { obType } ) ;
759773 if ( opImplicit != null )
760774 {
761- if ( ToManagedValue ( value , opImplicit . ReturnType , out result , setError , out usedImplicit ) )
775+ if ( ToManagedValue ( value , opImplicit . ReturnType , out result , setError , out usedImplicit , out conversionError ) )
762776 {
763777 opImplicit = obType . GetMethod ( "op_Implicit" , new [ ] { result . GetType ( ) } ) ;
764778 if ( opImplicit != null )
@@ -770,9 +784,11 @@ internal static bool ToManagedValue(BorrowedReference value, Type obType,
770784 catch
771785 {
772786 // Failed to convert using implicit conversion, must catch the error to stop program from exploding on Linux
773- // Raised even when setError is false: MethodBinder.Invoke surfaces a pending
774- // error as the binding failure reason instead of its generic no-match message
775- Exceptions . RaiseTypeError ( $ "Failed to implicitly convert { result . GetType ( ) } to { obType } ") ;
787+ conversionError = $ "Failed to implicitly convert { result . GetType ( ) } to { obType } ";
788+ if ( setError )
789+ {
790+ Exceptions . RaiseTypeError ( conversionError ) ;
791+ }
776792 return false ;
777793 }
778794 }
@@ -1051,11 +1067,11 @@ internal static bool ToPrimitive(BorrowedReference value, Type obType, out objec
10511067 // references returned through key, dicValue are borrowed.
10521068 if ( Runtime . PyDict_Next ( value , out pos , out key , out dicValue ) != 0 )
10531069 {
1054- if ( ! ToManaged ( key , typeArguments [ 0 ] , out var convertedKey , setError , out usedImplicit ) )
1070+ if ( ! ToManaged ( key , typeArguments [ 0 ] , out var convertedKey , setError , out usedImplicit , out _ ) )
10551071 {
10561072 goto type_error ;
10571073 }
1058- if ( ! ToManaged ( dicValue , typeArguments [ 1 ] , out var convertedValue , setError , out usedImplicit ) )
1074+ if ( ! ToManaged ( dicValue , typeArguments [ 1 ] , out var convertedValue , setError , out usedImplicit , out _ ) )
10591075 {
10601076 goto type_error ;
10611077 }
@@ -1656,8 +1672,7 @@ private static IList MakeList(BorrowedReference value, NewReference IterObject,
16561672
16571673 if ( ! Converter . ToManaged ( item . Borrow ( ) , elementType , out var obj , setError ) )
16581674 {
1659- // some element conversion paths raise even when setError is false
1660- // (e.g. failed implicit operators, deleted types)
1675+ // a conversion sub-path may have left an error pending (e.g. a raising user decoder)
16611676 if ( ! setError )
16621677 {
16631678 Exceptions . Clear ( ) ;
0 commit comments