diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2017-05-07 15:48:27 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2017-05-07 15:48:27 +0200 |
commit | 46399478463f2a16eb2c611ba74489682be95e1a (patch) | |
tree | a5dc87b318c41c62f1520d36c777457e742b856d | |
parent | ae5c4378b7a441cd2ed8cb5ab795669206b5c7c4 (diff) | |
download | psutil-46399478463f2a16eb2c611ba74489682be95e1a.tar.gz |
prevent windows tests to open error dialogs/windows
-rw-r--r-- | psutil/tests/__init__.py | 23 | ||||
-rwxr-xr-x | psutil/tests/test_process.py | 5 |
2 files changed, 22 insertions, 6 deletions
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py index 0cf64227..8bdd9dd6 100644 --- a/psutil/tests/__init__.py +++ b/psutil/tests/__init__.py @@ -233,6 +233,9 @@ def get_test_subprocess(cmd=None, **kwds): """ kwds.setdefault("stdin", DEVNULL) kwds.setdefault("stdout", DEVNULL) + if WINDOWS: + # Prevents the subprocess to open error dialogs. + kwds.setdefault("creationflags", 0x8000000) # CREATE_NO_WINDOW if cmd is None: safe_rmpath(_TESTFN) pyline = "from time import sleep;" @@ -272,7 +275,13 @@ def create_proc_children_pair(): subprocess.Popen([PYTHON, '-c', s]) time.sleep(60) """ % _TESTFN2) - subp = pyrun(s) + # On Windows if we create a subprocess with CREATE_NO_WINDOW flag + # set (which is the default) a "conhost.exe" extra process will be + # spawned as a child. We don't want that. + if WINDOWS: + subp = pyrun(s, creationflags=0) + else: + subp = pyrun(s) try: child1 = psutil.Process(subp.pid) data = wait_for_file(_TESTFN2, delete=False, empty=False) @@ -286,17 +295,18 @@ def create_proc_children_pair(): raise -def pyrun(src): +def pyrun(src, **kwds): """Run python 'src' code string in a separate interpreter. Returns a subprocess.Popen instance. """ + kwds.setdefault("stdout", None) + kwds.setdefault("stderr", None) with tempfile.NamedTemporaryFile( prefix=TESTFILE_PREFIX, mode="wt", delete=False) as f: _testfiles_created.add(f.name) f.write(src) f.flush() - subp = get_test_subprocess([PYTHON, f.name], stdout=None, - stderr=None) + subp = get_test_subprocess([PYTHON, f.name], **kwds) wait_for_pid(subp.pid) return subp @@ -306,8 +316,11 @@ def sh(cmd): raises RuntimeError on error. """ shell = True if isinstance(cmd, (str, unicode)) else False + # Prevents subprocess to open error dialogs in case of error. + flags = 0x8000000 if WINDOWS and shell else 0 p = subprocess.Popen(cmd, shell=shell, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, universal_newlines=True) + stderr=subprocess.PIPE, universal_newlines=True, + creationflags=flags) stdout, stderr = p.communicate() if p.returncode != 0: raise RuntimeError(stderr) diff --git a/psutil/tests/test_process.py b/psutil/tests/test_process.py index d1cb9657..b6c41899 100755 --- a/psutil/tests/test_process.py +++ b/psutil/tests/test_process.py @@ -1037,7 +1037,10 @@ class TestProcess(unittest.TestCase): p = psutil.Process() self.assertEqual(p.children(), []) self.assertEqual(p.children(recursive=True), []) - sproc = get_test_subprocess() + # On Windows we set the flag to 0 in order to cancel out the + # CREATE_NO_WINDOW flag (enabled by default) which creates + # an extra "conhost.exe" child. + sproc = get_test_subprocess(creationflags=0) children1 = p.children() children2 = p.children(recursive=True) for children in (children1, children2): |