mirror of
https://github.com/clearml/wexpect-venv
synced 2025-03-13 06:58:10 +00:00
[CLN] clean non_blocking_read
This commit is contained in:
parent
0d645c4ddf
commit
6a9b086d0b
@ -108,6 +108,8 @@ class ConsoleReaderBase:
|
||||
self.console_pid = os.getpid()
|
||||
self.host_pid = host_pid
|
||||
self.host_process = psutil.Process(host_pid)
|
||||
self.child_process = None
|
||||
self.child_pid = None
|
||||
|
||||
logger.info("ConsoleReader started")
|
||||
|
||||
@ -125,8 +127,9 @@ class ConsoleReaderBase:
|
||||
try:
|
||||
self.initConsole()
|
||||
si = win32process.GetStartupInfo()
|
||||
self.__childProcess, _, childPid, self.__tid = win32process.CreateProcess(None, path, None, None, False,
|
||||
self.__childProcess, _, self.child_pid, self.__tid = win32process.CreateProcess(None, path, None, None, False,
|
||||
0, None, None, si)
|
||||
self.child_process = psutil.Process(self.child_pid)
|
||||
|
||||
except:
|
||||
logger.info(traceback.format_exc())
|
||||
@ -141,6 +144,19 @@ class ConsoleReaderBase:
|
||||
logger.error(traceback.format_exc())
|
||||
time.sleep(.1)
|
||||
finally:
|
||||
try:
|
||||
self.terminate_child()
|
||||
time.sleep(.1)
|
||||
self.send_to_host(self.readConsoleToCursor())
|
||||
self.sendeof()
|
||||
time.sleep(.1)
|
||||
self.close_connection()
|
||||
logger.info('Console finished.')
|
||||
except:
|
||||
logger.error(traceback.format_exc())
|
||||
time.sleep(.1)
|
||||
|
||||
|
||||
self.terminate_child()
|
||||
time.sleep(.1)
|
||||
self.send_to_host(self.readConsoleToCursor())
|
||||
|
@ -769,7 +769,8 @@ class SpawnBase:
|
||||
if timeout is not None and end_time < time.time():
|
||||
logger.info('Timeout exceeded in expect_any().')
|
||||
raise TIMEOUT ('Timeout exceeded in expect_any().')
|
||||
# Still have time left, so read more data
|
||||
# Still have time left, so read more data
|
||||
self.isalive()
|
||||
c = self.read_nonblocking(self.maxread)
|
||||
freshlen = len(c)
|
||||
time.sleep (0.01)
|
||||
@ -863,41 +864,34 @@ class SpawnPipe(SpawnBase):
|
||||
if self.closed:
|
||||
logger.warning('I/O operation on closed file in read_nonblocking().')
|
||||
raise ValueError ('I/O operation on closed file in read_nonblocking().')
|
||||
|
||||
|
||||
try:
|
||||
self.isalive()
|
||||
s = win32file.ReadFile(self.pipe, size)[1]
|
||||
|
||||
if s:
|
||||
logger.debug(f'Readed: {s}')
|
||||
else:
|
||||
logger.spam(f'Readed: {s}')
|
||||
|
||||
try:
|
||||
s = win32file.ReadFile(self.pipe, size)[1]
|
||||
if b'\x04' in s:
|
||||
self.flag_eof = True
|
||||
logger.info("EOF: EOF character has been arrived")
|
||||
raise EOF('EOF character has been arrived')
|
||||
|
||||
if s:
|
||||
logger.debug(f'Readed: {s}')
|
||||
else:
|
||||
logger.spam(f'Readed: {s}')
|
||||
|
||||
if b'\x04' in s:
|
||||
self.flag_eof = True
|
||||
logger.info("EOF: EOF character has been arrived")
|
||||
raise EOF('EOF character has been arrived')
|
||||
|
||||
return s.decode()
|
||||
except pywintypes.error as e:
|
||||
if e.args[0] == winerror.ERROR_BROKEN_PIPE: #109
|
||||
self.flag_eof = True
|
||||
logger.info("EOF('broken pipe, bye bye')")
|
||||
raise EOF('broken pipe, bye bye')
|
||||
elif e.args[0] == winerror.ERROR_NO_DATA:
|
||||
'''232 (0xE8)
|
||||
The pipe is being closed.
|
||||
'''
|
||||
self.flag_eof = True
|
||||
logger.info("EOF('The pipe is being closed.')")
|
||||
raise EOF('The pipe is being closed.')
|
||||
else:
|
||||
raise
|
||||
except:
|
||||
raise
|
||||
return ''
|
||||
return s.decode()
|
||||
except pywintypes.error as e:
|
||||
if e.args[0] == winerror.ERROR_BROKEN_PIPE: #109
|
||||
self.flag_eof = True
|
||||
logger.info("EOF('broken pipe, bye bye')")
|
||||
raise EOF('broken pipe, bye bye')
|
||||
elif e.args[0] == winerror.ERROR_NO_DATA:
|
||||
'''232 (0xE8): The pipe is being closed.
|
||||
'''
|
||||
self.flag_eof = True
|
||||
logger.info("EOF('The pipe is being closed.')")
|
||||
raise EOF('The pipe is being closed.')
|
||||
else:
|
||||
raise
|
||||
|
||||
def send(self, s):
|
||||
"""This sends a string to the child process. This returns the number of
|
||||
@ -978,7 +972,6 @@ class SpawnSocket(SpawnBase):
|
||||
raise ValueError ('I/O operation on closed file in read_nonblocking().')
|
||||
|
||||
try:
|
||||
self.isalive()
|
||||
s = self.sock.recv(size)
|
||||
|
||||
if s:
|
||||
@ -991,10 +984,7 @@ class SpawnSocket(SpawnBase):
|
||||
self.flag_eof = True
|
||||
logger.info("EOF: EOF character has been arrived")
|
||||
raise EOF('EOF character has been arrived')
|
||||
|
||||
except EOF:
|
||||
self.flag_eof = True
|
||||
raise
|
||||
|
||||
except ConnectionResetError:
|
||||
self.flag_eof = True
|
||||
logger.info("EOF('ConnectionResetError')")
|
||||
|
Loading…
Reference in New Issue
Block a user