From bc11e3c074bdba1ae0fcaa0c10e3b32f781be9fd Mon Sep 17 00:00:00 2001 From: Christian Tismer Date: Wed, 26 Jan 2022 19:03:41 +0100 Subject: PyPySide: Fix locker handling which enables Mandelbrot Not only the QPainter, but also the QMutexLocker were not correctly modelled after PEP 343 in the examples. Since that is now fixed, we consider the PyPy project not as ready, but good enough to publish it. It also turned out that people have the expectation to use QSignalBlocker in the "as" form: with QSignalBlocker(self.double_spin_box) as blocker: self.double_spin_box.setValue(2.5) https://stackoverflow.com/questions/60384734/how-to-use-qsignalblocker-in-python But that blocker would be None. As a side effect, QMutexLocker, QReadLocker, QWriteLocker and QSignalBlocker were augmented with a default __enter__ implementation that returns the locker instance. [ChangeLog][PySide6] The Mandelbrot example needed context managers for QPainter and QMutexLocker to work in PyPy. Task-number: PYSIDE-535 Pick-to: 6.2 Change-Id: I2a62ca645a4fddcafbf11869f14a538141f32c39 Reviewed-by: Friedemann Kleint --- examples/corelib/threads/mandelbrot.py | 26 +++++++++++----------- .../blockingfortuneclient/blockingfortuneclient.py | 14 ++++++------ 2 files changed, 20 insertions(+), 20 deletions(-) (limited to 'examples') diff --git a/examples/corelib/threads/mandelbrot.py b/examples/corelib/threads/mandelbrot.py index 022f47438..3a92d6ce1 100644 --- a/examples/corelib/threads/mandelbrot.py +++ b/examples/corelib/threads/mandelbrot.py @@ -102,18 +102,17 @@ class RenderThread(QThread): self.wait(2000) def render(self, centerX, centerY, scale_factor, resultSize): - locker = QMutexLocker(self.mutex) - - self._center_x = centerX - self._center_y = centerY - self._scale_factor = scale_factor - self._result_size = resultSize - - if not self.isRunning(): - self.start(QThread.LowPriority) - else: - self.restart = True - self.condition.wakeOne() + with QMutexLocker(self.mutex): + self._center_x = centerX + self._center_y = centerY + self._scale_factor = scale_factor + self._result_size = resultSize + + if not self.isRunning(): + self.start(QThread.LowPriority) + else: + self.restart = True + self.condition.wakeOne() def run(self): timer = QElapsedTimer() @@ -185,7 +184,8 @@ class RenderThread(QThread): if elapsed > 2000: elapsed /= 1000 unit = 's' - text = f"Pass {curpass+1}/{NUM_PASSES}, max iterations: {max_iterations}, time: {elapsed}{unit}" + text = (f"Pass {curpass + 1}/{NUM_PASSES}, " + f"max iterations: {max_iterations}, time: {elapsed}{unit}") image.setText(INFO_KEY, text) self.rendered_image.emit(image, scale_factor) curpass += 1 diff --git a/examples/network/blockingfortuneclient/blockingfortuneclient.py b/examples/network/blockingfortuneclient/blockingfortuneclient.py index 50f4c1662..b997a8f7d 100644 --- a/examples/network/blockingfortuneclient/blockingfortuneclient.py +++ b/examples/network/blockingfortuneclient/blockingfortuneclient.py @@ -73,13 +73,13 @@ class FortuneThread(QThread): self.wait() def request_new_fortune(self, hostname, port): - locker = QMutexLocker(self.mutex) - self._host_name = hostname - self.port = port - if not self.isRunning(): - self.start() - else: - self.cond.wakeOne() + with QMutexLocker(self.mutex): + self._host_name = hostname + self.port = port + if not self.isRunning(): + self.start() + else: + self.cond.wakeOne() def run(self): self.mutex.lock() -- cgit v1.2.3