Skip to content

Commit

Permalink
Capture a free threaded build flag.
Browse files Browse the repository at this point in the history
  • Loading branch information
tonybaloney committed Aug 11, 2024
1 parent 2ede4f9 commit a2eb2e5
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 11 deletions.
8 changes: 8 additions & 0 deletions src/CSnakes.Runtime.Tests/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"CSnakes.Runtime.Tests": {
"commandName": "Project",
"nativeDebugging": true
}
}
}
6 changes: 4 additions & 2 deletions src/CSnakes.Runtime/CPython/Init.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ internal unsafe partial class CPythonAPI : IDisposable

private static string? pythonLibraryPath = null;
private static readonly object initLock = new();
private bool disposedValue;
private readonly bool freeThreaded = false;
private bool disposedValue = false;

public CPythonAPI(string pythonLibraryPath)
public CPythonAPI(string pythonLibraryPath, bool freeThreaded = false)
{
CPythonAPI.pythonLibraryPath = pythonLibraryPath;
this.freeThreaded = freeThreaded;
try
{
NativeLibrary.SetDllImportResolver(typeof(CPythonAPI).Assembly, DllImportResolver);
Expand Down
4 changes: 2 additions & 2 deletions src/CSnakes.Runtime/IServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ public static IPythonEnvironmentBuilder FromMacOSInstallerLocator(this IPythonEn
return builder;
}

public static IPythonEnvironmentBuilder FromSource(this IPythonEnvironmentBuilder builder, string folder, string version, bool debug = true)
public static IPythonEnvironmentBuilder FromSource(this IPythonEnvironmentBuilder builder, string folder, string version, bool debug = true, bool freeThreaded = false)
{
builder.Services.AddSingleton<PythonLocator>(new SourceLocator(folder, version, debug));
builder.Services.AddSingleton<PythonLocator>(new SourceLocator(folder, version, debug, freeThreaded));
return builder;
}

Expand Down
2 changes: 1 addition & 1 deletion src/CSnakes.Runtime/Locators/PythonLocationMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
/// </summary>
/// <param name="Folder">Path on disk where Python is to be loaded from.</param>
/// <param name="Version">Version of Python being used from the location.</param>
public sealed record PythonLocationMetadata(string Folder, string Version, bool Debug = false);
public sealed record PythonLocationMetadata(string Folder, string Version, bool Debug = false, bool FreeThreaded = false);
5 changes: 3 additions & 2 deletions src/CSnakes.Runtime/Locators/SourceLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace CSnakes.Runtime.Locators;

internal class SourceLocator(string folder, string version, bool debug = true) : PythonLocator(version: version)
internal class SourceLocator(string folder, string version, bool debug = true, bool freeThreaded = false) : PythonLocator(version: version)

Check warning on line 5 in src/CSnakes.Runtime/Locators/SourceLocator.cs

View workflow job for this annotation

GitHub Actions / publish-github-packages

Parameter 'string version' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 5 in src/CSnakes.Runtime/Locators/SourceLocator.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 3.12)

Parameter 'string version' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 5 in src/CSnakes.Runtime/Locators/SourceLocator.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 3.12)

Parameter 'string version' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 5 in src/CSnakes.Runtime/Locators/SourceLocator.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 3.12)

Parameter 'string version' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 5 in src/CSnakes.Runtime/Locators/SourceLocator.cs

View workflow job for this annotation

GitHub Actions / build (macos-13, 3.12)

Parameter 'string version' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.
{
public override PythonLocationMetadata LocatePython()
{
var buildFolder = Path.Combine(folder, "PCbuild", "amd64");
return new PythonLocationMetadata(
buildFolder,
version,
debug
debug,
freeThreaded
);
}

Expand Down
8 changes: 8 additions & 0 deletions src/CSnakes.Runtime/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"CSnakes.Runtime": {
"commandName": "Project",
"nativeDebugging": true
}
}
}
14 changes: 10 additions & 4 deletions src/CSnakes.Runtime/PythonEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,17 @@ private CPythonAPI SetupStandardLibrary(PythonLocationMetadata pythonLocationMet
string pythonDll = string.Empty;
string pythonPath = string.Empty;
string pythonLocation = pythonLocationMetadata.Folder;
string suffix = String.Empty;

if (pythonLocationMetadata.FreeThreaded)
{
suffix += "t";
}

// Add standard library to PYTHONPATH
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
string suffix = pythonLocationMetadata.Debug ? "_d" : string.Empty;
suffix += pythonLocationMetadata.Debug ? "_d" : string.Empty;
pythonDll = Path.Combine(pythonLocation, $"python{versionPath}{suffix}.dll");
if (pythonLocationMetadata.Debug)
{
Expand All @@ -156,12 +162,12 @@ private CPythonAPI SetupStandardLibrary(PythonLocationMetadata pythonLocationMet
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
pythonDll = Path.Combine(pythonLocation, "lib", $"libpython{majorVersion}.dylib");
pythonDll = Path.Combine(pythonLocation, "lib", $"libpython{majorVersion}{suffix}.dylib");
pythonPath = Path.Combine(pythonLocation, "lib", $"python{majorVersion}") + sep + Path.Combine(pythonLocation, "lib", $"python{majorVersion}", "lib-dynload");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
pythonDll = Path.Combine(pythonLocation, "lib", $"libpython{majorVersion}.so");
pythonDll = Path.Combine(pythonLocation, "lib", $"libpython{majorVersion}{suffix}.so");
pythonPath = Path.Combine(pythonLocation, "lib", $"python{majorVersion}") + sep + Path.Combine(pythonLocation, "lib", $"python{majorVersion}", "lib-dynload");
}
else
Expand All @@ -172,7 +178,7 @@ private CPythonAPI SetupStandardLibrary(PythonLocationMetadata pythonLocationMet
Logger.LogInformation("Python DLL: {PythonDLL}", pythonDll);
Logger.LogInformation("Python path: {PythonPath}", pythonPath);

var api = new CPythonAPI(pythonDll)
var api = new CPythonAPI(pythonDll, pythonLocationMetadata.FreeThreaded)
{
PythonPath = pythonPath
};
Expand Down

0 comments on commit a2eb2e5

Please sign in to comment.