Skip to content

Commit 87238d4

Browse files
Report converter failure causes explicitly instead of through the error indicator (#157)
Converter.ToManagedValue raised a TypeError for a throwing implicit operator (and for a deleted type) even when called with setError: false, so that MethodBinder.Invoke could surface it as the bind-failure reason. Every other caller that converts with setError: false and expects a clean error indicator could leak that error (ArrayObject.sq_contains, MetaType.sq_contains, PyObject's IConvertible.ToType and dynamic TryConvert, LookUpObject.Contains). The converter now reports that cause through an out conversionError and only raises it when setError is true. MethodBinder.Bind keeps the cause of the last conversion it attempted and Invoke raises it as the same TypeError when no overload matches, so the bind-failure messages are unchanged. LookUpObject.Contains also returned 0 after setting a TypeError, and went on to call ContainsKey with whatever the failed conversion left in its result, which crashed the process when that was a value of the wrong type. It now returns -1 so the TypeError is raised. Co-authored-by: quantconnect-kronos[bot] <346399058+quantconnect-kronos[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent bf7d5b5 commit 87238d4

7 files changed

Lines changed: 151 additions & 32 deletions

File tree

‎src/embed_tests/ClassManagerTests.cs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,31 @@ def contains(dictionary, key):
11591159
Assert.IsFalse(result);
11601160
}
11611161

1162+
[TestCase(typeof(TestKeyValueContainer<string, string>), 1)]
1163+
[TestCase(typeof(DynamicClassDictionary<string, string>), 1)]
1164+
// the failed conversion of the key leaves a string behind, which ContainsKey can't take
1165+
[TestCase(typeof(TestKeyValueContainer<TestMethodBinder.ErroredImplicitConversion, string>), "key")]
1166+
public void ContainsWithKeyOfWrongTypeRaisesTypeError(Type dictType, object key)
1167+
{
1168+
using var _ = Py.GIL();
1169+
1170+
var module = PyModule.FromString("ContainsWithKeyOfWrongTypeRaisesTypeError", $@"
1171+
def contains(dictionary, key):
1172+
try:
1173+
key in dictionary
1174+
except TypeError as e:
1175+
return str(e)
1176+
");
1177+
1178+
using var contains = module.GetAttr("contains");
1179+
using var pyDictionary = Activator.CreateInstance(dictType).ToPython();
1180+
using var pyKey = key.ToPython();
1181+
1182+
var error = contains.Invoke(pyDictionary, pyKey).As<string>();
1183+
StringAssert.StartsWith("invalid parameter type for sq_contains", error);
1184+
Assert.IsFalse(Exceptions.ErrorOccurred());
1185+
}
1186+
11621187
[TestCaseSource(nameof(IDictionaryContainsTestCases))]
11631188
public void CanCheckIfNoneIsInDictionary(Type dictType)
11641189
{

‎src/embed_tests/TestConverter.cs‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,72 @@ from System import Uri
718718
Exceptions.Clear();
719719
}
720720

721+
// The implicit operators of ErroredImplicitConversion throw: the conversion reports why,
722+
// and only raises it when asked to set errors
723+
[TestCase(true, true)]
724+
[TestCase(false, true)]
725+
[TestCase(true, false)]
726+
[TestCase(false, false)]
727+
public void ThrowingImplicitConversionOnlyLeavesErrorWhenSettingErrors(bool setError, bool fromClrObject)
728+
{
729+
using var _ = Py.GIL();
730+
731+
using var pyValue = fromClrObject ? new TestMethodBinder.ErroredImplicitConversion().ToPython() : "value".ToPython();
732+
var targetType = fromClrObject ? typeof(string) : typeof(TestMethodBinder.ErroredImplicitConversion);
733+
var expectedError = fromClrObject
734+
? "Failed to implicitly convert Python.EmbeddingTest.TestMethodBinder+ErroredImplicitConversion to System.String"
735+
: "Failed to implicitly convert System.String to Python.EmbeddingTest.TestMethodBinder+ErroredImplicitConversion";
736+
737+
Assert.IsFalse(Converter.ToManaged(pyValue, targetType, out var _, setError, out var _, out var conversionError));
738+
Assert.AreEqual(expectedError, conversionError);
739+
Assert.AreEqual(setError, Exceptions.ErrorOccurred());
740+
if (setError)
741+
{
742+
var error = PythonException.FetchCurrentRaw();
743+
Assert.AreEqual(expectedError, error.Message);
744+
}
745+
}
746+
747+
[Test]
748+
public void ThrowingImplicitConversionDoesNotLeakFromConvertibleToType()
749+
{
750+
using var _ = Py.GIL();
751+
752+
using var pyValue = new TestMethodBinder.ErroredImplicitConversion().ToPython();
753+
Assert.Throws<InvalidCastException>(() => ((IConvertible)pyValue).ToType(typeof(string), null));
754+
Assert.IsFalse(Exceptions.ErrorOccurred());
755+
}
756+
757+
[Test]
758+
public void ThrowingImplicitConversionDoesNotLeakFromDynamicConversion()
759+
{
760+
using var _ = Py.GIL();
761+
762+
dynamic pyValue = new TestMethodBinder.ErroredImplicitConversion().ToPython();
763+
Assert.Throws<Microsoft.CSharp.RuntimeBinder.RuntimeBinderException>(() => { var converted = (string)pyValue; });
764+
Assert.IsFalse(Exceptions.ErrorOccurred());
765+
pyValue.Dispose();
766+
}
767+
768+
[Test]
769+
public void ThrowingImplicitConversionDoesNotLeakFromArrayContains()
770+
{
771+
using var _ = Py.GIL();
772+
773+
var module = PyModule.FromString("ThrowingImplicitConversionArrayContainsModule", @"
774+
from clr import AddReference
775+
AddReference('Python.EmbeddingTest')
776+
from System import Array, String
777+
from Python.EmbeddingTest import TestMethodBinder
778+
779+
def contains():
780+
return TestMethodBinder.ErroredImplicitConversion() in Array[String](['value'])
781+
");
782+
using var contained = module.GetAttr("contains").Invoke();
783+
Assert.IsFalse(contained.As<bool>());
784+
Assert.IsFalse(Exceptions.ErrorOccurred());
785+
}
786+
721787
[TestCase(true)]
722788
[TestCase(false)]
723789
public void FailedConversionOfNonClrValueManagedTypesOnlyLeavesErrorWhenSettingErrors(bool setError)

‎src/runtime/Converter.cs‎

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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();

‎src/runtime/MethodBinder.cs‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -470,11 +470,17 @@ internal static int ArgPrecedence(Type t, bool isOperatorMethod)
470470
/// </summary>
471471
internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw)
472472
{
473-
return Bind(inst, args, kw, null);
473+
return Bind(inst, args, kw, null, out _);
474474
}
475475

476-
internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase info)
476+
/// <param name="conversionError">
477+
/// When no overload matches, receives why the last argument conversion attempted failed,
478+
/// if converting it threw (e.g. a throwing implicit operator)
479+
/// </param>
480+
internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase info, out string? conversionError)
477481
{
482+
conversionError = null;
483+
478484
// If we have KWArgs create dictionary and collect them.
479485
// An empty kwargs dict (e.g. calling with **{}) is equivalent to no kwargs at all,
480486
// so we only create the dictionary if there are actual keyword arguments,
@@ -597,6 +603,8 @@ internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedRe
597603
}
598604
}
599605

