@@ -60,7 +60,7 @@ function init_context()
6060 CTX. which = :CondaPkg
6161 elseif exe_path == " @PyCall"
6262 # PyCall compatibility mode
63- PyCall = Base. require (PYCALL_PKGID)
63+ PyCall = Base. require (PYCALL_PKGID):: Module
6464 exe_path = PyCall. python:: String
6565 CTX. lib_path = PyCall. libpython:: String
6666 CTX. which = :PyCall
@@ -86,7 +86,7 @@ function init_context()
8686 setenv (` $(CTX. exe_path) $args ` , env)
8787 end
8888
89- # Find Python library
89+ # Find and open Python library
9090 lib_path = something (
9191 CTX. lib_path=== missing ? nothing : CTX. lib_path,
9292 get (ENV , " JULIA_PYTHONCALL_LIB" , nothing ),
@@ -117,16 +117,22 @@ function init_context()
117117 If you know where the library is, set environment variable 'JULIA_PYTHONCALL_LIB' to its path.
118118 """ )
119119 end
120+
121+ # Close the library when Julia exits
122+ atexit () do
123+ dlclose (CTX. lib_ptr)
124+ end
125+
126+ # Get function pointers from the library
120127 init_pointers ()
121128
122- # Initialize
129+ # Initialize the interpreter
123130 with_gil () do
124131 CTX. is_preinitialized = Py_IsInitialized () != 0
125132 if CTX. is_preinitialized
126- # Already initialized (maybe you're using PyCall as well)
127- @assert CTX. which in (:embedded , :PyCall )
133+ @assert CTX. which == :PyCall
128134 else
129- @assert CTX. which in ( :unknown , :CondaPkg )
135+ @assert CTX. which != :PyCall
130136 # Find ProgramName and PythonHome
131137 script = if Sys. iswindows ()
132138 """
@@ -178,34 +184,12 @@ function init_context()
178184 if Py_AtExit (@cfunction (_atpyexit, Cvoid, ())) == - 1
179185 @warn " Py_AtExit() error"
180186 end
181- if CTX. which != :embedded
182- atexit () do
183- dlclose (CTX. lib_ptr)
184- end
185- end
186187 end
187188 end
188189
189190 # Compare libpath with PyCall
190191 @require PyCall= " 438e738f-606a-5dbb-bf0a-cddfbfd45ab0" init_pycall (PyCall)
191192
192- # C.PyObject_TryConvert_AddRule("builtins.object", PyObject, CTryConvertRule_wrapref, -100)
193- # C.PyObject_TryConvert_AddRule("builtins.object", PyRef, CTryConvertRule_wrapref, -200)
194- # C.PyObject_TryConvert_AddRule("collections.abc.Sequence", PyList, CTryConvertRule_wrapref, 100)
195- # C.PyObject_TryConvert_AddRule("collections.abc.Set", PySet, CTryConvertRule_wrapref, 100)
196- # C.PyObject_TryConvert_AddRule("collections.abc.Mapping", PyDict, CTryConvertRule_wrapref, 100)
197- # C.PyObject_TryConvert_AddRule("_io._IOBase", PyIO, CTryConvertRule_trywrapref, 100)
198- # C.PyObject_TryConvert_AddRule("io.IOBase", PyIO, CTryConvertRule_trywrapref, 100)
199- # C.PyObject_TryConvert_AddRule("<buffer>", PyArray, CTryConvertRule_trywrapref, 200)
200- # C.PyObject_TryConvert_AddRule("<buffer>", Array, CTryConvertRule_PyArray_tryconvert, 0)
201- # C.PyObject_TryConvert_AddRule("<buffer>", PyBuffer, CTryConvertRule_wrapref, -200)
202- # C.PyObject_TryConvert_AddRule("<arrayinterface>", PyArray, CTryConvertRule_trywrapref, 200)
203- # C.PyObject_TryConvert_AddRule("<arrayinterface>", Array, CTryConvertRule_PyArray_tryconvert, 0)
204- # C.PyObject_TryConvert_AddRule("<arraystruct>", PyArray, CTryConvertRule_trywrapref, 200)
205- # C.PyObject_TryConvert_AddRule("<arraystruct>", Array, CTryConvertRule_PyArray_tryconvert, 0)
206- # C.PyObject_TryConvert_AddRule("<array>", PyArray, CTryConvertRule_trywrapref, 0)
207- # C.PyObject_TryConvert_AddRule("<array>", Array, CTryConvertRule_PyArray_tryconvert, 0)
208-
209193 with_gil () do
210194
211195 # Get the python version
@@ -215,8 +199,8 @@ function init_context()
215199 error (" Cannot parse version from version string: $(repr (verstr)) " )
216200 end
217201 CTX. version = VersionNumber (vermatch. match)
218- v " 3" ≤ CTX. version < v " 4" || error (
219- " Only Python 3 is supported, this is Python $(CTX. version) at $(CTX. exe_path=== missing ? " unknown location" : CTX. exe_path) ." ,
202+ v " 3.5 " ≤ CTX. version < v " 4" || error (
203+ " Only Python 3.5+ is supported, this is Python $(CTX. version) at $(CTX. exe_path=== missing ? " unknown location" : CTX. exe_path) ." ,
220204 )
221205
222206 end
0 commit comments