Robot Framework Integrated Development Environment (RIDE)
jython.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 import sys
17 
18 from java.lang import Thread, Runnable
19 
20 
21 class Timeout():
22 
23  def __init__(self, timeout, error):
24  self._timeout_timeout = timeout
25  self._error_error = error
26 
27  def execute(self, runnable):
28  runner = Runner(runnable)
29  thread = Thread(runner, name='RobotFrameworkTimeoutThread')
30  thread.setDaemon(True)
31  thread.start()
32  thread.join(int(self._timeout_timeout * 1000))
33  if thread.isAlive():
34  thread.stop()
35  raise self._error_error
36  return runner.get_result()
37 
38 
39 class Runner(Runnable):
40 
41  def __init__(self, runnable):
42  self._runnable_runnable = runnable
43  self._result_result = None
44  self._error_error = None
45 
46  def run(self):
47  try:
48  self._result_result = self._runnable_runnable()
49  except:
50  self._error_error = sys.exc_info()
51 
52  def get_result(self):
53  if not self._error_error:
54  return self._result_result
55  # `exec` used to avoid errors with easy_install on Python 3:
56  # https://github.com/robotframework/robotframework/issues/2785
57  exec('raise self._error[0], self._error[1], self._error[2]')