Skip to content

Commit 51e5184

Browse files
committed
allow setting PythonDLL
1 parent 972c41d commit 51e5184

File tree

1 file changed

+15
-46
lines changed

1 file changed

+15
-46
lines changed

src/runtime/runtime.cs

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,46 +22,18 @@ public unsafe class Runtime
2222
public static int UCS => _UCS;
2323
internal static readonly int _UCS = PyUnicode_GetMax() <= 0xFFFF ? 2 : 4;
2424

25-
#if PYTHON36
26-
const string _minor = "6";
27-
#elif PYTHON37
28-
const string _minor = "7";
29-
#elif PYTHON38
30-
const string _minor = "8";
31-
#elif PYTHON39
32-
const string _minor = "9";
33-
#else
34-
#error You must define one of PYTHON36 to PYTHON39
35-
#endif
36-
37-
#if WINDOWS
38-
internal const string dllBase = "python3" + _minor;
39-
#else
40-
internal const string dllBase = "python3." + _minor;
41-
#endif
42-
43-
#if PYTHON_WITH_PYDEBUG
44-
internal const string dllWithPyDebug = "d";
45-
#else
46-
internal const string dllWithPyDebug = "";
47-
#endif
48-
#if PYTHON_WITH_PYMALLOC
49-
internal const string dllWithPyMalloc = "m";
50-
#else
51-
internal const string dllWithPyMalloc = "";
52-
#endif
53-
54-
// C# compiler copies constants to the assemblies that references this library.
55-
// We needs to replace all public constants to static readonly fields to allow
56-
// binary substitution of different Python.Runtime.dll builds in a target application.
57-
58-
public static readonly string PythonDLL = _PythonDll;
25+
public static string PythonDLL
26+
{
27+
get => _PythonDll;
28+
set
29+
{
30+
if (_isInitialized)
31+
throw new InvalidOperationException("This property must be set before runtime is initialized");
32+
_PythonDll = value;
33+
}
34+
}
5935

60-
#if PYTHON_WITHOUT_ENABLE_SHARED && !NETSTANDARD
61-
internal const string _PythonDll = "__Internal";
62-
#else
63-
internal const string _PythonDll = dllBase + dllWithPyDebug + dllWithPyMalloc;
64-
#endif
36+
static string _PythonDll;
6537

6638
// set to true when python is finalizing
6739
internal static object IsFinalizingLock = new object();
@@ -2215,6 +2187,8 @@ internal static IntPtr GetBuiltins()
22152187

22162188
private static class Delegates
22172189
{
2190+
static readonly ILibraryLoader libraryLoader = LibraryLoader.Get();
2191+
22182192
static Delegates()
22192193
{
22202194
PyDictProxy_New = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr>)GetFunctionByName(nameof(PyDictProxy_New), GetUnmanagedDll(_PythonDll));
@@ -2473,15 +2447,10 @@ static Delegates()
24732447
PyThreadState_SetAsyncExcLP64 = (delegate* unmanaged[Cdecl]<ulong, IntPtr, int>)GetFunctionByName("PyThreadState_SetAsyncExc", GetUnmanagedDll(_PythonDll));
24742448
}
24752449

2476-
static global::System.IntPtr GetUnmanagedDll(string libraryName)
2477-
{
2478-
throw new NotImplementedException();
2479-
}
2450+
static global::System.IntPtr GetUnmanagedDll(string libraryName) => libraryLoader.Load(libraryName);
24802451

24812452
static global::System.IntPtr GetFunctionByName(string functionName, global::System.IntPtr libraryHandle)
2482-
{
2483-
throw new NotImplementedException();
2484-
}
2453+
=> libraryLoader.GetFunction(libraryHandle, functionName);
24852454

24862455
internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr> PyDictProxy_New { get; }
24872456
internal static delegate* unmanaged[Cdecl]<IntPtr, void> Py_IncRef { get; }

0 commit comments

Comments
 (0)