Skip to content

Commit 63d9e06

Browse files
author
Christopher Doris
committed
update pyeval etc
1 parent eff1a65 commit 63d9e06

File tree

2 files changed

+57
-25
lines changed

2 files changed

+57
-25
lines changed

src/abstract/builtins.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,11 @@ function pycallable(x)
5555
z
5656
end
5757
export pycallable
58+
59+
"""
60+
pycompile(...)
61+
62+
Equivalent to `compile(...)` in Python.
63+
"""
64+
pycompile(args...; kwargs...) = pybuiltins.compile(args...; kwargs...)
65+
export pycompile

src/concrete/code.jl

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,18 @@ Evaluate the given Python `code`, returning the result as a `T`.
2525
2626
If `globals` is a `Module`, then a persistent `dict` unique to that module is used.
2727
28-
If `locals` is not specified, then it is set to `globals`, i.e. the code runs in global scope.
28+
By default the code runs in global scope (i.e. `locals===globals`). To use a temporary
29+
local scope, set `locals` to `()`, or to a `NamedTuple`` of variables to include in the
30+
scope.
2931
30-
For example the following computes `1.1+2.2` in the `Main` module as a `Float64`:
32+
See also [`@pyeval`](@ref).
33+
34+
# Examples
35+
36+
The following computes `1.1+2.2` in the `Main` module as a `Float64`:
3137
```
32-
pyeval(Float64, "x+y", Main, (x=1.1, y=2.2))
38+
pyeval(Float64, "x+y", Main, (x=1.1, y=2.2)) # returns 3.3
3339
```
34-
35-
See also [`@pyeval`](@ref).
3640
"""
3741
function pyeval(::Type{T}, code, globals, locals=nothing) where {T}
3842
globals_, locals_ = _pyeval_args(globals, locals)
@@ -66,17 +70,28 @@ Execute the given Python `code`.
6670
6771
If `globals` is a `Module`, then a persistent `dict` unique to that module is used.
6872
69-
If `locals` is not specified, then it is set to `globals`, i.e. the code runs in global scope.
73+
By default the code runs in global scope (i.e. `locals===globals`). To use a temporary
74+
local scope, set `locals` to `()`, or to a `NamedTuple`` of variables to include in the
75+
scope.
7076
7177
If `T==Nothing` then returns `nothing`. Otherwise `T` must be a concrete `NamedTuple` type
7278
and the corresponding items from `locals` are extracted and returned.
7379
74-
For example the following computes `1.1+2.2` in the `Main` module as a `Float64`:
80+
See also [`@pyexec`](@ref).
81+
82+
# Examples
83+
84+
The following computes `1.1+2.2` in the `Main` module as a `Float64`:
7585
```
76-
pyeval(@NamedTuple{ans::Float64}, "ans=x+y", Main, (x=1.1, y=2.2))
86+
pyexec(@NamedTuple{ans::Float64}, "ans=x+y", Main, (x=1.1, y=2.2)) # returns (ans = 3.3,)
7787
```
7888
79-
See also [`@pyexec`](@ref).
89+
Marking variables as `global` saves them into the module scope, so that they are available
90+
in subsequent invocations:
91+
```
92+
pyexec("global x; x=12", Main)
93+
pyeval(Int, "x", Main) # returns 12
94+
```
8095
"""
8196
function pyexec(::Type{T}, code, globals, locals=nothing) where {T}
8297
globals_, locals_ = _pyeval_args(globals, locals)
@@ -131,9 +146,7 @@ function _pyeval_macro_args(arg, filename, mode)
131146
$codeobj
132147
end
133148
# convert inputs to locals
134-
if inputs === :GLOBAL
135-
locals = nothing
136-
elseif inputs === nothing
149+
if inputs === nothing
137150
locals = ()
138151
else
139152
if inputs isa Expr && inputs.head === :tuple
@@ -160,18 +173,20 @@ end
160173
"""
161174
@pyeval [inputs =>] code [=> T]
162175
163-
Evaluate the given `code` in a scope unique to the current module and return the answer as a `T`.
176+
Evaluate the given `code` in a new local scope and return the answer as a `T`.
177+
178+
The global scope is persistent and unique to the current module.
164179
165180
The `code` must be a literal string or command.
166181
167-
The `inputs` is a tuple of inputs of the form `v=expr` to be included in a temporary new
168-
`locals` dict. Only `v` is required, `expr` defaults to `v`.
182+
The `inputs` is a tuple of inputs of the form `v=expr` to be included in the local scope.
183+
Only `v` is required, `expr` defaults to `v`.
169184
170-
As a special case, if `inputs` is `GLOBAL` then the code is run in global scope.
185+
# Examples
171186
172-
For example the following computes `1.1+2.2` and returns a `Float64`:
187+
The following computes `1.1+2.2` and returns a `Float64`:
173188
```
174-
@pyeval (x=1.1, y=2.2) => `x+y` => Float64
189+
@pyeval (x=1.1, y=2.2) => `x+y` => Float64 # returns 3.3
175190
```
176191
"""
177192
macro pyeval(arg)
@@ -186,22 +201,31 @@ export @pyeval
186201
"""
187202
@pyexec [inputs =>] code [=> outputs]
188203
189-
Execute the given `code` in a scope unique to the current module.
204+
Execute the given `code` in a new local scope.
190205
191-
The `code` must be a literal string or command.
206+
The global scope is persistent and unique to the current module.
192207
193-
The `inputs` is a tuple of inputs of the form `v=expr` to be included in a temporary new
194-
`locals` dict. Only `v` is required, `expr` defaults to `v`.
208+
The `code` must be a literal string or command.
195209
196-
As a special case, if `inputs` is `GLOBAL` then the code is run in global scope.
210+
The `inputs` is a tuple of inputs of the form `v=expr` to be included in the local scope.
211+
Only `v` is required, `expr` defaults to `v`.
197212
198213
The `outputs` is a tuple of outputs of the form `x::T=v`, meaning that `v` is extracted from
199214
locals, converted to `T` and assigned to `x`. Only `x` is required: `T` defaults to `Py`
200215
and `v` defaults to `x`.
201216
202-
For example the following computes `1.1+2.2` and assigns its value to `ans` as a `Float64`:
217+
# Examples
218+
219+
The following computes `1.1+2.2` and assigns its value to `ans` as a `Float64`:
220+
```
221+
@pyexec (x=1.1, y=2.2) => `ans=x+y` => ans::Float64 # returns 3.3
222+
```
223+
224+
Marking variables as `global` saves them into the module scope, so that they are available
225+
in subsequent invocations:
203226
```
204-
@pyexec (x=1.1, y=2.2) => `ans=x+y` => ans::Float64
227+
@pyexec `global x; x=12`
228+
@pyeval `x` => Int # returns 12
205229
```
206230
"""
207231
macro pyexec(arg)

0 commit comments

Comments
 (0)