Skip to content

Commit

Permalink
Python now inits from config and is isolated
Browse files Browse the repository at this point in the history
  • Loading branch information
eirannejad committed Oct 10, 2024
1 parent 556882d commit f5032c3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/runtime/Native/PyConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Runtime.InteropServices;

namespace Python.Runtime.Native;

#pragma warning disable CS0169

[StructLayout(LayoutKind.Sequential)]
struct PyConfig
{
int _config_init;
public int isolated;
int use_environment;
int dev_mode;
public int install_signal_handlers;

// Create an int array of size 256 as padding
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1024)]
int[] padding;
}

public enum PyStatusType : int
{
PyStatus_Ok,
PyStatus_Error,
PyStatus_Exception,
PyStatus_Exit
}

struct PyStatus
{
public PyStatusType type;
IntPtr func;
IntPtr err_msg;
int exitcode;
}
8 changes: 8 additions & 0 deletions src/runtime/Runtime.Delegates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ static Delegates()

PyType_Type = GetFunctionByName(nameof(PyType_Type), GetUnmanagedDll(_PythonDll));
Py_NoSiteFlag = (int*)GetFunctionByName(nameof(Py_NoSiteFlag), GetUnmanagedDll(_PythonDll));

PyConfig_InitPythonConfig = (delegate* unmanaged[Cdecl]<out PyConfig, void>)GetFunctionByName(nameof(PyConfig_InitPythonConfig), GetUnmanagedDll(_PythonDll));
Py_InitializeFromConfig = (delegate* unmanaged[Cdecl]<ref PyConfig, PyStatus>)GetFunctionByName(nameof(Py_InitializeFromConfig), GetUnmanagedDll(_PythonDll));
PyConfig_Clear = (delegate* unmanaged[Cdecl]<ref PyConfig, void>)GetFunctionByName(nameof(PyConfig_Clear), GetUnmanagedDll(_PythonDll));
}

static global::System.IntPtr GetUnmanagedDll(string? libraryName)
Expand Down Expand Up @@ -540,5 +544,9 @@ static Delegates()
internal static delegate* unmanaged[Cdecl]<int> _Py_IsFinalizing { get; }
internal static IntPtr PyType_Type { get; }
internal static int* Py_NoSiteFlag { get; }

internal static delegate* unmanaged[Cdecl]<out PyConfig, void> PyConfig_InitPythonConfig { get; }
internal static delegate* unmanaged[Cdecl]<ref PyConfig, PyStatus> Py_InitializeFromConfig { get; }
internal static delegate* unmanaged[Cdecl]<ref PyConfig, void> PyConfig_Clear { get; }
}
}
22 changes: 21 additions & 1 deletion src/runtime/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ private static string GetDefaultDllName(Version version)
return prefix + "python" + suffix + ext;
}

private static PyConfig _configs = new PyConfig();

private static bool _isInitialized = false;
internal static bool IsInitialized => _isInitialized;
private static bool _typesInitialized = false;
Expand Down Expand Up @@ -117,7 +119,7 @@ internal static void Initialize(bool initSigs = false)
);
if (!interpreterAlreadyInitialized)
{
Py_InitializeEx(initSigs ? 1 : 0);
Py_InitializeFromConfig(initSigs ? 1 : 0);

NewRun();

Expand All @@ -144,6 +146,7 @@ internal static void Initialize(bool initSigs = false)
NewRun();
}
}

MainManagedThreadId = Thread.CurrentThread.ManagedThreadId;

Finalizer.Initialize();
Expand Down Expand Up @@ -190,6 +193,21 @@ internal static void Initialize(bool initSigs = false)
hexCallable = new(() => new PyString("%x").GetAttr("__mod__"));
}

static void Py_InitializeFromConfig(int install_signal_handlers)
{
// Initialize PyConfig
Delegates.PyConfig_InitPythonConfig(out _configs);
_configs.isolated = 1;
_configs.install_signal_handlers = install_signal_handlers;

PyStatus status = Delegates.Py_InitializeFromConfig(ref _configs);
if (status.type != PyStatusType.PyStatus_Ok)
{
Delegates.PyConfig_Clear(ref _configs);
// Handle initialization error
}
}

static void NewRun()
{
run++;
Expand Down Expand Up @@ -324,6 +342,8 @@ internal static void Shutdown()
{
PyGILState_Release(state);
}

Delegates.PyConfig_Clear(ref _configs);
}

const int MaxCollectRetriesOnShutdown = 20;
Expand Down

0 comments on commit f5032c3

Please sign in to comment.