-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hooks: Force all unused subprocess pipes to DEVNULL in windowed mode. (…
…#6364) A windowed app's stdin, stdout and stderr are not valid pipes. Invoking subprocess.run() (or any of its siblings) without explicitly setting all three pipes to either subprocess.PIPE or subprocess.DEVNULL causes pipe inheritance where subprocess will attempt to use the invalid parent process's pipe. This causes an error. Add a runtime hook which monkey-patches subprocess.Popen() so that any unused pipes are replaced with DEVNULL.
- Loading branch information
Showing
5 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#----------------------------------------------------------------------------- | ||
# Copyright (c) 2021, PyInstaller Development Team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# | ||
# The full license is in the file COPYING.txt, distributed with this software. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
#----------------------------------------------------------------------------- | ||
|
||
import subprocess | ||
import sys | ||
import io | ||
|
||
|
||
class Popen(subprocess.Popen): | ||
|
||
# In windowed mode, force any unused pipes (stdin, stdout and stderr) to be DEVNULL instead of inheriting the | ||
# invalid corresponding handles from this parent process. | ||
if sys.platform == "win32" and not isinstance(sys.stdout, io.IOBase): | ||
|
||
def _get_handles(self, stdin, stdout, stderr): | ||
stdin, stdout, stderr = (subprocess.DEVNULL if pipe is None else pipe for pipe in (stdin, stdout, stderr)) | ||
return super()._get_handles(stdin, stdout, stderr) | ||
|
||
|
||
subprocess.Popen = Popen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Windows: Prevent invalid handle errors when an application compiled in :option:`--windowed` mode uses :mod:`subprocess` | ||
without explicitly setting **stdin**, **stdout** and **stderr** to either :data:`~subprocess.PIPE` or | ||
:data:`~subprocess.DEVNULL`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters