Skip to content

Commit 8639f09

Browse files
committed
Merge remote-tracking branch 'upstream/master' into domain-reload-test-cases-fixes
2 parents 94f29a7 + 9307bb3 commit 8639f09

File tree

6 files changed

+29
-16
lines changed

6 files changed

+29
-16
lines changed

src/runtime/interop.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,9 @@ internal static Type GetPrototype(string name)
491491
return pmap[name] as Type;
492492
}
493493

494+
495+
internal static Dictionary<IntPtr, Delegate> allocatedThunks = new Dictionary<IntPtr, Delegate>();
496+
494497
internal static ThunkInfo GetThunk(MethodInfo method, string funcType = null)
495498
{
496499
Type dt;
@@ -505,6 +508,7 @@ internal static ThunkInfo GetThunk(MethodInfo method, string funcType = null)
505508
}
506509
Delegate d = Delegate.CreateDelegate(dt, method);
507510
var info = new ThunkInfo(d);
511+
allocatedThunks[info.Address] = d;
508512
return info;
509513
}
510514

src/runtime/managedtype.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ internal static int PyVisit(IntPtr ob, IntPtr visit, IntPtr arg)
178178
{
179179
return 0;
180180
}
181-
var visitFunc = (Interop.ObjObjFunc)Marshal.GetDelegateForFunctionPointer(visit, typeof(Interop.ObjObjFunc));
181+
var visitFunc = NativeCall.GetDelegate<Interop.ObjObjFunc>(visit);
182182
return visitFunc(ob, arg);
183183
}
184184

@@ -196,7 +196,7 @@ internal void CallTypeClear()
196196
{
197197
return;
198198
}
199-
var clearFunc = (Interop.InquiryFunc)Marshal.GetDelegateForFunctionPointer(clearPtr, typeof(Interop.InquiryFunc));
199+
var clearFunc = NativeCall.GetDelegate<Interop.InquiryFunc>(clearPtr);
200200
clearFunc(pyHandle);
201201
}
202202

@@ -214,7 +214,8 @@ internal void CallTypeTraverse(Interop.ObjObjFunc visitproc, IntPtr arg)
214214
{
215215
return;
216216
}
217-
var traverseFunc = (Interop.ObjObjArgFunc)Marshal.GetDelegateForFunctionPointer(traversePtr, typeof(Interop.ObjObjArgFunc));
217+
var traverseFunc = NativeCall.GetDelegate<Interop.ObjObjArgFunc>(traversePtr);
218+
218219
var visiPtr = Marshal.GetFunctionPointerForDelegate(visitproc);
219220
traverseFunc(pyHandle, visiPtr, arg);
220221
}

src/runtime/nativecall.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,15 @@ public static int Int_Call_3(IntPtr fp, IntPtr a1, IntPtr a2, IntPtr a3)
4040
return d(a1, a2, a3);
4141
}
4242

43-
private static T GetDelegate<T>(IntPtr fp) where T: Delegate
43+
internal static T GetDelegate<T>(IntPtr fp) where T: Delegate
4444
{
45-
// Use Marshal.GetDelegateForFunctionPointer<> directly after upgrade the framework
46-
return (T)Marshal.GetDelegateForFunctionPointer(fp, typeof(T));
45+
Delegate d = null;
46+
if (!Interop.allocatedThunks.TryGetValue(fp, out d))
47+
{
48+
// We don't cache this delegate because this is a pure delegate ot unmanaged.
49+
d = Marshal.GetDelegateForFunctionPointer<T>(fp);
50+
}
51+
return (T)d;
4752
}
4853
}
4954
}

src/runtime/runtime.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,16 +1025,13 @@ internal static bool PyObject_IsIterable(IntPtr pointer)
10251025
}
10261026

10271027
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
1028-
internal static extern int PyObject_HasAttrString(IntPtr pointer, string name);
1028+
internal static extern int PyObject_HasAttrString(IntPtr pointer, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string name);
10291029

10301030
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
1031-
internal static extern IntPtr PyObject_GetAttrString(IntPtr pointer, string name);
1031+
internal static extern IntPtr PyObject_GetAttrString(IntPtr pointer, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string name);
10321032

10331033
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
1034-
internal static extern IntPtr PyObject_GetAttrString(IntPtr pointer, IntPtr name);
1035-
1036-
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
1037-
internal static extern int PyObject_SetAttrString(IntPtr pointer, string name, IntPtr value);
1034+
internal static extern int PyObject_SetAttrString(IntPtr pointer, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string name, IntPtr value);
10381035

10391036
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
10401037
internal static extern int PyObject_HasAttr(IntPtr pointer, IntPtr name);
@@ -1649,7 +1646,7 @@ internal static bool PyDict_Check(IntPtr ob)
16491646
/// Return value: Borrowed reference.
16501647
/// </summary>
16511648
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
1652-
internal static extern IntPtr PyDict_GetItemString(IntPtr pointer, string key);
1649+
internal static extern IntPtr PyDict_GetItemString(IntPtr pointer, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string key);
16531650

16541651
/// <summary>
16551652
/// Return 0 on success or -1 on failure.
@@ -1661,13 +1658,13 @@ internal static bool PyDict_Check(IntPtr ob)
16611658
/// Return 0 on success or -1 on failure.
16621659
/// </summary>
16631660
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
1664-
internal static extern int PyDict_SetItemString(IntPtr pointer, string key, IntPtr value);
1661+
internal static extern int PyDict_SetItemString(IntPtr pointer, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string key, IntPtr value);
16651662

16661663
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
16671664
internal static extern int PyDict_DelItem(IntPtr pointer, IntPtr key);
16681665

16691666
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
1670-
internal static extern int PyDict_DelItemString(IntPtr pointer, string key);
1667+
internal static extern int PyDict_DelItemString(IntPtr pointer, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string key);
16711668

16721669
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
16731670
internal static extern int PyMapping_HasKey(IntPtr pointer, IntPtr key);
@@ -2019,7 +2016,7 @@ internal static IntPtr PyMem_Realloc(IntPtr ptr, long size)
20192016
//====================================================================
20202017

20212018
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
2022-
internal static extern void PyErr_SetString(IntPtr ob, string message);
2019+
internal static extern void PyErr_SetString(IntPtr ob, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string message);
20232020

20242021
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
20252022
internal static extern void PyErr_SetObject(BorrowedReference type, BorrowedReference exceptionObject);

src/testing/methodtest.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,10 @@ public static string ParamsArrayOverloaded(int i, params int[] paramsArray)
713713
{
714714
return "with params-array";
715715
}
716+
717+
public static void EncodingTestÅngström()
718+
{
719+
}
716720
}
717721

718722

src/tests/test_method.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,3 +1221,5 @@ def test_params_array_overload():
12211221
# res = MethodTest.ParamsArrayOverloaded(paramsArray=[], i=1)
12221222
# assert res == "with params-array"
12231223

1224+
def test_method_encoding():
1225+
MethodTest.EncodingTestÅngström()

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