Skip to content

Commit f2e6f6f

Browse files
alexhelmsfilmor
authored andcommitted
Add function to set Py_NoSiteFlag global variable to 1 (#971)
* Add function to set Py_NoSiteFlag global variable to 1. * Add myself to authors, update changelog.
1 parent 4a9457f commit f2e6f6f

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

AUTHORS.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
## Contributors
1414

15+
- Alex Helms ([@alexhelms](https://github.com/alexhelms))
1516
- Alexandre Catarino([@AlexCatarino](https://github.com/AlexCatarino))
1617
- Arvid JB ([@ArvidJB](https://github.com/ArvidJB))
1718
- Benoît Hudson ([@benoithudson](https://github.com/benoithudson))

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
1010
### Added
1111

1212
- Added automatic NuGet package generation in appveyor and local builds
13+
- Added function that sets Py_NoSiteFlag to 1.
1314

1415
### Changed
1516

src/runtime/pythonengine.cs

+10
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ public static string Compiler
130130
get { return Marshal.PtrToStringAnsi(Runtime.Py_GetCompiler()); }
131131
}
132132

133+
/// <summary>
134+
/// Set the NoSiteFlag to disable loading the site module.
135+
/// Must be called before Initialize.
136+
/// https://docs.python.org/3/c-api/init.html#c.Py_NoSiteFlag
137+
/// </summary>
138+
public static void SetNoSiteFlag()
139+
{
140+
Runtime.SetNoSiteFlag();
141+
}
142+
133143
public static int RunSimpleString(string code)
134144
{
135145
return Runtime.PyRun_SimpleString(code);

src/runtime/runtime.cs

+26
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ internal static int AtExit()
414414
internal static IntPtr PyNoneType;
415415
internal static IntPtr PyTypeType;
416416

417+
internal static IntPtr Py_NoSiteFlag;
418+
417419
#if PYTHON3
418420
internal static IntPtr PyBytesType;
419421
#endif
@@ -1884,5 +1886,29 @@ internal static IntPtr PyMem_Realloc(IntPtr ptr, long size)
18841886

18851887
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
18861888
internal static extern int Py_MakePendingCalls();
1889+
1890+
internal static void SetNoSiteFlag()
1891+
{
1892+
var loader = LibraryLoader.Get(OperatingSystem);
1893+
1894+
IntPtr dllLocal;
1895+
if (_PythonDll != "__Internal")
1896+
{
1897+
dllLocal = loader.Load(_PythonDll);
1898+
}
1899+
1900+
try
1901+
{
1902+
Py_NoSiteFlag = loader.GetFunction(dllLocal, "Py_NoSiteFlag");
1903+
Marshal.WriteInt32(Py_NoSiteFlag, 1);
1904+
}
1905+
finally
1906+
{
1907+
if (dllLocal != IntPtr.Zero)
1908+
{
1909+
loader.Free(dllLocal);
1910+
}
1911+
}
1912+
}
18871913
}
18881914
}

0 commit comments

Comments
 (0)