# HG changeset patch # User Edouard Tisserant # Date 1626342644 -7200 # Node ID 725d3e9ac913532223b53cec3e5ac22de88ec3ea # Parent 1fc4274de64ed3b02cdb5d189ff42c714fd1fa70 Runtime: Handle errors in user's python code more gracefully : make exceptions and allow repair. diff -r 1fc4274de64e -r 725d3e9ac913 Beremiz_service.py --- a/Beremiz_service.py Thu Jul 15 11:48:02 2021 +0200 +++ b/Beremiz_service.py Thu Jul 15 11:50:44 2021 +0200 @@ -31,6 +31,7 @@ import getopt import threading import shlex +import traceback from threading import Thread, Semaphore, Lock, currentThread from builtins import str as text from past.builtins import execfile @@ -614,8 +615,11 @@ pyro_thread.join() plcobj = runtime.GetPLCObjectSingleton() -plcobj.StopPLC() -plcobj.UnLoadPLC() +try: + plcobj.StopPLC() + plcobj.UnLoadPLC() +except: + print(traceback.format_exc()) if havetwisted: reactor.stop() diff -r 1fc4274de64e -r 725d3e9ac913 runtime/PLCObject.py --- a/runtime/PLCObject.py Thu Jul 15 11:48:02 2021 +0200 +++ b/runtime/PLCObject.py Thu Jul 15 11:50:44 2021 +0200 @@ -113,7 +113,9 @@ if autostart: if self.LoadPLC(): self.StartPLC() - return + else: + self._fail(_("Problem autostarting PLC : can't load PLC")) + return except Exception: self.PLCStatus = PlcStatus.Empty self.CurrentPLCFilename = None @@ -269,7 +271,12 @@ def LoadPLC(self): res = self._LoadPLC() if res: - self.PythonRuntimeInit() + try: + self.PythonRuntimeInit() + except Exception: + self._loading_error = traceback.format_exc() + PLCprint(self._loading_error) + return False else: self._FreePLC() @@ -680,9 +687,9 @@ if self.LoadPLC(): self.PLCStatus = PlcStatus.Stopped + self.StatusChange() else: - self.PLCStatus = PlcStatus.Broken - self.StatusChange() + self._fail(_("Problem installing new PLC : can't load PLC")) return self.PLCStatus == PlcStatus.Stopped return False