Skip to content

Commit 55af237

Browse files
committed
change py_str to py_cmd
1 parent 064cf66 commit 55af237

File tree

4 files changed

+74
-64
lines changed

4 files changed

+74
-64
lines changed

src/PyCall.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ export pycall, pycall!, pyimport, pyimport_e, pybuiltin, PyObject, PyReverseDims
1313
pyisinstance, pywrap, pytypeof, pyeval, PyVector, pystring, pystr, pyrepr,
1414
pyraise, pytype_mapping, pygui, pygui_start, pygui_stop,
1515
pygui_stop_all, @pylab, set!, PyTextIO, @pysym, PyNULL, ispynull, @pydef,
16-
pyimport_conda, @py_str, @pywith, @pycall, pybytes, pyfunction, pyfunctionret,
17-
pywrapfn, pysetarg!, pysetargs!
16+
pyimport_conda, @py_cmd, @py_str, @pywith, @pycall, pybytes, pyfunction,
17+
pyfunctionret, pywrapfn, pysetarg!, pysetargs!
1818

1919
import Base: size, ndims, similar, copy, getindex, setindex!, stride,
2020
convert, pointer, summary, convert, show, haskey, keys, values,

src/pyeval.jl

+18-8
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ For example, `pyeval("x + y", x=1, y=2)` returns 3.
5353
function pyeval(s::AbstractString, returntype::TypeTuple=PyAny,
5454
locals=PyDict{AbstractString, PyObject}(),
5555
input_type=Py_eval_input; kwargs...)
56-
# construct deprecation warning in favor of py"..." strings
56+
# construct deprecation warning in favor of py`...` strings
5757
depbuf = IOBuffer()
5858
q = input_type==Py_eval_input ? "\"" : "\"\"\"\n"
5959
qr = reverse(q)
@@ -177,17 +177,18 @@ function interpolate_pycode(code::AbstractString)
177177
end
178178

179179
"""
180-
py".....python code....."
180+
py`.....python code.....`[o]
181+
py".....python code....."[o]
181182
182-
Evaluate the given Python code string in the main Python module.
183+
Evaluate the given Python code in the main Python module.
183184
184-
If the string is a single line (no newlines), then the Python
185+
If the input is a single line (no newlines), then the Python
185186
expression is evaluated and the result is returned.
186-
If the string is multiple lines (contains a newline), then the Python
187+
If the input has multiple lines (contains a newline), then the Python
187188
code is compiled and evaluated in the `__main__` Python module
188189
and nothing is returned.
189190
190-
If the `o` option is appended to the string, as in `py"..."o`, then the
191+
If the `o` option is appended to the command, as in `py\\`...\\`o`, then the
191192
return value is an unconverted `PyObject`; otherwise, it is
192193
automatically converted to a native Julia type if possible.
193194
@@ -196,12 +197,21 @@ Any `\$var` or `\$(expr)` expressions that appear in the Python code
196197
and passed to Python via auto-generated global variables. This
197198
allows you to "interpolate" Julia values into Python code.
198199
199-
Similarly, ny `\$\$var` or `\$\$(expr)` expressions in the Python code
200+
Similarly, `\$\$var` or `\$\$(expr)` expressions in the Python code
200201
are evaluated in Julia, converted to strings via `string`, and are
201-
pasted into the Python code. This allows you to evaluate code
202+
pasted into the Python code. This allows you to evaluate code
202203
where the code itself is generated by a Julia expression.
203204
"""
205+
macro py_cmd(code, options...)
206+
return py_cmd(__module__, code, options...)
207+
end
208+
209+
@doc (@doc @py_cmd)
204210
macro py_str(code, options...)
211+
return py_cmd(__module__, code, options...)
212+
end
213+
214+
function py_cmd(__module__, code, options...)
205215
T = length(options) == 1 && 'o' in options[1] ? PyObject : PyAny
206216
code, locals = interpolate_pycode(code)
207217
input_type = '\n' in code ? Py_file_input : Py_eval_input

test/runtests.jl

+51-51
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ const PyInt = pyversion < v"3" ? Int : Clonglong
155155

156156
# issue #92:
157157
let x = PyVector(PyAny[])
158-
py"lambda x: x.append(\"bar\")"(x)
158+
py`lambda x: x.append("bar")`(x)
159159
@test x == ["bar"]
160160
end
161161

@@ -267,21 +267,21 @@ const PyInt = pyversion < v"3" ? Int : Clonglong
267267
@test convert(BigInt, PyObject(1234)) == 1234
268268

