Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Improved TTFX in JuliaCall.
* Minimum supported Python version is now 3.11.
* JuliaCall option `compiled_modules` now accepts `existing` and `strict` (Julia 1.11+).
* Fixed a crash when Julia prints while the GIL is unlocked (such as in `_jl_call_nogil`)
with the JuliaCall IPython extension loaded in Jupyter.

## 0.9.37 (2026-10-12)
* Compatibility fix with Python 3.15 from conda-forge, which no longer bundles libpython.
Expand Down
11 changes: 9 additions & 2 deletions pysrc/juliacall/ipython.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,15 @@ def load_ipython_extension(ip):
const _redirected_stderr = redirect_stderr()
const _py_stdout = PyIO(pyimport("sys" => "stdout"); line_buffering=true)
const _py_stderr = PyIO(pyimport("sys" => "stderr"); line_buffering=true)
const _redirect_stdout_task = @async write($_py_stdout, $_redirected_stdout[1])
const _redirect_stderr_task = @async write($_py_stderr, $_redirected_stderr[1])
# These tasks can run while the GIL is unlocked, such as during _jl_call_nogil.
function _copy_to_python(py, pipe)
while !eof(pipe)
data = readavailable(pipe)
PythonCall.GIL.@lock write(py, data)
end
end
const _redirect_stdout_task = @async _copy_to_python(_py_stdout, _redirected_stdout[1])
const _redirect_stderr_task = @async _copy_to_python(_py_stderr, _redirected_stderr[1])
function _flush_stdio()
flush(stderr)
flush(stdout)
Expand Down
33 changes: 33 additions & 0 deletions pytest/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,39 @@ def test_call_nogil(yld, raw):
assert 0.9 < t2 < 1.5


def test_ipython_output_nogil():
"""Tests that Julia output under the IPython extension is safe while the GIL is unlocked."""
import subprocess
import sys

pytest.importorskip("IPython")
code = """
import io, os, sys
from IPython.core.interactiveshell import InteractiveShell
from juliacall import Main as jl, PythonCall
import juliacall.ipython
# Julia redirects file descriptors 1 and 2, so keep copies to report through.
out = os.fdopen(os.dup(1), "w")
err = os.fdopen(os.dup(2), "w")
# Like ipykernel, sys.stdout and sys.stderr are not file descriptors 1 and 2.
sys.stdout, sys.stderr = io.StringIO(), io.StringIO()
class KernelShell(InteractiveShell):
pass
juliacall.ipython.load_ipython_extension(KernelShell.instance())
try:
jl.seval('() -> (println("hello"); sleep(0.1))')._jl_call_nogil()
PythonCall._ipython._flush_stdio()
finally:
err.write(sys.stderr.getvalue())
out.write(sys.stdout.getvalue())
"""
result = subprocess.run(
[sys.executable, "-c", code], capture_output=True, text=True, timeout=180
)
assert result.returncode == 0, result.stderr
assert result.stdout == "hello\n"


@pytest.mark.parametrize("mode,value", [("existing", 2), ("strict", 3)])
def test_compiled_modules_option(mode, value):
import os
Expand Down
Loading