forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path__init__.py
More file actions
71 lines (51 loc) · 1.81 KB
/
__init__.py
File metadata and controls
71 lines (51 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
import sys
import clr_loader
_RUNTIME = None
_LOADER_ASSEMBLY = None
_FFI = None
_LOADED = False
def set_runtime(runtime):
global _RUNTIME
if _LOADED:
raise RuntimeError("The runtime {runtime} has already been loaded".format(_RUNTIME))
_RUNTIME = runtime
def set_default_runtime() -> None:
if sys.platform == 'win32':
set_runtime(clr_loader.get_netfx())
else:
set_runtime(clr_loader.get_mono())
def load():
global _FFI, _LOADED, _LOADER_ASSEMBLY
if _LOADED:
return
from .find_libpython import linked_libpython
from os.path import join, dirname
if _RUNTIME is None:
# TODO: Warn, in the future the runtime must be set explicitly, either
# as a config/env variable or via set_runtime
set_default_runtime()
dll_path = join(dirname(__file__), "runtime", "Python.Runtime.dll")
libpython = linked_libpython()
if libpython and _FFI is None and sys.platform != "win32":
# Load and leak libpython handle s.t. the .NET runtime doesn't dlcloses
# it
import posix
import cffi
_FFI = cffi.FFI()
_FFI.dlopen(libpython, posix.RTLD_NODELETE | posix.RTLD_LOCAL)
_LOADER_ASSEMBLY = _RUNTIME.get_assembly(dll_path)
func = _LOADER_ASSEMBLY["Python.Runtime.Loader.Initialize"]
if func(f"{libpython or ''}".encode("utf8")) != 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:
raise RuntimeError("Failed to call Python.NET shutdown")
if _RUNTIME is not None:
# TODO: Add explicit `close` to clr_loader
_RUNTIME = None