Skip to content

Commit

Permalink
Merge pull request #20 from ptr-yudai/winProcessFix
Browse files Browse the repository at this point in the history
Win process fix
  • Loading branch information
ptr-yudai authored Feb 16, 2023
2 parents 3028e81 + 726dd61 commit 413f2a1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions ptrlib/connection/tube.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ def __enter__(self):
def __exit__(self, e_type, e_value, traceback):
self.close()

@abstractmethod
def is_alive(self) -> bool:
pass

Expand Down
12 changes: 12 additions & 0 deletions ptrlib/connection/winproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,18 @@ def _recv(self, size: int, timeout: Optional[Union[int, float]]=None) -> bytes:
buf = self.stdout.recv(size, self.timeout)
return buf

def is_alive(self) -> bool:
"""Check if process is alive
Returns:
bool: True if process is alive, otherwise False
"""
if self.proc is None:
return False
else:
status = win32process.GetExitCodeProcess(self.proc)
return status == win32con.STILL_ACTIVE

def close(self):
if self.proc:
win32api.CloseHandle(self.proc)
Expand Down
9 changes: 7 additions & 2 deletions tests/connection/test_windows_proc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
from ptrlib import Process, is_scanf_safe
from logging import getLogger, FATAL

_is_windows = os.name == 'nt'

class TestProcess(unittest.TestCase):

class TestWinProcess(unittest.TestCase):
def setUp(self):
getLogger("ptrlib").setLevel(FATAL)
if not _is_windows:
self.skipTest("This test is for Windows architecture")

def test_basic(self):
while True:
Expand All @@ -34,8 +38,9 @@ def test_basic(self):
p.shutdown('write')
self.assertEqual(p.recvonce(len(msg)), msg[::-1])


self.assertEqual(p.is_alive(), True)
p.close()
self.assertEqual(p.is_alive(), False)

def test_timeout(self):
p = Process("./tests/test.bin/test_echo.pe.exe")
Expand Down

0 comments on commit 413f2a1

Please sign in to comment.