Skip to content

Use Delegates to access Py_NoSiteFlag #1659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 6, 2022
Merged
Changes from all commits
Commits
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
25 changes: 6 additions & 19 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,6 @@ private static void NullGCHandles(IEnumerable<IntPtr> 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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -2426,6 +2412,7 @@ static Delegates()
internal static delegate* unmanaged[Cdecl]<BorrowedReference, void> _Py_NewReference { get; }
internal static delegate* unmanaged[Cdecl]<int> _Py_IsFinalizing { get; }
internal static IntPtr PyType_Type { get; }
internal static int* Py_NoSiteFlag { get; }
}
}

Expand Down