Skip to content

Fixed __cause__ on overload bind failure and array conversion #1442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2021
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
5 changes: 5 additions & 0 deletions src/runtime/converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -814,9 +814,14 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo

private static void SetConversionError(IntPtr value, Type target)
{
// PyObject_Repr might clear the error
Runtime.PyErr_Fetch(out var causeType, out var causeVal, out var causeTrace);

IntPtr ob = Runtime.PyObject_Repr(value);
string src = Runtime.GetManagedString(ob);
Runtime.XDecref(ob);

Runtime.PyErr_Restore(causeType, causeVal, causeTrace);
Exceptions.RaiseTypeError($"Cannot convert {src} to {target}");
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/finalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class IncorrectRefCountException : Exception
public IncorrectRefCountException(IntPtr ptr)
{
PyPtr = ptr;
IntPtr pyname = Runtime.PyObject_Unicode(PyPtr);
IntPtr pyname = Runtime.PyObject_Str(PyPtr);
string name = Runtime.GetManagedString(pyname);
Runtime.XDecref(pyname);
_message = $"<{name}> may has a incorrect ref count";
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/methodbinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ protected static void AppendArgumentTypes(StringBuilder to, IntPtr args)
{
try
{
var description = Runtime.PyObject_Unicode(type);
var description = Runtime.PyObject_Str(type);
if (description != IntPtr.Zero)
{
to.Append(Runtime.GetManagedString(description));
Expand Down Expand Up @@ -926,7 +926,9 @@ internal virtual IntPtr Invoke(IntPtr inst, IntPtr args, IntPtr kw, MethodBase i
}

value.Append(": ");
Runtime.PyErr_Fetch(out var errType, out var errVal, out var errTrace);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might also be a fix for #1371

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might let us reenable the test case, but the issue that caused it will still be there.

AppendArgumentTypes(to: value, args);
Runtime.PyErr_Restore(errType, errVal, errTrace);
Exceptions.RaiseTypeError(value.ToString());
return IntPtr.Zero;
}
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/pyobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ public string Repr()
/// </remarks>
public override string ToString()
{
IntPtr strval = Runtime.PyObject_Unicode(obj);
IntPtr strval = Runtime.PyObject_Str(obj);
string result = Runtime.GetManagedString(strval);
Runtime.XDecref(strval);
return result;
Expand Down
20 changes: 11 additions & 9 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System.Reflection.Emit;
using System;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using System.Threading;
using System.Collections.Generic;
Expand Down Expand Up @@ -1127,13 +1126,18 @@ internal static int PyObject_Compare(IntPtr value1, IntPtr value2)
internal static nint PyObject_Hash(IntPtr op) => Delegates.PyObject_Hash(op);


internal static IntPtr PyObject_Repr(IntPtr pointer) => Delegates.PyObject_Repr(pointer);


internal static IntPtr PyObject_Str(IntPtr pointer) => Delegates.PyObject_Str(pointer);
internal static IntPtr PyObject_Repr(IntPtr pointer)
{
Debug.Assert(PyErr_Occurred() == IntPtr.Zero);
return Delegates.PyObject_Repr(pointer);
}


internal static IntPtr PyObject_Unicode(IntPtr pointer) => Delegates.PyObject_Unicode(pointer);
internal static IntPtr PyObject_Str(IntPtr pointer)
{
Debug.Assert(PyErr_Occurred() == IntPtr.Zero);
return Delegates.PyObject_Str(pointer);
}


internal static IntPtr PyObject_Dir(IntPtr pointer) => Delegates.PyObject_Dir(pointer);
Expand Down Expand Up @@ -2322,7 +2326,6 @@ static Delegates()
PyObject_Hash = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr>)GetFunctionByName(nameof(PyObject_Hash), GetUnmanagedDll(_PythonDll));
PyObject_Repr = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr>)GetFunctionByName(nameof(PyObject_Repr), GetUnmanagedDll(_PythonDll));
PyObject_Str = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr>)GetFunctionByName(nameof(PyObject_Str), GetUnmanagedDll(_PythonDll));
PyObject_Unicode = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr>)GetFunctionByName("PyObject_Str", GetUnmanagedDll(_PythonDll));
PyObject_Dir = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr>)GetFunctionByName(nameof(PyObject_Dir), GetUnmanagedDll(_PythonDll));
PyObject_GetBuffer = (delegate* unmanaged[Cdecl]<IntPtr, ref Py_buffer, int, int>)GetFunctionByName(nameof(PyObject_GetBuffer), GetUnmanagedDll(_PythonDll));
PyBuffer_Release = (delegate* unmanaged[Cdecl]<ref Py_buffer, void>)GetFunctionByName(nameof(PyBuffer_Release), GetUnmanagedDll(_PythonDll));
Expand Down Expand Up @@ -2607,7 +2610,6 @@ static Delegates()
internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr> PyObject_Hash { get; }
internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr> PyObject_Repr { get; }
internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr> PyObject_Str { get; }
internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr> PyObject_Unicode { get; }
internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr> PyObject_Dir { get; }
internal static delegate* unmanaged[Cdecl]<IntPtr, ref Py_buffer, int, int> PyObject_GetBuffer { get; }
internal static delegate* unmanaged[Cdecl]<ref Py_buffer, void> PyBuffer_Release { get; }
Expand Down