Better error messages from PyObject.AsManagedObject and DelegateManager.TrueDispatch by tminka · Pull Request #1344 · pythonnet/pythonnet · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/embed_tests/TestPyObject.cs
24 changes: 5 additions & 19 deletions src/runtime/delegatemanager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,19 +221,17 @@ public void Dispose()
public object Dispatch(ArrayList args)
{
IntPtr gs = PythonEngine.AcquireLock();
object ob = null;
object ob;

try
{
ob = TrueDispatch(args);
}
catch (Exception e)
finally
{
PythonEngine.ReleaseLock(gs);
throw e;
}

PythonEngine.ReleaseLock(gs);
return ob;
}

Expand Down Expand Up @@ -266,27 +264,15 @@ public object TrueDispatch(ArrayList args)
return null;
}

object result = null;
if (!Converter.ToManaged(op, rtype, out result, false))
object result;
if (!Converter.ToManaged(op, rtype, out result, true))
{
Runtime.XDecref(op);
throw new ConversionException($"could not convert Python result to {rtype}");
throw new PythonException();
}

Runtime.XDecref(op);
return result;
}
}


public class ConversionException : Exception
{
public ConversionException()
{
}

public ConversionException(string msg) : base(msg)
{
}
}
}
11 changes: 3 additions & 8 deletions src/runtime/pyobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ public static PyObject FromManagedObject(object ob)
public object AsManagedObject(Type t)
{
object result;
if (!Converter.ToManaged(obj, t, out result, false))
if (!Converter.ToManaged(obj, t, out result, true))
{
throw new InvalidCastException("cannot convert object to target type");
throw new InvalidCastException("cannot convert object to target type", new PythonException());
}
return result;
}
Expand All @@ -154,12 +154,7 @@ public T As<T>()
{
return (T)(this as object);
}
object result;
if (!Converter.ToManaged(obj, typeof(T), out result, false))
{
throw new InvalidCastException("cannot convert object to target type");
}
return (T)result;
return (T)AsManagedObject(typeof(T));
}


Expand Down
21 changes: 20 additions & 1 deletion src/tests/test_delegate.py