Skip to content

Commit a95945b

Browse files
committed
fixed __cause__ on overload bind failure and array conversion
also: added debug check
1 parent 0775458 commit a95945b

File tree

5 files changed

+21
-12
lines changed

5 files changed

+21
-12
lines changed

src/runtime/converter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,9 +814,14 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
814814

815815
private static void SetConversionError(IntPtr value, Type target)
816816
{
817+
// PyObject_Repr might clear the error
818+
Runtime.PyErr_Fetch(out var causeType, out var causeVal, out var causeTrace);
819+
817820
IntPtr ob = Runtime.PyObject_Repr(value);
818821
string src = Runtime.GetManagedString(ob);
819822
Runtime.XDecref(ob);
823+
824+
Runtime.PyErr_Restore(causeType, causeVal, causeTrace);
820825
Exceptions.RaiseTypeError($"Cannot convert {src} to {target}");
821826
}
822827

src/runtime/finalizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class IncorrectRefCountException : Exception
5454
public IncorrectRefCountException(IntPtr ptr)
5555
{
5656
PyPtr = ptr;
57-
IntPtr pyname = Runtime.PyObject_Unicode(PyPtr);
57+
IntPtr pyname = Runtime.PyObject_Str(PyPtr);
5858
string name = Runtime.GetManagedString(pyname);
5959
Runtime.XDecref(pyname);
6060
_message = $"<{name}> may has a incorrect ref count";

src/runtime/methodbinder.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ protected static void AppendArgumentTypes(StringBuilder to, IntPtr args)
876876
{
877877
try
878878
{
879-
var description = Runtime.PyObject_Unicode(type);
879+
var description = Runtime.PyObject_Str(type);
880880
if (description != IntPtr.Zero)
881881
{
882882
to.Append(Runtime.GetManagedString(description));
@@ -926,7 +926,9 @@ internal virtual IntPtr Invoke(IntPtr inst, IntPtr args, IntPtr kw, MethodBase i
926926
}
927927

928928
value.Append(": ");
929+
Runtime.PyErr_Fetch(out var errType, out var errVal, out var errTrace);
929930
AppendArgumentTypes(to: value, args);
931+
Runtime.PyErr_Restore(errType, errVal, errTrace);
930932
Exceptions.RaiseTypeError(value.ToString());
931933
return IntPtr.Zero;
932934
}

src/runtime/pyobject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ public string Repr()
10401040
/// </remarks>
10411041
public override string ToString()
10421042
{
1043-
IntPtr strval = Runtime.PyObject_Unicode(obj);
1043+
IntPtr strval = Runtime.PyObject_Str(obj);
10441044
string result = Runtime.GetManagedString(strval);
10451045
Runtime.XDecref(strval);
10461046
return result;

src/runtime/runtime.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
using System.Reflection.Emit;
21
using System;
2+
using System.Diagnostics;
33
using System.Diagnostics.Contracts;
44
using System.Runtime.InteropServices;
5-
using System.Security;
65
using System.Text;
76
using System.Threading;
87
using System.Collections.Generic;
@@ -1127,13 +1126,18 @@ internal static int PyObject_Compare(IntPtr value1, IntPtr value2)
11271126
internal static nint PyObject_Hash(IntPtr op) => Delegates.PyObject_Hash(op);
11281127

11291128

1130-
internal static IntPtr PyObject_Repr(IntPtr pointer) => Delegates.PyObject_Repr(pointer);
1131-
1132-
1133-
internal static IntPtr PyObject_Str(IntPtr pointer) => Delegates.PyObject_Str(pointer);
1129+
internal static IntPtr PyObject_Repr(IntPtr pointer)
1130+
{
1131+
Debug.Assert(PyErr_Occurred() == IntPtr.Zero);
1132+
return Delegates.PyObject_Repr(pointer);
1133+
}
11341134

11351135

1136-
internal static IntPtr PyObject_Unicode(IntPtr pointer) => Delegates.PyObject_Unicode(pointer);
1136+
internal static IntPtr PyObject_Str(IntPtr pointer)
1137+
{
1138+
Debug.Assert(PyErr_Occurred() == IntPtr.Zero);
1139+
return Delegates.PyObject_Str(pointer);
1140+
}
11371141

11381142

11391143
internal static IntPtr PyObject_Dir(IntPtr pointer) => Delegates.PyObject_Dir(pointer);
@@ -2322,7 +2326,6 @@ static Delegates()
23222326
PyObject_Hash = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr>)GetFunctionByName(nameof(PyObject_Hash), GetUnmanagedDll(_PythonDll));
23232327
PyObject_Repr = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr>)GetFunctionByName(nameof(PyObject_Repr), GetUnmanagedDll(_PythonDll));
23242328
PyObject_Str = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr>)GetFunctionByName(nameof(PyObject_Str), GetUnmanagedDll(_PythonDll));
2325-
PyObject_Unicode = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr>)GetFunctionByName("PyObject_Str", GetUnmanagedDll(_PythonDll));
23262329
PyObject_Dir = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr>)GetFunctionByName(nameof(PyObject_Dir), GetUnmanagedDll(_PythonDll));
23272330
PyObject_GetBuffer = (delegate* unmanaged[Cdecl]<IntPtr, ref Py_buffer, int, int>)GetFunctionByName(nameof(PyObject_GetBuffer), GetUnmanagedDll(_PythonDll));
23282331
PyBuffer_Release = (delegate* unmanaged[Cdecl]<ref Py_buffer, void>)GetFunctionByName(nameof(PyBuffer_Release), GetUnmanagedDll(_PythonDll));
@@ -2607,7 +2610,6 @@ static Delegates()
26072610
internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr> PyObject_Hash { get; }
26082611
internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr> PyObject_Repr { get; }
26092612
internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr> PyObject_Str { get; }
2610-
internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr> PyObject_Unicode { get; }
26112613
internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr> PyObject_Dir { get; }
26122614
internal static delegate* unmanaged[Cdecl]<IntPtr, ref Py_buffer, int, int> PyObject_GetBuffer { get; }
26132615
internal static delegate* unmanaged[Cdecl]<ref Py_buffer, void> PyBuffer_Release { get; }

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy