@@ -46,6 +46,10 @@ macro ccall(args...)
46
46
end
47
47
end
48
48
49
+ macro pyccall (f, args... )
50
+ :(@ccall (@pysym ($ (esc (f))), $ (esc .(args)... )))
51
+ end
52
+
49
53
# ########################################################################
50
54
51
55
# Mirror of C PyObject struct (for non-debugging Python builds).
@@ -109,7 +113,7 @@ it is equivalent to a `PyNULL()` object.
109
113
ispynull (o:: PyObject ) = o. o == PyPtr_NULL
110
114
111
115
function pydecref_ (o:: Union{PyPtr,PyObject} )
112
- @ccall ( @pysym ( :Py_DecRef ) , Cvoid, (PyPtr,), o)
116
+ @pyccall ( :Py_DecRef , Cvoid, (PyPtr,), o)
113
117
return o
114
118
end
115
119
@@ -120,7 +124,7 @@ function pydecref(o::PyObject)
120
124
end
121
125
122
126
function pyincref_ (o:: Union{PyPtr,PyObject} )
123
- @ccall (( @pysym :Py_IncRef ) , Cvoid, (PyPtr,), o)
127
+ @pyccall ( :Py_IncRef , Cvoid, (PyPtr,), o)
124
128
return o
125
129
end
126
130
@@ -160,10 +164,10 @@ function Base.copy!(dest::PyObject, src::PyObject)
160
164
end
161
165
162
166
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
164
168
165
169
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
167
171
168
172
pyquery (q:: Ptr{Cvoid} , o:: PyObject ) =
169
173
@ccall (q, Cint, (PyPtr,), o) == 1
@@ -181,7 +185,7 @@ PyObject(o::PyObject) = o
181
185
include (" exception.jl" )
182
186
include (" gui.jl" )
183
187
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))
185
189
186
190
# ########################################################################
187
191
@@ -211,7 +215,7 @@ PyObject(o::PyPtr, keep::Any) = pyembed(PyObject(o), keep)
211
215
Return a string representation of `o` corresponding to `str(o)` in Python.
212
216
"""
213
217
pystr (o:: PyObject ) = convert (AbstractString,
214
- PyObject (@pycheckn @ccall (( @pysym :PyObject_Str ) , PyPtr,
218
+ PyObject (@pycheckn @pyccall ( :PyObject_Str , PyPtr,
215
219
(PyPtr,), o)))
216
220
217
221
"""
@@ -220,7 +224,7 @@ pystr(o::PyObject) = convert(AbstractString,
220
224
Return a string representation of `o` corresponding to `repr(o)` in Python.
221
225
"""
222
226
pyrepr (o:: PyObject ) = convert (AbstractString,
223
- PyObject (@pycheckn @ccall (( @pysym :PyObject_Repr ) , PyPtr,
227
+ PyObject (@pycheckn @pyccall ( :PyObject_Repr , PyPtr,
224
228
(PyPtr,), o)))
225
229
226
230
"""
@@ -234,10 +238,10 @@ function pystring(o::PyObject)
234
238
if ispynull (o)
235
239
return " NULL"
236
240
else
237
- s = @ccall (( @pysym :PyObject_Repr ) , PyPtr, (PyPtr,), o)
241
+ s = @pyccall ( :PyObject_Repr , PyPtr, (PyPtr,), o)
238
242
if (s == C_NULL )
239
243
pyerr_clear ()
240
- s = @ccall (( @pysym :PyObject_Str ) , PyPtr, (PyPtr,), o)
244
+ s = @pyccall ( :PyObject_Str , PyPtr, (PyPtr,), o)
241
245
if (s == C_NULL )
242
246
pyerr_clear ()
243
247
return string (o. o)
@@ -275,7 +279,7 @@ function hash(o::PyObject)
275
279
# since on 64-bit Windows the Python 2.x hash is only 32 bits
276
280
hashsalt (unsafe_pyjlwrap_to_objref (o. o))
277
281
else
278
- h = @ccall (( @pysym :PyObject_Hash ) , Py_hash_t, (PyPtr,), o)
282
+ h = @pyccall ( :PyObject_Hash , Py_hash_t, (PyPtr,), o)
279
283
if h == - 1 # error
280
284
pyerr_clear ()
281
285
return hashsalt (o. o)
@@ -293,7 +297,7 @@ function getindex(o::PyObject, s::AbstractString)
293
297
if ispynull (o)
294
298
throw (ArgumentError (" ref of NULL PyObject" ))
295
299
end
296
- p = @ccall (( @pysym :PyObject_GetAttrString ) , PyPtr, (PyPtr, Cstring), o, s)
300
+ p = @pyccall ( :PyObject_GetAttrString , PyPtr, (PyPtr, Cstring), o, s)
297
301
if p == C_NULL
298
302
pyerr_clear ()
299
303
throw (KeyError (s))
@@ -307,7 +311,7 @@ function setindex!(o::PyObject, v, s::Union{Symbol,AbstractString})
307
311
if ispynull (o)
308
312
throw (ArgumentError (" assign of NULL PyObject" ))
309
313
end
310
- if - 1 == @ccall (( @pysym :PyObject_SetAttrString ) , Cint,
314
+ if - 1 == @pyccall ( :PyObject_SetAttrString , Cint,
311
315
(PyPtr, Cstring, PyPtr), o, s, PyObject (v))
312
316
pyerr_clear ()
313
317
throw (KeyError (s))
@@ -319,7 +323,7 @@ function haskey(o::PyObject, s::Union{Symbol,AbstractString})
319
323
if ispynull (o)
320
324
throw (ArgumentError (" haskey of NULL PyObject" ))
321
325
end
322
- return 1 == @ccall (( @pysym :PyObject_HasAttrString ) , Cint,
326
+ return 1 == @pyccall ( :PyObject_HasAttrString , Cint,
323
327
(PyPtr, Cstring), o, s)
324
328
end
325
329
418
422
function _pyimport (name:: AbstractString )
419
423
cookie = ActivatePyActCtx ()
420
424
try
421
- return PyObject (@ccall (( @pysym :PyImport_ImportModule ) , PyPtr, (Cstring,), name))
425
+ return PyObject (@pyccall ( :PyImport_ImportModule , PyPtr, (Cstring,), name))
422
426
finally
423
427
DeactivatePyActCtx (cookie)
424
428
end
@@ -719,7 +723,7 @@ include("pyfncall.jl")
719
723
# for now we can define "get".
720
724
721
725
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))
723
727
if r == C_NULL
724
728
pyerr_clear ()
725
729
default
@@ -729,22 +733,22 @@ function get(o::PyObject, returntype::TypeTuple, k, default)
729
733
end
730
734
731
735
get (o:: PyObject , returntype:: TypeTuple , k) =
732
- convert (returntype, PyObject (@pycheckn @ccall (( @pysym :PyObject_GetItem ) ,
736
+ convert (returntype, PyObject (@pycheckn @pyccall ( :PyObject_GetItem ,
733
737
PyPtr, (PyPtr,PyPtr), o, PyObject (k))))
734
738
735
739
get (o:: PyObject , k, default) = get (o, PyAny, k, default)
736
740
get (o:: PyObject , k) = get (o, PyAny, k)
737
741
738
742
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))
740
744
if e == - 1
741
745
pyerr_clear () # delete! ignores errors in Julia
742
746
end
743
747
return o
744
748
end
745
749
746
750
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),
748
752
o, PyObject (k), PyObject (v))
749
753
v
750
754
end
@@ -761,24 +765,24 @@ function ind2py(i::Integer)
761
765
return i- 1
762
766
end
763
767
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))))
765
769
getindex (o:: PyObject , i:: Integer ) = _getindex (o, i, PyAny)
766
770
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))
768
772
v
769
773
end
770
774
getindex (o:: PyObject , i1:: Integer , i2:: Integer ) = get (o, (ind2py (i1),ind2py (i2)))
771
775
setindex! (o:: PyObject , v, i1:: Integer , i2:: Integer ) = set! (o, (ind2py (i1),ind2py (i2)), v)
772
776
getindex (o:: PyObject , I:: Integer... ) = get (o, map (ind2py, I))
773
777
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)
775
779
size (o:: PyObject ) = (length (o),)
776
780
firstindex (o:: PyObject ) = 1
777
781
lastindex (o:: PyObject ) = length (o)
778
782
779
783
function splice! (a:: PyObject , i:: Integer )
780
784
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 )
782
786
v
783
787
end
784
788
@@ -787,20 +791,20 @@ popfirst!(a::PyObject) = splice!(a, 1)
787
791
788
792
function empty! (a:: PyObject )
789
793
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 )
791
795
end
792
796
a
793
797
end
794
798
795
799
# The following operations only work for the list type and subtypes thereof:
796
800
function push! (a:: PyObject , item)
797
- @pycheckz @ccall (( @pysym :PyList_Append ) , Cint, (PyPtr, PyPtr),
801
+ @pycheckz @pyccall ( :PyList_Append , Cint, (PyPtr, PyPtr),
798
802
a, PyObject (item))
799
803
a
800
804
end
801
805
802
806
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),
804
808
a, ind2py (i), PyObject (item))
805
809
a
806
810
end
@@ -826,7 +830,7 @@ function append!(a::PyObject, items)
826
830
end
827
831
828
832
append! (a:: PyObject , items:: PyObject ) =
829
- PyObject (@pycheckn @ccall (( @pysym :PySequence_InPlaceConcat ) ,
833
+ PyObject (@pycheckn @pyccall ( :PySequence_InPlaceConcat ,
830
834
PyPtr, (PyPtr, PyPtr), a, items))
831
835
832
836
# ########################################################################
0 commit comments