bpo-46720: Add support for path-like objects to multiprocessing.set_executable for Windows to be on a par with Unix-like systems #31279
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
On Unix-like systems, the function
multiprocessing.set_executableaccepts any path-like objects (str,bytes, andos.PathLike) as itsexecutableargument for setting up the path to the Python interpretermultiprocessing.spawn._python_exe.For instance these work (tested on MacOS with all start methods: ‘spawn’, ‘fork’, and ‘forkserver’):
multiprocessing.set_executable(sys.executable)(str);multiprocessing.set_executable(sys.executable.encode())(bytes);multiprocessing.set_executable(pathlib.Path(sys.executable))(os.PathLike).This is because
the ‘fork’ start method does not execute any program in the child process;
the ‘spawn’ start method converts
multiprocessing.spawn._python_exetobyteswithos.fsencodebefore passing it to the function_posixsubprocess.fork_execto create a child process, andbytesis the expectedargvargument type:the ‘forkserver’ start method spawns a server process (like with the ‘spawn’ start method) which then forks itself at each request (like the ‘fork’ start method).
But on Windows, the ‘spawn’ start method (the only start method available on this O.S.) does not convert
multiprocessing.spawn._python_exetostrwithos.fsdecodebefore passing it to the function_winapi.CreateProcessto create a child process, whereasstris the expectedapplication_nameargument type:So on Windows the function
multiprocessing.set_executableaccepts onlystrobjects as itsexecutableargument. This pull request fixes this to be on a par with Unix-like systems for which the function accepts any path-like objects.https://bugs.python.org/issue46720