Merge lp:~stephane-duchesneau/ubuntu/trusty/pexpect/errno_handling_fix into lp:ubuntu/trusty/pexpect

Proposed by Stéphane Duchesneau
Status: Rejected
Rejected by: Barry Warsaw
Proposed branch: lp:~stephane-duchesneau/ubuntu/trusty/pexpect/errno_handling_fix
Merge into: lp:ubuntu/trusty/pexpect
Diff against target: 2116 lines (+2073/-1)
6 files modified
.pc/applied-patches (+1/-0)
.pc/errno_handling_fix.patch/pexpect/__init__.py (+2051/-0)
debian/changelog (+6/-0)
debian/patches/errno_handling_fix.patch (+13/-0)
debian/patches/series (+1/-0)
pexpect/__init__.py (+1/-1)
To merge this branch: bzr merge lp:~stephane-duchesneau/ubuntu/trusty/pexpect/errno_handling_fix
Reviewer Review Type Date Requested Status
Barry Warsaw Pending
Review via email: mp+218813@code.launchpad.net

Description of the change

This fixes a bug causing and unhandled exception within exception
handling. sys.exc_info()[1] was not treated correctly as a tuple
when a signal was sent (errno.EINTR)

To post a comment you must log in.
Revision history for this message
Barry Warsaw (barry) wrote :

Hi Stéphane. First, thanks very much for your contribution to Ubuntu!

There's a bit of a problem with your merge proposal. As you'll see in the diff, it is including the .pc directory. This usually happens when the quilt patch stack for your branch is at a different level than the stack of the target branch. The fix for this when you're adding a new quilt patch, is to do one `quilt pop` so that the branch you're proposing for merge is at the same level as the branch you're proposing to merge into.

Aside from that, I'll note that the version of pexpect in Utopic (and Debian) is behind upstream's release on PyPI, and in fact this bug is fixed in the latest upstream release. Here's the upstream bug and diff:

http://pexpect.readthedocs.org/en/latest/history.html#releases

https://github.com/pexpect/pexpect/commit/a97f580d5cdd0f479ea64d36edc164ea769d4084 (this includes tests)

https://github.com/pexpect/pexpect/pull/38/

Since the package is team maintained in Debian, I will update it there, and that should automatically flow into Ubuntu 14.10. It's probably worth fixing this in Trusty though, so I'll file an SRU and attempt to backport the upstream fix into the Trusty branch.

Revision history for this message
Stéphane Duchesneau (stephane-duchesneau) wrote :

Hi, thank you for your extensive answer, I will work on my "quilt" skills and try to follow the SRU.

--
Stéphane Duchesneau
Savoir-faire Linux

----- Mail original -----

De: "Barry Warsaw" <email address hidden>
À: <email address hidden>
Envoyé: Jeudi 8 Mai 2014 15:50:12
Objet: Re: [Merge] lp:~stephane-duchesneau/ubuntu/trusty/pexpect/errno_handling_fix into lp:ubuntu/trusty/pexpect

Hi Stéphane. First, thanks very much for your contribution to Ubuntu!

There's a bit of a problem with your merge proposal. As you'll see in the diff, it is including the .pc directory. This usually happens when the quilt patch stack for your branch is at a different level than the stack of the target branch. The fix for this when you're adding a new quilt patch, is to do one `quilt pop` so that the branch you're proposing for merge is at the same level as the branch you're proposing to merge into.

Aside from that, I'll note that the version of pexpect in Utopic (and Debian) is behind upstream's release on PyPI, and in fact this bug is fixed in the latest upstream release. Here's the upstream bug and diff:

http://pexpect.readthedocs.org/en/latest/history.html#releases

https://github.com/pexpect/pexpect/commit/a97f580d5cdd0f479ea64d36edc164ea769d4084 (this includes tests)

https://github.com/pexpect/pexpect/pull/38/

Since the package is team maintained in Debian, I will update it there, and that should automatically flow into Ubuntu 14.10. It's probably worth fixing this in Trusty though, so I'll file an SRU and attempt to backport the upstream fix into the Trusty branch.
--
https://code.launchpad.net/~stephane-duchesneau/ubuntu/trusty/pexpect/errno_handling_fix/+merge/218813
You are the owner of lp:~stephane-duchesneau/ubuntu/trusty/pexpect/errno_handling_fix.

Revision history for this message
Barry Warsaw (barry) wrote :

LP: #1317600

Please subscribe to that bug and provide any additional information needed.

Revision history for this message
Barry Warsaw (barry) wrote :

Oops. It should be LP: #1317660

Revision history for this message
Barry Warsaw (barry) wrote :

I'm rejecting only because I fixed this in other ways as described above. Again, thanks for your contribution!

Unmerged revisions

10. By Stéphane Duchesneau

