Skip to content

Commit 0c41abd

Browse files
committed
Add pyccall macro to combine ccall and pysym macros
1 parent f832d77 commit 0c41abd

15 files changed

+136
-132
lines changed

src/PyCall.jl

+30-26
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ macro ccall(args...)
4646
end
4747
end
4848

49+
macro pyccall(f, args...)
50+
:(@ccall(@pysym($(esc(f))), $(esc.(args)...)))
51+
end
52+
4953
#########################################################################
5054

5155
# Mirror of C PyObject struct (for non-debugging Python builds).
@@ -109,7 +113,7 @@ it is equivalent to a `PyNULL()` object.
109113
ispynull(o::PyObject) = o.o == PyPtr_NULL
110114

111115
function pydecref_(o::Union{PyPtr,PyObject})
112-
@ccall(@pysym(:Py_DecRef), Cvoid, (PyPtr,), o)
116+
@pyccall(:Py_DecRef, Cvoid, (PyPtr,), o)
113117
return o
114118
end
115119

@@ -120,7 +124,7 @@ function pydecref(o::PyObject)
120124
end
121125

122126
function pyincref_(o::Union{PyPtr,PyObject})
123-
@ccall((@pysym :Py_IncRef), Cvoid, (PyPtr,), o)
127+
@pyccall(:Py_IncRef, Cvoid, (PyPtr,), o)
124128
return o
125129
end
126130

@@ -160,10 +164,10 @@ function Base.copy!(dest::PyObject, src::PyObject)
160164
end
161165

162166
pyisinstance(o::PyObject, t::PyObject) =
163-
!ispynull(t) && @ccall((@pysym :PyObject_IsInstance), Cint, (PyPtr,PyPtr), o, t.o) == 1
167+
!ispynull(t) && @pyccall(:PyObject_IsInstance, Cint, (PyPtr,PyPtr), o, t.o) == 1
164168

165169
pyisinstance(o::PyObject, t::Union{Ptr{Cvoid},PyPtr}) =
166-
t != C_NULL && @ccall((@pysym :PyObject_IsInstance), Cint, (PyPtr,PyPtr), o, t) == 1
170+
t != C_NULL && @pyccall(:PyObject_IsInstance, Cint, (PyPtr,PyPtr), o, t) == 1
167171

168172
pyquery(q::Ptr{Cvoid}, o::PyObject) =
169173
@ccall(q, Cint, (PyPtr,), o) == 1
@@ -181,7 +185,7 @@ PyObject(o::PyObject) = o
181185
include("exception.jl")
182186
include("gui.jl")
183187

184-
pytypeof(o::PyObject) = ispynull(o) ? throw(ArgumentError("NULL PyObjects have no Python type")) : PyObject(@pycheckn @ccall(@pysym(:PyObject_Type), PyPtr, (PyPtr,), o))
188+
pytypeof(o::PyObject) = ispynull(o) ? throw(ArgumentError("NULL PyObjects have no Python type")) : PyObject(@pycheckn @pyccall(:PyObject_Type, PyPtr, (PyPtr,), o))
185189

186190
#########################################################################
187191

