From 0e1cf2056b2718b0cc464f76a446263ae86c670e Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Tue, 10 Nov 2020 13:35:07 +0100 Subject: [PATCH 1/2] PropDict: fail early if internal_dict is not a dict --- src/borg/item.pyx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/borg/item.pyx b/src/borg/item.pyx index a97a423c9..b3bc7a113 100644 --- a/src/borg/item.pyx +++ b/src/borg/item.pyx @@ -43,11 +43,15 @@ class PropDict: if data_dict is None: data = kw elif not isinstance(data_dict, dict): - raise TypeError("data_dict must be dict") + raise TypeError("data_dict must be a dict") else: data = data_dict + if internal_dict is None: + internal_dict = {} + elif not isinstance(internal_dict, dict): + raise TypeError("internal_dict must be a dict") self._dict = {} - self.update_internal(internal_dict or {}) + self.update_internal(internal_dict) self.update(data) def update(self, d): From 95ee72908609340d67440e48b930e1cd122ece1c Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Tue, 10 Nov 2020 13:49:15 +0100 Subject: [PATCH 2/2] PropDict: refactor / micro-optimize - do not call update methods if there is nothing to do (empty dict) - order if/elif/else by simplicity / probability --- src/borg/item.pyx | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/borg/item.pyx b/src/borg/item.pyx index b3bc7a113..7a3c4a8ac 100644 --- a/src/borg/item.pyx +++ b/src/borg/item.pyx @@ -40,19 +40,21 @@ class PropDict: __slots__ = ("_dict", ) # avoid setting attributes not supported by properties def __init__(self, data_dict=None, internal_dict=None, **kw): + self._dict = {} + if internal_dict is None: + pass # nothing to do + elif isinstance(internal_dict, dict): + self.update_internal(internal_dict) + else: + raise TypeError("internal_dict must be a dict") if data_dict is None: data = kw - elif not isinstance(data_dict, dict): - raise TypeError("data_dict must be a dict") - else: + elif isinstance(data_dict, dict): data = data_dict - if internal_dict is None: - internal_dict = {} - elif not isinstance(internal_dict, dict): - raise TypeError("internal_dict must be a dict") - self._dict = {} - self.update_internal(internal_dict) - self.update(data) + else: + raise TypeError("data_dict must be a dict") + if data: + self.update(data) def update(self, d): for k, v in d.items():