]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - third_party/Python/module/pexpect-2.4/fdpexpect.py
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / third_party / Python / module / pexpect-2.4 / fdpexpect.py
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.
3
4 $Id: fdpexpect.py 505 2007-12-26 21:33:50Z noah $
5 """
6
7 from pexpect import *
8 import os
9
10 __all__ = ['fdspawn']
11
12
13 class fdspawn (spawn):
14
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. """
18
19     def __init__(
20             self,
21             fd,
22             args=[],
23             timeout=30,
24             maxread=2000,
25             searchwindowsize=None,
26             logfile=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
29         support fileno(). """
30
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
33         # does.
34
35         if not isinstance(fd, type(0)) and hasattr(fd, 'fileno'):
36             fd = fd.fileno()
37
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.')
41
42         try:  # make sure fd is a valid file descriptor
43             os.fstat(fd)
44         except OSError:
45             raise ExceptionPexpect(
46                 'The fd argument is not a valid file descriptor.')
47
48         self.args = None
49         self.command = None
50         spawn.__init__(
51             self,
52             None,
53             args,
54             timeout,
55             maxread,
56             searchwindowsize,
57             logfile)
58         self.child_fd = fd
59         self.own_fd = False
60         self.closed = False
61         self.name = '<file descriptor %d>' % fd
62
63     def __del__(self):
64
65         return
66
67     def close(self):
68
69         if self.child_fd == -1:
70             return
71         if self.own_fd:
72             self.close(self)
73         else:
74             self.flush()
75             os.close(self.child_fd)
76             self.child_fd = -1
77             self.closed = True
78
79     def isalive(self):
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. """
82
83         if self.child_fd == -1:
84             return False
85         try:
86             os.fstat(self.child_fd)
87             return True
88         except:
89             return False
90
91     def terminate(self, force=False):
92
93         raise ExceptionPexpect(
94             'This method is not valid for file descriptors.')
95
96     def kill(self, sig):
97
98         return