Skip to content
Merged
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
Prev Previous commit
Ensure that shutdown is called from Python
  • Loading branch information
lostmsu committed Apr 8, 2022
commit 58bd58c01c3ebe917c274d56737495a98eecb265
9 changes: 5 additions & 4 deletions pythonnet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def set_runtime(runtime):


def set_default_runtime() -> None:
if sys.platform == 'win32':
if sys.platform == "win32":
set_runtime(clr_loader.get_netfx())
else:
set_runtime(clr_loader.get_mono())
Expand All @@ -36,22 +36,23 @@ def load():
set_default_runtime()

dll_path = join(dirname(__file__), "runtime", "Python.Runtime.dll")

_LOADER_ASSEMBLY = _RUNTIME.get_assembly(dll_path)

func = _LOADER_ASSEMBLY["Python.Runtime.Loader.Initialize"]
if func(''.encode("utf8")) != 0:
if func(b"") != 0:
raise RuntimeError("Failed to initialize Python.Runtime.dll")

import atexit

atexit.register(unload)


def unload():
global _RUNTIME
if _LOADER_ASSEMBLY is not None:
func = _LOADER_ASSEMBLY["Python.Runtime.Loader.Shutdown"]
if func(b"") != 0:
if func(b"full_shutdown") != 0:
raise RuntimeError("Failed to call Python.NET shutdown")

if _RUNTIME is not None:
Expand Down