@@ -207,20 +207,19 @@ def __init__(self, keyword):
207
207
def run_keyword (self , args , kwargs = None ):
208
208
args , kwargs = self ._handle_binary_args (args , kwargs or {})
209
209
result = KeywordResult ()
210
- self ._intercept_std_streams ()
211
- try :
212
- return_value = self ._keyword (* args , ** kwargs )
213
- except Exception :
214
- result .set_error (* sys .exc_info ())
215
- else :
210
+ with StandardStreamInterceptor () as interceptor :
216
211
try :
217
- result . set_return ( return_value )
212
+ return_value = self . _keyword ( * args , ** kwargs )
218
213
except Exception :
219
- result .set_error (* sys .exc_info ()[: 2 ] )
214
+ result .set_error (* sys .exc_info ())
220
215
else :
221
- result .set_status ('PASS' )
222
- finally :
223
- result .set_output (self ._restore_std_streams ())
216
+ try :
217
+ result .set_return (return_value )
218
+ except Exception :
219
+ result .set_error (* sys .exc_info ()[:2 ])
220
+ else :
221
+ result .set_status ('PASS' )
222
+ result .set_output (interceptor .output )
224
223
return result .data
225
224
226
225
def _handle_binary_args (self , args , kwargs ):
@@ -231,11 +230,18 @@ def _handle_binary_args(self, args, kwargs):
231
230
def _handle_binary_arg (self , arg ):
232
231
return arg if not isinstance (arg , Binary ) else arg .data
233
232
234
- def _intercept_std_streams (self ):
233
+
234
+ class StandardStreamInterceptor (object ):
235
+
236
+ def __init__ (self ):
237
+ self .output = ''
238
+
239
+ def __enter__ (self ):
235
240
sys .stdout = StringIO ()
236
241
sys .stderr = StringIO ()
242
+ return self
237
243
238
- def _restore_std_streams (self ):
244
+ def __exit__ (self , * exc_info ):
239
245
stdout = sys .stdout .getvalue ()
240
246
stderr = sys .stderr .getvalue ()
241
247
close = [sys .stdout , sys .stderr ]
@@ -249,7 +255,7 @@ def _restore_std_streams(self):
249
255
stderr = '*INFO* %s' % stderr
250
256
if not stdout .endswith ('\n ' ):
251
257
stdout += '\n '
252
- return stdout + stderr
258
+ self . output = stdout + stderr
253
259
254
260
255
261
class KeywordResult (object ):
0 commit comments