Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
909ed1f
dropped net40 target from modern projects
lostmsu Nov 30, 2020
47e926e
use .NET Standard 2.0 platform detection features
lostmsu Dec 2, 2020
21683b3
drop NativeCodePage alltogether
lostmsu Dec 2, 2020
972c41d
WIP: use C# 9 function pointers for PInvoke
lostmsu Dec 4, 2020
51e5184
allow setting PythonDLL
lostmsu Dec 10, 2020
2498d47
always explicitly specify the way strings are marshaled
lostmsu Jan 22, 2021
70fc803
CI: figure out DLL name from environment
lostmsu Jan 22, 2021
28a5dab
use Roslyn preview in CI
lostmsu Jan 22, 2021
c75229a
fixed Linux and Mac DLL loaders breaking dll path
lostmsu Jan 22, 2021
a0a1dc1
correctly detect DLL on *nix when running from Python
lostmsu Jan 22, 2021
1b88783
Windows library loader: add support for hModule == 0
lostmsu Jan 22, 2021
2c1aaef
fix dll loading in tests
lostmsu Jan 22, 2021
39e41d0
mentiond PythonDLL in changelog
lostmsu Jan 22, 2021
17040fe
set PYDLL in AppVeyor
lostmsu Jan 22, 2021
b7410b6
revert automatically added 'm' suffix for *nix default dll name
lostmsu Jan 22, 2021
275cae9
specify full DLL name instead of PYVER in GH Actions
lostmsu Jan 22, 2021
b4cb37e
use Microsoft.Net.Compilers.Toolset instead of Microsoft.Net.Compilers
lostmsu Jan 23, 2021
f68e581
in CI MacOS python DLL has 'm' suffix
lostmsu Jan 23, 2021
cda604a
only set PYTHONNET_PYDLL for test runs from .NET
lostmsu Jan 23, 2021
3982892
workaround for pytest/clr module not preloading python C API library
lostmsu Jan 23, 2021
a6cbe20
addressed a few code comments
lostmsu Jan 26, 2021
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
Prev Previous commit
Next Next commit
allow setting PythonDLL
  • Loading branch information
lostmsu committed Jan 28, 2021
commit 51e5184f8899077a960a283a695aecdc8854a12c
61 changes: 15 additions & 46 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,18 @@ public unsafe class Runtime
public static int UCS => _UCS;
internal static readonly int _UCS = PyUnicode_GetMax() <= 0xFFFF ? 2 : 4;

#if PYTHON36
const string _minor = "6";
#elif PYTHON37
const string _minor = "7";
#elif PYTHON38
const string _minor = "8";
#elif PYTHON39
const string _minor = "9";
#else
#error You must define one of PYTHON36 to PYTHON39
#endif

#if WINDOWS
internal const string dllBase = "python3" + _minor;
#else
internal const string dllBase = "python3." + _minor;
#endif

#if PYTHON_WITH_PYDEBUG
internal const string dllWithPyDebug = "d";
#else
internal const string dllWithPyDebug = "";
#endif
#if PYTHON_WITH_PYMALLOC
internal const string dllWithPyMalloc = "m";
#else
internal const string dllWithPyMalloc = "";
#endif

// C# compiler copies constants to the assemblies that references this library.
// We needs to replace all public constants to static readonly fields to allow
// binary substitution of different Python.Runtime.dll builds in a target application.

public static readonly string PythonDLL = _PythonDll;
public static string PythonDLL
{
get => _PythonDll;
set
{
if (_isInitialized)
throw new InvalidOperationException("This property must be set before runtime is initialized");
_PythonDll = value;
}
}

#if PYTHON_WITHOUT_ENABLE_SHARED && !NETSTANDARD
internal const string _PythonDll = "__Internal";
#else
internal const string _PythonDll = dllBase + dllWithPyDebug + dllWithPyMalloc;
#endif
static string _PythonDll;

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

private static class Delegates
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make Runtime a partial class and move this into a separate file.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do this just before pushing. Otherwise it would require me to update the roslyn package, that does automatic conversion of [DllImport] into the pattern with Delegates.

{
static readonly ILibraryLoader libraryLoader = LibraryLoader.Get();

static Delegates()
{
PyDictProxy_New = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr>)GetFunctionByName(nameof(PyDictProxy_New), GetUnmanagedDll(_PythonDll));
Expand Down Expand Up @@ -2473,15 +2447,10 @@ static Delegates()
PyThreadState_SetAsyncExcLP64 = (delegate* unmanaged[Cdecl]<ulong, IntPtr, int>)GetFunctionByName("PyThreadState_SetAsyncExc", GetUnmanagedDll(_PythonDll));
}

static global::System.IntPtr GetUnmanagedDll(string libraryName)
{
throw new NotImplementedException();
}
static global::System.IntPtr GetUnmanagedDll(string libraryName) => libraryLoader.Load(libraryName);

static global::System.IntPtr GetFunctionByName(string functionName, global::System.IntPtr libraryHandle)
{
throw new NotImplementedException();
}
=> libraryLoader.GetFunction(libraryHandle, functionName);

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