Skip to content

Commit e4d60e9

Browse files
committed
Refactor JuliaAPI
1 parent 83b7ffc commit e4d60e9

5 files changed

Lines changed: 64 additions & 16 deletions

File tree

src/IPython.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module IPython
22

33
using Compat
44
using Compat: @warn, @info
5+
include("julia_api.jl")
56
include("core.jl")
67
include("convenience.jl")
78
include("julia_repl.jl")

src/core.jl

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,10 @@ julia_exepath() =
55
joinpath(VERSION < v"0.7.0-DEV.3073" ? JULIA_HOME : Base.Sys.BINDIR,
66
Base.julia_exename())
77

8-
function eval_str(code::String)
9-
Base.eval(Main, Meta.parse(strip(code)))
10-
end
11-
12-
set_var(name::String, value) = set_var(Symbol(name), value)
13-
14-
function set_var(name::Symbol, value)
15-
Base.eval(Main, :($name = $value))
16-
nothing
17-
end
18-
198
function _start_ipython(name; kwargs...)
209
pyimport("replhelper")[name](;
21-
eval_str = eval_str,
22-
set_var = set_var,
10+
eval_str = JuliaAPI.eval_str,
11+
api = JuliaAPI,
2312
kwargs...)
2413
end
2514

src/julia_api.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module JuliaAPI
2+
3+
using Compat
4+
using Compat.REPL
5+
6+
function eval_str(code::AbstractString;
7+
scope::Module = Main,
8+
filename::AbstractString = "string",
9+
auto_jlwrap = true,
10+
force_jlwrap = false)
11+
return include_string(scope, code, filename)
12+
end
13+
14+
@static if VERSION < v"0.7-"
15+
getattr(obj, name) = getfield(obj, Symbol(name))
16+
function setattr(obj, name, value)
17+
setfield!(obj, Symbol(name), value)
18+
return nothing
19+
end
20+
else
21+
getattr(obj, name) = getproperty(obj, Symbol(name))
22+
function setattr(obj, name, value)
23+
setproperty!(obj, Symbol(name), value)
24+
return nothing
25+
end
26+
end
27+
28+
function setattr(mod::Module, name, value)
29+
Base.eval(mod, :($(Symbol(name)) = $value))
30+
return nothing
31+
end
32+
33+
end # module

src/replhelper/core.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,19 @@ def py_name(name):
3232

3333
class JuliaAPI(object):
3434

35-
def __init__(self, eval_str, set_var):
35+
def __init__(self, eval_str, api):
3636
self.eval = eval_str
37-
self.set_var = set_var
37+
self.api = api
38+
39+
def __getattr__(self, name):
40+
return self.eval(name, scope=self.api)
3841

3942

4043
class JuliaNameSpace(object):
4144

4245
def __init__(self, julia):
4346
self.__julia = julia
47+
self.__scope = julia.eval("Main")
4448

4549
eval = property(lambda self: self.__julia.eval)
4650

@@ -49,7 +53,7 @@ def __setattr__(self, name, value):
4953
object.__setattr__(self, name, value)
5054
# super().__setattr__(name, value)
5155
else:
52-
self.__julia.set_var(name, value)
56+
self.__julia.setattr(self.__scope, name, value)
5357

5458
def __getattr__(self, name):
5559
if name.startswith('_'):

src/replhelper/tests/test_api.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def test_eval(julia):
2+
assert julia.eval("123456789") == 123456789
3+
4+
5+
def test_mutable_struct(julia):
6+
struct = julia.eval("""Base.eval(Module(), quote
7+
mutable struct Spam
8+
egg
9+
end
10+
Spam(123)
11+
end)""")
12+
assert julia.getattr(struct, "egg") == 123
13+
assert julia.setattr(struct, "egg", 456) is None
14+
assert julia.getattr(struct, "egg") == 456
15+
16+
17+
def test_mutate_main(julia):
18+
scope = julia.eval("Main")
19+
assert julia.setattr(scope, "spam", 789) is None
20+
assert julia.getattr(scope, "spam") == 789
21+
assert julia.eval("Main.spam") == 789

0 commit comments

Comments
 (0)