Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
120 commits
Select commit Hold shift + click to select a range
5d6e5d5
use reference types instead of IntPtr where possible in runtime.cs
lostmsu Oct 17, 2021
2d33902
switched converter.cs to the new style references
lostmsu Oct 17, 2021
f8b761a
switched most of classbase.cs to the new style references (except cro…
lostmsu Oct 17, 2021
09d8e41
switched pyobject.cs to the new style references
lostmsu Oct 17, 2021
1b58cf4
mostly switched moduleobject.cs to the new style references
lostmsu Oct 17, 2021
c05c6ec
switched methodbinder.cs to the new style references
lostmsu Oct 17, 2021
d626f7e
partially switched classderived.cs to the new reference style
lostmsu Oct 17, 2021
ff60ec4
switched arrayobject.cs to the new style references
lostmsu Oct 17, 2021
0010fa0
switched delegatemanager.cs to the new style references
lostmsu Oct 17, 2021
178a359
partially switched metatype.cs to the new style references
lostmsu Oct 18, 2021
9764b25
switched typemanager.cs to the new style references
lostmsu Oct 18, 2021
11edcc3
switched pytype.cs to the new style references
lostmsu Oct 18, 2021
2e71874
mass enable nullable types
lostmsu Oct 18, 2021
3076040
fixed nullablity in arrayobject.cs
lostmsu Oct 18, 2021
56f3bd5
fixed nullability in assemblymanager.cs
lostmsu Oct 18, 2021
58cb0e6
switched classmanager.cs to the new style references
lostmsu Oct 18, 2021
2095b46
switched classobject.cs to the new style references
lostmsu Oct 18, 2021
f6b84da
partially switched managedtype.cs to the new style references
lostmsu Oct 18, 2021
ee65632
partially switched classmanager.cs to the new style references
lostmsu Oct 18, 2021
5266dc4
PyIdentifier public members to return borrowed references
lostmsu Oct 6, 2021
0bc3670
added nullability annotations to methodbinder.cs
lostmsu Oct 18, 2021
de9a8cb
switched methodbinding.cs and methodobject.cs to the new style refere…
lostmsu Oct 18, 2021
9195c30
switched overload.cs to the new style references
lostmsu Oct 18, 2021
590de7a
switched propertyobject.cs to the new style references
lostmsu Oct 18, 2021
7fa537a
switched delegateobject.cs to the new style references
lostmsu Oct 18, 2021
49124fc
switched module.cs to the new style references
lostmsu Oct 18, 2021
9db9b0b
nullability annotations for PyObject
lostmsu Oct 18, 2021
00fd17a
switched modulefunctionobject.cs to the new style references
lostmsu Oct 18, 2021
5798b41
minor refactorings
lostmsu Oct 18, 2021
ebdf7c5
partially switched moduleobject.cs and importhook.cs to the new style…
lostmsu Oct 18, 2021
5ad09e4
switched exceptions.cs to the new style references
lostmsu Oct 18, 2021
d1abd9a
switched interfaceobject.cs to the new style references
lostmsu Oct 18, 2021
43a862a
switched pythonexception.cs to the new style references
lostmsu Oct 18, 2021
1d80162
switched pytuple.cs to the new style references
lostmsu Oct 18, 2021
0241b38
switched eventobject.cs and eventbiding.cs to the new style references
lostmsu Oct 18, 2021
2ac952a
switched all PyObject derived classes to the new style references
lostmsu Oct 18, 2021
7adf98a
implemented non-confusing PyModule_AddObject
lostmsu Oct 18, 2021
2dd3f8f
switched pythonengine.cs to the new style references
lostmsu Oct 18, 2021
3b79019
switched fieldobject.cs and constructorbinding.cs to the new style re…
lostmsu Oct 18, 2021
9b990c1
switched finalizer.cs to the new style references
lostmsu Oct 19, 2021
027e529
switched debughelper.cs to the new style references
lostmsu Oct 19, 2021
4793818
switched converter extensions and sample codecs to the new style refe…
lostmsu Oct 19, 2021
0d60500
switched collection wrappers (from sample codec) to the new style ref…
lostmsu Oct 19, 2021
cf606a2
switched iterator.cs and indexer.cs to the new style references
lostmsu Oct 19, 2021
bb84c48
getting rid of a few minor warnings and compile errors
lostmsu Oct 19, 2021
e295679
switched to new references in some tests
lostmsu Oct 19, 2021
7a9e411
switched state serialization to new reference types (untested)
lostmsu Oct 19, 2021
9a9ed3b
minor error fixes
lostmsu Oct 19, 2021
581f695
assume remaning manual refcounting is not needed, because we use smar…
lostmsu Oct 19, 2021
07f1657
fixed new reference uses, that are not allowed in C#
lostmsu Oct 19, 2021
7deebd4
renamed parameter in tp_dealloc functions for clarity
lostmsu Oct 19, 2021
8619e77
allowed untested calls to PyObject_GC_Del and XDecref (3 in total)
lostmsu Oct 19, 2021
672aef6
fixed compile errors in TypeMethod (untested)
lostmsu Oct 19, 2021
c4909d4
workaround for analyzer not permitting copying a reference as the las…
lostmsu Oct 19, 2021
6fa2004
switched tests to match the new reference changes
lostmsu Oct 19, 2021
14949fb
fixed thunk loading for slots, that use new reference types
lostmsu Oct 19, 2021
0728e21
fixed PyObject_DelAttr load from DLL failing
lostmsu Oct 19, 2021
fe4c481
fixed uses of Marshal.Read/Marshal.Write overloads with first argumen…
lostmsu Oct 19, 2021
4346d41
fixed OnSerialized and OnDeserialized in PyObject not being typed cor…
lostmsu Oct 19, 2021
62e193a
fixed bad equality comparisons
lostmsu Oct 20, 2021
2fa8b9c
improved reliability of Clean and Dealloc implementations
lostmsu Oct 20, 2021
d6607b0
bad if condition
lostmsu Oct 20, 2021
6335d97
improved GetThunk reliability
lostmsu Oct 20, 2021
d649d6c
fixed circular dependency in Runtime PyMembers and InternString initi…
lostmsu Oct 20, 2021
d1bc193
tiny refactor
lostmsu Oct 20, 2021
32c4bb6
switched generictype.cs to the new style references
lostmsu Oct 20, 2021
a1427ac
increfs in OnSave are no longer necessary with the new references
lostmsu Oct 20, 2021
cd97a46
fixed MethodBinding failing for reference types
lostmsu Oct 20, 2021
05ecbcf
nullability annotation fix in MaybeMethodBase
lostmsu Oct 20, 2021
a3591b6
minor improvements
lostmsu Oct 20, 2021
7ed0c7a
WIP
lostmsu Oct 24, 2021
d6a853f
avoid generating and handling useless SerializationException when May…
lostmsu Oct 28, 2021
b0c25c1
finalizer does not attempt to finalize objects when runtime is shut down
lostmsu Oct 28, 2021
5ca474a
PyType Dict and MRO properties to assist debugging
lostmsu Oct 28, 2021
48078b3
WIP 2
lostmsu Oct 28, 2021
a624dd8
fixed PyObject disposal crashing when runtime is still finalizing
lostmsu Oct 29, 2021
e7ab071
arrays: use 64 bit indexing, and avoid first chance .NET exceptions o…
lostmsu Oct 29, 2021
cbe1dd2
refactored conditional ClassBase slot initialization
lostmsu Oct 29, 2021
d5f1c48
removed DisposePythonWrappersForClrTypes
lostmsu Oct 29, 2021
74d87c5
simplified outdated condition in ClassBase.tp_clear
lostmsu Oct 29, 2021
82d6c33
sprinkled a few DebuggerHidden to make debugging easier
lostmsu Oct 29, 2021
eeebcd7
fixed derived classes not inheriting slots correctly
lostmsu Oct 29, 2021
8ee8d3d
remove unused TypeManager._slotImpls
lostmsu Oct 29, 2021
1a4ada7
fixed TestRuntime not building in Release mode
lostmsu Oct 30, 2021
a610aa3
can't really clear managed references to Python objects from ManagedT…
lostmsu Oct 30, 2021
03f32cb
PythonException is serializable
lostmsu Oct 30, 2021
b1c9f5b
EventObject no longer used for static events. EventBinding is constru…
lostmsu Oct 30, 2021
cb4bb9a
use a special class to stub .NET types that no longer exist after a d…
lostmsu Nov 2, 2021
652f946
make EventHandlerCollection serializable
lostmsu Nov 2, 2021
84db670
fixed MaybeMemberInfo always failing for properties
lostmsu Nov 2, 2021
56fafe3
fixed construct_removed_class domain reload test case
lostmsu Nov 2, 2021
d33dcdd
domain reload test runner can run test by index
lostmsu Nov 2, 2021
b737e10
minor docs change
lostmsu Nov 2, 2021
d3e4fba
assert check in GetUnmanagedBaseType for null base
lostmsu Nov 2, 2021
e003e12
PythonEngine .Exec and .Eval no longer work with raw pointers
lostmsu Nov 9, 2021
d0a6f44
a few annotation to ease debugging
lostmsu Nov 9, 2021
e31f7ba
ensure Python types continue to exist when registered decoders for th…
lostmsu Nov 9, 2021
48c0dfc
GC-related WIP
lostmsu Nov 9, 2021
44d65d9
merge latest master
lostmsu Nov 23, 2021
cb58147
merge latest changes from upstream
lostmsu Nov 23, 2021
2fdbf0e
added TraceAlloc solution configuration
lostmsu Nov 24, 2021
a8ef06c
fixed sending PyObject across domain boundary
lostmsu Nov 24, 2021
7167229
fixed accidental premature disposal of Runtime.PyNone
lostmsu Nov 24, 2021
ab11fa2
made freeing GCHandles more robust
lostmsu Nov 24, 2021
7a4daeb
removed bad assert in generated constructor for derived classes
lostmsu Nov 24, 2021
e422367
fixed __pyobj__ access
lostmsu Nov 24, 2021
a74ea86
minor
lostmsu Nov 24, 2021
0325a8c
fixed Python derived types trying to double-free GCHandle when collec…
lostmsu Nov 24, 2021
85fab3b
reinstate collection assert on shutdown from Python
lostmsu Nov 24, 2021
932fce2
fixed crash when Python derived class instances survive past early sh…
lostmsu Nov 24, 2021
c2e207a
delay nulling GC handles of reflected instances until the last moment…
lostmsu Nov 24, 2021
c8f0f09
fixed assert in XDecref in case _Py_IsFinalizing is not present
lostmsu Nov 24, 2021
e269cf0
when initialized from Python, reset slots implemented in CLR: CLR mig…
lostmsu Nov 25, 2021
d7d5cb7
fixed minor warnings
lostmsu Nov 25, 2021
d6edace
fixed line endings in intern_.cs
lostmsu Nov 25, 2021
a86994f
use NonCopyableAnalyzer 0.7.0-m05
lostmsu Nov 25, 2021
2e040ea
Merge pull request #2 from losttech/ManagedType-DontKeepBorrowedRefs
lostmsu Dec 9, 2021
b614dba
merge latest master
lostmsu Dec 9, 2021
f09a48b
fixed MacOS bad assembly test by using PythonDLL (which is never a .N…
lostmsu Dec 9, 2021
3794fea
Merge branch 'master' into precise-API-references
lostmsu Dec 14, 2021
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
Prev Previous commit
Next Next commit
switched methodbinder.cs to the new style references
  • Loading branch information
lostmsu committed Oct 17, 2021
commit c05c6ec39d37812e5ed9d8675606154238efb5a4
2 changes: 2 additions & 0 deletions src/runtime/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ internal static class Util
internal const string UseOverloadWithReferenceTypes =
"This API is unsafe, and will be removed in the future. Use overloads working with *Reference types";

internal const string BadStr = "bad __str__";


[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static int ReadInt32(BorrowedReference ob, int offset)
Expand Down
130 changes: 59 additions & 71 deletions src/runtime/methodbinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ internal static int ArgPrecedence(Type t)
/// <param name="args">The Python arguments.</param>
/// <param name="kw">The Python keyword arguments.</param>
/// <returns>A Binding if successful. Otherwise null.</returns>
internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw)
internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw)
{
return Bind(inst, args, kw, null, null);
}
Expand All @@ -316,7 +316,7 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw)
/// <param name="kw">The Python keyword arguments.</param>
/// <param name="info">If not null, only bind to that method.</param>
/// <returns>A Binding if successful. Otherwise null.</returns>
internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info)
internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase info)
{
return Bind(inst, args, kw, info, null);
}
Expand Down Expand Up @@ -363,24 +363,23 @@ public MismatchedMethod(Exception exception, MethodBase mb)
/// <param name="info">If not null, only bind to that method.</param>
/// <param name="methodinfo">If not null, additionally attempt to bind to the generic methods in this array by inferring generic type parameters.</param>
/// <returns>A Binding if successful. Otherwise null.</returns>
internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, MethodInfo[] methodinfo)
internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase info, MethodInfo[] methodinfo)
{
// loop to find match, return invoker w/ or w/o error
MethodBase[] _methods = null;

var kwargDict = new Dictionary<string, IntPtr>();
if (kw != IntPtr.Zero)
var kwargDict = new Dictionary<string, PyObject>();
if (kw != null)
{
var pynkwargs = (int)Runtime.PyDict_Size(kw);
IntPtr keylist = Runtime.PyDict_Keys(kw);
IntPtr valueList = Runtime.PyDict_Values(kw);
nint pynkwargs = Runtime.PyDict_Size(kw);
using var keylist = Runtime.PyDict_Keys(kw);
using var valueList = Runtime.PyDict_Values(kw);
for (int i = 0; i < pynkwargs; ++i)
{
var keyStr = Runtime.GetManagedString(Runtime.PyList_GetItem(new BorrowedReference(keylist), i));
kwargDict[keyStr] = Runtime.PyList_GetItem(new BorrowedReference(valueList), i).DangerousGetAddress();
var keyStr = Runtime.GetManagedString(Runtime.PyList_GetItem(keylist.Borrow(), i));
BorrowedReference value = Runtime.PyList_GetItem(valueList.Borrow(), i);
kwargDict[keyStr] = new PyObject(value);
}
Runtime.XDecref(keylist);
Runtime.XDecref(valueList);
}

var pynargs = (int)Runtime.PyTuple_Size(args);
Expand Down Expand Up @@ -442,7 +441,7 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
}
if (isOperator)
{
if (inst != IntPtr.Zero)
if (inst != null)
{
if (ManagedType.GetManagedObject(inst) is CLRObject co)
{
Expand Down Expand Up @@ -514,7 +513,7 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
var mi = bestMatch.Method;

object target = null;
if (!mi.IsStatic && inst != IntPtr.Zero)
if (!mi.IsStatic && inst != null)
{
//CLRObject co = (CLRObject)ManagedType.GetManagedObject(inst);
// InvalidCastException: Unable to cast object of type
Expand Down Expand Up @@ -561,33 +560,33 @@ static AggregateException GetAggregateException(IEnumerable<MismatchedMethod> mi
return new AggregateException(mismatchedMethods.Select(m => new ArgumentException($"{m.Exception.Message} in method {m.Method}", m.Exception)));
}

static IntPtr HandleParamsArray(IntPtr args, int arrayStart, int pyArgCount, out bool isNewReference)
static BorrowedReference HandleParamsArray(BorrowedReference args, int arrayStart, int pyArgCount, out NewReference tempObject)
{
isNewReference = false;
IntPtr op;
BorrowedReference op;
tempObject = default;
// for a params method, we may have a sequence or single/multiple items
// here we look to see if the item at the paramIndex is there or not
// and then if it is a sequence itself.
if ((pyArgCount - arrayStart) == 1)
{
// we only have one argument left, so we need to check it
// to see if it is a sequence or a single item
IntPtr item = Runtime.PyTuple_GetItem(args, arrayStart);
BorrowedReference item = Runtime.PyTuple_GetItem(args, arrayStart);
if (!Runtime.PyString_Check(item) && Runtime.PySequence_Check(item))
{
// it's a sequence (and not a string), so we use it as the op
op = item;
}
else
{
isNewReference = true;
op = Runtime.PyTuple_GetSlice(args, arrayStart, pyArgCount);
tempObject = Runtime.PyTuple_GetSlice(args, arrayStart, pyArgCount);
op = tempObject.Borrow();
}
}
else
{
isNewReference = true;
op = Runtime.PyTuple_GetSlice(args, arrayStart, pyArgCount);
tempObject = Runtime.PyTuple_GetSlice(args, arrayStart, pyArgCount);
op = tempObject.Borrow();
}
return op;
}
Expand All @@ -607,8 +606,8 @@ static IntPtr HandleParamsArray(IntPtr args, int arrayStart, int pyArgCount, out
/// <param name="outs">Returns number of output parameters</param>
/// <returns>If successful, an array of .NET arguments that can be passed to the method. Otherwise null.</returns>
static object[] TryConvertArguments(ParameterInfo[] pi, bool paramsArray,
IntPtr args, int pyArgCount,
Dictionary<string, IntPtr> kwargDict,
BorrowedReference args, int pyArgCount,
Dictionary<string, PyObject> kwargDict,
ArrayList defaultArgList,
out int outs)
{
Expand All @@ -620,7 +619,6 @@ static object[] TryConvertArguments(ParameterInfo[] pi, bool paramsArray,
{
var parameter = pi[paramIndex];
bool hasNamedParam = parameter.Name != null ? kwargDict.ContainsKey(parameter.Name) : false;
bool isNewReference = false;

if (paramIndex >= pyArgCount && !(hasNamedParam || (paramsArray && paramIndex == arrayStart)))
{
Expand All @@ -632,7 +630,8 @@ static object[] TryConvertArguments(ParameterInfo[] pi, bool paramsArray,
continue;
}

IntPtr op;
BorrowedReference op;
NewReference tempObject = default;
if (hasNamedParam)
{
op = kwargDict[parameter.Name];
Expand All @@ -641,7 +640,7 @@ static object[] TryConvertArguments(ParameterInfo[] pi, bool paramsArray,
{
if(arrayStart == paramIndex)
{
op = HandleParamsArray(args, arrayStart, pyArgCount, out isNewReference);
op = HandleParamsArray(args, arrayStart, pyArgCount, out tempObject);
}
else
{
Expand All @@ -652,16 +651,11 @@ static object[] TryConvertArguments(ParameterInfo[] pi, bool paramsArray,
bool isOut;
if (!TryConvertArgument(op, parameter.ParameterType, out margs[paramIndex], out isOut))
{
tempObject.Dispose();
return null;
}

if (isNewReference)
{
// TODO: is this a bug? Should this happen even if the conversion fails?
// GetSlice() creates a new reference but GetItem()
// returns only a borrow reference.
Runtime.XDecref(op);
}
tempObject.Dispose();

if (isOut)
{
Expand All @@ -681,7 +675,7 @@ static object[] TryConvertArguments(ParameterInfo[] pi, bool paramsArray,
/// <param name="arg">Converted argument.</param>
/// <param name="isOut">Whether the CLR type is passed by reference.</param>
/// <returns>true on success</returns>
static bool TryConvertArgument(IntPtr op, Type parameterType,
static bool TryConvertArgument(BorrowedReference op, Type parameterType,
out object arg, out bool isOut)
{
arg = null;
Expand All @@ -707,22 +701,21 @@ static bool TryConvertArgument(IntPtr op, Type parameterType,
/// <param name="parameterType">The parameter's managed type.</param>
/// <param name="argument">Pointer to the Python argument object.</param>
/// <returns>null if conversion is not possible</returns>
static Type TryComputeClrArgumentType(Type parameterType, IntPtr argument)
static Type TryComputeClrArgumentType(Type parameterType, BorrowedReference argument)
{
// this logic below handles cases when multiple overloading methods
// are ambiguous, hence comparison between Python and CLR types
// is necessary
Type clrtype = null;
IntPtr pyoptype;

if (clrtype != null)
{
if ((parameterType != typeof(object)) && (parameterType != clrtype))
{
IntPtr pytype = Converter.GetPythonTypeByAlias(parameterType);
pyoptype = Runtime.PyObject_Type(argument);
BorrowedReference pytype = Converter.GetPythonTypeByAlias(parameterType);
BorrowedReference pyoptype = Runtime.PyObject_TYPE(argument);
var typematch = false;
if (pyoptype != IntPtr.Zero)
if (pyoptype != null)
{
if (pytype != pyoptype)
{
Expand All @@ -749,7 +742,6 @@ static Type TryComputeClrArgumentType(Type parameterType, IntPtr argument)
Exceptions.RaiseTypeError($"Expected {parameterTypeCode}, got {clrTypeCode}");
}
}
Runtime.XDecref(pyoptype);
if (!typematch)
{
return null;
Expand Down Expand Up @@ -779,7 +771,7 @@ static Type TryComputeClrArgumentType(Type parameterType, IntPtr argument)
/// <param name="defaultsNeeded">Number of non-null defaultsArgs.</param>
/// <returns></returns>
static bool MatchesArgumentCount(int positionalArgumentCount, ParameterInfo[] parameters,
Dictionary<string, IntPtr> kwargDict,
Dictionary<string, PyObject> kwargDict,
out bool paramsArray,
out ArrayList defaultArgList,
out int kwargsMatched,
Expand Down Expand Up @@ -847,30 +839,29 @@ internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference a
return Invoke(inst, args, kw, info, null);
}

protected static void AppendArgumentTypes(StringBuilder to, IntPtr args)
protected static void AppendArgumentTypes(StringBuilder to, BorrowedReference args)
{
long argCount = Runtime.PyTuple_Size(args);
Runtime.AssertNoErorSet();

nint argCount = Runtime.PyTuple_Size(args);
to.Append("(");
for (long argIndex = 0; argIndex < argCount; argIndex++)
for (nint argIndex = 0; argIndex < argCount; argIndex++)
{
var arg = Runtime.PyTuple_GetItem(args, argIndex);
if (arg != IntPtr.Zero)
BorrowedReference arg = Runtime.PyTuple_GetItem(args, argIndex);
if (arg != null)
{
var type = Runtime.PyObject_Type(arg);
if (type != IntPtr.Zero)
BorrowedReference type = Runtime.PyObject_TYPE(arg);
if (type != null)
{
try
using var description = Runtime.PyObject_Str(type);
if (description.IsNull())
{
var description = Runtime.PyObject_Str(type);
if (description != IntPtr.Zero)
{
to.Append(Runtime.GetManagedString(description));
Runtime.XDecref(description);
}
Exceptions.Clear();
to.Append(Util.BadStr);
}
finally
else
{
Runtime.XDecref(type);
to.Append(Runtime.GetManagedString(description.Borrow()));
}
}
}
Expand Down Expand Up @@ -914,8 +905,7 @@ internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference a
Runtime.PyErr_Fetch(out var errType, out var errVal, out var errTrace);
AppendArgumentTypes(to: value, args);
Runtime.PyErr_Restore(errType.StealNullable(), errVal.StealNullable(), errTrace.StealNullable());
Exceptions.RaiseTypeError(value.ToString());
return IntPtr.Zero;
return Exceptions.RaiseTypeError(value.ToString());
}

if (allow_threads)
Expand All @@ -938,7 +928,7 @@ internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference a
PythonEngine.EndAllowThreads(ts);
}
Exceptions.SetError(e);
return IntPtr.Zero;
return default;
}

if (allow_threads)
Expand All @@ -962,11 +952,11 @@ internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference a

bool isVoid = mi.ReturnType == typeof(void);
int tupleSize = binding.outs + (isVoid ? 0 : 1);
IntPtr t = Runtime.PyTuple_New(tupleSize);
using var t = Runtime.PyTuple_New(tupleSize);
if (!isVoid)
{
IntPtr v = Converter.ToPython(result, mi.ReturnType);
Runtime.PyTuple_SetItem(t, n, v);
using var v = Converter.ToPython(result, mi.ReturnType);
Runtime.PyTuple_SetItem(t.Borrow(), n, v.Steal());
n++;
}

Expand All @@ -975,21 +965,19 @@ internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference a
Type pt = pi[i].ParameterType;
if (pt.IsByRef)
{
IntPtr v = Converter.ToPython(binding.args[i], pt.GetElementType());
Runtime.PyTuple_SetItem(t, n, v);
using var v = Converter.ToPython(binding.args[i], pt.GetElementType());
Runtime.PyTuple_SetItem(t.Borrow(), n, v.Steal());
n++;
}
}

if (binding.outs == 1 && mi.ReturnType == typeof(void))
{
IntPtr v = Runtime.PyTuple_GetItem(t, 0);
Runtime.XIncref(v);
Runtime.XDecref(t);
return v;
BorrowedReference item = Runtime.PyTuple_GetItem(t.Borrow(), 0);
return new NewReference(item);
}

return t;
return new NewReference(t.Borrow());
}

return Converter.ToPython(result, mi.ReturnType);
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1586,7 +1586,7 @@ internal static int PyTuple_SetItem(BorrowedReference pointer, nint index, Borro

internal static int PyTuple_SetItem(BorrowedReference pointer, nint index, StolenReference value) => Delegates.PyTuple_SetItem(pointer, index, value);

private static NewReference PyTuple_GetSlice(BorrowedReference pointer, nint start, nint end) => Delegates.PyTuple_GetSlice(pointer, start, end);
internal static NewReference PyTuple_GetSlice(BorrowedReference pointer, nint start, nint end) => Delegates.PyTuple_GetSlice(pointer, start, end);

internal static nint PyTuple_Size(BorrowedReference pointer) => Delegates.PyTuple_Size(pointer);

Expand Down