From d7350e73524844632d48612074fb269a5bfe6827 Mon Sep 17 00:00:00 2001 From: Alex Helms Date: Thu, 17 Oct 2019 14:47:24 -0700 Subject: [PATCH 1/3] Add function to set Py_NoSiteFlag global variable to 1. --- src/runtime/pythonengine.cs | 10 ++++++++++ src/runtime/runtime.cs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/runtime/pythonengine.cs b/src/runtime/pythonengine.cs index c1b663d22..700543839 100644 --- a/src/runtime/pythonengine.cs +++ b/src/runtime/pythonengine.cs @@ -130,6 +130,16 @@ public static string Compiler get { return Marshal.PtrToStringAnsi(Runtime.Py_GetCompiler()); } } + /// + /// Set the NoSiteFlag to disable loading the site module. + /// Must be called before Initialize. + /// https://docs.python.org/3/c-api/init.html#c.Py_NoSiteFlag + /// + public static void SetNoSiteFlag() + { + Runtime.SetNoSiteFlag(); + } + public static int RunSimpleString(string code) { return Runtime.PyRun_SimpleString(code); diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index 4985a57f5..af50594cc 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -414,6 +414,8 @@ internal static int AtExit() internal static IntPtr PyNoneType; internal static IntPtr PyTypeType; + internal static IntPtr Py_NoSiteFlag; + #if PYTHON3 internal static IntPtr PyBytesType; #endif @@ -1884,5 +1886,33 @@ internal static IntPtr PyMem_Realloc(IntPtr ptr, long size) [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int Py_MakePendingCalls(); + + internal static void SetNoSiteFlag() + { + var loader = LibraryLoader.Get(OperatingSystem); + + IntPtr dllLocal; + if (_PythonDll != "__Internal") + { + dllLocal = loader.Load(_PythonDll); + } + + try + { + Py_NoSiteFlag = loader.GetFunction(dllLocal, "Py_NoSiteFlag"); + + if (Is32Bit) + Marshal.WriteInt32(Py_NoSiteFlag, 1); + else + Marshal.WriteInt64(Py_NoSiteFlag, 1); + } + finally + { + if (dllLocal != IntPtr.Zero) + { + loader.Free(dllLocal); + } + } + } } } From f074b8b72847e340b7dd5ce37de0f9c547678838 Mon Sep 17 00:00:00 2001 From: Alex Helms Date: Thu, 17 Oct 2019 14:55:12 -0700 Subject: [PATCH 2/3] Add myself to authors, update changelog. --- AUTHORS.md | 1 + CHANGELOG.md | 1 + 2 files changed, 2 insertions(+) diff --git a/AUTHORS.md b/AUTHORS.md index 9e13ca569..66c9167ba 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -12,6 +12,7 @@ ## Contributors +- Alex Helms ([@alexhelms](https://github.com/alexhelms)) - Alexandre Catarino([@AlexCatarino](https://github.com/AlexCatarino)) - Arvid JB ([@ArvidJB](https://github.com/ArvidJB)) - BenoƮt Hudson ([@benoithudson](https://github.com/benoithudson)) diff --git a/CHANGELOG.md b/CHANGELOG.md index b5b11cd77..cd851f0a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][]. ### Added - Added automatic NuGet package generation in appveyor and local builds +- Added function that sets Py_NoSiteFlag to 1. ### Changed From cc8b1c1c18e0fdd0a2e24c062a41329fe105bdd1 Mon Sep 17 00:00:00 2001 From: Alex Helms Date: Tue, 22 Oct 2019 09:36:30 -0700 Subject: [PATCH 3/3] Remove 64-bit branch. --- src/runtime/runtime.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index af50594cc..a1f9a38aa 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -1900,11 +1900,7 @@ internal static void SetNoSiteFlag() try { Py_NoSiteFlag = loader.GetFunction(dllLocal, "Py_NoSiteFlag"); - - if (Is32Bit) - Marshal.WriteInt32(Py_NoSiteFlag, 1); - else - Marshal.WriteInt64(Py_NoSiteFlag, 1); + Marshal.WriteInt32(Py_NoSiteFlag, 1); } finally {