From 50164a92c68f2b74905d2aecdb783b1ff5efd110 Mon Sep 17 00:00:00 2001 From: rparedis Date: Wed, 2 Mar 2022 15:21:39 +0100 Subject: [PATCH] Python3 Updates --- .gitignore | 1 + doc/experiment_tk.py | 5 ++++- examples/trafficlight_realtime/experiment_tk.py | 5 ++++- src/pypdevs/realtime/threadingGameLoop.py | 10 +++++++--- src/pypdevs/util.py | 4 +++- test/testmodels/experiment.py | 5 ++++- test/testmodels/stacktracer.py | 6 +++--- 7 files changed, 26 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 215b39d..086bb48 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ *.pyc *build* test/output/* +/.idea/* diff --git a/doc/experiment_tk.py b/doc/experiment_tk.py index d4f4ab1..66d1491 100644 --- a/doc/experiment_tk.py +++ b/doc/experiment_tk.py @@ -15,7 +15,10 @@ from pypdevs.simulator import Simulator -from Tkinter import * +try: + from Tkinter import * +except ImportError: + from tkinter import * from trafficLightModel import * isBlinking = None diff --git a/examples/trafficlight_realtime/experiment_tk.py b/examples/trafficlight_realtime/experiment_tk.py index d4f4ab1..66d1491 100644 --- a/examples/trafficlight_realtime/experiment_tk.py +++ b/examples/trafficlight_realtime/experiment_tk.py @@ -15,7 +15,10 @@ from pypdevs.simulator import Simulator -from Tkinter import * +try: + from Tkinter import * +except ImportError: + from tkinter import * from trafficLightModel import * isBlinking = None diff --git a/src/pypdevs/realtime/threadingGameLoop.py b/src/pypdevs/realtime/threadingGameLoop.py index be1d024..856b9f6 100644 --- a/src/pypdevs/realtime/threadingGameLoop.py +++ b/src/pypdevs/realtime/threadingGameLoop.py @@ -16,6 +16,9 @@ import pypdevs.accurate_time as time from threading import Lock +_GLLOCK = Lock() + + class ThreadingGameLoop(object): """ Game loop subsystem for realtime simulation. Time will only progress when a *step* call is made. @@ -30,9 +33,10 @@ class ThreadingGameLoop(object): """ Perform a step in the simulation. Actual processing is done in a seperate thread. """ - if time.time() >= self.next_event: - self.next_event = float('inf') - getattr(self, "func")() + with _GLLOCK: # Thread-safety + if time.time() >= self.next_event: + self.next_event = float('inf') + getattr(self, "func")() def wait(self, delay, func): """ diff --git a/src/pypdevs/util.py b/src/pypdevs/util.py index b6e57ca..03cda2d 100644 --- a/src/pypdevs/util.py +++ b/src/pypdevs/util.py @@ -144,7 +144,9 @@ class DEVSException(Exception): """ String representation of the exception """ - return "DEVS Exception: " + str(self.message) + if hasattr(self, "message"): + return "DEVS Exception: " + str(self.message) + return "DEVS Exception: " + str(self.args[0]) class QuickStopException(Exception): """ diff --git a/test/testmodels/experiment.py b/test/testmodels/experiment.py index 78ef937..04b6437 100644 --- a/test/testmodels/experiment.py +++ b/test/testmodels/experiment.py @@ -185,7 +185,10 @@ elif mn.startswith("realtime"): elif mn.startswith("realtime_loop"): args["setRealTimePlatformGameLoop"] = [] elif mn.startswith("realtime_tk"): - from Tkinter import * + try: + from Tkinter import * + except ImportError: + from tkinter import * myTk = Tk() args["setRealTimePlatformTk"] = [myTk] else: diff --git a/test/testmodels/stacktracer.py b/test/testmodels/stacktracer.py index f39b2c0..b2a1776 100644 --- a/test/testmodels/stacktracer.py +++ b/test/testmodels/stacktracer.py @@ -73,7 +73,7 @@ class TraceDumper(threading.Thread): pass def stacktraces(self): - fout = file(self.fpath,"wb+") + fout = open(self.fpath,"wb+") try: fout.write(stacktraces()) finally: @@ -97,5 +97,5 @@ def trace_stop(): if _tracer is None: raise Exception("Not tracing, cannot stop.") else: - _trace.stop() - _trace = None + _tracer.stop() + _tracer = None