@@ -63,17 +63,18 @@ def daemonize(self):
6363 # redirect standard file descriptors
6464 sys .stdout .flush ()
6565 sys .stderr .flush ()
66- si = open (self .stdin , 'r' )
67- so = open ( self . stdout , 'a+' )
68- se = open (self .stderr , 'ba+' , 0 )
69- os .dup2 (si .fileno (), sys .stdin .fileno ())
70- os . dup2 ( so . fileno (), sys . stdout . fileno ())
71- os .dup2 (se .fileno (), sys .stderr .fileno ())
66+ with open (self .stdin , 'r' ) as sti :
67+ os . dup2 ( sti . fileno (), sys . stdin . fileno () )
68+ with open (self .stdout , 'a+' ) as sto :
69+ os .dup2 (sto .fileno (), sys .stdout .fileno ())
70+ with open ( self . stderr , 'ba+' , 0 ) as ste :
71+ os .dup2 (ste .fileno (), sys .stderr .fileno ())
7272
7373 # write pidfile
7474 atexit .register (self .delpid )
7575 pid = str (os .getpid ())
76- open (self .pidfile , 'w+' ).write ("%s\n " % pid )
76+ with open (self .pidfile , 'w+' ) as pidf :
77+ pidf .write ("%s\n " % pid )
7778
7879 def delpid (self ):
7980 os .remove (self .pidfile )
@@ -84,9 +85,8 @@ def start(self):
8485 """
8586 # Check for a pidfile to see if the daemon already runs
8687 try :
87- pf = open (self .pidfile , 'r' )
88- pid = int (pf .read ().strip ())
89- pf .close ()
88+ with open (self .pidfile , 'r' ) as pidf :
89+ pid = int (pidf .read ().strip ())
9090 except IOError :
9191 pid = None
9292
@@ -105,9 +105,8 @@ def stop(self):
105105 """
106106 # Get the pid from the pidfile
107107 try :
108- pf = open (self .pidfile , 'r' )
109- pid = int (pf .read ().strip ())
110- pf .close ()
108+ with open (self .pidfile , 'r' ) as pidf :
109+ pid = int (pidf .read ().strip ())
111110 except IOError :
112111 pid = None
113112
0 commit comments