88from subprocess import PIPE
99from subprocess import Popen
1010
11- from py ._path .local import LocalPath
12-
13-
1411# these signals cause dumb-init to suspend itself
1512SUSPEND_SIGNALS = frozenset ([
1613 signal .SIGTSTP ,
@@ -49,9 +46,10 @@ def print_signals(args=()):
4946def child_pids (pid ):
5047 """Return a list of direct child PIDs for the given PID."""
5148 children = set ()
52- for p in LocalPath ('/proc' ). listdir ( ):
49+ for p in os . listdir ('/proc' ):
5350 try :
54- stat = open (p .join ('stat' ).strpath ).read ()
51+ with open (os .path .join ('/proc' , p , 'stat' )) as f :
52+ stat = f .read ()
5553 m = re .match (
5654 r'^\d+ \(.+?\) '
5755 # This field, state, is normally a single letter, but can be
@@ -65,7 +63,7 @@ def child_pids(pid):
6563 assert m , stat
6664 ppid = int (m .group (1 ))
6765 if ppid == pid :
68- children .add (int (p . basename ))
66+ children .add (int (p ))
6967 except OSError :
7068 # Happens when the process exits after listing it, or between
7169 # opening stat and reading it.
@@ -85,12 +83,13 @@ def pid_tree(pid):
8583
8684def is_alive (pid ):
8785 """Return whether a process is running with the given PID."""
88- return LocalPath ( '/proc' ). join ( str (pid )). isdir ( )
86+ return os . path . isdir ( os . path . join ( '/proc' , str (pid )))
8987
9088
9189def process_state (pid ):
9290 """Return a process' state, such as "stopped" or "running"."""
93- status = LocalPath ('/proc' ).join (str (pid ), 'status' ).read ()
91+ with open (os .path .join ('/proc' , str (pid ), 'status' )) as f :
92+ status = f .read ()
9493 m = re .search (r'^State:\s+[A-Z] \(([a-z]+)\)$' , status , re .MULTILINE )
9594 return m .group (1 )
9695
0 commit comments