Fix unobserve_property RuntimeError

This commit is contained in:
Matt Deacalion Stevens 2017-05-21 10:58:36 +01:00
parent 5ca3a0250c
commit b1f81ac561
No known key found for this signature in database
GPG key ID: 99263B3169B149E8
2 changed files with 36 additions and 2 deletions

View file

@ -206,5 +206,32 @@ class TestLifecycle(unittest.TestCase):
handler.assert_any_call('info', 'cplayer', 'Playing: ./test.webm')
class RegressionTests(unittest.TestCase):
def test_unobserve_property_runtime_error(self):
"""
Ensure a `RuntimeError` is not thrown within
`unobserve_property`.
"""
handler = mock.Mock()
handler.observed_mpv_properties = []
m = mpv.MPV()
m.observe_property('loop', handler)
try:
m.unobserve_property('loop', handler)
except RuntimeError:
self.fail(
"""
"RuntimeError" exception thrown within
`unobserve_property`
""",
)
finally:
m.terminate()
if __name__ == '__main__':
unittest.main()

9
mpv.py
View file

@ -681,8 +681,15 @@ class MPV(object):
fmts = self._property_handlers[name]
for fmt, handlers in fmts.items():
handlers.remove(handler)
if not handlers:
# remove all properties that have no handlers
empty_props = [
fmt for fmt, handler in fmts.items() if not handler
]
for fmt in empty_props:
del fmts[fmt]
if not fmts:
_mpv_unobserve_property(self._event_handle, hash(name)&0xffffffffffffffff)