From 04562300e8751fc82b7442000b3057e9d70dc51a Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Fri, 4 Sep 2020 17:46:04 +0200 Subject: [PATCH] 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 ``` --- build_tools/pexpect_helper.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/build_tools/pexpect_helper.py b/build_tools/pexpect_helper.py index 2b8ac9253..f94943fdb 100644 --- a/build_tools/pexpect_helper.py +++ b/build_tools/pexpect_helper.py @@ -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)