diff --git a/julia/core.py b/julia/core.py index 57b13b07..2167217c 100644 --- a/julia/core.py +++ b/julia/core.py @@ -98,6 +98,23 @@ def __try_getattr(self, name): raise AttributeError(name) +class JuliaMainModule(JuliaModule): + + def __setattr__(self, name, value): + if name.startswith('_'): + super(JuliaMainModule, self).__setattr__(name, value) + else: + juliapath = self.__name__.lstrip("julia.") + setter = ''' + Main.PyCall.pyfunctionret( + (x) -> eval({}, :({} = $x)), + Any, + PyCall.PyAny) + '''.format(juliapath, jl_name(name)) + self._julia.eval(setter)(value) + + using = property(lambda self: self._julia.using) + # add custom import behavior for the julia "module" class JuliaImporter(object): @@ -120,7 +137,10 @@ def __init__(self, julia): # load module was deprecated in v3.4 def load_module(self, fullname): juliapath = fullname.lstrip("julia.") - if isamodule(self.julia, juliapath): + if juliapath == 'Main': + return sys.modules.setdefault(fullname, + JuliaMainModule(self, fullname)) + elif isamodule(self.julia, juliapath): return sys.modules.setdefault(fullname, JuliaModule(self, fullname)) elif isafunction(self.julia, juliapath): return getattr(self.julia, juliapath) diff --git a/test/test_core.py b/test/test_core.py index cc7d8125..96d84187 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -88,6 +88,11 @@ def test_star_import_julia_module(self): from . import _star_import _star_import.BasicREPL + def test_main_module(self): + from julia import Main + Main.x = x = 123456 + assert julia.eval('x') == x + #TODO: this causes a segfault """ def test_import_julia_modules(self):