-
Notifications
You must be signed in to change notification settings - Fork 189
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
refactor py_str macro to allow passing custom global/locals #777
Draft
marius311
wants to merge
2
commits into
JuliaPy:master
Choose a base branch
from
marius311:pystr_refactor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -77,10 +77,6 @@ function pyeval(s::AbstractString, returntype::TypeTuple=PyAny, | |||||
return convert(returntype, pyeval_(s, pynamespace(Main), locals, input_type)) | ||||||
end | ||||||
|
||||||
# get filename from @__FILE__ macro, which returns nothing in the REPL | ||||||
make_fname(fname::AbstractString) = String(fname) | ||||||
make_fname(fname::Any) = "REPL" | ||||||
|
||||||
# a little finite-state-machine dictionary to keep track of where | ||||||
# we are in Python code, since $ is ignored in string literals and comments. | ||||||
# 'p' = Python code, '#' = comment, '$' = Julia interpolation | ||||||
|
@@ -202,32 +198,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__)) | ||||||
fname = String(__source__.file) | ||||||
esc(:(PyCall.@_py_str($m, $m, $fname, $code, $(options...)))) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think you need this? I think we better test baremodule TestPyStr
using PyCall: @py_str
pystr(x) = py"$$x"
end
@test TestPyStr.pystr("1+1") == 2 |
||||||
end | ||||||
|
||||||
macro _py_str(pyglobals, pylocals, fname, code, options...) | ||||||
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__) | ||||||
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 | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this call to
esc
necessary, since there areesc
calls within@_py_str
?Indeed, the call to
esc
here seems like it might cause problems, because it prevents the namespylocals
,pyglobals
, andret
from being hygienized.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No,
pylocals
etc... get hygenized by@_py_str
, theesc
here makes it so the interpolated variables don't also get hygenized, e.g. with the code in this PR:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I should've asked this before but why is
insert_pyevals
run at run-time? It looks like this doesn't depend on the run-time value ofglobals
andlocals
(or maybe I'm missing something)? If we expand everything at macro-expansion time, perhapsesc
of@py_str
here and@prepare_for_pyjulia_call
there can be removed? Then maybe_py_str
doesn't have to be a macro?