3535
3636type PyObject
3737 o:: PyPtr # the actual PyObject*
38-
39- # For copy-free wrapping of arrays, the PyObject may reference a
40- # pointer to Julia array data. In this case, we must keep a
41- # reference to the Julia object inside the PyObject so that the
42- # former is not garbage collected before the latter.
43- keep:: Any
44-
45- function PyObject (o:: PyPtr , keep:: Any )
46- po = new (o, keep)
38+ function PyObject (o:: PyPtr )
39+ po = new (o)
4740 finalizer (po, pydecref)
4841 return po
4942 end
5043end
5144
52- PyObject (o:: PyPtr ) = PyObject (o, nothing ) # no Julia object to keep
53-
5445function pydecref (o:: PyObject )
5546 if initialized:: Bool # don't decref after pyfinalize!
5647 ccall (pyfunc (:Py_DecRef ), Void, (PyPtr,), o. o)
5748 end
5849 o. o = C_NULL
59- o. keep = nothing
6050 o
6151end
6252
@@ -99,6 +89,10 @@ MethodWrapperType = PyObject(C_NULL)
9989# special function type used in NumPy and SciPy (if available)
10090ufuncType = PyObject (C_NULL )
10191
92+ # cache Python None and PyNoneType
93+ pynothing = PyObject (C_NULL )
94+ PyNoneType = PyObject (C_NULL )
95+
10296# Py_SetProgramName needs its argument to persist as long as Python does
10397pyprogramname = bytestring (" " )
10498
@@ -116,6 +110,8 @@ function pyinitialize(libpy::Ptr{Void})
116110 global MethodType
117111 global MethodWrapperType
118112 global ufuncType
113+ global pynothing
114+ global PyNoneType
119115 if ! initialized:: Bool
120116 if finalized:: Bool
121117 # From the Py_Finalize documentation:
@@ -147,6 +143,8 @@ function pyinitialize(libpy::Ptr{Void})
147143 catch
148144 ufuncType = PyObject (C_NULL ) # NumPy not available
149145 end
146+ pynothing = pybuiltin (" None" )
147+ PyNoneType = types[" NoneType" ]
150148 end
151149 return
152150end
@@ -185,14 +183,20 @@ function pyfinalize()
185183 global TypeType
186184 global MethodType
187185 global MethodWrapperType
186+ global ufuncType
187+ global pynothing
188+ global PyNoneType
188189 if initialized:: Bool
189- npyfinalize ()
190190 pydecref (ufuncType)
191+ npyfinalize ()
192+ pydecref (PyNoneType)
193+ pydecref (pynothing)
191194 pydecref (BuiltinFunctionType:: PyObject )
192195 pydecref (TypeType:: PyObject )
193196 pydecref (MethodType:: PyObject )
194197 pydecref (MethodWrapperType:: PyObject )
195198 pydecref (inspect:: PyObject )
199+ pygc_finalize ()
196200 gc () # collect/decref any remaining PyObjects
197201 ccall (pyfunc (:Py_Finalize ), Void, ())
198202 dlclose (libpython:: Ptr{Void} )
285289
286290# ########################################################################
287291
292+ include (" gc.jl" )
293+
294+ # make a PyObject that embeds a reference to keep, to prevent Julia
295+ # from garbage-collecting keep until o is finalized.
296+ PyObject (o:: PyPtr , keep:: Any ) = pyembed (PyObject (o), keep)
297+
298+ # ########################################################################
299+
300+ include (" callback.jl" )
301+
288302include (" conversions.jl" )
289303
290304# ########################################################################
0 commit comments