606+
// Only a conversion that rejects the last candidate tried explains why nothing matched
607+
conversionError = null;
600608
NewReference tempObject = default;
601609

602610
// At this point, if op is IntPtr.Zero we don't have a KWArg and are not using default
@@ -687,7 +695,7 @@ internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedRe
687695
if (underlyingType == typeof(decimal) || underlyingType == typeof(double))
688696
{
689697
clrtype = parameter.ParameterType;
690-
typematch = Converter.ToManaged(op, clrtype, out arg, false);
698+
typematch = Converter.ToManaged(op, clrtype, out arg, false, out _, out conversionError);
691699
if (typematch)
692700
{
693701
implicitConversions++;
@@ -700,7 +708,7 @@ internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedRe
700708
else if (Runtime.PyFloat_Check(op) && argtypecode.IsInteger() && !underlyingType.IsEnum)
701709
{
702710
clrtype = parameter.ParameterType;
703-
typematch = Converter.ToManaged(op, clrtype, out arg, false);
711+
typematch = Converter.ToManaged(op, clrtype, out arg, false, out _, out conversionError);
704712
if (typematch)
705713
{
706714
implicitConversions++;
@@ -745,7 +753,7 @@ internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedRe
745753
outs++;
746754
}
747755

748-
if (!Converter.ToManaged(op, clrtype, out arg, false))
756+
if (!Converter.ToManaged(op, clrtype, out arg, false, out _, out conversionError))
749757
{
750758
tempObject.Dispose();
751759
margs = null;
@@ -812,10 +820,8 @@ internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedRe
812820

813821
if (matches.Count > 0 || (matchesUsingImplicitConversion != null && matchesUsingImplicitConversion.Count > 0))
814822
{
815-
// A rejected overload's conversion probe may have raised a Python error
816-
// even though probing converts with setError: false (e.g. a throwing
817-
// implicit operator raises unconditionally so Invoke can surface it as
818-
// the failure reason when nothing matches). Once an overload matched,
823+
// A rejected overload's conversion probe may have left a Python error
824+
// pending (e.g. a raising user decoder). Once an overload matched,
819825
// that error must not survive the successful call: CPython would fail
820826
// it with "SystemError: ... returned a result with an error set".
821827
Exceptions.Clear();
@@ -1027,12 +1033,18 @@ internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference a
10271033

10281034
internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase info, MethodInfo[] methodinfo)
10291035
{
1030-
Binding binding = Bind(inst, args, kw, info);
1036+
Binding binding = Bind(inst, args, kw, info, out var conversionError);
10311037
object result;
10321038
IntPtr ts = IntPtr.Zero;
10331039

10341040
if (binding == null)
10351041
{
1042+
// A conversion that threw has a more specific cause than the generic no-match message
1043+
if (conversionError != null && !Exceptions.ErrorOccurred())
1044+
{
1045+
Exceptions.RaiseTypeError(conversionError);
1046+
}
1047+
10361048
// If we already have an exception pending, don't create a new one
10371049
if (!Exceptions.ErrorOccurred())
10381050
{

‎src/runtime/PythonException.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ internal static Exception FetchCurrent()
142142
return null;
143143
}
144144

145-
if (Converter.ToManagedValue(pyInfo.Borrow(), typeof(ExceptionDispatchInfo), out object? result, setError: false, out var _))
145+
if (Converter.ToManagedValue(pyInfo.Borrow(), typeof(ExceptionDispatchInfo), out object? result, setError: false, out var _, out _))
146146
{
147147
return (ExceptionDispatchInfo)result!;
148148
}

‎src/runtime/TypeManager.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ internal static NewReference CreateSubType(BorrowedReference py_name, BorrowedRe
385385
{
386386
if (Exceptions.ErrorOccurred()) return default;
387387
}
388-
else if (!Converter.ToManagedValue(assemblyPtr, typeof(string), out assembly, true, out var _))
388+
else if (!Converter.ToManagedValue(assemblyPtr, typeof(string), out assembly, true, out var _, out _))
389389
{
390390
return Exceptions.RaiseTypeError("Couldn't convert __assembly__ value to string");
391391
}
@@ -397,7 +397,7 @@ internal static NewReference CreateSubType(BorrowedReference py_name, BorrowedRe
397397
{
398398
if (Exceptions.ErrorOccurred()) return default;
399399
}
400-
else if (!Converter.ToManagedValue(pyNamespace, typeof(string), out namespaceStr, true, out var _))
400+
else if (!Converter.ToManagedValue(pyNamespace, typeof(string), out namespaceStr, true, out var _, out _))
401401
{
402402
return Exceptions.RaiseTypeError("Couldn't convert __namespace__ value to string");
403403
}

0 commit comments

Comments
 (0)