Coverage for src/engines/pexpect_popen.py: 100.00%
25 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-22 10:53 +0000
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-22 10:53 +0000
1#!/usr/bin/env python3
3# Coverage
4# pragma: windows cover file
6# Standard libraries
7from signal import SIGTERM
9# Modules libraries
10from pexpect import EOF, TIMEOUT # pylint: disable=import-error
11from pexpect.popen_spawn import PopenSpawn # pylint: disable=import-error
13# Components
14from .base import Base
16# PexpectPopen class
17class PexpectPopen(Base):
19 # Constructor
20 def __init__(self, command: str) -> None: # pylint: disable=super-init-not-called
22 # Spawn command
23 self._child = PopenSpawn(command)
25 # Read
26 def _read(self) -> bytes | str:
28 # Acquire output, pylint: disable=duplicate-code
29 assert self._child is not None
30 try:
31 result = self._child.read_nonblocking(size=1024, timeout=1)
32 assert isinstance(result, (bytes, str))
33 return result
34 except (EOF, TIMEOUT):
35 pass
37 # Empty output
38 return ''
40 # Is alive
41 def isalive(self) -> bool:
43 # Result
44 assert self._child is not None
45 return not self._child.terminated
47 # Terminate
48 def terminate(self, force: bool = False) -> None:
50 # Terminate process
51 assert self._child is not None
52 if force:
53 try:
54 self._child.kill(sig=SIGTERM)
55 except PermissionError: # pragma: no cover
56 pass
58 # Wait process
59 self._child.wait()