Skip to content

Commit b642a5d

Browse files
committed
Support Windows process
1 parent 68a0975 commit b642a5d

8 files changed

Lines changed: 616 additions & 33 deletions

File tree

ptrlib/connection/sock.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,9 @@ def _is_alive_impl(self) -> bool:
300300
try:
301301
self._sock.setblocking(False)
302302
return self._sock.recv(1, socket.MSG_PEEK) == 1
303-
except (BlockingIOError, ValueError):
303+
except (BlockingIOError, ValueError, BrokenPipeError):
304304
# SSLSocket may raise ValueError but we treat it as alive
305+
# BrokenPipeError may happen when recv connection is closed
305306
return True
306307
except (ConnectionResetError, socket.timeout):
307308
return False

ptrlib/connection/ssh.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"""
33
import shlex
44
import os
5-
from typing import Optional
65
from ptrlib.arch.common import which
76
from .proc import Process
87

ptrlib/connection/tube.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
- Abstract methods for transport-specific implementations.
1414
"""
1515
import abc
16-
import code
1716
import contextlib
1817
import io
1918
import os
@@ -105,8 +104,8 @@ def _log_error(self, message: str, *args, stacklevel: int = 2):
105104
# --- Properties -------------------------------------------------------
106105

107106
@property
108-
def newline(self) -> list[bytes]:
109-
"""List of byte sequences considered as newline terminators.
107+
def newline(self) -> bytes:
108+
"""A byte sequence considered as newline terminators.
110109
111110
Examples:
112111
```
@@ -116,6 +115,20 @@ def newline(self) -> list[bytes]:
116115
sock.newline = "\\r\\n"
117116
```
118117
"""
118+
return self._newline[0]
119+
120+
@property
121+
def newlines(self) -> list[bytes]:
122+
"""List of byte sequences considered as newline terminators.
123+
124+
Examples:
125+
```
126+
p = Process(["wine", "a.exe"])
127+
p.newline = [b"\\n", "\\r\\n"]
128+
p.newline # b"\n"
129+
p.newlines # b"\r\n"
130+
```
131+
"""
119132
return self._newline
120133

121134
@newline.setter
@@ -302,7 +315,7 @@ def recvline(self,
302315
OSError: If a system error occurred.
303316
"""
304317
return self.recvuntil(
305-
delim=self._newline,
318+
delim=self.newlines,
306319
blocksize=blocksize,
307320
regex=None,
308321
timeout=timeout,
@@ -583,7 +596,7 @@ def sendline(self,
583596
data = str(data)
584597

585598
data = str2bytes(data)
586-
return self.sendall(data + self.newline[0])
599+
return self.sendall(data + self.newline)
587600

588601
def sendkey(self):
589602
pass
@@ -1098,7 +1111,7 @@ class TubeTimeout(TimeoutError):
10981111
"""
10991112
def __init__(self, message: str, *, buffered: bytes = b''):
11001113
super().__init__(message)
1101-
self.buffered = buffered
1114+
self.buffered = bytes(buffered)
11021115

11031116
def __bytes__(self) -> bytes:
11041117
return self.buffered

ptrlib/connection/unixproc.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,6 @@ def __str__(self) -> str:
9090
@property
9191
def returncode(self) -> int:
9292
"""Get the return code of the spawned process.
93-
94-
Raises:
95-
ChildProcessError: If the process is still running.
9693
"""
9794
return self.wait()
9895

@@ -286,8 +283,10 @@ def wait(self, timeout: float | None = None) -> int:
286283
if self._proc is None:
287284
return 0
288285
code = self._proc.wait(timeout)
289-
self._is_alive = False
290-
self._log_info(f"Process {str(self)} stopped with exit code {code}")
286+
287+
if self._is_alive:
288+
self._is_alive = False
289+
self._log_info(f"Process {str(self)} stopped with exit code {code}")
291290
return code
292291

293292
def kill(self,

0 commit comments

Comments
 (0)