Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mono Domain Set Config #9

Merged
merged 6 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 7 additions & 1 deletion clr_loader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
def get_mono(
domain: Optional[str] = None,
config_file: Optional[str] = None,
global_config_file: Optional[str] = None,
libmono: Optional[str] = None,
sgen: bool = True,
) -> Runtime:
Expand All @@ -17,7 +18,12 @@ def get_mono(
if libmono is None:
libmono = find_libmono(sgen)

impl = Mono(domain=domain, config_file=config_file, libmono=libmono)
impl = Mono(
domain=domain,
config_file=config_file,
global_config_file=global_config_file,
libmono=libmono,
)
return Runtime(impl)


Expand Down
1 change: 1 addition & 0 deletions clr_loader/ffi/mono.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
MonoAssembly* mono_domain_assembly_open(MonoDomain *domain, const char *name);
MonoImage* mono_assembly_get_image(MonoAssembly *assembly);
void mono_domain_set_config(MonoDomain *domain, const char *base_dir, const char *config_file_name);
void mono_config_parse(const char* path);
MonoMethodDesc* mono_method_desc_new(const char* name, bool include_namespace);
Expand Down
36 changes: 29 additions & 7 deletions clr_loader/mono.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import atexit
from typing import Optional

from .ffi import load_mono, ffi

Expand All @@ -11,10 +12,21 @@


class Mono:
def __init__(self, libmono, domain=None, config_file=None):
def __init__(
self,
libmono,
*,
domain=None,
config_file: Optional[str] = None,
global_config_file: Optional[str] = None,
):
self._assemblies = {}

initialize(config_file=config_file, libmono=libmono)
initialize(
config_file=config_file,
global_config_file=global_config_file,
libmono=libmono,
)

if domain is None:
self._domain = _ROOT_DOMAIN
Expand Down Expand Up @@ -81,18 +93,28 @@ def __call__(self, ptr, size):
return unboxed[0]


def initialize(config_file: str, libmono: str) -> None:
def initialize(
libmono: str,
config_file: Optional[str] = None,
global_config_file: Optional[str] = None,
) -> None:
global _MONO, _ROOT_DOMAIN
if _MONO is None:
_MONO = load_mono(libmono)

# Load in global config (i.e /etc/mono/config)
global_encoded = global_config_file or ffi.NULL
_MONO.mono_config_parse(global_encoded)

# Even if we don't have a domain config file, we still need to set it
# as something, see https://github.com/pythonnet/clr-loader/issues/8
if config_file is None:
config_bytes = ffi.NULL
else:
config_bytes = config_file.encode("utf8")
config_file = ""

config_encoded = config_file.encode("utf8")

_ROOT_DOMAIN = _MONO.mono_jit_init(b"clr_loader")
_MONO.mono_config_parse(config_bytes)
_MONO.mono_domain_set_config(_ROOT_DOMAIN, b".", config_encoded)
_check_result(_ROOT_DOMAIN, "Failed to initialize Mono")
atexit.register(_release)

Expand Down