@@ -211,7 +215,7 @@ PyObject(o::PyPtr, keep::Any) = pyembed(PyObject(o), keep)
211215
Return a string representation of `o` corresponding to `str(o)` in Python.
212216
"""
213217
pystr(o::PyObject) = convert(AbstractString,
214-
PyObject(@pycheckn @ccall((@pysym :PyObject_Str), PyPtr,
218+
PyObject(@pycheckn @pyccall(:PyObject_Str, PyPtr,
215219
(PyPtr,), o)))
216220

217221
"""
@@ -220,7 +224,7 @@ pystr(o::PyObject) = convert(AbstractString,
220224
Return a string representation of `o` corresponding to `repr(o)` in Python.
221225
"""
222226
pyrepr(o::PyObject) = convert(AbstractString,
223-
PyObject(@pycheckn @ccall((@pysym :PyObject_Repr), PyPtr,
227+
PyObject(@pycheckn @pyccall(:PyObject_Repr, PyPtr,
224228
(PyPtr,), o)))
225229

226230
"""
@@ -234,10 +238,10 @@ function pystring(o::PyObject)
234238
if ispynull(o)
235239
return "NULL"
236240
else
237-
s = @ccall((@pysym :PyObject_Repr), PyPtr, (PyPtr,), o)
241+
s = @pyccall(:PyObject_Repr, PyPtr, (PyPtr,), o)
238242
if (s == C_NULL)
239243
pyerr_clear()
240-
s = @ccall((@pysym :PyObject_Str), PyPtr, (PyPtr,), o)
244+
s = @pyccall(:PyObject_Str, PyPtr, (PyPtr,), o)
241245
if (s == C_NULL)
242246
pyerr_clear()
243247
return string(o.o)
@@ -275,7 +279,7 @@ function hash(o::PyObject)
275279
# since on 64-bit Windows the Python 2.x hash is only 32 bits
276280
hashsalt(unsafe_pyjlwrap_to_objref(o.o))
277281
else
278-
h = @ccall((@pysym :PyObject_Hash), Py_hash_t, (PyPtr,), o)
282+
h = @pyccall(:PyObject_Hash, Py_hash_t, (PyPtr,), o)
279283
if h == -1 # error
280284
pyerr_clear()
281285
return hashsalt(o.o)
@@ -293,7 +297,7 @@ function getindex(o::PyObject, s::AbstractString)
293297
if ispynull(o)
294298
throw(ArgumentError("ref of NULL PyObject"))
295299
end
296-
p = @ccall((@pysym :PyObject_GetAttrString), PyPtr, (PyPtr, Cstring), o, s)
300+
p = @pyccall(:PyObject_GetAttrString, PyPtr, (PyPtr, Cstring), o, s)
297301
if p == C_NULL
298302
pyerr_clear()
299303
throw(KeyError(s))
@@ -307,7 +311,7 @@ function setindex!(o::PyObject, v, s::Union{Symbol,AbstractString})
307311
if ispynull(o)
308312
throw(ArgumentError("assign of NULL PyObject"))
309313
end
310-
if -1 == @ccall((@pysym :PyObject_SetAttrString), Cint,
314+
if -1 == @pyccall(:PyObject_SetAttrString, Cint,
311315
(PyPtr, Cstring, PyPtr), o, s, PyObject(v))
312316
pyerr_clear()
313317
throw(KeyError(s))
@@ -319,7 +323,7 @@ function haskey(o::PyObject, s::Union{Symbol,AbstractString})
319323
if ispynull(o)
320324
throw(ArgumentError("haskey of NULL PyObject"))
321325
end
322-
return 1 == @ccall((@pysym :PyObject_HasAttrString), Cint,
326+
return 1 == @pyccall(:PyObject_HasAttrString, Cint,
323327
(PyPtr, Cstring), o, s)
324328
end
325329

@@ -418,7 +422,7 @@ end
418422
function _pyimport(name::AbstractString)
419423
cookie = ActivatePyActCtx()
420424
try
421-
return PyObject(@ccall((@pysym :PyImport_ImportModule), PyPtr, (Cstring,), name))
425+
return PyObject(@pyccall(:PyImport_ImportModule, PyPtr, (Cstring,), name))
422426
finally
423427
DeactivatePyActCtx(cookie)
424428
end
@@ -719,7 +723,7 @@ include("pyfncall.jl")
719723
# for now we can define "get".
720724

721725
function get(o::PyObject, returntype::TypeTuple, k, default)
722-
r = @ccall((@pysym :PyObject_GetItem), PyPtr, (PyPtr,PyPtr), o,PyObject(k))
726+
r = @pyccall(:PyObject_GetItem, PyPtr, (PyPtr,PyPtr), o,PyObject(k))
723727
if r == C_NULL
724728
pyerr_clear()
725729
default
@@ -729,22 +733,22 @@ function get(o::PyObject, returntype::TypeTuple, k, default)
729733
end
730734

731735
get(o::PyObject, returntype::TypeTuple, k) =
732-
convert(returntype, PyObject(@pycheckn @ccall((@pysym :PyObject_GetItem),
736+
convert(returntype, PyObject(@pycheckn @pyccall(:PyObject_GetItem,
733737
PyPtr, (PyPtr,PyPtr), o, PyObject(k))))
734738

735739
get(o::PyObject, k, default) = get(o, PyAny, k, default)
736740
get(o::PyObject, k) = get(o, PyAny, k)
737741

738742
function delete!(o::PyObject, k)
739-
e = @ccall((@pysym :PyObject_DelItem), Cint, (PyPtr, PyPtr), o, PyObject(k))
743+
e = @pyccall(:PyObject_DelItem, Cint, (PyPtr, PyPtr), o, PyObject(k))
740744
if e == -1
741745
pyerr_clear() # delete! ignores errors in Julia
742746
end
743747
return o
744748
end
745749

746750
function set!(o::PyObject, k, v)
747-
@pycheckz @ccall((@pysym :PyObject_SetItem), Cint, (PyPtr, PyPtr, PyPtr),
751+
@pycheckz @pyccall(:PyObject_SetItem, Cint, (PyPtr, PyPtr, PyPtr),
748752
o, PyObject(k), PyObject(v))
749753
v
750754
end
@@ -761,24 +765,24 @@ function ind2py(i::Integer)
761765
return i-1
762766
end
763767

764-
_getindex(o::PyObject, i::Integer, T) = convert(T, PyObject(@pycheckn @ccall((@pysym :PySequence_GetItem), PyPtr, (PyPtr, Int), o, ind2py(i))))
768+
_getindex(o::PyObject, i::Integer, T) = convert(T, PyObject(@pycheckn @pyccall(:PySequence_GetItem, PyPtr, (PyPtr, Int), o, ind2py(i))))
765769
getindex(o::PyObject, i::Integer) = _getindex(o, i, PyAny)
766770
function setindex!(o::PyObject, v, i::Integer)
767-
@pycheckz @ccall((@pysym :PySequence_SetItem), Cint, (PyPtr, Int, PyPtr), o, ind2py(i), PyObject(v))
771+
@pycheckz @pyccall(:PySequence_SetItem, Cint, (PyPtr, Int, PyPtr), o, ind2py(i), PyObject(v))
768772
v
769773
end
770774
getindex(o::PyObject, i1::Integer, i2::Integer) = get(o, (ind2py(i1),ind2py(i2)))
771775
setindex!(o::PyObject, v, i1::Integer, i2::Integer) = set!(o, (ind2py(i1),ind2py(i2)), v)
772776
getindex(o::PyObject, I::Integer...) = get(o, map(ind2py, I))
773777
setindex!(o::PyObject, v, I::Integer...) = set!(o, map(ind2py, I), v)
774-
length(o::PyObject) = @pycheckz @ccall((@pysym :PySequence_Size), Int, (PyPtr,), o)
778+
length(o::PyObject) = @pycheckz @pyccall(:PySequence_Size, Int, (PyPtr,), o)
775779
size(o::PyObject) = (length(o),)
776780
firstindex(o::PyObject) = 1
777781
lastindex(o::PyObject) = length(o)
778782

779783
function splice!(a::PyObject, i::Integer)
780784
v = a[i]
781-
@pycheckz @ccall((@pysym :PySequence_DelItem), Cint, (PyPtr, Int), a, i-1)
785+
@pycheckz @pyccall(:PySequence_DelItem, Cint, (PyPtr, Int), a, i-1)
782786
v
783787
end
784788

@@ -787,20 +791,20 @@ popfirst!(a::PyObject) = splice!(a, 1)
787791

788792
function empty!(a::PyObject)
789793
for i in length(a):-1:1
790-
@pycheckz @ccall((@pysym :PySequence_DelItem), Cint, (PyPtr, Int), a, i-1)
794+
@pycheckz @pyccall(:PySequence_DelItem, Cint, (PyPtr, Int), a, i-1)
791795
end
792796
a
793797
end
794798

795799
# The following operations only work for the list type and subtypes thereof:
796800
function push!(a::PyObject, item)
797-
@pycheckz @ccall((@pysym :PyList_Append), Cint, (PyPtr, PyPtr),
801+
@pycheckz @pyccall(:PyList_Append, Cint, (PyPtr, PyPtr),
798802
a, PyObject(item))
799803
a
800804
end
801805

802806
function insert!(a::PyObject, i::Integer, item)
803-
@pycheckz @ccall((@pysym :PyList_Insert), Cint, (PyPtr, Int, PyPtr),
807+
@pycheckz @pyccall(:PyList_Insert, Cint, (PyPtr, Int, PyPtr),
804808
a, ind2py(i), PyObject(item))
805809
a
806810
end
@@ -826,7 +830,7 @@ function append!(a::PyObject, items)
826830
end
827831

828832
append!(a::PyObject, items::PyObject) =
829-
PyObject(@pycheckn @ccall((@pysym :PySequence_InPlaceConcat),
833+
PyObject(@pycheckn @pyccall(:PySequence_InPlaceConcat,
830834
PyPtr, (PyPtr, PyPtr), a, items))
831835

832836
#########################################################################

0 commit comments

Comments
 (0)