Blocking in shell functions. Correctly report exceptions in threads. See #196.

This commit is contained in:
Juan Pablo Caram
2016-03-23 14:58:53 -04:00
parent 790f53dd55
commit 95676f21e2

View File

@@ -2059,30 +2059,40 @@ class App(QtCore.QObject):
yield yield
oeh = sys.excepthook
ex = []
def exceptHook(type_, value, traceback):
ex.append(value)
oeh(type_, value, traceback)
sys.excepthook = exceptHook
if timeout is not None: if timeout is not None:
QtCore.QTimer.singleShot(timeout, report_quit) QtCore.QTimer.singleShot(timeout, report_quit)
loop.exec_() loop.exec_()
sys.excepthook = oeh
if ex:
self.raiseTclError(str(ex[0]))
if status['timed_out']: if status['timed_out']:
raise Exception('Timed out!') raise Exception('Timed out!')
def wait_signal2(signal, timeout=10000): # def wait_signal2(signal, timeout=10000):
"""Block loop until signal emitted, or timeout (ms) elapses.""" # """Block loop until signal emitted, or timeout (ms) elapses."""
loop = QtCore.QEventLoop() # loop = QtCore.QEventLoop()
signal.connect(loop.quit) # signal.connect(loop.quit)
status = {'timed_out': False} # status = {'timed_out': False}
#
def report_quit(): # def report_quit():
status['timed_out'] = True # status['timed_out'] = True
loop.quit() # loop.quit()
#
if timeout is not None: # if timeout is not None:
QtCore.QTimer.singleShot(timeout, report_quit) # QtCore.QTimer.singleShot(timeout, report_quit)
loop.exec_() # loop.exec_()
#
if status['timed_out']: # if status['timed_out']:
raise Exception('Timed out!') # raise Exception('Timed out!')
def mytest(*args): def mytest(*args):
to = int(args[0]) to = int(args[0])
@@ -2110,24 +2120,45 @@ class App(QtCore.QObject):
def mytest2(*args): def mytest2(*args):
to = int(args[0]) to = int(args[0])
try: for rec in self.recent:
for rec in self.recent: if rec['kind'] == 'gerber':
if rec['kind'] == 'gerber': self.open_gerber(str(rec['filename']))
self.open_gerber(str(rec['filename'])) break
break
basename = self.collection.get_names()[0] basename = self.collection.get_names()[0]
isolate(basename, '-passes', '10', '-combine', '1') isolate(basename, '-passes', '10', '-combine', '1')
iso = self.collection.get_by_name(basename + "_iso") iso = self.collection.get_by_name(basename + "_iso")
with wait_signal(self.new_object_available, to): with wait_signal(self.new_object_available, to):
1/0 # Force exception 1/0 # Force exception
iso.generatecncjob() iso.generatecncjob()
return str(self.collection.get_names()) return str(self.collection.get_names())
except Exception as e: def mytest3(*args):
return str(e) to = int(args[0])
def sometask(*args):
time.sleep(2)
self.inform.emit("mytest3")
with wait_signal(self.inform, to):
self.worker_task.emit({'fcn': sometask, 'params': []})
return "mytest3 done"
def mytest4(*args):
to = int(args[0])
def sometask(*args):
time.sleep(2)
1/0 # Force exception
self.inform.emit("mytest4")
with wait_signal(self.inform, to):
self.worker_task.emit({'fcn': sometask, 'params': []})
return "mytest3 done"
def import_svg(filename, *args): def import_svg(filename, *args):
a, kwa = h(*args) a, kwa = h(*args)
@@ -3239,6 +3270,14 @@ class App(QtCore.QObject):
'fcn': mytest2, 'fcn': mytest2,
'help': "Test function. Only for testing." 'help': "Test function. Only for testing."
}, },
'mytest3': {
'fcn': mytest3,
'help': "Test function. Only for testing."
},
'mytest4': {
'fcn': mytest4,
'help': "Test function. Only for testing."
},
'help': { 'help': {
'fcn': shelp, 'fcn': shelp,
'help': "Shows list of commands." 'help': "Shows list of commands."