269269
# hasproperty, getproperty, and propertynames
270-
py"""
270+
py```
271271
class A:
272272
class B:
273273
C = 1
274-
"""
275-
A = py"A"
274+
```
275+
A = py`A`
276276
@test hasproperty(A, "B")
277-
@test getproperty(A, "B") == py"A.B"
277+
@test getproperty(A, "B") == py`A.B`
278278
@test :B in propertynames(A)
279279
@static if VERSION >= v"0.7-"
280280
@test A.B.C == 1
281281
@test_throws KeyError A.X
282282
end
283-
setproperty!(py"A.B", "C", 2)
284-
@test py"A.B.C" == 2
283+
setproperty!(py`A.B`, "C", 2)
284+
@test py`A.B.C` == 2
285285

286286
# buffers
287287
let b = PyCall.PyBuffer(pyutf8("test string"))
@@ -373,23 +373,23 @@ const PyInt = pyversion < v"3" ? Int : Clonglong
373373
end
374374

375375
let x = 7
376-
py"""
376+
py```
377377
def myfun(x):
378378
return x + $x
379-
"""
380-
@test py"1 + 2" == 3
381-
@test py"1 + $x" == 8
382-
@test py"1 + $(x^2)" == 50
383-
@test py"myfun"(10) == 17
379+
```
380+
@test py`1 + 2` == 3
381+
@test py`1 + $x` == 8
382+
@test py`1 + $(x^2)` == 50
383+
@test py`myfun`(10) == 17
384384
end
385385

386386
# issue #352
387387
let x = "1+1"
388-
@test py"$x" == "1+1"
389-
@test py"$$x" == py"$$(x)" == 2
390-
@test py"7 - $$x - 7" == 0 # evaluates "7 - 1 + 1 - 7"
391-
@test py"7 - ($$x) - 7" == -2 # evaluates "7 - (1 + 1) - 7"
392-
@test py"1 + $$(x[1:2]) 3" == 5 # evals 1 + 1+ 3
388+
@test py`$x` == "1+1"
389+
@test py`$$x` == py`$$(x)` == 2
390+
@test py`7 - $$x - 7` == 0 # evaluates "7 - 1 + 1 - 7"
391+
@test py`7 - ($$x) - 7` == -2 # evaluates "7 - (1 + 1) - 7"
392+
@test py`1 + $$(x[1:2]) 3` == 5 # evals 1 + 1+ 3
393393
end
394394

395395
# Float16 support:
@@ -429,15 +429,15 @@ const PyInt = pyversion < v"3" ? Int : Clonglong
429429
end
430430

431431
# Expose python docs to Julia doc system
432-
py"""
432+
py```
433433
def foo():
434434
"foo docstring"
435435
return 0
436436
class bar:
437437
foo = foo
438-
"""
439-
global foo354 = py"foo"
440-
global barclass = py"bar"
438+
```
439+
global foo354 = py`foo`
440+
global barclass = py`bar`
441441
# use 'content' since `Text` objects test equality by object identity
442442
@test @doc(foo354).content == "foo docstring"
443443
@test @doc(barclass.foo).content == "foo docstring"
@@ -518,7 +518,7 @@ const PyInt = pyversion < v"3" ? Int : Clonglong
518518
end
519519

520520
@test occursin("integer", Base.Docs.doc(PyObject(1)).content)
521-
@test occursin("no docstring", Base.Docs.doc(PyObject(py"lambda x: x+1")).content)
521+
@test occursin("no docstring", Base.Docs.doc(PyObject(py`lambda x: x+1`)).content)
522522

523523
let b = rand(UInt8, 1000)
524524
@test(convert(Vector{UInt8}, pybytes(b)) == b
@@ -555,9 +555,9 @@ const PyInt = pyversion < v"3" ? Int : Clonglong
555555
@test_throws KeyError PyObject(TestConstruct(1)).y
556556

557557
# iterating over Julia objects in Python:
558-
@test py"[x**2 for x in $(PyCall.pyjlwrap_new(1:4))]" ==
559-
py"[x**2 for x in $(x for x in 1:4)]" ==
560-
py"[x**2 for x in $(PyCall.jlwrap_iterator(1:4))]" ==
558+
@test py`[x**2 for x in $(PyCall.pyjlwrap_new(1:4))]` ==
559+
py`[x**2 for x in $(x for x in 1:4)]` ==
560+
py`[x**2 for x in $(PyCall.jlwrap_iterator(1:4))]` ==
561561
[1,4,9,16]
562562

563563
let o = PyObject("foo")
@@ -580,7 +580,7 @@ const PyInt = pyversion < v"3" ? Int : Clonglong
580580
end
581581

582582
# issue #533
583-
@test py"lambda x,y,z: (x,y,z)"(3:6,4:10,5:11) === (PyInt(3):PyInt(6), PyInt(4):PyInt(10), PyInt(5):PyInt(11))
583+
@test py`lambda x,y,z: (x,y,z)`(3:6,4:10,5:11) === (PyInt(3):PyInt(6), PyInt(4):PyInt(10), PyInt(5):PyInt(11))
584584

585585
@test float(PyObject(1)) === 1.0
586586
@test float(PyObject(1+2im)) === 1.0 + 2.0im
@@ -650,12 +650,12 @@ end
650650
using PyCall
651651
obj = pyimport("sys") # get some PyObject
652652
end)
653-
py"""
653+
py```
654654
ns = {}
655655
def set(name):
656656
ns[name] = $include_string($anonymous, name)
657-
"""
658-
py"set"("obj")
657+
```
658+
py`set`("obj")
659659
@test anonymous.obj != PyNULL()
660660

661661
# Test above for pyjlwrap_getattr too:
@@ -668,12 +668,12 @@ end
668668
end
669669
obj = S(pyimport("sys"))
670670
end)
671-
py"""
671+
py```
672672
ns = {}
673673
def set(name):
674674
ns[name] = $include_string($anonymous, name).x
675-
"""
676-
py"set"("obj")
675+
```
676+
py`set`("obj")
677677
@test anonymous.obj.x != PyNULL()
678678

679679
# Test above for pyjlwrap_iternext too:
@@ -684,41 +684,41 @@ end
684684
sys = pyimport("sys")
685685
obj = (sys for _ in 1:1)
686686
end)
687-
py"""
687+
py```
688688
ns = {}
689689
def set(name):
690690
ns[name] = list(iter($include_string($anonymous, name)))
691-
"""
692-
py"set"("obj")
691+
```
692+
py`set`("obj")
693693
@test anonymous.sys != PyNULL()
694694
end
695695

