Skip to content

Commit 6db2d79

Browse files
Ensure pythonx application is started before eval (#22)
1 parent bf63ea0 commit 6db2d79

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

lib/pythonx.ex

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@ defmodule Pythonx do
216216
{Object.t() | nil, %{optional(String.t()) => Object.t()}}
217217
def eval(code, globals, opts \\ [])
218218
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+
219224
opts = Keyword.validate!(opts, [:stdout_device, :stderr_device])
220225

221226
globals =
@@ -244,6 +249,10 @@ defmodule Pythonx do
244249
result
245250
end
246251

252+
defp pythonx_started?() do
253+
Process.whereis(Pythonx.Supervisor) != nil
254+
end
255+
247256
@doc ~S'''
248257
Convenience macro for Python code evaluation.
249258
@@ -313,12 +322,19 @@ defmodule Pythonx do
313322
result
314323
end
315324
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
322338
end
323339

324340
@doc """

0 commit comments

Comments
 (0)