Pexpect: Return the match object instead of the result

The result is just the *index* of the pattern that matched. But since
we never pass a *list* it's just always 0.

spawn.match is the MatchObject that produced the match, so it can be
used to post-process the matched output, e.g.

```python
m = expect_re('\d+')
m.group() # is now the matched number
```
This commit is contained in:
Fabian Homborg 2020-09-04 17:46:04 +02:00
parent 8f5a84cdc7
commit 04562300e8

View file

@ -182,12 +182,15 @@ class SpawnedProc(object):
On failure, this prints an error and exits.
"""
try:
res = self.spawn.expect(pat, **kwargs)
self.spawn.expect(pat, **kwargs)
when = self.time_since_first_message()
self.messages.append(
Message.received_output(self.spawn.match.group(), when)
)
return res
# When a match is found,
# spawn.match is the MatchObject that produced it.
# This can be used to check what exactly was matched.
return self.spawn.match
except pexpect.ExceptionPexpect as err:
if not pat_desc:
pat_desc = str(pat)