696696
struct Unprintable end
697697
Base.show(::IO, ::Unprintable) = error("show(::IO, ::Unprintable) called")
698698
Base.show(::IO, ::Type{Unprintable}) = error("show(::IO, ::Type{Unprintable}) called")
699699

700-
py"""
700+
py```
701701
def try_repr(x):
702702
try:
703703
return repr(x)
704704
except Exception as err:
705705
return err
706-
"""
706+
```
707707

708-
py"""
708+
py```
709709
def try_call(f):
710710
try:
711711
return f()
712712
except Exception as err:
713713
return err
714-
"""
714+
```
715715

716716
@testset "throwing show" begin
717717
unp = Unprintable()
718718
@test_throws Exception show(unp)
719-
@test py"try_repr"("printable") isa String
720-
@test pyisinstance(py"try_repr"(unp), pybuiltin("Exception"))
721-
@test pyisinstance(py"try_call"(() -> throw(Unprintable())),
719+
@test py`try_repr`("printable") isa String
720+
@test pyisinstance(py`try_repr`(unp), pybuiltin("Exception"))
721+
@test pyisinstance(py`try_call`(() -> throw(Unprintable())),
722722
pybuiltin("Exception"))
723723
end
724724

@@ -749,25 +749,25 @@ end
749749
@test Base.IteratorSize(PyCall.PyIterator(PyObject([1]))) == Base.HasLength()
750750

751751
# 594
752-
@test collect(zip(py"iter([1, 2, 3])", 1:3)) ==
752+
@test collect(zip(py`iter([1, 2, 3])`, 1:3)) ==
753753
[(1, 1), (2, 2), (3, 3)]
754-
@test collect(zip(PyCall.PyIterator{Int}(py"iter([1, 2, 3])"), 1:3)) ==
754+
@test collect(zip(PyCall.PyIterator{Int}(py`iter([1, 2, 3])`), 1:3)) ==
755755
[(1, 1), (2, 2), (3, 3)]
756-
@test collect(zip(PyCall.PyIterator(py"[1, 2, 3]"o), 1:3)) ==
756+
@test collect(zip(PyCall.PyIterator(py`[1, 2, 3]`o), 1:3)) ==
757757
[(1, 1), (2, 2), (3, 3)]
758758
end
759759

760-
@test_throws PyCall.PyError py"__testing_pynamespace"
760+
@test_throws PyCall.PyError py`__testing_pynamespace`
761761

762762
module __isolated_namespace
763763
using PyCall
764-
py"""
764+
py```
765765
__testing_pynamespace = True
766-
"""
767-
get_testing_pynamespace() = py"__testing_pynamespace"
766+
```
767+
get_testing_pynamespace() = py`__testing_pynamespace`
768768
end
769769

770-
@test_throws PyCall.PyError py"__testing_pynamespace"
770+
@test_throws PyCall.PyError py`__testing_pynamespace`
771771
@test __isolated_namespace.get_testing_pynamespace()
772772

773773
@testset "atexit" begin

test/test_pyfncall.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
using Test, PyCall
22

3-
py"""
3+
py```
44
def mklist(*args, **kwargs):
55
l = list(args)
66
l.extend(kwargs.items())
77
return l
8-
"""
8+
```
99
@testset "pycall!" begin
10-
pymklist = py"mklist"
10+
pymklist = py`mklist`
1111
ret = PyNULL()
1212

1313
function pycall_checks(res, pyf, RetType, args...; kwargs...)

0 commit comments

Comments
 (0)