2 TODO: This is BETA. When it gets stable I will move it into the pexpect.py file.
10 class fdspawn (spawn):
11 """This is like pexpect.spawn but allows you to supply your own,
12 already open file descriptor. For example, you could use it to
13 read through a file looking for patterns, or to control a modem or
16 def __init__ (self, fd, args=[], timeout=30, maxread=2000, searchwindowsize=None, logfile=None):
17 """This takes a file descriptor (an int) or an object that support the fileno() method
18 (returning an int). All Python file-like objects support fileno().
20 ### TODO: Add better handling of trying to use fdspawn in place of spawn
21 ### TODO: (overload to allow fdspawn to also handle commands as spawn does.
23 if type(fd) != type(0) and hasattr(fd, 'fileno'):
26 if type(fd) != type(0):
27 raise ExceptionPexpect ('The fd argument is not an int. If this is a command string then maybe you want to use pexpect.spawn.')
29 try: # make sure fd is a valid file descriptor
32 raise ExceptionPexpect, 'The fd argument is not a valid file descriptor.'
36 spawn.__init__(self, None, args, timeout, maxread, searchwindowsize, logfile)
40 self.name = '<file descriptor %d>' % fd
46 if super(fdspawn, self).child_fd == -1:
49 super(fdspawn, self).close (self)
52 os.close(super(fdspawn, self).child_fd)
57 """This checks if the file descriptor is still valid.
58 If os.fstat() does not raise an exception then we assume it is alive.
60 if self.child_fd == -1:
63 os.fstat(self.child_fd)
68 def terminate (self, force=False):
69 raise ExceptionPexpect ('This method is not valid for file descriptors.')