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
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
### 3.8.0 - In development

- ``loky.backend.spawn.get_executable`` now returns ``bytes`` on POSIX (and
``str`` on Windows), consistently with ``multiprocessing.spawn``, and a
matching ``set_executable`` helper is available. (#655)

- Fix ``Popen._launch`` on POSIX raising ``UnboundLocalError`` instead of the
real ``OSError`` when ``os.pipe()`` fails, for instance when the parent process
runs out of file descriptors (``EMFILE``). (#668)
Expand Down
4 changes: 0 additions & 4 deletions loky/backend/resource_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,6 @@ def spawnv_passfds(path, args, passfds):
On Linux process_handle is None.
"""
if sys.platform != "win32":
# loky: additional encoding needed here
# TODO We should fix loky.backend.spawn.get_executable to return bytes
# on POSIX so that this can be removed
path = path.encode("utf-8")
return util.spawnv_passfds(path, args, passfds), None
else:
# loky: Windows support
Expand Down
24 changes: 20 additions & 4 deletions loky/backend/spawn.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,32 @@
WINEXE = sys.platform == "win32" and getattr(sys, "frozen", False)
WINSERVICE = sys.executable.lower().endswith("pythonservice.exe")

if WINSERVICE:
_python_exe = os.path.join(sys.exec_prefix, "python.exe")
else:
_python_exe = sys.executable

def set_executable(exe):
"""Set the path to the Python interpreter used to start child processes.

As in the standard library ``multiprocessing.spawn``, the path is stored
as ``bytes`` on POSIX and as ``str`` on Windows.
"""
global _python_exe
if exe is None:
_python_exe = exe
elif sys.platform == "win32":
_python_exe = os.fsdecode(exe)
else:
_python_exe = os.fsencode(exe)


def get_executable():
return _python_exe


if WINSERVICE:
set_executable(os.path.join(sys.exec_prefix, "python.exe"))
else:
set_executable(sys.executable)


def _check_not_importing_main():
if getattr(process.current_process(), "_inheriting", False):
raise RuntimeError(
Expand Down
26 changes: 26 additions & 0 deletions tests/test_loky_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,3 +805,29 @@ def pipe():
with pytest.raises(OSError) as excinfo:
p.start()
assert excinfo.value.errno == errno.EMFILE


def test_get_executable_type():
# Consistently with multiprocessing.spawn, the executable is stored as
# bytes on POSIX and as str on Windows (#655).
from loky.backend import spawn

exe = spawn.get_executable()
if sys.platform == "win32":
assert isinstance(exe, str)
else:
assert isinstance(exe, bytes)
assert exe == os.fsencode(sys.executable)


def test_set_executable_roundtrip():
from loky.backend import spawn

old_exe = spawn.get_executable()
try:
spawn.set_executable(os.fsdecode(old_exe))
assert spawn.get_executable() == old_exe
spawn.set_executable(os.fsencode(old_exe))
assert spawn.get_executable() == old_exe
finally:
spawn.set_executable(old_exe)
Loading