Skip to content

Commit 2edbc87

Browse files
author
dse
committed
NetCoreApp 2.0 fix. EmitCalli does not supports cdecl. Falling-back to a slow Marshal-based solution. + x86 fix.
1 parent 1cc9008 commit 2edbc87

File tree

5 files changed

+36
-18
lines changed

5 files changed

+36
-18
lines changed

appveyor.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ init:
4343
install:
4444
- pip install --upgrade -r requirements.txt --quiet
4545
- choco install vswhere -y
46-
# - cmd: curl -O https://download.microsoft.com/download/5/6/B/56BFEF92-9045-4414-970C-AB31E0FC07EC/dotnet-runtime-2.0.0-win-x86.exe
47-
# - cmd: dotnet-runtime-2.0.0-win-x86.exe /install /quiet /norestart /log install.log
46+
- cmd: curl -O https://download.microsoft.com/download/5/6/B/56BFEF92-9045-4414-970C-AB31E0FC07EC/dotnet-runtime-2.0.0-win-x86.exe
47+
- cmd: dotnet-runtime-2.0.0-win-x86.exe /install /quiet /norestart /log install.log
4848

4949
# Install OpenCover. Can't put on `packages.config`, not Mono compatible
5050
- .\tools\nuget\nuget.exe install OpenCover -OutputDirectory packages -Verbosity quiet

src/runtime/importhook.cs

-3
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,7 @@ internal static void Shutdown()
7575
if (Runtime.Py_IsInitialized() != 0)
7676
{
7777
Runtime.XDecref(py_clr_module);
78-
// TODO: Very strange behavior under CoreCLR. System.ExecutionEngineException (Crash)
79-
#if !NETCOREAPP
8078
Runtime.XDecref(root.pyHandle);
81-
#endif
8279
Runtime.XDecref(py_import);
8380
}
8481
}

src/runtime/metatype.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Runtime.InteropServices;
33

44
namespace Python.Runtime
@@ -201,7 +201,7 @@ public static int tp_setattro(IntPtr tp, IntPtr name, IntPtr value)
201201
IntPtr fp = Marshal.ReadIntPtr(dt, TypeOffset.tp_descr_set);
202202
if (fp != IntPtr.Zero)
203203
{
204-
return NativeCall.Impl.Int_Call_3(fp, descr, name, value);
204+
return NativeCall.Int_Call_3(fp, descr, name, value);
205205
}
206206
Exceptions.SetError(Exceptions.AttributeError, "attribute is read-only");
207207
return -1;

src/runtime/nativecall.cs

+30-9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,32 @@ namespace Python.Runtime
2323
/// </summary>
2424
internal class NativeCall
2525
{
26+
#if NETCOREAPP
27+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
28+
private delegate void Void_1_Delegate(IntPtr a1);
29+
30+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
31+
private delegate IntPtr IntPtr_3_Delegate(IntPtr a1, IntPtr a2, IntPtr a3);
32+
33+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
34+
private delegate int Int_3_Delegate(IntPtr a1, IntPtr a2, IntPtr a3);
35+
36+
public static void Void_Call_1(IntPtr fp, IntPtr a1)
37+
{
38+
((Void_1_Delegate)Marshal.GetDelegateForFunctionPointer(fp, typeof(Void_1_Delegate)))(a1);
39+
}
40+
41+
public static IntPtr Call_3(IntPtr fp, IntPtr a1, IntPtr a2, IntPtr a3)
42+
{
43+
return ((IntPtr_3_Delegate)Marshal.GetDelegateForFunctionPointer(fp, typeof(IntPtr_3_Delegate)))(a1, a2, a3);
44+
}
45+
46+
47+
public static int Int_Call_3(IntPtr fp, IntPtr a1, IntPtr a2, IntPtr a3)
48+
{
49+
return ((Int_3_Delegate)Marshal.GetDelegateForFunctionPointer(fp, typeof(Int_3_Delegate)))(a1, a2, a3);
50+
}
51+
#else
2652
private static AssemblyBuilder aBuilder;
2753
private static ModuleBuilder mBuilder;
2854

@@ -106,19 +132,12 @@ private static void GenerateThunk(TypeBuilder tb, MethodInfo method)
106132

107133
il.Emit(OpCodes.Ldarg_1);
108134

109-
#if NETCOREAPP
110-
il.EmitCalli(OpCodes.Calli,
111-
CallingConventions.ExplicitThis,
112-
method.ReturnType,
113-
nargs, null
114-
);
115-
#else
116135
il.EmitCalli(OpCodes.Calli,
117136
CallingConvention.Cdecl,
118137
method.ReturnType,
119138
nargs
120139
);
121-
#endif
140+
122141
il.Emit(OpCodes.Ret);
123142

124143
tb.DefineMethodOverride(mb, method);
@@ -139,9 +158,10 @@ public static int Int_Call_3(IntPtr fp, IntPtr a1, IntPtr a2, IntPtr a3)
139158
{
140159
return Impl.Int_Call_3(fp, a1, a2, a3);
141160
}
161+
#endif
142162
}
143163

144-
164+
#if !NETCORAPP
145165
/// <summary>
146166
/// Defines native call signatures to be generated by NativeCall.
147167
/// </summary>
@@ -155,4 +175,5 @@ public interface INativeCall
155175

156176
IntPtr Call_3(IntPtr funcPtr, IntPtr a1, IntPtr a2, IntPtr a3);
157177
}
178+
#endif
158179
}

src/runtime/runtime.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ internal static Type[] PythonArgsToTypeArray(IntPtr arg, bool mangleObjects)
522522
/// </summary>
523523
internal static unsafe void XIncref(IntPtr op)
524524
{
525-
#if PYTHON_WITH_PYDEBUG
525+
#if PYTHON_WITH_PYDEBUG || NETCOREAPP
526526
Py_IncRef(op);
527527
return;
528528
#else
@@ -543,7 +543,7 @@ internal static unsafe void XIncref(IntPtr op)
543543

544544
internal static unsafe void XDecref(IntPtr op)
545545
{
546-
#if PYTHON_WITH_PYDEBUG
546+
#if PYTHON_WITH_PYDEBUG || NETCOREAPP
547547
Py_DecRef(op);
548548
return;
549549
#else

0 commit comments

Comments
 (0)