Merge pull request #1721 from pypa/bugfix/pew-win-processes

Fix ppid handling in patched pew._win_utils
This commit is contained in:
2018-03-13 17:49:21 -04:00
committed by GitHub
+12 -3
View File
@@ -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)