- Notifications
You must be signed in to change notification settings - Fork750
Closed
Labels
Description
Hi. In response tothis query I had one of my colleagues debug an issue, and he found that PythonNet does not correctly generate code when calling into a function with a "ref ValueType" parameter. In his words:
The problem ishere in the PythonNet code:
for (var i = 0; i < parameters.Length; ++i) { il.Emit(OpCodes.Ldloc_0); il.Emit(OpCodes.Ldc_I4, i); il.Emit(OpCodes.Ldarg, i + 1); if (parameterTypes[i].IsValueType) { il.Emit(OpCodes.Box, parameterTypes[i]); } il.Emit(OpCodes.Stelem, typeof(object)); }
The code is trying to store all of the incoming arguments in an array of type Object[]. The cases are:
- If the incoming argument is of reference type, the reference can simply be stored in the array.
- If the incoming argument is of struct type, the argument needs to be boxed into an object, which can then be stored in the array.
- If the incoming argument is a “byref” (i.e., “ref” keyword in C#), more complicated handling is needed, including:
oOn the way in, the byref needs to read (via ldobj if the underlying type is a struct, or ldind.ref otherwise) to load the actual argument data that needs to flow through.
oOn the way out, the byref needs to written (via stobj or stind.ref) to make sure any updates applied by the callee are reflected back to the caller.
The code above handles#1 and#2, but doesn’t handle#3.
Hopefully that's enough for someone to create or contribute a fix.