diff options
author | Christoph Reiter <reiter.christoph@gmail.com> | 2018-04-19 15:55:25 +0200 |
---|---|---|
committer | Christoph Reiter <reiter.christoph@gmail.com> | 2018-04-19 15:55:25 +0200 |
commit | 1eef22e813a86156dc3abd4c0511d4132f0bcf0e (patch) | |
tree | f0436c5e6dd9767f5d12c07f745584b5e2ef4a3b /tests/test_source.py | |
parent | c78c36bba137486ccf45e432ec88fa633158f5de (diff) | |
download | pygobject-1eef22e813a86156dc3abd4c0511d4132f0bcf0e.tar.gz |
Destroy custom GLib.Source instances when they get freed. See #193
When we create a custom GLib.Source instance and attach it to a MainContext
the methods will be called by the context. In case the Python wrapper
gets freed this means the object used in the callbacks is no longer valid
and things crash.
To prevent this case simply destroy the source in case it gets deallocated
so that its methods will no longer be called after that. This means the
user has to make sure that the object stays alive, like now, but without
having crashes if one doesn't.
Diffstat (limited to 'tests/test_source.py')
-rw-r--r-- | tests/test_source.py | 24 |
1 files changed, 8 insertions, 16 deletions
diff --git a/tests/test_source.py b/tests/test_source.py index 8cd0085c..e90cd212 100644 --- a/tests/test_source.py +++ b/tests/test_source.py @@ -229,10 +229,9 @@ class TestSource(unittest.TestCase): gc.collect() self.assertTrue(self.finalized) - @unittest.skip('https://bugzilla.gnome.org/show_bug.cgi?id=722387') def test_python_unref_with_active_source(self): # Tests a Python derived Source which is free'd in the context of - # Python, but remains active in the MainContext (via source.attach()) + # Python, but which was attached to a MainContext (via source.attach()) self.dispatched = False self.finalized = False @@ -250,29 +249,22 @@ class TestSource(unittest.TestCase): def finalize(s): self.finalized = True + context = GLib.MainContext.new() source = S() - id = source.attach() + id_ = source.attach(context) self.assertFalse(self.finalized) self.assertFalse(source.is_destroyed()) - # Delete the source from Python but should still remain - # active in the main context. + # Delete the source from Python, it should detach del source + gc.collect() - context = GLib.MainContext.default() while context.iteration(may_block=False): pass - self.assertTrue(self.dispatched) - self.assertFalse(self.finalized) - - source = context.find_source_by_id(id) - source.destroy() # Remove from main context. - self.assertTrue(source.is_destroyed()) - - # Source should be finalized called after del - del source - self.assertTrue(self.finalized) + assert self.finalized + assert not self.dispatched + assert context.find_source_by_id(id_) is None def test_extra_init_args(self): class SourceWithInitArgs(GLib.Source): |