Skip to content

Commit fa4cf5c

Browse files
committed
Split intercepting std streams to own class.
1 parent 82e348e commit fa4cf5c

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/robotremoteserver.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -207,20 +207,19 @@ def __init__(self, keyword):
207207
def run_keyword(self, args, kwargs=None):
208208
args, kwargs = self._handle_binary_args(args, kwargs or {})
209209
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:
216211
try:
217-
result.set_return(return_value)
212+
return_value = self._keyword(*args, **kwargs)
218213
except Exception:
219-
result.set_error(*sys.exc_info()[:2])
214+
result.set_error(*sys.exc_info())
220215
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)
224223
return result.data
225224

226225
def _handle_binary_args(self, args, kwargs):
@@ -231,11 +230,18 @@ def _handle_binary_args(self, args, kwargs):
231230
def _handle_binary_arg(self, arg):
232231
return arg if not isinstance(arg, Binary) else arg.data
233232

234-
def _intercept_std_streams(self):
233+
234+
class StandardStreamInterceptor(object):
235+
236+
def __init__(self):
237+
self.output = ''
238+
239+
def __enter__(self):
235240
sys.stdout = StringIO()
236241
sys.stderr = StringIO()
242+
return self
237243

238-
def _restore_std_streams(self):
244+
def __exit__(self, *exc_info):
239245
stdout = sys.stdout.getvalue()
240246
stderr = sys.stderr.getvalue()
241247
close = [sys.stdout, sys.stderr]
@@ -249,7 +255,7 @@ def _restore_std_streams(self):
249255
stderr = '*INFO* %s' % stderr
250256
if not stdout.endswith('\n'):
251257
stdout += '\n'
252-
return stdout + stderr
258+
self.output = stdout + stderr
253259

254260

255261
class KeywordResult(object):

0 commit comments

Comments
 (0)