Skip to content
Prev Previous commit
Next Next commit
Leak libpython handle
  • Loading branch information
filmor committed Apr 12, 2020
commit d1babcf4cd16518368b08b6b5fa09c21a0435f4d
7 changes: 6 additions & 1 deletion pythonnet/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sys
import ctypes

_RUNTIME = None

Expand All @@ -20,12 +21,16 @@ def load():
dll_path = os.path.join(os.path.dirname(__file__), "dlls", "Python.Loader.dll")
runtime_dll_path = os.path.join(os.path.dirname(dll_path), "Python.Runtime.dll")

# Load and leak libpython handle s.t. the .NET runtime doesn't dlcloses it
libpython = _find_libpython()
_ = ctypes.CDLL(libpython)

assembly = _RUNTIME.get_assembly(dll_path)

print("Got assembly", assembly)
func = assembly["Python.Internal.Initialize"]

print("Got func:", func)

if func(f"{runtime_dll_path};{_find_libpython()}".encode("utf8")) != 0:
if func(f"{runtime_dll_path};{libpython}".encode("utf8")) != 0:
raise RuntimeError("Failed to initialize Python.Runtime.dll")