@@ -67,8 +67,17 @@ def write(self, s: AnyStr) -> None:
6767 def _pexpect_func (func ) -> Callable [..., Union [Match , AnyStr ]]:
6868 @functools .wraps (func )
6969 def wrapper (
70- self , pattern , * args , expect_all : bool = False , not_matching : List [Union [str , re .Pattern ]] = (), ** kwargs
70+ self ,
71+ pattern ,
72+ * args ,
73+ expect_all : bool = False ,
74+ not_matching : List [Union [str , re .Pattern ]] = (),
75+ return_what_before_match : bool = False ,
76+ ** kwargs ,
7177 ) -> Union [Union [Match , AnyStr ], List [Union [Match , AnyStr ]]]:
78+ if return_what_before_match and expect_all :
79+ raise ValueError ('`return_what_before_match` and `expect_all` cannot be `True` at the same time.' )
80+
7281 patterns = to_list (pattern )
7382 res = []
7483 while patterns :
@@ -101,6 +110,9 @@ def wrapper(
101110 else :
102111 break # one succeeded. leave the loop
103112
113+ if return_what_before_match :
114+ return self .pexpect_proc .before
115+
104116 if len (res ) == 1 :
105117 return res [0 ]
106118
@@ -121,6 +133,8 @@ def expect(self, pattern, **kwargs) -> Match:
121133 expect_all (bool): need to match all specified patterns if this flag is `True`.
122134 Otherwise match any of them could pass
123135 not_matching: string, or compiled regex, or a list of string and compiled regex.
136+ return_what_before_match (bool): return the bytes before the matched pattern.
137+ Cannot be specified together with `expect_all`
124138
125139 Returns:
126140 `AnyStr` or `re.Match`
@@ -143,6 +157,8 @@ def expect_exact(self, pattern, **kwargs) -> Match:
143157 expect_all (bool): need to match all specified patterns if this flag is `True`.
144158 Otherwise match any of them could pass
145159 not_matching: string, or compiled regex, or a list of string and compiled regex.
160+ return_what_before_match (bool): return the bytes before the matched pattern.
161+ Cannot be specified together with `expect_all`
146162
147163 Returns:
148164 `AnyStr` or `re.Match`
0 commit comments