@@ -20,11 +20,6 @@ import Base: size, ndims, similar, copy, getindex, setindex!, stride,
20
20
append!, insert!, prepend!, unsafe_convert
21
21
import Compat: pushfirst!, popfirst!, firstindex, lastindex
22
22
23
- # Python C API is not interrupt-safe. In principle, we should
24
- # use sigatomic for every ccall to the Python library, but this
25
- # should really be fixed in Julia (#2622). However, we will
26
- # use the sigatomic_begin/end functions to protect pycall and
27
- # similar long-running (or potentially long-running) code.
28
23
import Base: sigatomic_begin, sigatomic_end
29
24
30
25
import Conda
@@ -36,6 +31,21 @@ import Base.Iterators: filter
36
31
include (joinpath (dirname (@__FILE__ ), " .." , " deps" ," depsutils.jl" ))
37
32
include (" startup.jl" )
38
33
34
+ # Python C API is not interrupt-safe. In principle, we should
35
+ # use sigatomic for every ccall to the Python library, but this
36
+ # should really be fixed in Julia (#2622). However, we will
37
+ # use the `disable_sigint` function to protect *all* invocations
38
+ # of Python API. This is required since `pyjlwrap_call` uses
39
+ # `reenable_sigint` which has to be called within `disable_sigint`
40
+ # context.
41
+ macro ccall (args... )
42
+ quote
43
+ disable_sigint () do
44
+ ccall ($ (esc .(args)... ))
45
+ end
46
+ end
47
+ end
48
+
39
49
# ########################################################################
40
50
41
51
# Mirror of C PyObject struct (for non-debugging Python builds).
@@ -99,7 +109,7 @@ it is equivalent to a `PyNULL()` object.
99
109
ispynull (o:: PyObject ) = o. o == PyPtr_NULL
100
110
101
111
function pydecref_ (o:: Union{PyPtr,PyObject} )
102
- ccall (@pysym (:Py_DecRef ), Cvoid, (PyPtr,), o)
112
+ @ ccall (@pysym (:Py_DecRef ), Cvoid, (PyPtr,), o)
103
113
return o
104
114
end
105
115
@@ -110,7 +120,7 @@ function pydecref(o::PyObject)
110
120
end
111
121
112
122
function pyincref_ (o:: Union{PyPtr,PyObject} )
113
- ccall ((@pysym :Py_IncRef ), Cvoid, (PyPtr,), o)
123
+ @ ccall ((@pysym :Py_IncRef ), Cvoid, (PyPtr,), o)
114
124
return o
115
125
end
116
126
@@ -150,13 +160,13 @@ function Base.copy!(dest::PyObject, src::PyObject)
150
160
end
151
161
152
162
pyisinstance (o:: PyObject , t:: PyObject ) =
153
- ! ispynull (t) && ccall ((@pysym :PyObject_IsInstance ), Cint, (PyPtr,PyPtr), o, t. o) == 1
163
+ ! ispynull (t) && @ ccall ((@pysym :PyObject_IsInstance ), Cint, (PyPtr,PyPtr), o, t. o) == 1
154
164
155
165
pyisinstance (o:: PyObject , t:: Union{Ptr{Cvoid},PyPtr} ) =
156
- t != C_NULL && ccall ((@pysym :PyObject_IsInstance ), Cint, (PyPtr,PyPtr), o, t) == 1
166
+ t != C_NULL && @ ccall ((@pysym :PyObject_IsInstance ), Cint, (PyPtr,PyPtr), o, t) == 1
157
167
158
168
pyquery (q:: Ptr{Cvoid} , o:: PyObject ) =
159
- ccall (q, Cint, (PyPtr,), o) == 1
169
+ @ ccall (q, Cint, (PyPtr,), o) == 1
160
170
161
171
# conversion to pass PyObject as ccall arguments:
162
172
unsafe_convert (:: Type{PyPtr} , po:: PyObject ) = po. o
@@ -171,7 +181,7 @@ PyObject(o::PyObject) = o
171
181
include (" exception.jl" )
172
182
include (" gui.jl" )
173
183
174
- pytypeof (o:: PyObject ) = ispynull (o) ? throw (ArgumentError (" NULL PyObjects have no Python type" )) : PyObject (@pycheckn ccall (@pysym (:PyObject_Type ), PyPtr, (PyPtr,), o))
184
+ pytypeof (o:: PyObject ) = ispynull (o) ? throw (ArgumentError (" NULL PyObjects have no Python type" )) : PyObject (@pycheckn @ ccall (@pysym (:PyObject_Type ), PyPtr, (PyPtr,), o))
175
185
176
186
# ########################################################################
177
187
@@ -201,7 +211,7 @@ PyObject(o::PyPtr, keep::Any) = pyembed(PyObject(o), keep)
201
211
Return a string representation of `o` corresponding to `str(o)` in Python.
202
212
"""
203
213
pystr (o:: PyObject ) = convert (AbstractString,
204
- PyObject (@pycheckn ccall ((@pysym :PyObject_Str ), PyPtr,
214
+ PyObject (@pycheckn @ ccall ((@pysym :PyObject_Str ), PyPtr,
205
215
(PyPtr,), o)))
206
216
207
217
"""
@@ -210,7 +220,7 @@ pystr(o::PyObject) = convert(AbstractString,
210
220
Return a string representation of `o` corresponding to `repr(o)` in Python.
211
221
"""
212
222
pyrepr (o:: PyObject ) = convert (AbstractString,
213
- PyObject (@pycheckn ccall ((@pysym :PyObject_Repr ), PyPtr,
223
+ PyObject (@pycheckn @ ccall ((@pysym :PyObject_Repr ), PyPtr,
214
224
(PyPtr,), o)))
215
225
216
226
"""
@@ -224,10 +234,10 @@ function pystring(o::PyObject)
224
234
if ispynull (o)
225
235
return " NULL"
226
236
else
227
- s = ccall ((@pysym :PyObject_Repr ), PyPtr, (PyPtr,), o)
237
+ s = @ ccall ((@pysym :PyObject_Repr ), PyPtr, (PyPtr,), o)
228
238
if (s == C_NULL )
229
239
pyerr_clear ()
230
- s = ccall ((@pysym :PyObject_Str ), PyPtr, (PyPtr,), o)
240
+ s = @ ccall ((@pysym :PyObject_Str ), PyPtr, (PyPtr,), o)
231
241
if (s == C_NULL )
232
242
pyerr_clear ()
233
243
return string (o. o)
@@ -265,7 +275,7 @@ function hash(o::PyObject)
265
275
# since on 64-bit Windows the Python 2.x hash is only 32 bits
266
276
hashsalt (unsafe_pyjlwrap_to_objref (o. o))
267
277
else
268
- h = ccall ((@pysym :PyObject_Hash ), Py_hash_t, (PyPtr,), o)
278
+ h = @ ccall ((@pysym :PyObject_Hash ), Py_hash_t, (PyPtr,), o)
269
279
if h == - 1 # error
270
280
pyerr_clear ()
271
281
return hashsalt (o. o)
@@ -283,7 +293,7 @@ function getindex(o::PyObject, s::AbstractString)
283
293
if ispynull (o)
284
294
throw (ArgumentError (" ref of NULL PyObject" ))
285
295
end
286
- p = ccall ((@pysym :PyObject_GetAttrString ), PyPtr, (PyPtr, Cstring), o, s)
296
+ p = @ ccall ((@pysym :PyObject_GetAttrString ), PyPtr, (PyPtr, Cstring), o, s)
287
297
if p == C_NULL
288
298
pyerr_clear ()
289
299
throw (KeyError (s))
@@ -297,7 +307,7 @@ function setindex!(o::PyObject, v, s::Union{Symbol,AbstractString})
297
307
if ispynull (o)
298
308
throw (ArgumentError (" assign of NULL PyObject" ))
299
309
end
300
- if - 1 == ccall ((@pysym :PyObject_SetAttrString ), Cint,
310
+ if - 1 == @ ccall ((@pysym :PyObject_SetAttrString ), Cint,
301
311
(PyPtr, Cstring, PyPtr), o, s, PyObject (v))
302
312
pyerr_clear ()
303
313
throw (KeyError (s))
@@ -309,7 +319,7 @@ function haskey(o::PyObject, s::Union{Symbol,AbstractString})
309
319
if ispynull (o)
310
320
throw (ArgumentError (" haskey of NULL PyObject" ))
311
321
end
312
- return 1 == ccall ((@pysym :PyObject_HasAttrString ), Cint,
322
+ return 1 == @ ccall ((@pysym :PyObject_HasAttrString ), Cint,
313
323
(PyPtr, Cstring), o, s)
314
324
end
315
325
408
418
function _pyimport (name:: AbstractString )
409
419
cookie = ActivatePyActCtx ()
410
420
try
411
- return PyObject (ccall ((@pysym :PyImport_ImportModule ), PyPtr, (Cstring,), name))
421
+ return PyObject (@ ccall ((@pysym :PyImport_ImportModule ), PyPtr, (Cstring,), name))
412
422
finally
413
423
DeactivatePyActCtx (cookie)
414
424
end
@@ -709,7 +719,7 @@ include("pyfncall.jl")
709
719
# for now we can define "get".
710
720
711
721
function get (o:: PyObject , returntype:: TypeTuple , k, default)
712
- r = ccall ((@pysym :PyObject_GetItem ), PyPtr, (PyPtr,PyPtr), o,PyObject (k))
722
+ r = @ ccall ((@pysym :PyObject_GetItem ), PyPtr, (PyPtr,PyPtr), o,PyObject (k))
713
723
if r == C_NULL
714
724
pyerr_clear ()
715
725
default
@@ -719,22 +729,22 @@ function get(o::PyObject, returntype::TypeTuple, k, default)
719
729
end
720
730
721
731
get (o:: PyObject , returntype:: TypeTuple , k) =
722
- convert (returntype, PyObject (@pycheckn ccall ((@pysym :PyObject_GetItem ),
732
+ convert (returntype, PyObject (@pycheckn @ ccall ((@pysym :PyObject_GetItem ),
723
733
PyPtr, (PyPtr,PyPtr), o, PyObject (k))))
724
734
725
735
get (o:: PyObject , k, default) = get (o, PyAny, k, default)
726
736
get (o:: PyObject , k) = get (o, PyAny, k)
727
737
728
738
function delete! (o:: PyObject , k)
729
- e = ccall ((@pysym :PyObject_DelItem ), Cint, (PyPtr, PyPtr), o, PyObject (k))
739
+ e = @ ccall ((@pysym :PyObject_DelItem ), Cint, (PyPtr, PyPtr), o, PyObject (k))
730
740
if e == - 1
731
741
pyerr_clear () # delete! ignores errors in Julia
732
742
end
733
743
return o
734
744
end
735
745
736
746
function set! (o:: PyObject , k, v)
737
- @pycheckz ccall ((@pysym :PyObject_SetItem ), Cint, (PyPtr, PyPtr, PyPtr),
747
+ @pycheckz @ ccall ((@pysym :PyObject_SetItem ), Cint, (PyPtr, PyPtr, PyPtr),
738
748
o, PyObject (k), PyObject (v))
739
749
v
740
750
end
@@ -751,24 +761,24 @@ function ind2py(i::Integer)
751
761
return i- 1
752
762
end
753
763
754
- _getindex (o:: PyObject , i:: Integer , T) = convert (T, PyObject (@pycheckn ccall ((@pysym :PySequence_GetItem ), PyPtr, (PyPtr, Int), o, ind2py (i))))
764
+ _getindex (o:: PyObject , i:: Integer , T) = convert (T, PyObject (@pycheckn @ ccall ((@pysym :PySequence_GetItem ), PyPtr, (PyPtr, Int), o, ind2py (i))))
755
765
getindex (o:: PyObject , i:: Integer ) = _getindex (o, i, PyAny)
756
766
function setindex! (o:: PyObject , v, i:: Integer )
757
- @pycheckz ccall ((@pysym :PySequence_SetItem ), Cint, (PyPtr, Int, PyPtr), o, ind2py (i), PyObject (v))
767
+ @pycheckz @ ccall ((@pysym :PySequence_SetItem ), Cint, (PyPtr, Int, PyPtr), o, ind2py (i), PyObject (v))
758
768
v
759
769
end
760
770
getindex (o:: PyObject , i1:: Integer , i2:: Integer ) = get (o, (ind2py (i1),ind2py (i2)))
761
771
setindex! (o:: PyObject , v, i1:: Integer , i2:: Integer ) = set! (o, (ind2py (i1),ind2py (i2)), v)
762
772
getindex (o:: PyObject , I:: Integer... ) = get (o, map (ind2py, I))
763
773
setindex! (o:: PyObject , v, I:: Integer... ) = set! (o, map (ind2py, I), v)
764
- length (o:: PyObject ) = @pycheckz ccall ((@pysym :PySequence_Size ), Int, (PyPtr,), o)
774
+ length (o:: PyObject ) = @pycheckz @ ccall ((@pysym :PySequence_Size ), Int, (PyPtr,), o)
765
775
size (o:: PyObject ) = (length (o),)
766
776
firstindex (o:: PyObject ) = 1
767
777
lastindex (o:: PyObject ) = length (o)
768
778
769
779
function splice! (a:: PyObject , i:: Integer )
770
780
v = a[i]
771
- @pycheckz ccall ((@pysym :PySequence_DelItem ), Cint, (PyPtr, Int), a, i- 1 )
781
+ @pycheckz @ ccall ((@pysym :PySequence_DelItem ), Cint, (PyPtr, Int), a, i- 1 )
772
782
v
773
783
end
774
784
@@ -777,20 +787,20 @@ popfirst!(a::PyObject) = splice!(a, 1)
777
787
778
788
function empty! (a:: PyObject )
779
789
for i in length (a): - 1 : 1
780
- @pycheckz ccall ((@pysym :PySequence_DelItem ), Cint, (PyPtr, Int), a, i- 1 )
790
+ @pycheckz @ ccall ((@pysym :PySequence_DelItem ), Cint, (PyPtr, Int), a, i- 1 )
781
791
end
782
792
a
783
793
end
784
794
785
795
# The following operations only work for the list type and subtypes thereof:
786
796
function push! (a:: PyObject , item)
787
- @pycheckz ccall ((@pysym :PyList_Append ), Cint, (PyPtr, PyPtr),
797
+ @pycheckz @ ccall ((@pysym :PyList_Append ), Cint, (PyPtr, PyPtr),
788
798
a, PyObject (item))
789
799
a
790
800
end
791
801
792
802
function insert! (a:: PyObject , i:: Integer , item)
793
- @pycheckz ccall ((@pysym :PyList_Insert ), Cint, (PyPtr, Int, PyPtr),
803
+ @pycheckz @ ccall ((@pysym :PyList_Insert ), Cint, (PyPtr, Int, PyPtr),
794
804
a, ind2py (i), PyObject (item))
795
805
a
796
806
end
@@ -816,7 +826,7 @@ function append!(a::PyObject, items)
816
826
end
817
827
818
828
append! (a:: PyObject , items:: PyObject ) =
819
- PyObject (@pycheckn ccall ((@pysym :PySequence_InPlaceConcat ),
829
+ PyObject (@pycheckn @ ccall ((@pysym :PySequence_InPlaceConcat ),
820
830
PyPtr, (PyPtr, PyPtr), a, items))
821
831
822
832
# ########################################################################
0 commit comments