Skip to content

Commit 459030e

Browse files
committed
Call Julia(init_julia=False) automatically
1 parent 991ccb3 commit 459030e

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/ipython_jl/ipyext.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
from __future__ import print_function
2+
3+
from contextlib import contextmanager
4+
import os
5+
import sys
6+
import traceback
7+
18
from .core import get_cached_api
29

310
try:
@@ -53,6 +60,43 @@ def julia_inputhook(context):
5360
jl_sleep(0.05)
5461

5562

63+
@contextmanager
64+
def init_julia_message_on_failure():
65+
try:
66+
yield
67+
except Exception:
68+
traceback.print_exc()
69+
print(file=sys.stderr)
70+
print("Executing `julia.Julia(init_julia=False)` failed.",
71+
"It is safe to ignore this exception unless you are",
72+
"going to use PyJulia.",
73+
file=sys.stderr)
74+
print("To suppress automatic PyJulia initialization,",
75+
"set environment variable `IPYTHON_JL_SETUP_PYJULIA`",
76+
'to "no".',
77+
file=sys.stderr)
78+
79+
80+
def maybe_load_pyjulia():
81+
"""
82+
Execute ``julia.Julia(init_julia=False)`` if appropriate.
83+
84+
It is useful since it skips initialization when creating the
85+
global "cached" API. This makes PyJuli initialization slightly
86+
faster and also makes sure to not load incompatible `libjulia`
87+
when the name of the julia command of this process is not `julia`.
88+
"""
89+
if (os.environ.get("IPYTHON_JL_SETUP_PYJULIA", "yes").lower()
90+
in ("yes", "t", "true")):
91+
try:
92+
from julia import Julia
93+
except ImportError:
94+
pass
95+
else:
96+
with init_julia_message_on_failure():
97+
Julia(init_julia=False)
98+
99+
56100
def load_ipython_extension(ip):
57101
global _unregister_key_bindings
58102
_unregister_key_bindings = register_key_bindings(ip)
@@ -62,6 +106,8 @@ def load_ipython_extension(ip):
62106
if not ip.active_eventloop:
63107
ip.enable_gui("julia")
64108

109+
maybe_load_pyjulia()
110+
65111
ip.set_hook("complete_command", _julia_completer,
66112
re_key=r""".*\bMain\.eval\(["']""")
67113
# See:

src/ipython_jl/tests/test_ipyext.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from ..ipyext import julia_completer
3+
from ..ipyext import julia_completer, init_julia_message_on_failure
44

55
try:
66
from types import SimpleNamespace
@@ -60,3 +60,18 @@ def test_uncompletable_events(julia, event):
6060

6161
def test_inputhook_registration(ipy_with_magic):
6262
assert ipy_with_magic.active_eventloop == "julia"
63+
64+
65+
def test_init_julia_message_on_failure__with_exception(capsys):
66+
msg = "exception must be captured"
67+
with init_julia_message_on_failure():
68+
raise Exception(msg)
69+
captured = capsys.readouterr()
70+
assert not captured.out
71+
assert msg in captured.err
72+
assert "It is safe to ignore this exception" in captured.err
73+
74+
75+
def test_init_julia_message_on_failure__no_exception():
76+
with init_julia_message_on_failure():
77+
pass

0 commit comments

Comments
 (0)