1 """This is like pexpect, but will work on any file descriptor that you pass it.
2 So you are reponsible for opening and close the file descriptor.
4 $Id: fdpexpect.py 505 2007-12-26 21:33:50Z noah $
13 class fdspawn (spawn):
15 """This is like pexpect.spawn but allows you to supply your own open file
16 descriptor. For example, you could use it to read through a file looking
17 for patterns, or to control a modem or serial device. """
25 searchwindowsize=None,
27 """This takes a file descriptor (an int) or an object that support the
28 fileno() method (returning an int). All Python file-like objects
31 # TODO: Add better handling of trying to use fdspawn in place of spawn
32 # TODO: (overload to allow fdspawn to also handle commands as spawn
35 if not isinstance(fd, type(0)) and hasattr(fd, 'fileno'):
38 if not isinstance(fd, type(0)):
39 raise ExceptionPexpect(
40 'The fd argument is not an int. If this is a command string then maybe you want to use pexpect.spawn.')
42 try: # make sure fd is a valid file descriptor
45 raise ExceptionPexpect(
46 'The fd argument is not a valid file descriptor.')
61 self.name = '<file descriptor %d>' % fd
69 if self.child_fd == -1:
75 os.close(self.child_fd)
80 """This checks if the file descriptor is still valid. If os.fstat()
81 does not raise an exception then we assume it is alive. """
83 if self.child_fd == -1:
86 os.fstat(self.child_fd)
91 def terminate(self, force=False):
93 raise ExceptionPexpect(
94 'This method is not valid for file descriptors.')