Skip to content

Commit 0d7498b

Browse files
committed
Simplify the Unity custom patch
Lay the burden to provide the library name and directory on the user. A.K.A.: Improve portability.
1 parent 32fdc9c commit 0d7498b

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

src/runtime/platform/LibraryLoader.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using System;
22
using System.ComponentModel;
3+
using System.IO;
34
using System.Runtime.InteropServices;
45

56
namespace Python.Runtime.Platform
67
{
78
interface ILibraryLoader
89
{
9-
IntPtr Load(string dllToLoad);
10+
IntPtr Load(string dllToLoad, string directory = "");
1011

1112
IntPtr GetFunction(IntPtr hModule, string procedureName);
1213

@@ -47,9 +48,9 @@ class LinuxLoader : ILibraryLoader
4748
private static IntPtr RTLD_DEFAULT = IntPtr.Zero;
4849
private const string NativeDll = "libdl.so";
4950

50-
public IntPtr Load(string dllToLoad)
51+
public IntPtr Load(string dllToLoad, string directory = "")
5152
{
52-
var filename = $"lib{dllToLoad}.so";
53+
var filename = $"{directory}lib{dllToLoad}.so";
5354
ClearError();
5455
var res = dlopen(filename, RTLD_NOW | RTLD_GLOBAL);
5556
if (res == IntPtr.Zero)
@@ -118,9 +119,9 @@ class DarwinLoader : ILibraryLoader
118119
private const string NativeDll = "/usr/lib/libSystem.dylib";
119120
private static IntPtr RTLD_DEFAULT = new IntPtr(-2);
120121

121-
public IntPtr Load(string dllToLoad)
122+
public IntPtr Load(string dllToLoad, string directory = "")
122123
{
123-
var filename = $"lib{dllToLoad}.dylib";
124+
var filename = $"{directory}lib{dllToLoad}.dylib";
124125
ClearError();
125126
var res = dlopen(filename, RTLD_NOW | RTLD_GLOBAL);
126127
if (res == IntPtr.Zero)
@@ -187,8 +188,9 @@ class WindowsLoader : ILibraryLoader
187188
private const string NativeDll = "kernel32.dll";
188189

189190

190-
public IntPtr Load(string dllToLoad)
191+
public IntPtr Load(string dllToLoad, string directory = "")
191192
{
193+
dllToLoad = Path.GetFullPath($"{directory}{dllToLoad}.dll");
192194
var res = WindowsLoader.LoadLibrary(dllToLoad);
193195
if (res == IntPtr.Zero)
194196
throw new DllNotFoundException($"Could not load {dllToLoad}", new Win32Exception());

src/runtime/pythonengine.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,17 @@ public static int RunSimpleString(string code)
150150
return Runtime.PyRun_SimpleString(code);
151151
}
152152

153+
static bool libraryLoaded = false;
154+
public static void InitializeLibrary(string library, string directory)
155+
{
156+
if (!libraryLoaded)
157+
{
158+
var _loader = Python.Runtime.Platform.LibraryLoader.Get(Python.Runtime.Platform.NativeCodePageHelper.OperatingSystem);
159+
_loader.Load(library, directory);
160+
libraryLoaded = true;
161+
}
162+
}
163+
153164
public static void Initialize()
154165
{
155166
Initialize(setSysArgv: true);

src/runtime/runtime.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,7 @@ public class Runtime
5757

5858
public static readonly string PythonDLL = _PythonDll;
5959

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

6662
// set to true when python is finalizing
6763
internal static object IsFinalizingLock = new object();

0 commit comments

Comments
 (0)