Visitar URL original
Add function to set Py_NoSiteFlag global variable to 1 by alexhelms · Pull Request #971 · pythonnet/pythonnet · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Next Next commit
Add function to set Py_NoSiteFlag global variable to 1.
  • Loading branch information
Alex Helms
Alex Helms committed Oct 17, 2019
commit d7350e73524844632d48612074fb269a5bfe6827
10 changes: 10 additions & 0 deletions src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ public static string Compiler
get { return Marshal.PtrToStringAnsi(Runtime.Py_GetCompiler()); }
}

/// <summary>
/// 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
/// </summary>
public static void SetNoSiteFlag()
{
Runtime.SetNoSiteFlag();
}

public static int RunSimpleString(string code)
{
return Runtime.PyRun_SimpleString(code);
Expand Down
30 changes: 30 additions & 0 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Comment thread
filmor marked this conversation as resolved.
Outdated
}
finally
{
if (dllLocal != IntPtr.Zero)
{
loader.Free(dllLocal);
}
}
}
}
}