File tree Expand file tree Collapse file tree 2 files changed +33
-4
lines changed Expand file tree Collapse file tree 2 files changed +33
-4
lines changed Original file line number Diff line number Diff line change @@ -631,24 +631,35 @@ def _impl_test_interactive_timers():
631
631
# increase pause duration on CI to let things spin up
632
632
# particularly relevant for gtk3cairo
633
633
pause_time = 2 if os .getenv ("CI" ) else 0.5
634
+ expected_100ms_calls = int (pause_time / 0.1 )
634
635
fig = plt .figure ()
635
636
plt .pause (pause_time )
636
637
timer = fig .canvas .new_timer (0.1 )
637
638
mock = Mock ()
638
639
timer .add_callback (mock )
639
640
timer .start ()
640
641
plt .pause (pause_time )
641
- timer .stop ()
642
- assert mock .call_count > 1
642
+ # NOTE: The timer is as fast as possible, but this is different between backends
643
+ # so we can't assert on the exact number but it should be faster than 100ms
644
+ assert mock .call_count > expected_100ms_calls , \
645
+ f"Expected more than { expected_100ms_calls } calls, got { mock .call_count } "
646
+
647
+ # Test updating the interval updates a running timer
648
+ timer .interval = 100
649
+ mock .call_count = 0
650
+ plt .pause (pause_time )
651
+ # Could be off due to when the timers actually get fired (especially on CI)
652
+ assert 1 < mock .call_count <= expected_100ms_calls , \
653
+ f"Expected less than { expected_100ms_calls } calls, got { mock .call_count } "
643
654
644
655
# Now turn it into a single shot timer and verify only one gets triggered
645
656
mock .call_count = 0
646
657
timer .single_shot = True
647
- timer .start ()
648
658
plt .pause (pause_time )
649
659
assert mock .call_count == 1
650
660
651
- # Make sure we can start the timer a second time
661
+ # Make sure we can start the timer after stopping
662
+ timer .stop ()
652
663
timer .start ()
653
664
plt .pause (pause_time )
654
665
assert mock .call_count == 2
Original file line number Diff line number Diff line change @@ -1789,6 +1789,18 @@ - (void)flagsChanged:(NSEvent *)event
1789
1789
Py_RETURN_NONE;
1790
1790
}
1791
1791
1792
+ static PyObject*
1793
+ Timer__timer_update (Timer* self)
1794
+ {
1795
+ // stop and invalidate a timer if it is already running and then create a new one
1796
+ // where the start() method retrieves the updated interval internally
1797
+ if (self->timer ) {
1798
+ Timer__timer_stop_impl (self);
1799
+ gil_call_method ((PyObject*)self, " _timer_start" );
1800
+ }
1801
+ Py_RETURN_NONE;
1802
+ }
1803
+
1792
1804
static void
1793
1805
Timer_dealloc (Timer* self)
1794
1806
{
@@ -1815,6 +1827,12 @@ - (void)flagsChanged:(NSEvent *)event
1815
1827
{" _timer_stop" ,
1816
1828
(PyCFunction)Timer__timer_stop,
1817
1829
METH_NOARGS},
1830
+ {" _timer_set_interval" ,
1831
+ (PyCFunction)Timer__timer_update,
1832
+ METH_NOARGS},
1833
+ {" _timer_set_single_shot" ,
1834
+ (PyCFunction)Timer__timer_update,
1835
+ METH_NOARGS},
1818
1836
{} // sentinel
1819
1837
},
1820
1838
};
You can’t perform that action at this time.
0 commit comments