diff options
| author | noah <noah@656d521f-e311-0410-88e0-e7920216d269> | 2006-02-17 23:49:54 +0000 |
|---|---|---|
| committer | noah <noah@656d521f-e311-0410-88e0-e7920216d269> | 2006-02-17 23:49:54 +0000 |
| commit | 2b2ee76492ab2f4629fb691d5aeed22cf320736b (patch) | |
| tree | e2d50ed2471db678cbcca6ce1b170cc04f715f59 /pexpect/fdpexpect.py | |
| parent | 060aca73f30303e9f5dcd8a76086f231e3732dca (diff) | |
| download | pexpect-2b2ee76492ab2f4629fb691d5aeed22cf320736b.tar.gz | |
Added some changes suggested by Nicolas Cesar.
Diffstat (limited to 'pexpect/fdpexpect.py')
| -rw-r--r-- | pexpect/fdpexpect.py | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/pexpect/fdpexpect.py b/pexpect/fdpexpect.py index 6dbaa26..b124836 100644 --- a/pexpect/fdpexpect.py +++ b/pexpect/fdpexpect.py @@ -3,25 +3,36 @@ already open file descriptor. For example, you could use it to read through a file looking for patterns, or to control a modem or serial device. +TODO: This is BETA. """ -import pexpect +from pexpect import * -class fdspawn (pexpect.spawn): +class fdspawn (spawn): def __init__ (self, fd, args=[], timeout=30, maxread=2000, searchwindowsize=None, logfile=None): """This takes a file descriptor (an int) or an object that support the fileno() method (returning an int). All Python file-like objects support fileno(). """ ### TODO: Add better handling of trying to use fdspawn in place of spawn ### TODO: (overload to allow fdspawn to also handle commands as spawn does. - if type(fd) == type(''): - return - if type(fd)!=type(1) and hasattr(fd, 'fileno'): + if type(fd) != type(0) and hasattr(fd, 'fileno'): fd = fd.fileno() - pexpect.spawn.__init__(self, None, args, timeout, maxread, searchwindowsize, logfile) + + if type(fd) != type(0): + raise ExceptionPexpect ('The fd argument is not an int. If this is a command string then maybe you want to use pexpect.spawn.') + + try: # make sure fd is a valid file descriptor + os.fstat(command) + except OSError: + raise ExceptionPexpect, 'The fd argument is not a valid file descriptor.' + + self.args = None + self.command = None + spawn.__init__(self, None, args, timeout, maxread, searchwindowsize, logfile) self.child_fd = fd self.own_fd = False self.closed = False + self.name = '<file descriptor %d>' % fd def __del__ (self): return |
