summaryrefslogtreecommitdiff
path: root/pexpect/fdpexpect.py
diff options
context:
space:
mode:
authornoah <noah@656d521f-e311-0410-88e0-e7920216d269>2006-02-09 20:36:29 +0000
committernoah <noah@656d521f-e311-0410-88e0-e7920216d269>2006-02-09 20:36:29 +0000
commitaf08b6dfb696de11f1db755b207718fb757f5e77 (patch)
tree9ae6fbafe409dfcdded7e5aac0349f15a553906d /pexpect/fdpexpect.py
parent03456ab3bb3a0a591d457a260692410dec12caf2 (diff)
downloadpexpect-af08b6dfb696de11f1db755b207718fb757f5e77.tar.gz
pexpect.spawn() allows cmd argument to be None to support subclasses
that don't use cmd or args. Moved irix hack string check out of a loop. irix hack flag now gets set in __init__. Added fdpexpect.py to support users already open filedexcriptors. Pexpect used to allow this, but it was messy. I'm trying to add it back in by request. Added an ad-hoc fd_test.py which is just a temporary test and should be deleted some day.
Diffstat (limited to 'pexpect/fdpexpect.py')
-rw-r--r--pexpect/fdpexpect.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/pexpect/fdpexpect.py b/pexpect/fdpexpect.py
new file mode 100644
index 0000000..6dbaa26
--- /dev/null
+++ b/pexpect/fdpexpect.py
@@ -0,0 +1,51 @@
+"""This is like pexpect.spawn but allows you to supply your own,
+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.
+
+"""
+import pexpect
+
+class fdspawn (pexpect.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'):
+ fd = fd.fileno()
+ pexpect.spawn.__init__(self, None, args, timeout, maxread, searchwindowsize, logfile)
+ self.child_fd = fd
+ self.own_fd = False
+ self.closed = False
+
+ def __del__ (self):
+ return
+
+ def close (self):
+ if super(fdspawn, self).child_fd == -1:
+ return
+ if self.own_fd:
+ super(fdspawn, self).close (self)
+ else:
+ self.flush()
+ os.close(super(fdspawn, self).child_fd)
+ self.child_fd = -1
+ self.closed = True
+
+ def isalive (self):
+ print "isalive()"
+ return True
+
+ def terminate (self, force=False):
+ print "terminate()"
+ return
+
+ def kill (self, sig):
+ print "kill()"
+ return
+