From 1129a73838903f27bc2d4b0dc5e2c1bef033637f Mon Sep 17 00:00:00 2001 From: Victor Nova Date: Wed, 5 Jan 2022 15:40:41 -0800 Subject: [PATCH] use the same facility to access Py_NoSiteFlag as the one used to load Python C API functions fixes https://github.com/pythonnet/pythonnet/issues/1517 --- src/runtime/runtime.cs | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index a2ee45e9c..fc851c8ea 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -495,8 +495,6 @@ private static void NullGCHandles(IEnumerable objects) internal static PyType PyNoneType; internal static BorrowedReference PyTypeType => new(Delegates.PyType_Type); - internal static int* Py_NoSiteFlag; - internal static PyObject PyBytesType; internal static NativeFunc* _PyObject_NextNotImplemented; @@ -1881,24 +1879,11 @@ internal static IntPtr PyCapsule_GetPointer(BorrowedReference capsule, IntPtr na internal static void SetNoSiteFlag() { - var loader = LibraryLoader.Instance; - IntPtr dllLocal = IntPtr.Zero; - if (_PythonDll != "__Internal") - { - dllLocal = loader.Load(_PythonDll); - } - try - { - Py_NoSiteFlag = (int*)loader.GetFunction(dllLocal, "Py_NoSiteFlag"); - *Py_NoSiteFlag = 1; - } - finally + TryUsingDll(() => { - if (dllLocal != IntPtr.Zero) - { - loader.Free(dllLocal); - } - } + *Delegates.Py_NoSiteFlag = 1; + return *Delegates.Py_NoSiteFlag; + }); } internal static class Delegates @@ -2170,6 +2155,7 @@ static Delegates() catch (MissingMethodException) { } PyType_Type = GetFunctionByName(nameof(PyType_Type), GetUnmanagedDll(_PythonDll)); + Py_NoSiteFlag = (int*)GetFunctionByName(nameof(Py_NoSiteFlag), GetUnmanagedDll(_PythonDll)); } static global::System.IntPtr GetUnmanagedDll(string? libraryName) @@ -2426,6 +2412,7 @@ static Delegates() internal static delegate* unmanaged[Cdecl] _Py_NewReference { get; } internal static delegate* unmanaged[Cdecl] _Py_IsFinalizing { get; } internal static IntPtr PyType_Type { get; } + internal static int* Py_NoSiteFlag { get; } } }