forked from matplotlib/matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.py
More file actions
76 lines (67 loc) · 2.15 KB
/
Copy pathutil.py
File metadata and controls
76 lines (67 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import sys
from matplotlib.compat import subprocess
class MiniExpect:
"""
This is a very basic version of pexpect, providing only the
functionality necessary for the testing framework, built on top of
`subprocess` rather than directly on lower-level calls.
"""
def __init__(self, args):
"""
Start the subprocess so it may start accepting commands.
*args* is a list of commandline arguments to pass to
`subprocess.Popen`.
"""
self._name = args[0]
self._process = subprocess.Popen(
args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
def check_alive(self):
"""
Raises a RuntimeError if the process is no longer alive.
"""
returncode = self._process.poll()
if returncode is not None:
raise RuntimeError("%s unexpectedly quit" % self._name)
def sendline(self, line):
"""
Send a line to the process.
"""
line = line.encode('ascii')
self.check_alive()
stdin = self._process.stdin
stdin.write(line)
stdin.write(b'\n')
stdin.flush()
def expect(self, s, output=None):
"""
Wait for the string *s* to appear in the child process's output.
*output* (optional) is a writable file object where all of the
content preceding *s* will be written.
"""
s = s.encode('ascii')
read = self._process.stdout.read
pos = 0
buf = b''
while True:
self.check_alive()
char = read(1)
if not char:
raise IOError("Unexpected end-of-file")
if sys.version_info[0] >= 3:
match = (ord(char) == s[pos])
else:
match = (char == s[pos])
if match:
buf += char
pos += 1
if pos == len(s):
return
else:
if output is not None:
output.write(buf)
output.write(char)
buf = b''
pos = 0