Skip to content

Commit

Permalink
Merge pull request #220 from tonybaloney/ld_fixers
Browse files Browse the repository at this point in the history
Fix LD_PRELOAD workarounds
  • Loading branch information
tonybaloney authored Sep 23, 2024
2 parents bf17d78 + 8b17ca8 commit 2724ba2
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
1 change: 0 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,5 @@
"remoteUser": "root",
"remoteEnv": {
"Python3_ROOT_DIR": "/usr/local/python/current",
"LD_PRELOAD": "/usr/local/python/current/lib/libpython3.12.so.1.0"
}
}
1 change: 0 additions & 1 deletion .github/workflows/dotnet-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ jobs:
working-directory: src
env:
PYTHON_VERSION: ${{ steps.installpython.outputs.python-version }}
LD_PRELOAD: /opt/hostedtoolcache/Python/${{ steps.installpython.outputs.python-version }}/x64/lib/libpython3.so

- name: Upload logs
uses: actions/upload-artifact@v4
Expand Down
20 changes: 20 additions & 0 deletions src/CSnakes.Runtime/CPython/Init.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,30 @@ public CPythonAPI(string pythonLibraryPath, Version version)
}
}

[Flags]
private enum RTLD : int
{
LOCAL = 0,
LAZY = 1,
NOW = 2,
NOLOAD = 4,
DEEPBIND = 8,
GLOBAL = 0x00100
}

[LibraryImport("libdl.so.2", StringMarshalling = StringMarshalling.Utf8)]
private static partial nint dlopen(string path, int flags);

private static IntPtr DllImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
{
if (libraryName == PythonLibraryName)
{
// Override default dlopen flags on linux to allow global symbol resolution (required in extension modules)
// See https://github.com/tonybaloney/CSnakes/issues/112#issuecomment-2290643468
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)){
return dlopen(pythonLibraryPath!, (int)(RTLD.LAZY | RTLD.GLOBAL));
}

return NativeLibrary.Load(pythonLibraryPath!, assembly, null);
}
return NativeLibrary.Load(libraryName, assembly, searchPath);
Expand Down
2 changes: 2 additions & 0 deletions src/Integration.Tests/BufferTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using CSnakes.Runtime.Python;
using Xunit;

namespace Integration.Tests;

public class BufferTests : IntegrationTestBase
{
[Fact]
[Trait("requires", "numpy")]
public void TestBoolBuffer()
{
var testModule = Env.TestBuffer();
Expand Down
5 changes: 1 addition & 4 deletions src/Integration.Tests/TestDependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ public class TestDependency : IntegrationTestBase
[Fact]
public void VerifyInstalledPackage()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) // TODO: Fix virtual environments on Linux
{
Assert.True(Env.TestDependency().TestNothing());
}
Assert.True(Env.TestDependency().TestNothing());
}
}

0 comments on commit 2724ba2

Please sign in to comment.