From 07ac194fddc4a219a91c356b78559f9fd42a246c Mon Sep 17 00:00:00 2001 From: Dan Ryan Date: Tue, 13 Mar 2018 16:38:25 -0400 Subject: [PATCH] Fix ppid handling in patched pew._win_utils - Minor fix for #1689 --- pipenv/patched/pew/_win_utils.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/pipenv/patched/pew/_win_utils.py b/pipenv/patched/pew/_win_utils.py index ee39b535..7fbca370 100644 --- a/pipenv/patched/pew/_win_utils.py +++ b/pipenv/patched/pew/_win_utils.py @@ -16,6 +16,7 @@ ERROR_NO_MORE_FILES = 18 INVALID_HANDLE_VALUE = c_void_p(-1).value SHELL_NAMES = ['cmd', 'powershell', 'pwsh', 'cmder'] + class PROCESSENTRY32(Structure): _fields_ = [ ('dwSize', DWORD), @@ -92,7 +93,15 @@ def get_all_processes(): return pids -def get_shell(pid=None, max_depth=4): +def _get_executable(process_dict): + if hasattr(process_dict, 'keys'): + executable = process_dict.get('executable') + if isinstance(executable, six.STRING_TYPES): + return executable.lower().rsplit('.', 1)[0] + return '' + + +def get_shell(pid=None, max_depth=6): """Get the shell that the supplied pid or os.getpid() is running in. """ if not pid: @@ -101,11 +110,11 @@ def get_shell(pid=None, max_depth=4): def check_parent(pid, lvl=0): ppid = processes[pid].get('parent_pid') - if ppid and processes[ppid]['executable'].lower().rsplit('.', 1)[0] in SHELL_NAMES: + if ppid and _get_executable(processes.get(ppid)) in SHELL_NAMES: return processes[ppid]['executable'] if lvl >= max_depth: return return check_parent(ppid, lvl=lvl+1) - if processes[pid]['executable'].lower().rsplit('.', 1)[0] in SHELL_NAMES: + if _get_executable(processes.get([pid])) in SHELL_NAMES: return processes[pid]['executable'] return check_parent(pid)