@@ -145,63 +145,61 @@ function init_context()
145
145
@require PyCall = " 438e738f-606a-5dbb-bf0a-cddfbfd45ab0" init_pycall (PyCall)
146
146
147
147
# Initialize the interpreter
148
- with_gil () do
149
- CTX. is_preinitialized = Py_IsInitialized () != 0
150
- if CTX. is_preinitialized
151
- @assert CTX. which == :PyCall || CTX. matches_pycall isa Bool
148
+ CTX. is_preinitialized = Py_IsInitialized () != 0
149
+ if CTX. is_preinitialized
150
+ @assert CTX. which == :PyCall || CTX. matches_pycall isa Bool
151
+ else
152
+ @assert CTX. which != :PyCall
153
+ # Find ProgramName and PythonHome
154
+ script = if Sys. iswindows ()
155
+ """
156
+ import sys
157
+ print(sys.executable)
158
+ if hasattr(sys, "base_exec_prefix"):
159
+ sys.stdout.write(sys.base_exec_prefix)
160
+ else:
161
+ sys.stdout.write(sys.exec_prefix)
162
+ """
152
163
else
153
- @assert CTX. which != :PyCall
154
- # Find ProgramName and PythonHome
155
- script = if Sys. iswindows ()
156
- """
157
- import sys
158
- print(sys.executable)
159
- if hasattr(sys, "base_exec_prefix"):
160
- sys.stdout.write(sys.base_exec_prefix)
161
- else:
162
- sys.stdout.write(sys.exec_prefix)
163
- """
164
- else
165
- """
166
- import sys
167
- print(sys.executable)
168
- if hasattr(sys, "base_exec_prefix"):
169
- sys.stdout.write(sys.base_prefix)
170
- sys.stdout.write(":")
171
- sys.stdout.write(sys.base_exec_prefix)
172
- else:
173
- sys.stdout.write(sys.prefix)
174
- sys.stdout.write(":")
175
- sys.stdout.write(sys.exec_prefix)
176
- """
177
- end
178
- CTX. pyprogname, CTX. pyhome = readlines (python_cmd ([" -c" , script]))
164
+ """
165
+ import sys
166
+ print(sys.executable)
167
+ if hasattr(sys, "base_exec_prefix"):
168
+ sys.stdout.write(sys.base_prefix)
169
+ sys.stdout.write(":")
170
+ sys.stdout.write(sys.base_exec_prefix)
171
+ else:
172
+ sys.stdout.write(sys.prefix)
173
+ sys.stdout.write(":")
174
+ sys.stdout.write(sys.exec_prefix)
175
+ """
176
+ end
177
+ CTX. pyprogname, CTX. pyhome = readlines (python_cmd ([" -c" , script]))
179
178
180
- # Set PythonHome
181
- CTX. pyhome_w = Base. cconvert (Cwstring, CTX. pyhome)
182
- Py_SetPythonHome (pointer (CTX. pyhome_w))
179
+ # Set PythonHome
180
+ CTX. pyhome_w = Base. cconvert (Cwstring, CTX. pyhome)
181
+ Py_SetPythonHome (pointer (CTX. pyhome_w))
183
182
184
- # Set ProgramName
185
- CTX. pyprogname_w = Base. cconvert (Cwstring, CTX. pyprogname)
186
- Py_SetProgramName (pointer (CTX. pyprogname_w))
183
+ # Set ProgramName
184
+ CTX. pyprogname_w = Base. cconvert (Cwstring, CTX. pyprogname)
185
+ Py_SetProgramName (pointer (CTX. pyprogname_w))
187
186
188
- # Start the interpreter and register exit hooks
189
- Py_InitializeEx (0 )
190
- atexit () do
191
- CTX. is_initialized = false
192
- if CTX. version === missing || CTX. version < v " 3.6"
193
- Py_Finalize ()
194
- else
195
- if Py_FinalizeEx () == - 1
196
- @warn " Py_FinalizeEx() error"
197
- end
187
+ # Start the interpreter and register exit hooks
188
+ Py_InitializeEx (0 )
189
+ atexit () do
190
+ CTX. is_initialized = false
191
+ if CTX. version === missing || CTX. version < v " 3.6"
192
+ Py_Finalize ()
193
+ else
194
+ if Py_FinalizeEx () == - 1
195
+ @warn " Py_FinalizeEx() error"
198
196
end
199
197
end
200
198
end
201
- CTX . is_initialized = true
202
- if Py_AtExit ( @cfunction (_atpyexit, Cvoid, ())) == - 1
203
- @warn " Py_AtExit() error "
204
- end
199
+ end
200
+ CTX . is_initialized = true
201
+ if Py_AtExit ( @cfunction (_atpyexit, Cvoid, ())) == - 1
202
+ @warn " Py_AtExit() error "
205
203
end
206
204
end
207
205
@@ -218,20 +216,16 @@ function init_context()
218
216
ENV [" JULIA_PYTHONCALL_EXE" ] = CTX. exe_path:: String
219
217
end
220
218
221
- with_gil () do
222
-
223
- # Get the python version
224
- verstr = Base. unsafe_string (Py_GetVersion ())
225
- vermatch = match (r" ^[0-9.]+" , verstr)
226
- if vermatch === nothing
227
- error (" Cannot parse version from version string: $(repr (verstr)) " )
228
- end
229
- CTX. version = VersionNumber (vermatch. match)
230
- v " 3.5" ≤ CTX. version < v " 4" || error (
231
- " Only Python 3.5+ is supported, this is Python $(CTX. version) at $(CTX. exe_path=== missing ? " unknown location" : CTX. exe_path) ." ,
232
- )
233
-
219
+ # Get the python version
220
+ verstr = Base. unsafe_string (Py_GetVersion ())
221
+ vermatch = match (r" ^[0-9.]+" , verstr)
222
+ if vermatch === nothing
223
+ error (" Cannot parse version from version string: $(repr (verstr)) " )
234
224
end
225
+ CTX. version = VersionNumber (vermatch. match)
226
+ v " 3.5" ≤ CTX. version < v " 4" || error (
227
+ " Only Python 3.5+ is supported, this is Python $(CTX. version) at $(CTX. exe_path=== missing ? " unknown location" : CTX. exe_path) ." ,
228
+ )
235
229
236
230
@debug " Initialized PythonCall.jl" CTX. is_embedded CTX. is_initialized CTX. exe_path CTX. lib_path CTX. lib_ptr CTX. pyprogname CTX. pyhome CTX. version
237
231
0 commit comments