Add patch errno_handling_fix to fix interruption handling bug

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file '.pc/applied-patches'
2--- .pc/applied-patches 2013-12-06 20:20:26 +0000
3+++ .pc/applied-patches 2014-05-08 13:59:49 +0000
4@@ -0,0 +1,1 @@
5+errno_handling_fix.patch
6
7=== added directory '.pc/errno_handling_fix.patch'
8=== added file '.pc/errno_handling_fix.patch/.timestamp'
9=== added directory '.pc/errno_handling_fix.patch/pexpect'
10=== added file '.pc/errno_handling_fix.patch/pexpect/__init__.py'
11--- .pc/errno_handling_fix.patch/pexpect/__init__.py 1970-01-01 00:00:00 +0000
12+++ .pc/errno_handling_fix.patch/pexpect/__init__.py 2014-05-08 13:59:49 +0000
13@@ -0,0 +1,2051 @@
14+'''Pexpect is a Python module for spawning child applications and controlling
15+them automatically. Pexpect can be used for automating interactive applications
16+such as ssh, ftp, passwd, telnet, etc. It can be used to a automate setup
17+scripts for duplicating software package installations on different servers. It
18+can be used for automated software testing. Pexpect is in the spirit of Don
19+Libes' Expect, but Pexpect is pure Python. Other Expect-like modules for Python
20+require TCL and Expect or require C extensions to be compiled. Pexpect does not
21+use C, Expect, or TCL extensions. It should work on any platform that supports
22+the standard Python pty module. The Pexpect interface focuses on ease of use so
23+that simple tasks are easy.
24+
25+There are two main interfaces to the Pexpect system; these are the function,
26+run() and the class, spawn. The spawn class is more powerful. The run()
27+function is simpler than spawn, and is good for quickly calling program. When
28+you call the run() function it executes a given program and then returns the
29+output. This is a handy replacement for os.system().
30+
31+For example::
32+
33+ pexpect.run('ls -la')
34+
35+The spawn class is the more powerful interface to the Pexpect system. You can
36+use this to spawn a child program then interact with it by sending input and
37+expecting responses (waiting for patterns in the child's output).
38+
39+For example::
40+
41+ child = pexpect.spawn('scp foo user@example.com:.')
42+ child.expect('Password:')
43+ child.sendline(mypassword)
44+
45+This works even for commands that ask for passwords or other input outside of
46+the normal stdio streams. For example, ssh reads input directly from the TTY
47+device which bypasses stdin.
48+
49+Credits: Noah Spurrier, Richard Holden, Marco Molteni, Kimberley Burchett,
50+Robert Stone, Hartmut Goebel, Chad Schroeder, Erick Tryzelaar, Dave Kirby, Ids
51+vander Molen, George Todd, Noel Taylor, Nicolas D. Cesar, Alexander Gattin,
52+Jacques-Etienne Baudoux, Geoffrey Marshall, Francisco Lourenco, Glen Mabey,
53+Karthik Gurusamy, Fernando Perez, Corey Minyard, Jon Cohen, Guillaume
54+Chazarain, Andrew Ryan, Nick Craig-Wood, Andrew Stone, Jorgen Grahn, John
55+Spiegel, Jan Grant, and Shane Kerr. Let me know if I forgot anyone.
56+
57+Pexpect is free, open source, and all that good stuff.
58+http://pexpect.sourceforge.net/
59+
60+PEXPECT LICENSE
61+
62+ This license is approved by the OSI and FSF as GPL-compatible.
63+ http://opensource.org/licenses/isc-license.txt
64+
65+ Copyright (c) 2012, Noah Spurrier <noah@noah.org>
66+ PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
67+ PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
68+ COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
69+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
70+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
71+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
72+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
73+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
74+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
75+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
76+
77+'''
78+
79+try:
80+ import os
81+ import sys
82+ import time
83+ import select
84+ import re
85+ import struct
86+ import resource
87+ import types
88+ import pty
89+ import tty
90+ import termios
91+ import fcntl
92+ import errno
93+ import traceback
94+ import signal
95+ import codecs
96+except ImportError: # pragma: no cover
97+ err = sys.exc_info()[1]
98+ raise ImportError(str(err) + '''
99+
100+A critical module was not found. Probably this operating system does not
101+support it. Pexpect is intended for UNIX-like operating systems.''')
102+
103+__version__ = '3.1'
104+__revision__ = ''
105+__all__ = ['ExceptionPexpect', 'EOF', 'TIMEOUT', 'spawn', 'spawnu', 'run', 'runu',
106+ 'which', 'split_command_line', '__version__', '__revision__']
107+
108+PY3 = (sys.version_info[0] >= 3)
109+
110+# Exception classes used by this module.
111+class ExceptionPexpect(Exception):
112+ '''Base class for all exceptions raised by this module.
113+ '''
114+
115+ def __init__(self, value):
116+ super(ExceptionPexpect, self).__init__(value)
117+ self.value = value
118+
119+ def __str__(self):
120+ return str(self.value)
121+
122+ def get_trace(self):
123+ '''This returns an abbreviated stack trace with lines that only concern
124+ the caller. In other words, the stack trace inside the Pexpect module
125+ is not included. '''
126+
127+ tblist = traceback.extract_tb(sys.exc_info()[2])
128+ tblist = [item for item in tblist if 'pexpect/__init__' not in item[0]]
129+ tblist = traceback.format_list(tblist)
130+ return ''.join(tblist)
131+
132+
133+class EOF(ExceptionPexpect):
134+ '''Raised when EOF is read from a child.
135+ This usually means the child has exited.'''
136+
137+
138+class TIMEOUT(ExceptionPexpect):
139+ '''Raised when a read time exceeds the timeout. '''
140+
141+##class TIMEOUT_PATTERN(TIMEOUT):
142+## '''Raised when the pattern match time exceeds the timeout.
143+## This is different than a read TIMEOUT because the child process may
144+## give output, thus never give a TIMEOUT, but the output
145+## may never match a pattern.
146+## '''
147+##class MAXBUFFER(ExceptionPexpect):
148+## '''Raised when a buffer fills before matching an expected pattern.'''
149+
150+
151+def run(command, timeout=-1, withexitstatus=False, events=None,
152+ extra_args=None, logfile=None, cwd=None, env=None):
153+
154+ '''
155+ This function runs the given command; waits for it to finish; then
156+ returns all output as a string. STDERR is included in output. If the full
157+ path to the command is not given then the path is searched.
158+
159+ Note that lines are terminated by CR/LF (\\r\\n) combination even on
160+ UNIX-like systems because this is the standard for pseudottys. If you set
161+ 'withexitstatus' to true, then run will return a tuple of (command_output,
162+ exitstatus). If 'withexitstatus' is false then this returns just
163+ command_output.
164+
165+ The run() function can often be used instead of creating a spawn instance.
166+ For example, the following code uses spawn::
167+
168+ from pexpect import *
169+ child = spawn('scp foo user@example.com:.')
170+ child.expect('(?i)password')
171+ child.sendline(mypassword)
172+
173+ The previous code can be replace with the following::
174+
175+ from pexpect import *
176+ run('scp foo user@example.com:.', events={'(?i)password': mypassword})
177+
178+ **Examples**
179+
180+ Start the apache daemon on the local machine::
181+
182+ from pexpect import *
183+ run("/usr/local/apache/bin/apachectl start")
184+
185+ Check in a file using SVN::
186+
187+ from pexpect import *
188+ run("svn ci -m 'automatic commit' my_file.py")
189+
190+ Run a command and capture exit status::
191+
192+ from pexpect import *
193+ (command_output, exitstatus) = run('ls -l /bin', withexitstatus=1)
194+
195+ The following will run SSH and execute 'ls -l' on the remote machine. The
196+ password 'secret' will be sent if the '(?i)password' pattern is ever seen::
197+
198+ run("ssh username@machine.example.com 'ls -l'",
199+ events={'(?i)password':'secret\\n'})
200+
201+ This will start mencoder to rip a video from DVD. This will also display
202+ progress ticks every 5 seconds as it runs. For example::
203+
204+ from pexpect import *
205+ def print_ticks(d):
206+ print d['event_count'],
207+ run("mencoder dvd://1 -o video.avi -oac copy -ovc copy",
208+ events={TIMEOUT:print_ticks}, timeout=5)
209+
210+ The 'events' argument should be a dictionary of patterns and responses.
211+ Whenever one of the patterns is seen in the command out run() will send the
212+ associated response string. Note that you should put newlines in your
213+ string if Enter is necessary. The responses may also contain callback
214+ functions. Any callback is function that takes a dictionary as an argument.
215+ The dictionary contains all the locals from the run() function, so you can
216+ access the child spawn object or any other variable defined in run()
217+ (event_count, child, and extra_args are the most useful). A callback may
218+ return True to stop the current run process otherwise run() continues until
219+ the next event. A callback may also return a string which will be sent to
220+ the child. 'extra_args' is not used by directly run(). It provides a way to
221+ pass data to a callback function through run() through the locals
222+ dictionary passed to a callback.
223+ '''
224+ return _run(command, timeout=timeout, withexitstatus=withexitstatus,
225+ events=events, extra_args=extra_args, logfile=logfile, cwd=cwd,
226+ env=env, _spawn=spawn)
227+
228+def runu(command, timeout=-1, withexitstatus=False, events=None,
229+ extra_args=None, logfile=None, cwd=None, env=None, **kwargs):
230+ """This offers the same interface as :func:`run`, but using unicode.
231+
232+ Like :class:`spawnu`, you can pass ``encoding`` and ``errors`` parameters,
233+ which will be used for both input and output.
234+ """
235+ return _run(command, timeout=timeout, withexitstatus=withexitstatus,
236+ events=events, extra_args=extra_args, logfile=logfile, cwd=cwd,
237+ env=env, _spawn=spawnu, **kwargs)
238+
239+def _run(command, timeout, withexitstatus, events, extra_args, logfile, cwd,
240+ env, _spawn, **kwargs):
241+ if timeout == -1:
242+ child = _spawn(command, maxread=2000, logfile=logfile, cwd=cwd, env=env,
243+ **kwargs)
244+ else:
245+ child = _spawn(command, timeout=timeout, maxread=2000, logfile=logfile,
246+ cwd=cwd, env=env, **kwargs)
247+ if events is not None:
248+ patterns = list(events.keys())
249+ responses = list(events.values())
250+ else:
251+ # This assumes EOF or TIMEOUT will eventually cause run to terminate.
252+ patterns = None
253+ responses = None
254+ child_result_list = []
255+ event_count = 0
256+ while True:
257+ try:
258+ index = child.expect(patterns)
259+ if isinstance(child.after, child.allowed_string_types):
260+ child_result_list.append(child.before + child.after)
261+ else:
262+ # child.after may have been a TIMEOUT or EOF,
263+ # which we don't want appended to the list.
264+ child_result_list.append(child.before)
265+ if isinstance(responses[index], child.allowed_string_types):
266+ child.send(responses[index])
267+ elif isinstance(responses[index], types.FunctionType):
268+ callback_result = responses[index](locals())
269+ sys.stdout.flush()
270+ if isinstance(callback_result, child.allowed_string_types):
271+ child.send(callback_result)
272+ elif callback_result:
273+ break
274+ else:
275+ raise TypeError('The callback must be a string or function.')
276+ event_count = event_count + 1
277+ except TIMEOUT:
278+ child_result_list.append(child.before)
279+ break
280+ except EOF:
281+ child_result_list.append(child.before)
282+ break
283+ child_result = child.string_type().join(child_result_list)
284+ if withexitstatus:
285+ child.close()
286+ return (child_result, child.exitstatus)
287+ else:
288+ return child_result
289+
290+class spawn(object):
291+ '''This is the main class interface for Pexpect. Use this class to start
292+ and control child applications. '''
293+ string_type = bytes
294+ if PY3:
295+ allowed_string_types = (bytes, str)
296+ @staticmethod
297+ def _chr(c):
298+ return bytes([c])
299+ linesep = os.linesep.encode('ascii')
300+
301+ @staticmethod
302+ def write_to_stdout(b):
303+ try:
304+ return sys.stdout.buffer.write(b)
305+ except AttributeError:
306+ # If stdout has been replaced, it may not have .buffer
307+ return sys.stdout.write(b.decode('ascii', 'replace'))
308+ else:
309+ allowed_string_types = (basestring,) # analysis:ignore
310+ _chr = staticmethod(chr)
311+ linesep = os.linesep
312+ write_to_stdout = sys.stdout.write
313+
314+ encoding = None
315+
316+ def __init__(self, command, args=[], timeout=30, maxread=2000,
317+ searchwindowsize=None, logfile=None, cwd=None, env=None,
318+ ignore_sighup=True):
319+
320+ '''This is the constructor. The command parameter may be a string that
321+ includes a command and any arguments to the command. For example::
322+
323+ child = pexpect.spawn('/usr/bin/ftp')
324+ child = pexpect.spawn('/usr/bin/ssh user@example.com')
325+ child = pexpect.spawn('ls -latr /tmp')
326+
327+ You may also construct it with a list of arguments like so::
328+
329+ child = pexpect.spawn('/usr/bin/ftp', [])
330+ child = pexpect.spawn('/usr/bin/ssh', ['user@example.com'])
331+ child = pexpect.spawn('ls', ['-latr', '/tmp'])
332+
333+ After this the child application will be created and will be ready to
334+ talk to. For normal use, see expect() and send() and sendline().
335+
336+ Remember that Pexpect does NOT interpret shell meta characters such as
337+ redirect, pipe, or wild cards (``>``, ``|``, or ``*``). This is a
338+ common mistake. If you want to run a command and pipe it through
339+ another command then you must also start a shell. For example::
340+
341+ child = pexpect.spawn('/bin/bash -c "ls -l | grep LOG > logs.txt"')
342+ child.expect(pexpect.EOF)
343+
344+ The second form of spawn (where you pass a list of arguments) is useful
345+ in situations where you wish to spawn a command and pass it its own
346+ argument list. This can make syntax more clear. For example, the
347+ following is equivalent to the previous example::
348+
349+ shell_cmd = 'ls -l | grep LOG > logs.txt'
350+ child = pexpect.spawn('/bin/bash', ['-c', shell_cmd])
351+ child.expect(pexpect.EOF)
352+
353+ The maxread attribute sets the read buffer size. This is maximum number
354+ of bytes that Pexpect will try to read from a TTY at one time. Setting
355+ the maxread size to 1 will turn off buffering. Setting the maxread
356+ value higher may help performance in cases where large amounts of
357+ output are read back from the child. This feature is useful in
358+ conjunction with searchwindowsize.
359+
360+ The searchwindowsize attribute sets the how far back in the incoming
361+ seach buffer Pexpect will search for pattern matches. Every time
362+ Pexpect reads some data from the child it will append the data to the
363+ incoming buffer. The default is to search from the beginning of the
364+ incoming buffer each time new data is read from the child. But this is
365+ very inefficient if you are running a command that generates a large
366+ amount of data where you want to match. The searchwindowsize does not
367+ affect the size of the incoming data buffer. You will still have
368+ access to the full buffer after expect() returns.
369+
370+ The logfile member turns on or off logging. All input and output will
371+ be copied to the given file object. Set logfile to None to stop
372+ logging. This is the default. Set logfile to sys.stdout to echo
373+ everything to standard output. The logfile is flushed after each write.
374+
375+ Example log input and output to a file::
376+
377+ child = pexpect.spawn('some_command')
378+ fout = file('mylog.txt','w')
379+ child.logfile = fout
380+
381+ Example log to stdout::
382+
383+ child = pexpect.spawn('some_command')
384+ child.logfile = sys.stdout
385+
386+ The logfile_read and logfile_send members can be used to separately log
387+ the input from the child and output sent to the child. Sometimes you
388+ don't want to see everything you write to the child. You only want to
389+ log what the child sends back. For example::
390+
391+ child = pexpect.spawn('some_command')
392+ child.logfile_read = sys.stdout
393+
394+ To separately log output sent to the child use logfile_send::
395+
396+ self.logfile_send = fout
397+
398+ If ``ignore_sighup`` is True, the child process will ignore SIGHUP
399+ signals. For now, the default is True, to preserve the behaviour of
400+ earlier versions of Pexpect, but you should pass this explicitly if you
401+ want to rely on it.
402+
403+ The delaybeforesend helps overcome a weird behavior that many users
404+ were experiencing. The typical problem was that a user would expect() a
405+ "Password:" prompt and then immediately call sendline() to send the
406+ password. The user would then see that their password was echoed back
407+ to them. Passwords don't normally echo. The problem is caused by the
408+ fact that most applications print out the "Password" prompt and then
409+ turn off stdin echo, but if you send your password before the
410+ application turned off echo, then you get your password echoed.
411+ Normally this wouldn't be a problem when interacting with a human at a
412+ real keyboard. If you introduce a slight delay just before writing then
413+ this seems to clear up the problem. This was such a common problem for
414+ many users that I decided that the default pexpect behavior should be
415+ to sleep just before writing to the child application. 1/20th of a
416+ second (50 ms) seems to be enough to clear up the problem. You can set
417+ delaybeforesend to 0 to return to the old behavior. Most Linux machines
418+ don't like this to be below 0.03. I don't know why.
419+
420+ Note that spawn is clever about finding commands on your path.
421+ It uses the same logic that "which" uses to find executables.
422+
423+ If you wish to get the exit status of the child you must call the
424+ close() method. The exit or signal status of the child will be stored
425+ in self.exitstatus or self.signalstatus. If the child exited normally
426+ then exitstatus will store the exit return code and signalstatus will
427+ be None. If the child was terminated abnormally with a signal then
428+ signalstatus will store the signal value and exitstatus will be None.
429+ If you need more detail you can also read the self.status member which
430+ stores the status returned by os.waitpid. You can interpret this using
431+ os.WIFEXITED/os.WEXITSTATUS or os.WIFSIGNALED/os.TERMSIG. '''
432+
433+ self.STDIN_FILENO = pty.STDIN_FILENO
434+ self.STDOUT_FILENO = pty.STDOUT_FILENO
435+ self.STDERR_FILENO = pty.STDERR_FILENO
436+ self.stdin = sys.stdin
437+ self.stdout = sys.stdout
438+ self.stderr = sys.stderr
439+
440+ self.searcher = None
441+ self.ignorecase = False
442+ self.before = None
443+ self.after = None
444+ self.match = None
445+ self.match_index = None
446+ self.terminated = True
447+ self.exitstatus = None
448+ self.signalstatus = None
449+ # status returned by os.waitpid
450+ self.status = None
451+ self.flag_eof = False
452+ self.pid = None
453+ # the chile filedescriptor is initially closed
454+ self.child_fd = -1
455+ self.timeout = timeout
456+ self.delimiter = EOF
457+ self.logfile = logfile
458+ # input from child (read_nonblocking)
459+ self.logfile_read = None
460+ # output to send (send, sendline)
461+ self.logfile_send = None
462+ # max bytes to read at one time into buffer
463+ self.maxread = maxread
464+ # This is the read buffer. See maxread.
465+ self.buffer = self.string_type()
466+ # Data before searchwindowsize point is preserved, but not searched.
467+ self.searchwindowsize = searchwindowsize
468+ # Delay used before sending data to child. Time in seconds.
469+ # Most Linux machines don't like this to be below 0.03 (30 ms).
470+ self.delaybeforesend = 0.05
471+ # Used by close() to give kernel time to update process status.
472+ # Time in seconds.
473+ self.delayafterclose = 0.1
474+ # Used by terminate() to give kernel time to update process status.
475+ # Time in seconds.
476+ self.delayafterterminate = 0.1
477+ self.softspace = False
478+ self.name = '<' + repr(self) + '>'
479+ self.closed = True
480+ self.cwd = cwd
481+ self.env = env
482+ self.ignore_sighup = ignore_sighup
483+ # This flags if we are running on irix
484+ self.__irix_hack = (sys.platform.lower().find('irix') >= 0)
485+ # Solaris uses internal __fork_pty(). All others use pty.fork().
486+ if ((sys.platform.lower().find('solaris') >= 0)
487+ or (sys.platform.lower().find('sunos5') >= 0)):
488+ self.use_native_pty_fork = False
489+ else:
490+ self.use_native_pty_fork = True
491+
492+ # Support subclasses that do not use command or args.
493+ if command is None:
494+ self.command = None
495+ self.args = None
496+ self.name = '<pexpect factory incomplete>'
497+ else:
498+ self._spawn(command, args)
499+
500+ @staticmethod
501+ def _coerce_expect_string(s):
502+ if not isinstance(s, bytes):
503+ return s.encode('ascii')
504+ return s
505+
506+ @staticmethod
507+ def _coerce_send_string(s):
508+ if not isinstance(s, bytes):
509+ return s.encode('utf-8')
510+ return s
511+
512+ @staticmethod
513+ def _coerce_read_string(s):
514+ return s
515+
516+ def __del__(self):
517+ '''This makes sure that no system resources are left open. Python only
518+ garbage collects Python objects. OS file descriptors are not Python
519+ objects, so they must be handled explicitly. If the child file
520+ descriptor was opened outside of this class (passed to the constructor)
521+ then this does not close it. '''
522+
523+ if not self.closed:
524+ # It is possible for __del__ methods to execute during the
525+ # teardown of the Python VM itself. Thus self.close() may
526+ # trigger an exception because os.close may be None.
527+ try:
528+ self.close()
529+ # which exception, shouldnt' we catch explicitly .. ?
530+ except:
531+ pass
532+
533+ def __str__(self):
534+ '''This returns a human-readable string that represents the state of
535+ the object. '''
536+
537+ s = []
538+ s.append(repr(self))
539+ s.append('version: ' + __version__)
540+ s.append('command: ' + str(self.command))
541+ s.append('args: %r' % (self.args,))
542+ s.append('searcher: %r' % (self.searcher,))
543+ s.append('buffer (last 100 chars): %r' % (self.buffer)[-100:],)
544+ s.append('before (last 100 chars): %r' % (self.before)[-100:],)
545+ s.append('after: %r' % (self.after,))
546+ s.append('match: %r' % (self.match,))
547+ s.append('match_index: ' + str(self.match_index))
548+ s.append('exitstatus: ' + str(self.exitstatus))
549+ s.append('flag_eof: ' + str(self.flag_eof))
550+ s.append('pid: ' + str(self.pid))
551+ s.append('child_fd: ' + str(self.child_fd))
552+ s.append('closed: ' + str(self.closed))
553+ s.append('timeout: ' + str(self.timeout))
554+ s.append('delimiter: ' + str(self.delimiter))
555+ s.append('logfile: ' + str(self.logfile))
556+ s.append('logfile_read: ' + str(self.logfile_read))
557+ s.append('logfile_send: ' + str(self.logfile_send))
558+ s.append('maxread: ' + str(self.maxread))
559+ s.append('ignorecase: ' + str(self.ignorecase))
560+ s.append('searchwindowsize: ' + str(self.searchwindowsize))
561+ s.append('delaybeforesend: ' + str(self.delaybeforesend))
562+ s.append('delayafterclose: ' + str(self.delayafterclose))
563+ s.append('delayafterterminate: ' + str(self.delayafterterminate))
564+ return '\n'.join(s)
565+
566+ def _spawn(self, command, args=[]):
567+ '''This starts the given command in a child process. This does all the
568+ fork/exec type of stuff for a pty. This is called by __init__. If args
569+ is empty then command will be parsed (split on spaces) and args will be
570+ set to parsed arguments. '''
571+
572+ # The pid and child_fd of this object get set by this method.
573+ # Note that it is difficult for this method to fail.
574+ # You cannot detect if the child process cannot start.
575+ # So the only way you can tell if the child process started
576+ # or not is to try to read from the file descriptor. If you get
577+ # EOF immediately then it means that the child is already dead.
578+ # That may not necessarily be bad because you may have spawned a child
579+ # that performs some task; creates no stdout output; and then dies.
580+
581+ # If command is an int type then it may represent a file descriptor.
582+ if isinstance(command, type(0)):
583+ raise ExceptionPexpect('Command is an int type. ' +
584+ 'If this is a file descriptor then maybe you want to ' +
585+ 'use fdpexpect.fdspawn which takes an existing ' +
586+ 'file descriptor instead of a command string.')
587+
588+ if not isinstance(args, type([])):
589+ raise TypeError('The argument, args, must be a list.')
590+
591+ if args == []:
592+ self.args = split_command_line(command)
593+ self.command = self.args[0]
594+ else:
595+ # Make a shallow copy of the args list.
596+ self.args = args[:]
597+ self.args.insert(0, command)
598+ self.command = command
599+
600+ command_with_path = which(self.command)
601+ if command_with_path is None:
602+ raise ExceptionPexpect('The command was not found or was not ' +
603+ 'executable: %s.' % self.command)
604+ self.command = command_with_path
605+ self.args[0] = self.command
606+
607+ self.name = '<' + ' '.join(self.args) + '>'
608+
609+ assert self.pid is None, 'The pid member must be None.'
610+ assert self.command is not None, 'The command member must not be None.'
611+
612+ if self.use_native_pty_fork:
613+ try:
614+ self.pid, self.child_fd = pty.fork()
615+ except OSError:
616+ err = sys.exc_info()[1]
617+ raise ExceptionPexpect('pty.fork() failed: ' + str(err))
618+ else:
619+ # Use internal __fork_pty
620+ self.pid, self.child_fd = self.__fork_pty()
621+
622+ if self.pid == 0:
623+ # Child
624+ try:
625+ # used by setwinsize()
626+ self.child_fd = sys.stdout.fileno()
627+ self.setwinsize(24, 80)
628+ # which exception, shouldnt' we catch explicitly .. ?
629+ except:
630+ # Some platforms do not like setwinsize (Cygwin).
631+ # This will cause problem when running applications that
632+ # are very picky about window size.
633+ # This is a serious limitation, but not a show stopper.
634+ pass
635+ # Do not allow child to inherit open file descriptors from parent.
636+ max_fd = resource.getrlimit(resource.RLIMIT_NOFILE)[0]
637+ for i in range(3, max_fd):
638+ try:
639+ os.close(i)
640+ except OSError:
641+ pass
642+
643+ if self.ignore_sighup:
644+ signal.signal(signal.SIGHUP, signal.SIG_IGN)
645+
646+ if self.cwd is not None:
647+ os.chdir(self.cwd)
648+ if self.env is None:
649+ os.execv(self.command, self.args)
650+ else:
651+ os.execvpe(self.command, self.args, self.env)
652+
653+ # Parent
654+ self.terminated = False
655+ self.closed = False
656+
657+ def __fork_pty(self):
658+ '''This implements a substitute for the forkpty system call. This
659+ should be more portable than the pty.fork() function. Specifically,
660+ this should work on Solaris.
661+
662+ Modified 10.06.05 by Geoff Marshall: Implemented __fork_pty() method to
663+ resolve the issue with Python's pty.fork() not supporting Solaris,
664+ particularly ssh. Based on patch to posixmodule.c authored by Noah
665+ Spurrier::
666+
667+ http://mail.python.org/pipermail/python-dev/2003-May/035281.html
668+
669+ '''
670+
671+ parent_fd, child_fd = os.openpty()
672+ if parent_fd < 0 or child_fd < 0:
673+ raise ExceptionPexpect("Could not open with os.openpty().")
674+
675+ pid = os.fork()
676+ if pid < 0:
677+ raise ExceptionPexpect("Failed os.fork().")
678+ elif pid == 0:
679+ # Child.
680+ os.close(parent_fd)
681+ self.__pty_make_controlling_tty(child_fd)
682+
683+ os.dup2(child_fd, 0)
684+ os.dup2(child_fd, 1)
685+ os.dup2(child_fd, 2)
686+
687+ if child_fd > 2:
688+ os.close(child_fd)
689+ else:
690+ # Parent.
691+ os.close(child_fd)
692+
693+ return pid, parent_fd
694+
695+ def __pty_make_controlling_tty(self, tty_fd):
696+ '''This makes the pseudo-terminal the controlling tty. This should be
697+ more portable than the pty.fork() function. Specifically, this should
698+ work on Solaris. '''
699+
700+ child_name = os.ttyname(tty_fd)
701+
702+ # Disconnect from controlling tty. Harmless if not already connected.
703+ try:
704+ fd = os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY)
705+ if fd >= 0:
706+ os.close(fd)
707+ # which exception, shouldnt' we catch explicitly .. ?
708+ except:
709+ # Already disconnected. This happens if running inside cron.
710+ pass
711+
712+ os.setsid()
713+
714+ # Verify we are disconnected from controlling tty
715+ # by attempting to open it again.
716+ try:
717+ fd = os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY)
718+ if fd >= 0:
719+ os.close(fd)
720+ raise ExceptionPexpect('Failed to disconnect from ' +
721+ 'controlling tty. It is still possible to open /dev/tty.')
722+ # which exception, shouldnt' we catch explicitly .. ?
723+ except:
724+ # Good! We are disconnected from a controlling tty.
725+ pass
726+
727+ # Verify we can open child pty.
728+ fd = os.open(child_name, os.O_RDWR)
729+ if fd < 0:
730+ raise ExceptionPexpect("Could not open child pty, " + child_name)
731+ else:
732+ os.close(fd)
733+
734+ # Verify we now have a controlling tty.
735+ fd = os.open("/dev/tty", os.O_WRONLY)
736+ if fd < 0:
737+ raise ExceptionPexpect("Could not open controlling tty, /dev/tty")
738+ else:
739+ os.close(fd)
740+
741+ def fileno(self):
742+ '''This returns the file descriptor of the pty for the child.
743+ '''
744+ return self.child_fd
745+
746+ def close(self, force=True):
747+ '''This closes the connection with the child application. Note that
748+ calling close() more than once is valid. This emulates standard Python
749+ behavior with files. Set force to True if you want to make sure that
750+ the child is terminated (SIGKILL is sent if the child ignores SIGHUP
751+ and SIGINT). '''
752+
753+ if not self.closed:
754+ self.flush()
755+ os.close(self.child_fd)
756+ # Give kernel time to update process status.
757+ time.sleep(self.delayafterclose)
758+ if self.isalive():
759+ if not self.terminate(force):
760+ raise ExceptionPexpect('Could not terminate the child.')
761+ self.child_fd = -1
762+ self.closed = True
763+ #self.pid = None
764+
765+ def flush(self):
766+ '''This does nothing. It is here to support the interface for a
767+ File-like object. '''
768+
769+ pass
770+
771+ def isatty(self):
772+ '''This returns True if the file descriptor is open and connected to a
773+ tty(-like) device, else False. '''
774+
775+ return os.isatty(self.child_fd)
776+
777+ def waitnoecho(self, timeout=-1):
778+ '''This waits until the terminal ECHO flag is set False. This returns
779+ True if the echo mode is off. This returns False if the ECHO flag was
780+ not set False before the timeout. This can be used to detect when the
781+ child is waiting for a password. Usually a child application will turn
782+ off echo mode when it is waiting for the user to enter a password. For
783+ example, instead of expecting the "password:" prompt you can wait for
784+ the child to set ECHO off::
785+
786+ p = pexpect.spawn('ssh user@example.com')
787+ p.waitnoecho()
788+ p.sendline(mypassword)
789+
790+ If timeout==-1 then this method will use the value in self.timeout.
791+ If timeout==None then this method to block until ECHO flag is False.
792+ '''
793+
794+ if timeout == -1:
795+ timeout = self.timeout
796+ if timeout is not None:
797+ end_time = time.time() + timeout
798+ while True:
799+ if not self.getecho():
800+ return True
801+ if timeout < 0 and timeout is not None:
802+ return False
803+ if timeout is not None:
804+ timeout = end_time - time.time()
805+ time.sleep(0.1)
806+
807+ def getecho(self):
808+ '''This returns the terminal echo mode. This returns True if echo is
809+ on or False if echo is off. Child applications that are expecting you
810+ to enter a password often set ECHO False. See waitnoecho(). '''
811+
812+ attr = termios.tcgetattr(self.child_fd)
813+ if attr[3] & termios.ECHO:
814+ return True
815+ return False
816+
817+ def setecho(self, state):
818+ '''This sets the terminal echo mode on or off. Note that anything the
819+ child sent before the echo will be lost, so you should be sure that
820+ your input buffer is empty before you call setecho(). For example, the
821+ following will work as expected::
822+
823+ p = pexpect.spawn('cat') # Echo is on by default.
824+ p.sendline('1234') # We expect see this twice from the child...
825+ p.expect(['1234']) # ... once from the tty echo...
826+ p.expect(['1234']) # ... and again from cat itself.
827+ p.setecho(False) # Turn off tty echo
828+ p.sendline('abcd') # We will set this only once (echoed by cat).
829+ p.sendline('wxyz') # We will set this only once (echoed by cat)
830+ p.expect(['abcd'])
831+ p.expect(['wxyz'])
832+
833+ The following WILL NOT WORK because the lines sent before the setecho
834+ will be lost::
835+
836+ p = pexpect.spawn('cat')
837+ p.sendline('1234')
838+ p.setecho(False) # Turn off tty echo
839+ p.sendline('abcd') # We will set this only once (echoed by cat).
840+ p.sendline('wxyz') # We will set this only once (echoed by cat)
841+ p.expect(['1234'])
842+ p.expect(['1234'])
843+ p.expect(['abcd'])
844+ p.expect(['wxyz'])
845+ '''
846+
847+ self.child_fd
848+ attr = termios.tcgetattr(self.child_fd)
849+ if state:
850+ attr[3] = attr[3] | termios.ECHO
851+ else:
852+ attr[3] = attr[3] & ~termios.ECHO
853+ # I tried TCSADRAIN and TCSAFLUSH, but
854+ # these were inconsistent and blocked on some platforms.
855+ # TCSADRAIN would probably be ideal if it worked.
856+ termios.tcsetattr(self.child_fd, termios.TCSANOW, attr)
857+
858+ def _log(self, s, direction):
859+ if self.logfile is not None:
860+ self.logfile.write(s)
861+ self.logfile.flush()
862+ second_log = self.logfile_send if (direction=='send') else self.logfile_read
863+ if second_log is not None:
864+ second_log.write(s)
865+ second_log.flush()
866+
867+ def read_nonblocking(self, size=1, timeout=-1):
868+ '''This reads at most size characters from the child application. It
869+ includes a timeout. If the read does not complete within the timeout
870+ period then a TIMEOUT exception is raised. If the end of file is read
871+ then an EOF exception will be raised. If a log file was set using
872+ setlog() then all data will also be written to the log file.
873+
874+ If timeout is None then the read may block indefinitely.
875+ If timeout is -1 then the self.timeout value is used. If timeout is 0
876+ then the child is polled and if there is no data immediately ready
877+ then this will raise a TIMEOUT exception.
878+
879+ The timeout refers only to the amount of time to read at least one
880+ character. This is not effected by the 'size' parameter, so if you call
881+ read_nonblocking(size=100, timeout=30) and only one character is
882+ available right away then one character will be returned immediately.
883+ It will not wait for 30 seconds for another 99 characters to come in.
884+
885+ This is a wrapper around os.read(). It uses select.select() to
886+ implement the timeout. '''
887+
888+ if self.closed:
889+ raise ValueError('I/O operation on closed file.')
890+
891+ if timeout == -1:
892+ timeout = self.timeout
893+
894+ # Note that some systems such as Solaris do not give an EOF when
895+ # the child dies. In fact, you can still try to read
896+ # from the child_fd -- it will block forever or until TIMEOUT.
897+ # For this case, I test isalive() before doing any reading.
898+ # If isalive() is false, then I pretend that this is the same as EOF.
899+ if not self.isalive():
900+ # timeout of 0 means "poll"
901+ r, w, e = self.__select([self.child_fd], [], [], 0)
902+ if not r:
903+ self.flag_eof = True
904+ raise EOF('End Of File (EOF). Braindead platform.')
905+ elif self.__irix_hack:
906+ # Irix takes a long time before it realizes a child was terminated.
907+ # FIXME So does this mean Irix systems are forced to always have
908+ # FIXME a 2 second delay when calling read_nonblocking? That sucks.
909+ r, w, e = self.__select([self.child_fd], [], [], 2)
910+ if not r and not self.isalive():
911+ self.flag_eof = True
912+ raise EOF('End Of File (EOF). Slow platform.')
913+
914+ r, w, e = self.__select([self.child_fd], [], [], timeout)
915+
916+ if not r:
917+ if not self.isalive():
918+ # Some platforms, such as Irix, will claim that their
919+ # processes are alive; timeout on the select; and
920+ # then finally admit that they are not alive.
921+ self.flag_eof = True
922+ raise EOF('End of File (EOF). Very slow platform.')
923+ else:
924+ raise TIMEOUT('Timeout exceeded.')
925+
926+ if self.child_fd in r:
927+ try:
928+ s = os.read(self.child_fd, size)
929+ except OSError:
930+ # Linux does this
931+ self.flag_eof = True
932+ raise EOF('End Of File (EOF). Exception style platform.')
933+ if s == b'':
934+ # BSD style
935+ self.flag_eof = True
936+ raise EOF('End Of File (EOF). Empty string style platform.')
937+
938+ s = self._coerce_read_string(s)
939+ self._log(s, 'read')
940+ return s
941+
942+ raise ExceptionPexpect('Reached an unexpected state.')
943+
944+ def read(self, size=-1):
945+ '''This reads at most "size" bytes from the file (less if the read hits
946+ EOF before obtaining size bytes). If the size argument is negative or
947+ omitted, read all data until EOF is reached. The bytes are returned as
948+ a string object. An empty string is returned when EOF is encountered
949+ immediately. '''
950+
951+ if size == 0:
952+ return self.string_type()
953+ if size < 0:
954+ # delimiter default is EOF
955+ self.expect(self.delimiter)
956+ return self.before
957+
958+ # I could have done this more directly by not using expect(), but
959+ # I deliberately decided to couple read() to expect() so that
960+ # I would catch any bugs early and ensure consistant behavior.
961+ # It's a little less efficient, but there is less for me to
962+ # worry about if I have to later modify read() or expect().
963+ # Note, it's OK if size==-1 in the regex. That just means it
964+ # will never match anything in which case we stop only on EOF.
965+ cre = re.compile(self._coerce_expect_string('.{%d}' % size), re.DOTALL)
966+ # delimiter default is EOF
967+ index = self.expect([cre, self.delimiter])
968+ if index == 0:
969+ ### FIXME self.before should be ''. Should I assert this?
970+ return self.after
971+ return self.before
972+
973+ def readline(self, size=-1):
974+ '''This reads and returns one entire line. The newline at the end of
975+ line is returned as part of the string, unless the file ends without a
976+ newline. An empty string is returned if EOF is encountered immediately.
977+ This looks for a newline as a CR/LF pair (\\r\\n) even on UNIX because
978+ this is what the pseudotty device returns. So contrary to what you may
979+ expect you will receive newlines as \\r\\n.
980+
981+ If the size argument is 0 then an empty string is returned. In all
982+ other cases the size argument is ignored, which is not standard
983+ behavior for a file-like object. '''
984+
985+ if size == 0:
986+ return self.string_type()
987+ # delimiter default is EOF
988+ index = self.expect([b'\r\n', self.delimiter])
989+ if index == 0:
990+ return self.before + b'\r\n'
991+ else:
992+ return self.before
993+
994+ def __iter__(self):
995+ '''This is to support iterators over a file-like object.
996+ '''
997+ return iter(self.readline, self.string_type())
998+
999+ def readlines(self, sizehint=-1):
1000+ '''This reads until EOF using readline() and returns a list containing
1001+ the lines thus read. The optional 'sizehint' argument is ignored.
1002+ Remember, because this reads until EOF that means the child
1003+ process should have closed its stdout. If you run this method on
1004+ a child that is still running with its stdout open then this
1005+ method will block until it timesout.'''
1006+
1007+ lines = []
1008+ while True:
1009+ line = self.readline()
1010+ if not line:
1011+ break
1012+ lines.append(line)
1013+ return lines
1014+
1015+ def write(self, s):
1016+ '''This is similar to send() except that there is no return value.
1017+ '''
1018+
1019+ self.send(s)
1020+
1021+ def writelines(self, sequence):
1022+ '''This calls write() for each element in the sequence. The sequence
1023+ can be any iterable object producing strings, typically a list of
1024+ strings. This does not add line separators. There is no return value.
1025+ '''
1026+
1027+ for s in sequence:
1028+ self.write(s)
1029+
1030+ def send(self, s):
1031+ '''Sends string ``s`` to the child process, returning the number of
1032+ bytes written. If a logfile is specified, a copy is written to that
1033+ log. '''
1034+
1035+ time.sleep(self.delaybeforesend)
1036+
1037+ s = self._coerce_send_string(s)
1038+ self._log(s, 'send')
1039+
1040+ return self._send(s)
1041+
1042+ def _send(self, s):
1043+ return os.write(self.child_fd, s)
1044+
1045+ def sendline(self, s=''):
1046+ '''Wraps send(), sending string ``s`` to child process, with os.linesep
1047+ automatically appended. Returns number of bytes written. '''
1048+
1049+ n = self.send(s)
1050+ n = n + self.send(self.linesep)
1051+ return n
1052+
1053+ def sendcontrol(self, char):
1054+
1055+ '''Helper method that wraps send() with mnemonic access for sending control
1056+ character to the child (such as Ctrl-C or Ctrl-D). For example, to send
1057+ Ctrl-G (ASCII 7, bell, '\a')::
1058+
1059+ child.sendcontrol('g')
1060+
1061+ See also, sendintr() and sendeof().
1062+ '''
1063+
1064+ char = char.lower()
1065+ a = ord(char)
1066+ if a >= 97 and a <= 122:
1067+ a = a - ord('a') + 1
1068+ return self.send(self._chr(a))
1069+ d = {'@': 0, '`': 0,
1070+ '[': 27, '{': 27,
1071+ '\\': 28, '|': 28,
1072+ ']': 29, '}': 29,
1073+ '^': 30, '~': 30,
1074+ '_': 31,
1075+ '?': 127}
1076+ if char not in d:
1077+ return 0
1078+ return self.send(self._chr(d[char]))
1079+
1080+ def sendeof(self):
1081+
1082+ '''This sends an EOF to the child. This sends a character which causes
1083+ the pending parent output buffer to be sent to the waiting child
1084+ program without waiting for end-of-line. If it is the first character
1085+ of the line, the read() in the user program returns 0, which signifies
1086+ end-of-file. This means to work as expected a sendeof() has to be
1087+ called at the beginning of a line. This method does not send a newline.
1088+ It is the responsibility of the caller to ensure the eof is sent at the
1089+ beginning of a line. '''
1090+
1091+ ### Hmmm... how do I send an EOF?
1092+ ###C if ((m = write(pty, *buf, p - *buf)) < 0)
1093+ ###C return (errno == EWOULDBLOCK) ? n : -1;
1094+ #fd = sys.stdin.fileno()
1095+ #old = termios.tcgetattr(fd) # remember current state
1096+ #attr = termios.tcgetattr(fd)
1097+ #attr[3] = attr[3] | termios.ICANON # ICANON must be set to see EOF
1098+ #try: # use try/finally to ensure state gets restored
1099+ # termios.tcsetattr(fd, termios.TCSADRAIN, attr)
1100+ # if hasattr(termios, 'CEOF'):
1101+ # os.write(self.child_fd, '%c' % termios.CEOF)
1102+ # else:
1103+ # # Silly platform does not define CEOF so assume CTRL-D
1104+ # os.write(self.child_fd, '%c' % 4)
1105+ #finally: # restore state
1106+ # termios.tcsetattr(fd, termios.TCSADRAIN, old)
1107+ if hasattr(termios, 'VEOF'):
1108+ char = ord(termios.tcgetattr(self.child_fd)[6][termios.VEOF])
1109+ else:
1110+ # platform does not define VEOF so assume CTRL-D
1111+ char = 4
1112+ self.send(self._chr(char))
1113+
1114+ def sendintr(self):
1115+
1116+ '''This sends a SIGINT to the child. It does not require
1117+ the SIGINT to be the first character on a line. '''
1118+
1119+ if hasattr(termios, 'VINTR'):
1120+ char = ord(termios.tcgetattr(self.child_fd)[6][termios.VINTR])
1121+ else:
1122+ # platform does not define VINTR so assume CTRL-C
1123+ char = 3
1124+ self.send(self._chr(char))
1125+
1126+ def eof(self):
1127+
1128+ '''This returns True if the EOF exception was ever raised.
1129+ '''
1130+
1131+ return self.flag_eof
1132+
1133+ def terminate(self, force=False):
1134+
1135+ '''This forces a child process to terminate. It starts nicely with
1136+ SIGHUP and SIGINT. If "force" is True then moves onto SIGKILL. This
1137+ returns True if the child was terminated. This returns False if the
1138+ child could not be terminated. '''
1139+
1140+ if not self.isalive():
1141+ return True
1142+ try:
1143+ self.kill(signal.SIGHUP)
1144+ time.sleep(self.delayafterterminate)
1145+ if not self.isalive():
1146+ return True
1147+ self.kill(signal.SIGCONT)
1148+ time.sleep(self.delayafterterminate)
1149+ if not self.isalive():
1150+ return True
1151+ self.kill(signal.SIGINT)
1152+ time.sleep(self.delayafterterminate)
1153+ if not self.isalive():
1154+ return True
1155+ if force:
1156+ self.kill(signal.SIGKILL)
1157+ time.sleep(self.delayafterterminate)
1158+ if not self.isalive():
1159+ return True
1160+ else:
1161+ return False
1162+ return False
1163+ except OSError:
1164+ # I think there are kernel timing issues that sometimes cause
1165+ # this to happen. I think isalive() reports True, but the
1166+ # process is dead to the kernel.
1167+ # Make one last attempt to see if the kernel is up to date.
1168+ time.sleep(self.delayafterterminate)
1169+ if not self.isalive():
1170+ return True
1171+ else:
1172+ return False
1173+
1174+ def wait(self):
1175+
1176+ '''This waits until the child exits. This is a blocking call. This will
1177+ not read any data from the child, so this will block forever if the
1178+ child has unread output and has terminated. In other words, the child
1179+ may have printed output then called exit(), but, the child is
1180+ technically still alive until its output is read by the parent. '''
1181+
1182+ if self.isalive():
1183+ pid, status = os.waitpid(self.pid, 0)
1184+ else:
1185+ raise ExceptionPexpect('Cannot wait for dead child process.')
1186+ self.exitstatus = os.WEXITSTATUS(status)
1187+ if os.WIFEXITED(status):
1188+ self.status = status
1189+ self.exitstatus = os.WEXITSTATUS(status)
1190+ self.signalstatus = None
1191+ self.terminated = True
1192+ elif os.WIFSIGNALED(status):
1193+ self.status = status
1194+ self.exitstatus = None
1195+ self.signalstatus = os.WTERMSIG(status)
1196+ self.terminated = True
1197+ elif os.WIFSTOPPED(status):
1198+ # You can't call wait() on a child process in the stopped state.
1199+ raise ExceptionPexpect('Called wait() on a stopped child ' +
1200+ 'process. This is not supported. Is some other ' +
1201+ 'process attempting job control with our child pid?')
1202+ return self.exitstatus
1203+
1204+ def isalive(self):
1205+
1206+ '''This tests if the child process is running or not. This is
1207+ non-blocking. If the child was terminated then this will read the
1208+ exitstatus or signalstatus of the child. This returns True if the child
1209+ process appears to be running or False if not. It can take literally
1210+ SECONDS for Solaris to return the right status. '''
1211+
1212+ if self.terminated:
1213+ return False
1214+
1215+ if self.flag_eof:
1216+ # This is for Linux, which requires the blocking form
1217+ # of waitpid to # get status of a defunct process.
1218+ # This is super-lame. The flag_eof would have been set
1219+ # in read_nonblocking(), so this should be safe.
1220+ waitpid_options = 0
1221+ else:
1222+ waitpid_options = os.WNOHANG
1223+
1224+ try:
1225+ pid, status = os.waitpid(self.pid, waitpid_options)
1226+ except OSError:
1227+ err = sys.exc_info()[1]
1228+ # No child processes
1229+ if err.errno == errno.ECHILD:
1230+ raise ExceptionPexpect('isalive() encountered condition ' +
1231+ 'where "terminated" is 0, but there was no child ' +
1232+ 'process. Did someone else call waitpid() ' +
1233+ 'on our process?')
1234+ else:
1235+ raise err
1236+
1237+ # I have to do this twice for Solaris.
1238+ # I can't even believe that I figured this out...
1239+ # If waitpid() returns 0 it means that no child process
1240+ # wishes to report, and the value of status is undefined.
1241+ if pid == 0:
1242+ try:
1243+ ### os.WNOHANG) # Solaris!
1244+ pid, status = os.waitpid(self.pid, waitpid_options)
1245+ except OSError as e:
1246+ # This should never happen...
1247+ if e.errno == errno.ECHILD:
1248+ raise ExceptionPexpect('isalive() encountered condition ' +
1249+ 'that should never happen. There was no child ' +
1250+ 'process. Did someone else call waitpid() ' +
1251+ 'on our process?')
1252+ else:
1253+ raise
1254+
1255+ # If pid is still 0 after two calls to waitpid() then the process
1256+ # really is alive. This seems to work on all platforms, except for
1257+ # Irix which seems to require a blocking call on waitpid or select,
1258+ # so I let read_nonblocking take care of this situation
1259+ # (unfortunately, this requires waiting through the timeout).
1260+ if pid == 0:
1261+ return True
1262+
1263+ if pid == 0:
1264+ return True
1265+
1266+ if os.WIFEXITED(status):
1267+ self.status = status
1268+ self.exitstatus = os.WEXITSTATUS(status)
1269+ self.signalstatus = None
1270+ self.terminated = True
1271+ elif os.WIFSIGNALED(status):
1272+ self.status = status
1273+ self.exitstatus = None
1274+ self.signalstatus = os.WTERMSIG(status)
1275+ self.terminated = True
1276+ elif os.WIFSTOPPED(status):
1277+ raise ExceptionPexpect('isalive() encountered condition ' +
1278+ 'where child process is stopped. This is not ' +
1279+ 'supported. Is some other process attempting ' +
1280+ 'job control with our child pid?')
1281+ return False
1282+
1283+ def kill(self, sig):
1284+
1285+ '''This sends the given signal to the child application. In keeping
1286+ with UNIX tradition it has a misleading name. It does not necessarily
1287+ kill the child unless you send the right signal. '''
1288+
1289+ # Same as os.kill, but the pid is given for you.
1290+ if self.isalive():
1291+ os.kill(self.pid, sig)
1292+
1293+ def _pattern_type_err(self, pattern):
1294+ raise TypeError('got {badtype} ({badobj!r}) as pattern, must be one'
1295+ ' of: {goodtypes}, pexpect.EOF, pexpect.TIMEOUT'\
1296+ .format(badtype=type(pattern),
1297+ badobj=pattern,
1298+ goodtypes=', '.join([str(ast)\
1299+ for ast in self.allowed_string_types])
1300+ )
1301+ )
1302+
1303+ def compile_pattern_list(self, patterns):
1304+
1305+ '''This compiles a pattern-string or a list of pattern-strings.
1306+ Patterns must be a StringType, EOF, TIMEOUT, SRE_Pattern, or a list of
1307+ those. Patterns may also be None which results in an empty list (you
1308+ might do this if waiting for an EOF or TIMEOUT condition without
1309+ expecting any pattern).
1310+
1311+ This is used by expect() when calling expect_list(). Thus expect() is
1312+ nothing more than::
1313+
1314+ cpl = self.compile_pattern_list(pl)
1315+ return self.expect_list(cpl, timeout)
1316+
1317+ If you are using expect() within a loop it may be more
1318+ efficient to compile the patterns first and then call expect_list().
1319+ This avoid calls in a loop to compile_pattern_list()::
1320+
1321+ cpl = self.compile_pattern_list(my_pattern)
1322+ while some_condition:
1323+ ...
1324+ i = self.expect_list(clp, timeout)
1325+ ...
1326+ '''
1327+
1328+ if patterns is None:
1329+ return []
1330+ if not isinstance(patterns, list):
1331+ patterns = [patterns]
1332+
1333+ # Allow dot to match \n
1334+ compile_flags = re.DOTALL
1335+ if self.ignorecase:
1336+ compile_flags = compile_flags | re.IGNORECASE
1337+ compiled_pattern_list = []
1338+ for idx, p in enumerate(patterns):
1339+ if isinstance(p, self.allowed_string_types):
1340+ p = self._coerce_expect_string(p)
1341+ compiled_pattern_list.append(re.compile(p, compile_flags))
1342+ elif p is EOF:
1343+ compiled_pattern_list.append(EOF)
1344+ elif p is TIMEOUT:
1345+ compiled_pattern_list.append(TIMEOUT)
1346+ elif isinstance(p, type(re.compile(''))):
1347+ compiled_pattern_list.append(p)
1348+ else:
1349+ self._pattern_type_err(p)
1350+ return compiled_pattern_list
1351+
1352+ def expect(self, pattern, timeout=-1, searchwindowsize=-1):
1353+
1354+ '''This seeks through the stream until a pattern is matched. The
1355+ pattern is overloaded and may take several types. The pattern can be a
1356+ StringType, EOF, a compiled re, or a list of any of those types.
1357+ Strings will be compiled to re types. This returns the index into the
1358+ pattern list. If the pattern was not a list this returns index 0 on a
1359+ successful match. This may raise exceptions for EOF or TIMEOUT. To
1360+ avoid the EOF or TIMEOUT exceptions add EOF or TIMEOUT to the pattern
1361+ list. That will cause expect to match an EOF or TIMEOUT condition
1362+ instead of raising an exception.
1363+
1364+ If you pass a list of patterns and more than one matches, the first
1365+ match in the stream is chosen. If more than one pattern matches at that
1366+ point, the leftmost in the pattern list is chosen. For example::
1367+
1368+ # the input is 'foobar'
1369+ index = p.expect(['bar', 'foo', 'foobar'])
1370+ # returns 1('foo') even though 'foobar' is a "better" match
1371+
1372+ Please note, however, that buffering can affect this behavior, since
1373+ input arrives in unpredictable chunks. For example::
1374+
1375+ # the input is 'foobar'
1376+ index = p.expect(['foobar', 'foo'])
1377+ # returns 0('foobar') if all input is available at once,
1378+ # but returs 1('foo') if parts of the final 'bar' arrive late
1379+
1380+ After a match is found the instance attributes 'before', 'after' and
1381+ 'match' will be set. You can see all the data read before the match in
1382+ 'before'. You can see the data that was matched in 'after'. The
1383+ re.MatchObject used in the re match will be in 'match'. If an error
1384+ occurred then 'before' will be set to all the data read so far and
1385+ 'after' and 'match' will be None.
1386+
1387+ If timeout is -1 then timeout will be set to the self.timeout value.
1388+
1389+ A list entry may be EOF or TIMEOUT instead of a string. This will
1390+ catch these exceptions and return the index of the list entry instead
1391+ of raising the exception. The attribute 'after' will be set to the
1392+ exception type. The attribute 'match' will be None. This allows you to
1393+ write code like this::
1394+
1395+ index = p.expect(['good', 'bad', pexpect.EOF, pexpect.TIMEOUT])
1396+ if index == 0:
1397+ do_something()
1398+ elif index == 1:
1399+ do_something_else()
1400+ elif index == 2:
1401+ do_some_other_thing()
1402+ elif index == 3:
1403+ do_something_completely_different()
1404+
1405+ instead of code like this::
1406+
1407+ try:
1408+ index = p.expect(['good', 'bad'])
1409+ if index == 0:
1410+ do_something()
1411+ elif index == 1:
1412+ do_something_else()
1413+ except EOF:
1414+ do_some_other_thing()
1415+ except TIMEOUT:
1416+ do_something_completely_different()
1417+
1418+ These two forms are equivalent. It all depends on what you want. You
1419+ can also just expect the EOF if you are waiting for all output of a
1420+ child to finish. For example::
1421+
1422+ p = pexpect.spawn('/bin/ls')
1423+ p.expect(pexpect.EOF)
1424+ print p.before
1425+
1426+ If you are trying to optimize for speed then see expect_list().
1427+ '''
1428+
1429+ compiled_pattern_list = self.compile_pattern_list(pattern)
1430+ return self.expect_list(compiled_pattern_list,
1431+ timeout, searchwindowsize)
1432+
1433+ def expect_list(self, pattern_list, timeout=-1, searchwindowsize=-1):
1434+
1435+ '''This takes a list of compiled regular expressions and returns the
1436+ index into the pattern_list that matched the child output. The list may
1437+ also contain EOF or TIMEOUT(which are not compiled regular
1438+ expressions). This method is similar to the expect() method except that
1439+ expect_list() does not recompile the pattern list on every call. This
1440+ may help if you are trying to optimize for speed, otherwise just use
1441+ the expect() method. This is called by expect(). If timeout==-1 then
1442+ the self.timeout value is used. If searchwindowsize==-1 then the
1443+ self.searchwindowsize value is used. '''
1444+
1445+ return self.expect_loop(searcher_re(pattern_list),
1446+ timeout, searchwindowsize)
1447+
1448+ def expect_exact(self, pattern_list, timeout=-1, searchwindowsize=-1):
1449+
1450+ '''This is similar to expect(), but uses plain string matching instead
1451+ of compiled regular expressions in 'pattern_list'. The 'pattern_list'
1452+ may be a string; a list or other sequence of strings; or TIMEOUT and
1453+ EOF.
1454+
1455+ This call might be faster than expect() for two reasons: string
1456+ searching is faster than RE matching and it is possible to limit the
1457+ search to just the end of the input buffer.
1458+
1459+ This method is also useful when you don't want to have to worry about
1460+ escaping regular expression characters that you want to match.'''
1461+
1462+ if (isinstance(pattern_list, self.allowed_string_types) or
1463+ pattern_list in (TIMEOUT, EOF)):
1464+ pattern_list = [pattern_list]
1465+
1466+ def prepare_pattern(pattern):
1467+ if pattern in (TIMEOUT, EOF):
1468+ return pattern
1469+ if isinstance(pattern, self.allowed_string_types):
1470+ return self._coerce_expect_string(pattern)
1471+ self._pattern_type_err(pattern)
1472+
1473+ try:
1474+ pattern_list = iter(pattern_list)
1475+ except TypeError:
1476+ self._pattern_type_err(pattern_list)
1477+ pattern_list = [prepare_pattern(p) for p in pattern_list]
1478+ return self.expect_loop(searcher_string(pattern_list),
1479+ timeout, searchwindowsize)
1480+
1481+ def expect_loop(self, searcher, timeout=-1, searchwindowsize=-1):
1482+
1483+ '''This is the common loop used inside expect. The 'searcher' should be
1484+ an instance of searcher_re or searcher_string, which describes how and
1485+ what to search for in the input.
1486+
1487+ See expect() for other arguments, return value and exceptions. '''
1488+
1489+ self.searcher = searcher
1490+
1491+ if timeout == -1:
1492+ timeout = self.timeout
1493+ if timeout is not None:
1494+ end_time = time.time() + timeout
1495+ if searchwindowsize == -1:
1496+ searchwindowsize = self.searchwindowsize
1497+
1498+ try:
1499+ incoming = self.buffer
1500+ freshlen = len(incoming)
1501+ while True:
1502+ # Keep reading until exception or return.
1503+ index = searcher.search(incoming, freshlen, searchwindowsize)
1504+ if index >= 0:
1505+ self.buffer = incoming[searcher.end:]
1506+ self.before = incoming[: searcher.start]
1507+ self.after = incoming[searcher.start: searcher.end]
1508+ self.match = searcher.match
1509+ self.match_index = index
1510+ return self.match_index
1511+ # No match at this point
1512+ if (timeout is not None) and (timeout < 0):
1513+ raise TIMEOUT('Timeout exceeded in expect_any().')
1514+ # Still have time left, so read more data
1515+ c = self.read_nonblocking(self.maxread, timeout)
1516+ freshlen = len(c)
1517+ time.sleep(0.0001)
1518+ incoming = incoming + c
1519+ if timeout is not None:
1520+ timeout = end_time - time.time()
1521+ except EOF:
1522+ err = sys.exc_info()[1]
1523+ self.buffer = self.string_type()
1524+ self.before = incoming
1525+ self.after = EOF
1526+ index = searcher.eof_index
1527+ if index >= 0:
1528+ self.match = EOF
1529+ self.match_index = index
1530+ return self.match_index
1531+ else:
1532+ self.match = None
1533+ self.match_index = None
1534+ raise EOF(str(err) + '\n' + str(self))
1535+ except TIMEOUT:
1536+ err = sys.exc_info()[1]
1537+ self.buffer = incoming
1538+ self.before = incoming
1539+ self.after = TIMEOUT
1540+ index = searcher.timeout_index
1541+ if index >= 0:
1542+ self.match = TIMEOUT
1543+ self.match_index = index
1544+ return self.match_index
1545+ else:
1546+ self.match = None
1547+ self.match_index = None
1548+ raise TIMEOUT(str(err) + '\n' + str(self))
1549+ except:
1550+ self.before = incoming
1551+ self.after = None
1552+ self.match = None
1553+ self.match_index = None
1554+ raise
1555+
1556+ def getwinsize(self):
1557+
1558+ '''This returns the terminal window size of the child tty. The return
1559+ value is a tuple of (rows, cols). '''
1560+
1561+ TIOCGWINSZ = getattr(termios, 'TIOCGWINSZ', 1074295912)
1562+ s = struct.pack('HHHH', 0, 0, 0, 0)
1563+ x = fcntl.ioctl(self.child_fd, TIOCGWINSZ, s)
1564+ return struct.unpack('HHHH', x)[0:2]
1565+
1566+ def setwinsize(self, rows, cols):
1567+
1568+ '''This sets the terminal window size of the child tty. This will cause
1569+ a SIGWINCH signal to be sent to the child. This does not change the
1570+ physical window size. It changes the size reported to TTY-aware
1571+ applications like vi or curses -- applications that respond to the
1572+ SIGWINCH signal. '''
1573+
1574+ # Check for buggy platforms. Some Python versions on some platforms
1575+ # (notably OSF1 Alpha and RedHat 7.1) truncate the value for
1576+ # termios.TIOCSWINSZ. It is not clear why this happens.
1577+ # These platforms don't seem to handle the signed int very well;
1578+ # yet other platforms like OpenBSD have a large negative value for
1579+ # TIOCSWINSZ and they don't have a truncate problem.
1580+ # Newer versions of Linux have totally different values for TIOCSWINSZ.
1581+ # Note that this fix is a hack.
1582+ TIOCSWINSZ = getattr(termios, 'TIOCSWINSZ', -2146929561)
1583+ if TIOCSWINSZ == 2148037735:
1584+ # Same bits, but with sign.
1585+ TIOCSWINSZ = -2146929561
1586+ # Note, assume ws_xpixel and ws_ypixel are zero.
1587+ s = struct.pack('HHHH', rows, cols, 0, 0)
1588+ fcntl.ioctl(self.fileno(), TIOCSWINSZ, s)
1589+
1590+ def interact(self, escape_character=chr(29),
1591+ input_filter=None, output_filter=None):
1592+
1593+ '''This gives control of the child process to the interactive user (the
1594+ human at the keyboard). Keystrokes are sent to the child process, and
1595+ the stdout and stderr output of the child process is printed. This
1596+ simply echos the child stdout and child stderr to the real stdout and
1597+ it echos the real stdin to the child stdin. When the user types the
1598+ escape_character this method will stop. The default for
1599+ escape_character is ^]. This should not be confused with ASCII 27 --
1600+ the ESC character. ASCII 29 was chosen for historical merit because
1601+ this is the character used by 'telnet' as the escape character. The
1602+ escape_character will not be sent to the child process.
1603+
1604+ You may pass in optional input and output filter functions. These
1605+ functions should take a string and return a string. The output_filter
1606+ will be passed all the output from the child process. The input_filter
1607+ will be passed all the keyboard input from the user. The input_filter
1608+ is run BEFORE the check for the escape_character.
1609+
1610+ Note that if you change the window size of the parent the SIGWINCH
1611+ signal will not be passed through to the child. If you want the child
1612+ window size to change when the parent's window size changes then do
1613+ something like the following example::
1614+
1615+ import pexpect, struct, fcntl, termios, signal, sys
1616+ def sigwinch_passthrough (sig, data):
1617+ s = struct.pack("HHHH", 0, 0, 0, 0)
1618+ a = struct.unpack('hhhh', fcntl.ioctl(sys.stdout.fileno(),
1619+ termios.TIOCGWINSZ , s))
1620+ global p
1621+ p.setwinsize(a[0],a[1])
1622+ # Note this 'p' global and used in sigwinch_passthrough.
1623+ p = pexpect.spawn('/bin/bash')
1624+ signal.signal(signal.SIGWINCH, sigwinch_passthrough)
1625+ p.interact()
1626+ '''
1627+
1628+ # Flush the buffer.
1629+ self.write_to_stdout(self.buffer)
1630+ self.stdout.flush()
1631+ self.buffer = self.string_type()
1632+ mode = tty.tcgetattr(self.STDIN_FILENO)
1633+ tty.setraw(self.STDIN_FILENO)
1634+ if PY3:
1635+ escape_character = escape_character.encode('latin-1')
1636+ try:
1637+ self.__interact_copy(escape_character, input_filter, output_filter)
1638+ finally:
1639+ tty.tcsetattr(self.STDIN_FILENO, tty.TCSAFLUSH, mode)
1640+
1641+ def __interact_writen(self, fd, data):
1642+ '''This is used by the interact() method.
1643+ '''
1644+
1645+ while data != b'' and self.isalive():
1646+ n = os.write(fd, data)
1647+ data = data[n:]
1648+
1649+ def __interact_read(self, fd):
1650+ '''This is used by the interact() method.
1651+ '''
1652+
1653+ return os.read(fd, 1000)
1654+
1655+ def __interact_copy(self, escape_character=None,
1656+ input_filter=None, output_filter=None):
1657+
1658+ '''This is used by the interact() method.
1659+ '''
1660+
1661+ while self.isalive():
1662+ r, w, e = self.__select([self.child_fd, self.STDIN_FILENO], [], [])
1663+ if self.child_fd in r:
1664+ try:
1665+ data = self.__interact_read(self.child_fd)
1666+ except OSError as e:
1667+ # The subprocess may have closed before we get to reading it
1668+ if e.errno != errno.EIO:
1669+ raise
1670+ if output_filter:
1671+ data = output_filter(data)
1672+ if self.logfile is not None:
1673+ self.logfile.write(data)
1674+ self.logfile.flush()
1675+ os.write(self.STDOUT_FILENO, data)
1676+ if self.STDIN_FILENO in r:
1677+ data = self.__interact_read(self.STDIN_FILENO)
1678+ if input_filter:
1679+ data = input_filter(data)
1680+ i = data.rfind(escape_character)
1681+ if i != -1:
1682+ data = data[:i]
1683+ self.__interact_writen(self.child_fd, data)
1684+ break
1685+ self.__interact_writen(self.child_fd, data)
1686+
1687+ def __select(self, iwtd, owtd, ewtd, timeout=None):
1688+
1689+ '''This is a wrapper around select.select() that ignores signals. If
1690+ select.select raises a select.error exception and errno is an EINTR
1691+ error then it is ignored. Mainly this is used to ignore sigwinch
1692+ (terminal resize). '''
1693+
1694+ # if select() is interrupted by a signal (errno==EINTR) then
1695+ # we loop back and enter the select() again.
1696+ if timeout is not None:
1697+ end_time = time.time() + timeout
1698+ while True:
1699+ try:
1700+ return select.select(iwtd, owtd, ewtd, timeout)
1701+ except select.error:
1702+ err = sys.exc_info()[1]
1703+ if err.errno == errno.EINTR:
1704+ # if we loop back we have to subtract the
1705+ # amount of time we already waited.
1706+ if timeout is not None:
1707+ timeout = end_time - time.time()
1708+ if timeout < 0:
1709+ return([], [], [])
1710+ else:
1711+ # something else caused the select.error, so
1712+ # this actually is an exception.
1713+ raise
1714+
1715+##############################################################################
1716+# The following methods are no longer supported or allowed.
1717+
1718+ def setmaxread(self, maxread):
1719+
1720+ '''This method is no longer supported or allowed. I don't like getters
1721+ and setters without a good reason. '''
1722+
1723+ raise ExceptionPexpect('This method is no longer supported ' +
1724+ 'or allowed. Just assign a value to the ' +
1725+ 'maxread member variable.')
1726+
1727+ def setlog(self, fileobject):
1728+
1729+ '''This method is no longer supported or allowed.
1730+ '''
1731+
1732+ raise ExceptionPexpect('This method is no longer supported ' +
1733+ 'or allowed. Just assign a value to the logfile ' +
1734+ 'member variable.')
1735+
1736+##############################################################################
1737+# End of spawn class
1738+##############################################################################
1739+
1740+class spawnu(spawn):
1741+ """Works like spawn, but accepts and returns unicode strings.
1742+
1743+ Extra parameters:
1744+
1745+ :param encoding: The encoding to use for communications (default: 'utf-8')
1746+ :param errors: How to handle encoding/decoding errors; one of 'strict'
1747+ (the default), 'ignore', or 'replace', as described
1748+ for :meth:`~bytes.decode` and :meth:`~str.encode`.
1749+ """
1750+ if PY3:
1751+ string_type = str
1752+ allowed_string_types = (str, )
1753+ _chr = staticmethod(chr)
1754+ linesep = os.linesep
1755+ else:
1756+ string_type = unicode
1757+ allowed_string_types = (unicode, )
1758+ _chr = staticmethod(unichr)
1759+ linesep = os.linesep.decode('ascii')
1760+ # This can handle unicode in both Python 2 and 3
1761+ write_to_stdout = sys.stdout.write
1762+
1763+ def __init__(self, *args, **kwargs):
1764+ self.encoding = kwargs.pop('encoding', 'utf-8')
1765+ self.errors = kwargs.pop('errors', 'strict')
1766+ self._decoder = codecs.getincrementaldecoder(self.encoding)(errors=self.errors)
1767+ super(spawnu, self).__init__(*args, **kwargs)
1768+
1769+ @staticmethod
1770+ def _coerce_expect_string(s):
1771+ return s
1772+
1773+ @staticmethod
1774+ def _coerce_send_string(s):
1775+ return s
1776+
1777+ def _coerce_read_string(self, s):
1778+ return self._decoder.decode(s, final=False)
1779+
1780+ def _send(self, s):
1781+ return os.write(self.child_fd, s.encode(self.encoding, self.errors))
1782+
1783+
1784+class searcher_string(object):
1785+
1786+ '''This is a plain string search helper for the spawn.expect_any() method.
1787+ This helper class is for speed. For more powerful regex patterns
1788+ see the helper class, searcher_re.
1789+
1790+ Attributes:
1791+
1792+ eof_index - index of EOF, or -1
1793+ timeout_index - index of TIMEOUT, or -1
1794+
1795+ After a successful match by the search() method the following attributes
1796+ are available:
1797+
1798+ start - index into the buffer, first byte of match
1799+ end - index into the buffer, first byte after match
1800+ match - the matching string itself
1801+
1802+ '''
1803+
1804+ def __init__(self, strings):
1805+
1806+ '''This creates an instance of searcher_string. This argument 'strings'
1807+ may be a list; a sequence of strings; or the EOF or TIMEOUT types. '''
1808+
1809+ self.eof_index = -1
1810+ self.timeout_index = -1
1811+ self._strings = []
1812+ for n, s in enumerate(strings):
1813+ if s is EOF:
1814+ self.eof_index = n
1815+ continue
1816+ if s is TIMEOUT:
1817+ self.timeout_index = n
1818+ continue
1819+ self._strings.append((n, s))
1820+
1821+ def __str__(self):
1822+
1823+ '''This returns a human-readable string that represents the state of
1824+ the object.'''
1825+
1826+ ss = [(ns[0], ' %d: "%s"' % ns) for ns in self._strings]
1827+ ss.append((-1, 'searcher_string:'))
1828+ if self.eof_index >= 0:
1829+ ss.append((self.eof_index, ' %d: EOF' % self.eof_index))
1830+ if self.timeout_index >= 0:
1831+ ss.append((self.timeout_index,
1832+ ' %d: TIMEOUT' % self.timeout_index))
1833+ ss.sort()
1834+ ss = list(zip(*ss))[1]
1835+ return '\n'.join(ss)
1836+
1837+ def search(self, buffer, freshlen, searchwindowsize=None):
1838+
1839+ '''This searches 'buffer' for the first occurence of one of the search
1840+ strings. 'freshlen' must indicate the number of bytes at the end of
1841+ 'buffer' which have not been searched before. It helps to avoid
1842+ searching the same, possibly big, buffer over and over again.
1843+
1844+ See class spawn for the 'searchwindowsize' argument.
1845+
1846+ If there is a match this returns the index of that string, and sets
1847+ 'start', 'end' and 'match'. Otherwise, this returns -1. '''
1848+
1849+ first_match = None
1850+
1851+ # 'freshlen' helps a lot here. Further optimizations could
1852+ # possibly include:
1853+ #
1854+ # using something like the Boyer-Moore Fast String Searching
1855+ # Algorithm; pre-compiling the search through a list of
1856+ # strings into something that can scan the input once to
1857+ # search for all N strings; realize that if we search for
1858+ # ['bar', 'baz'] and the input is '...foo' we need not bother
1859+ # rescanning until we've read three more bytes.
1860+ #
1861+ # Sadly, I don't know enough about this interesting topic. /grahn
1862+
1863+ for index, s in self._strings:
1864+ if searchwindowsize is None:
1865+ # the match, if any, can only be in the fresh data,
1866+ # or at the very end of the old data
1867+ offset = -(freshlen + len(s))
1868+ else:
1869+ # better obey searchwindowsize
1870+ offset = -searchwindowsize
1871+ n = buffer.find(s, offset)
1872+ if n >= 0 and (first_match is None or n < first_match):
1873+ first_match = n
1874+ best_index, best_match = index, s
1875+ if first_match is None:
1876+ return -1
1877+ self.match = best_match
1878+ self.start = first_match
1879+ self.end = self.start + len(self.match)
1880+ return best_index
1881+
1882+
1883+class searcher_re(object):
1884+
1885+ '''This is regular expression string search helper for the
1886+ spawn.expect_any() method. This helper class is for powerful
1887+ pattern matching. For speed, see the helper class, searcher_string.
1888+
1889+ Attributes:
1890+
1891+ eof_index - index of EOF, or -1
1892+ timeout_index - index of TIMEOUT, or -1
1893+
1894+ After a successful match by the search() method the following attributes
1895+ are available:
1896+
1897+ start - index into the buffer, first byte of match
1898+ end - index into the buffer, first byte after match
1899+ match - the re.match object returned by a succesful re.search
1900+
1901+ '''
1902+
1903+ def __init__(self, patterns):
1904+
1905+ '''This creates an instance that searches for 'patterns' Where
1906+ 'patterns' may be a list or other sequence of compiled regular
1907+ expressions, or the EOF or TIMEOUT types.'''
1908+
1909+ self.eof_index = -1
1910+ self.timeout_index = -1
1911+ self._searches = []
1912+ for n, s in zip(list(range(len(patterns))), patterns):
1913+ if s is EOF:
1914+ self.eof_index = n
1915+ continue
1916+ if s is TIMEOUT:
1917+ self.timeout_index = n
1918+ continue
1919+ self._searches.append((n, s))
1920+
1921+ def __str__(self):
1922+
1923+ '''This returns a human-readable string that represents the state of
1924+ the object.'''
1925+
1926+ #ss = [(n, ' %d: re.compile("%s")' %
1927+ # (n, repr(s.pattern))) for n, s in self._searches]
1928+ ss = list()
1929+ for n, s in self._searches:
1930+ try:
1931+ ss.append((n, ' %d: re.compile("%s")' % (n, s.pattern)))
1932+ except UnicodeEncodeError:
1933+ # for test cases that display __str__ of searches, dont throw
1934+ # another exception just because stdout is ascii-only, using
1935+ # repr()
1936+ ss.append((n, ' %d: re.compile(%r)' % (n, s.pattern)))
1937+ ss.append((-1, 'searcher_re:'))
1938+ if self.eof_index >= 0:
1939+ ss.append((self.eof_index, ' %d: EOF' % self.eof_index))
1940+ if self.timeout_index >= 0:
1941+ ss.append((self.timeout_index, ' %d: TIMEOUT' %
1942+ self.timeout_index))
1943+ ss.sort()
1944+ ss = list(zip(*ss))[1]
1945+ return '\n'.join(ss)
1946+
1947+ def search(self, buffer, freshlen, searchwindowsize=None):
1948+
1949+ '''This searches 'buffer' for the first occurence of one of the regular
1950+ expressions. 'freshlen' must indicate the number of bytes at the end of
1951+ 'buffer' which have not been searched before.
1952+
1953+ See class spawn for the 'searchwindowsize' argument.
1954+
1955+ If there is a match this returns the index of that string, and sets
1956+ 'start', 'end' and 'match'. Otherwise, returns -1.'''
1957+
1958+ first_match = None
1959+ # 'freshlen' doesn't help here -- we cannot predict the
1960+ # length of a match, and the re module provides no help.
1961+ if searchwindowsize is None:
1962+ searchstart = 0
1963+ else:
1964+ searchstart = max(0, len(buffer) - searchwindowsize)
1965+ for index, s in self._searches:
1966+ match = s.search(buffer, searchstart)
1967+ if match is None:
1968+ continue
1969+ n = match.start()
1970+ if first_match is None or n < first_match:
1971+ first_match = n
1972+ the_match = match
1973+ best_index = index
1974+ if first_match is None:
1975+ return -1
1976+ self.start = first_match
1977+ self.match = the_match
1978+ self.end = self.match.end()
1979+ return best_index
1980+
1981+
1982+def which(filename):
1983+
1984+ '''This takes a given filename; tries to find it in the environment path;
1985+ then checks if it is executable. This returns the full path to the filename
1986+ if found and executable. Otherwise this returns None.'''
1987+
1988+ # Special case where filename contains an explicit path.
1989+ if os.path.dirname(filename) != '':
1990+ if os.access(filename, os.X_OK):
1991+ return filename
1992+ if 'PATH' not in os.environ or os.environ['PATH'] == '':
1993+ p = os.defpath
1994+ else:
1995+ p = os.environ['PATH']
1996+ pathlist = p.split(os.pathsep)
1997+ for path in pathlist:
1998+ ff = os.path.join(path, filename)
1999+ if os.access(ff, os.X_OK):
2000+ return ff
2001+ return None
2002+
2003+
2004+def split_command_line(command_line):
2005+
2006+ '''This splits a command line into a list of arguments. It splits arguments
2007+ on spaces, but handles embedded quotes, doublequotes, and escaped
2008+ characters. It's impossible to do this with a regular expression, so I
2009+ wrote a little state machine to parse the command line. '''
2010+
2011+ arg_list = []
2012+ arg = ''
2013+
2014+ # Constants to name the states we can be in.
2015+ state_basic = 0
2016+ state_esc = 1
2017+ state_singlequote = 2
2018+ state_doublequote = 3
2019+ # The state when consuming whitespace between commands.
2020+ state_whitespace = 4
2021+ state = state_basic
2022+
2023+ for c in command_line:
2024+ if state == state_basic or state == state_whitespace:
2025+ if c == '\\':
2026+ # Escape the next character
2027+ state = state_esc
2028+ elif c == r"'":
2029+ # Handle single quote
2030+ state = state_singlequote
2031+ elif c == r'"':
2032+ # Handle double quote
2033+ state = state_doublequote
2034+ elif c.isspace():
2035+ # Add arg to arg_list if we aren't in the middle of whitespace.
2036+ if state == state_whitespace:
2037+ # Do nothing.
2038+ None
2039+ else:
2040+ arg_list.append(arg)
2041+ arg = ''
2042+ state = state_whitespace
2043+ else:
2044+ arg = arg + c
2045+ state = state_basic
2046+ elif state == state_esc:
2047+ arg = arg + c
2048+ state = state_basic
2049+ elif state == state_singlequote:
2050+ if c == r"'":
2051+ state = state_basic
2052+ else:
2053+ arg = arg + c
2054+ elif state == state_doublequote:
2055+ if c == r'"':
2056+ state = state_basic
2057+ else:
2058+ arg = arg + c
2059+
2060+ if arg != '':
2061+ arg_list.append(arg)
2062+ return arg_list
2063+
2064+# vi:set sr et ts=4 sw=4 ft=python :
2065
2066=== modified file 'debian/changelog'
2067--- debian/changelog 2014-01-26 11:22:03 +0000
2068+++ debian/changelog 2014-05-08 13:59:49 +0000
2069@@ -1,3 +1,9 @@
2070+pexpect (3.1-1ubuntu1) UNRELEASED; urgency=medium
2071+
2072+ * Add patch errno_handling_fix to fix interruption handling bug
2073+
2074+ -- Stéphane Duchesneau <stephane.duchesneau@savoirfairelinux.com> Thu, 08 May 2014 09:45:52 -0400
2075+
2076 pexpect (3.1-1) unstable; urgency=medium
2077
2078 * Team upload.
2079
2080=== added directory 'debian/patches'
2081=== added file 'debian/patches/errno_handling_fix.patch'
2082--- debian/patches/errno_handling_fix.patch 1970-01-01 00:00:00 +0000
2083+++ debian/patches/errno_handling_fix.patch 2014-05-08 13:59:49 +0000
2084@@ -0,0 +1,13 @@
2085+Index: pexpect.dev/pexpect/__init__.py
2086+===================================================================
2087+--- pexpect.dev.orig/pexpect/__init__.py 2014-05-08 09:44:22.130208000 -0400
2088++++ pexpect.dev/pexpect/__init__.py 2014-05-08 09:44:53.147617432 -0400
2089+@@ -1687,7 +1687,7 @@
2090+ return select.select(iwtd, owtd, ewtd, timeout)
2091+ except select.error:
2092+ err = sys.exc_info()[1]
2093+- if err.errno == errno.EINTR:
2094++ if err[0] == errno.EINTR:
2095+ # if we loop back we have to subtract the
2096+ # amount of time we already waited.
2097+ if timeout is not None:
2098
2099=== added file 'debian/patches/series'
2100--- debian/patches/series 1970-01-01 00:00:00 +0000
2101+++ debian/patches/series 2014-05-08 13:59:49 +0000
2102@@ -0,0 +1,1 @@
2103+errno_handling_fix.patch
2104
2105=== modified file 'pexpect/__init__.py'
2106--- pexpect/__init__.py 2014-01-26 11:22:03 +0000
2107+++ pexpect/__init__.py 2014-05-08 13:59:49 +0000
2108@@ -1687,7 +1687,7 @@
2109 return select.select(iwtd, owtd, ewtd, timeout)
2110 except select.error:
2111 err = sys.exc_info()[1]
2112- if err.errno == errno.EINTR:
2113+ if err[0] == errno.EINTR:
2114 # if we loop back we have to subtract the
2115 # amount of time we already waited.
2116 if timeout is not None:

Subscribers

People subscribed via source and target branches