tests: kill for windows in killdaemons.py checks return values
The return values of the windll calls are checked and when an error is
indicated, it is raised. The handle is still closed properly.
[ original upstream message ]
#!/usr/bin/env pythonimportos,sys,time,errno,signalifos.name=='nt':importctypesdef_check(ret,expectederr=None):ifret==0:winerrno=ctypes.GetLastError()ifwinerrno==expectederr:returnTrueraisectypes.WinError(winerrno)defkill(pid,logfn,tryhard=True):logfn('# Killing daemon process %d'%pid)PROCESS_TERMINATE=1handle=ctypes.windll.kernel32.OpenProcess(PROCESS_TERMINATE,False,pid)ifhandle==0:# TODO: call _check(0, expected) to check if "process not found"return# process not found, already finishedtry:_check(ctypes.windll.kernel32.TerminateProcess(handle,-1),5)# windows error 5 when process does not exist or no access TODOexcept:#re-raisesctypes.windll.kernel32.CloseHandle(handle)# no _check, keep errorraise_check(ctypes.windll.kernel32.CloseHandle(handle))else:defkill(pid,logfn,tryhard=True):try:os.kill(pid,0)logfn('# Killing daemon process %d'%pid)os.kill(pid,signal.SIGTERM)iftryhard:foriinrange(10):time.sleep(0.05)os.kill(pid,0)else:time.sleep(0.1)os.kill(pid,0)logfn('# Daemon process %d is stuck - really killing it'%pid)os.kill(pid,signal.SIGKILL)exceptOSError,err:iferr.errno!=errno.ESRCH:raisedefkilldaemons(pidfile,tryhard=True,remove=False,logfn=None):ifnotlogfn:logfn=lambdas:s# Kill off any leftover daemon processestry:fp=open(pidfile)forlineinfp:try:pid=int(line)exceptValueError:continuekill(pid,logfn,tryhard)fp.close()ifremove:os.unlink(pidfile)exceptIOError:passif__name__=='__main__':path,=sys.argv[1:]killdaemons(path)