@@ -216,6 +216,11 @@ defmodule Pythonx do
216
216
{ Object . t ( ) | nil , % { optional ( String . t ( ) ) => Object . t ( ) } }
217
217
def eval ( code , globals , opts \\ [ ] )
218
218
when is_binary ( code ) and is_map ( globals ) and is_list ( opts ) do
219
+ if not pythonx_started? ( ) do
220
+ raise RuntimeError ,
221
+ "the :pythonx application needs to be started before calling Pythonx.eval/3"
222
+ end
223
+
219
224
opts = Keyword . validate! ( opts , [ :stdout_device , :stderr_device ] )
220
225
221
226
globals =
@@ -244,6 +249,10 @@ defmodule Pythonx do
244
249
result
245
250
end
246
251
252
+ defp pythonx_started? ( ) do
253
+ Process . whereis ( Pythonx.Supervisor ) != nil
254
+ end
255
+
247
256
@ doc ~S'''
248
257
Convenience macro for Python code evaluation.
249
258
@@ -313,12 +322,19 @@ defmodule Pythonx do
313
322
result
314
323
end
315
324
rescue
316
- RuntimeError ->
317
- raise RuntimeError ,
318
- "using ~PY sigil requires the Python interpreter to be already initialized. " <>
319
- "This sigil is designed for dynamic evaluation environments, such as IEx or Livebook. " <>
320
- "If that is your case, make sure you initialized the interpreter first, otherwise " <>
321
- "use Pythonx.eval/2 instead. For more details see Pythonx.sigil_PY/2 docs"
325
+ error in RuntimeError ->
326
+ message = Exception . message ( error )
327
+
328
+ if message =~ "has not been initialized" do
329
+ raise RuntimeError ,
330
+ Exception . message ( error ) <>
331
+ "using ~PY sigil requires the Python interpreter to be already initialized. " <>
332
+ "This sigil is designed for dynamic evaluation environments, such as IEx or Livebook. " <>
333
+ "If that is your case, make sure you initialized the interpreter first, otherwise " <>
334
+ "use Pythonx.eval/2 instead. For more details see Pythonx.sigil_PY/2 docs"
335
+ else
336
+ reraise ( error , __STACKTRACE__ )
337
+ end
322
338
end
323
339
324
340
@ doc """
0 commit comments