From b06f831528e5a9c9f3fb3c1949bb842e6a6f799b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Thu, 21 May 2020 04:24:55 +0200 Subject: [PATCH] Deprecate property-del:name events and introduce property-reset:name instead And the same for -pre- events. The property-del name is really confusing (it makes sense only for those with deep knowledge of the implementation), because the property isn't really deleted - it is only reverted to the "default" state (which most properties have). So, name the event property-reset, intentionally similar to property-set, as it is also kind of a value change. Additionally the property-reset event is meant to be called when the (dynamic) default value changes. Due to the current implementation, it is a manual process so it can't be guaranteed to be called in all those cases, but lets try to cover as much as possible. Fixes QubesOS/qubes-issues#5834 --- qubes/__init__.py | 42 ++++++++++++++++++++++++++++++++++++++++++ qubes/tests/init.py | 8 ++++++++ 2 files changed, 50 insertions(+) diff --git a/qubes/__init__.py b/qubes/__init__.py index 74f51092..f8668ddf 100644 --- a/qubes/__init__.py +++ b/qubes/__init__.py @@ -284,6 +284,10 @@ class property: # pylint: disable=redefined-builtin,invalid-name has_oldvalue = False if has_oldvalue: + instance.fire_event('property-pre-reset:' + self.__name__, + pre_event=True, + name=self.__name__, oldvalue=oldvalue) + # deprecated, to be removed in Qubes 5.0 instance.fire_event('property-pre-del:' + self.__name__, pre_event=True, name=self.__name__, oldvalue=oldvalue) @@ -291,13 +295,23 @@ class property: # pylint: disable=redefined-builtin,invalid-name delattr(instance, self._attr_name) except AttributeError: pass + instance.fire_event('property-reset:' + self.__name__, + name=self.__name__, oldvalue=oldvalue) + # deprecated, to be removed in Qubes 5.0 instance.fire_event('property-del:' + self.__name__, name=self.__name__, oldvalue=oldvalue) else: + instance.fire_event('property-pre-reset:' + self.__name__, + pre_event=True, + name=self.__name__) + # deprecated, to be removed in Qubes 5.0 instance.fire_event('property-pre-del:' + self.__name__, pre_event=True, name=self.__name__) + instance.fire_event('property-reset:' + self.__name__, + name=self.__name__) + # deprecated, to be removed in Qubes 5.0 instance.fire_event('property-del:' + self.__name__, name=self.__name__) @@ -470,6 +484,9 @@ class PropertyHolder(qubes.events.Emitter): Fired when property gets deleted (is set to default). Signature is variable, *oldvalue* is present only if there was an old value. + This event is deprecated and will be removed in Qubes 5.0. + Use property-reset instead. + :param name: Property name :param oldvalue: Old value of the property @@ -479,6 +496,31 @@ class PropertyHolder(qubes.events.Emitter): Fired before property gets deleted (is set to default). Signature is variable, *oldvalue* is present only if there was an old value. + This event is deprecated and will be removed in Qubes 5.0. + Use property-pre-reset instead. + + :param name: Property name + :param oldvalue: Old value of the property + + .. event:: property-reset: \ + (subject, event, name[, oldvalue]) + + Fired when property gets reset to the (possibly dynamic) default. + This even may be also fired when the property is already in + "default" state, but the calculated default value changes. + Signature is variable, *oldvalue* is present only if there was an + old value. + + :param name: Property name + :param oldvalue: Old value of the property + + .. event:: property-pre-reset: \ + (subject, event, name[, oldvalue]) + + Fired before property gets reset to the (possibly dynamic) default. + Signature is variable, *oldvalue* is present only if there was an + old value. + :param name: Property name :param oldvalue: Old value of the property diff --git a/qubes/tests/init.py b/qubes/tests/init.py index a2d41de9..fa4cc66c 100644 --- a/qubes/tests/init.py +++ b/qubes/tests/init.py @@ -170,8 +170,12 @@ class TC_10_property(qubes.tests.QubesTestCase): self.assertEventFired(self.holder, 'property-pre-del:testprop1', kwargs={'name': 'testprop1', 'oldvalue': 'testvalue'}) + self.assertEventFired(self.holder, 'property-pre-reset:testprop1', + kwargs={'name': 'testprop1', 'oldvalue': 'testvalue'}) self.assertEventFired(self.holder, 'property-del:testprop1', kwargs={'name': 'testprop1', 'oldvalue': 'testvalue'}) + self.assertEventFired(self.holder, 'property-reset:testprop1', + kwargs={'name': 'testprop1', 'oldvalue': 'testvalue'}) def test_081_delete_by_assign(self): self.holder.testprop1 = 'testvalue' @@ -203,8 +207,12 @@ class TC_10_property(qubes.tests.QubesTestCase): self.assertEqual(holder.testprop1, 'defaultvalue') self.assertEventFired(holder, 'property-pre-del:testprop1', kwargs={ 'name': 'testprop1', 'oldvalue': 'testvalue'}) + self.assertEventFired(holder, 'property-pre-reset:testprop1', kwargs={ + 'name': 'testprop1', 'oldvalue': 'testvalue'}) self.assertEventFired(holder, 'property-del:testprop1', kwargs={ 'name': 'testprop1', 'oldvalue': 'testvalue'}) + self.assertEventFired(holder, 'property-reset:testprop1', kwargs={ + 'name': 'testprop1', 'oldvalue': 'testvalue'}) def test_090_write_once_set(self): class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):