-
Couldn't load subscription status.
- Fork 187
refactor py_str macro to allow passing custom global/locals #777
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -202,32 +202,37 @@ pasted into the Python code. This allows you to evaluate code | |
| where the code itself is generated by a Julia expression. | ||
| """ | ||
| macro py_str(code, options...) | ||
| m = :(pynamespace($__module__)) | ||
| esc(macroexpand(__module__, :(PyCall.@_py_str($m, $m, $code, $(options...))))) | ||
| end | ||
|
|
||
| macro _py_str(pyglobals, pylocals, code, options...) | ||
stevengj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| T = length(options) == 1 && 'o' in options[1] ? PyObject : PyAny | ||
| code, locals = interpolate_pycode(code) | ||
| input_type = '\n' in code ? Py_file_input : Py_eval_input | ||
| fname = make_fname(@__FILE__) | ||
tkf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| assignlocals = Expr(:block, [(isa(v,String) ? | ||
| :(m[$v] = PyObject($(esc(ex)))) : | ||
| :(pylocals[$v] = PyObject($(esc(ex)))) : | ||
| nothing) for (v,ex) in locals]...) | ||
| code_expr = Expr(:call, esc(:(Base.string))) | ||
| code_expr = Expr(:call, Base.string) | ||
| i0 = firstindex(code) | ||
| for i in sort!(collect(filter(k -> isa(k,Integer), keys(locals)))) | ||
| push!(code_expr.args, code[i0:prevind(code,i)], esc(locals[i])) | ||
| i0 = i | ||
| end | ||
| push!(code_expr.args, code[i0:lastindex(code)]) | ||
| if input_type == Py_eval_input | ||
| removelocals = Expr(:block, [:(delete!(m, $v)) for v in keys(locals)]...) | ||
| removelocals = Expr(:block, [:(delete!(pylocals, $v)) for v in keys(locals)]...) | ||
| else | ||
| # if we are evaluating multi-line input, then it is not | ||
| # safe to remove the local variables, because they might be referred | ||
| # to in Python function definitions etc. that will be called later. | ||
| removelocals = nothing | ||
| end | ||
| quote | ||
| m = pynamespace($__module__) | ||
| pyglobals, pylocals = $pyglobals, $pylocals | ||
| $assignlocals | ||
| ret = $T(pyeval_($code_expr, m, m, $input_type, $fname)) | ||
| ret = $T(pyeval_($code_expr, pyglobals, pylocals, $input_type, $fname)) | ||
| $removelocals | ||
| ret | ||
| end | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.