Skip to content

Commit

Permalink
Mono Domain Set Config (#9)
Browse files Browse the repository at this point in the history
* set config for mono, dummy if none given

* Encode config_file string

* byte literall string for config location

* Use config parameter for domain config

* Allow the global config path to be set

* Fix mono_config_parse call

Co-authored-by: Benedikt Reinartz <[email protected]>
  • Loading branch information
C-SELLERS and filmor authored Mar 18, 2021
1 parent e90553b commit 4c35802
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
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

0 comments on commit 4c35802

Please sign in to comment.