Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Tools/autotest/pysim/fdpexpect.py
Views: 1799
"""This is like pexpect, but will work on any file descriptor that you pass it.1So you are responsible for opening and close the file descriptor.23$Id: fdpexpect.py 505 2007-12-26 21:33:50Z noah $4"""5from __future__ import print_function6import os78from pexpect import ExceptionPexpect, spawn910__all__ = ['fdspawn']111213class fdspawn(spawn):14"""This is like pexpect.spawn but allows you to supply your own open file15descriptor. For example, you could use it to read through a file looking16for patterns, or to control a modem or serial device. """1718def __init__(self, fd, args=[], timeout=30, maxread=2000, searchwindowsize=None, logfile=None):1920"""This takes a file descriptor (an int) or an object that support the21fileno() method (returning an int). All Python file-like objects22support fileno(). """2324# TODO: Add better handling of trying to use fdspawn in place of spawn25# TODO: (overload to allow fdspawn to also handle commands as spawn does.2627if not isinstance(fd, int) and hasattr(fd, 'fileno'):28fd = fd.fileno()2930if not isinstance(fd, int):31raise ExceptionPexpect(32"The fd argument is not an int. If this is a command string then maybe you want to use pexpect.spawn.")3334try: # make sure fd is a valid file descriptor35os.fstat(fd)36except OSError:37raise ExceptionPexpect("The fd argument is not a valid file descriptor.")3839self.args = None40self.command = None41spawn.__init__(self, None, args, timeout, maxread, searchwindowsize, logfile)42self.child_fd = fd43self.own_fd = False44self.closed = False45self.name = '<file descriptor %d>' % fd4647def __del__(self):4849return5051def close(self):5253if self.child_fd == -1:54return55if self.own_fd:56self.close(self)57else:58self.flush()59os.close(self.child_fd)60self.child_fd = -161self.closed = True6263def isalive(self):6465"""This checks if the file descriptor is still valid. If os.fstat()66does not raise an exception then we assume it is alive. """6768if self.child_fd == -1:69return False70try:71os.fstat(self.child_fd)72return True73except:74return False7576def terminate(self, force=False):7778raise ExceptionPexpect('This method is not valid for file descriptors.')7980def kill(self, sig):8182return838485