diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2020-01-31 17:32:55 -0800 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2020-01-31 17:32:55 -0800 |
commit | dc31f33683dc6bf6d4fe0641532a41db9b7bf24d (patch) | |
tree | cf2aed3271f0206025c38fd0642e6052f14a463d | |
parent | 690fea0cf0009cae62307af2904f474f41ea2ba0 (diff) | |
download | psutil-dc31f33683dc6bf6d4fe0641532a41db9b7bf24d.tar.gz |
handle the case where Buffer is NULL
-rw-r--r-- | psutil/_psutil_windows.c | 10 | ||||
-rw-r--r-- | psutil/_pswindows.py | 7 | ||||
-rwxr-xr-x | psutil/tests/test_contracts.py | 2 |
3 files changed, 15 insertions, 4 deletions
diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 7b0f1cce..64b58f4f 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -500,8 +500,14 @@ psutil_proc_exe(PyObject *self, PyObject *args) { return NULL; } - py_exe = PyUnicode_FromWideChar(processIdInfo.ImageName.Buffer, - processIdInfo.ImageName.Length / 2); + if (processIdInfo.ImageName.Buffer == NULL) { + // Happens for PID 4. + py_exe = Py_BuildValue("s", ""); + } + else { + py_exe = PyUnicode_FromWideChar(processIdInfo.ImageName.Buffer, + processIdInfo.ImageName.Length / 2); + } FREE(buffer); return py_exe; } diff --git a/psutil/_pswindows.py b/psutil/_pswindows.py index 34295f9e..83793c5a 100644 --- a/psutil/_pswindows.py +++ b/psutil/_pswindows.py @@ -753,7 +753,11 @@ class Process(object): @memoize_when_activated def exe(self): exe = cext.proc_exe(self.pid) - return convert_dos_path(exe) + if not PY3: + exe = py2_strencode(exe) + if exe.startswith('\\'): + return convert_dos_path(exe) + return exe # May be "Registry", "MemCompression", ... @wrap_exceptions @retry_error_partial_copy @@ -839,7 +843,6 @@ class Process(object): for addr, perm, path, rss in raw: path = convert_dos_path(path) if not PY3: - assert isinstance(path, unicode), type(path) path = py2_strencode(path) addr = hex(addr) yield (addr, perm, path, rss) diff --git a/psutil/tests/test_contracts.py b/psutil/tests/test_contracts.py index bdf055d0..5e258b30 100755 --- a/psutil/tests/test_contracts.py +++ b/psutil/tests/test_contracts.py @@ -431,6 +431,8 @@ class TestFetchAllProcesses(unittest.TestCase): if not ret: self.assertEqual(ret, '') else: + if WINDOWS and not ret.endswith('.exe'): + return # May be "Registry", "MemCompression", ... assert os.path.isabs(ret), ret # Note: os.stat() may return False even if the file is there # hence we skip the test, see: |