1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2025-03-06 11:40:31 +00:00

disallow setting unknown attributes, use StableDict as .as_dict() result

This commit is contained in:
Thomas Waldmann 2016-04-27 23:17:10 +02:00
parent 46362a1962
commit e6cf4ee627
2 changed files with 14 additions and 1 deletions

View file

@ -17,6 +17,8 @@ class PropDict:
"""
VALID_KEYS = None # override with <set of str> in child class
__slots__ = ("_dict", ) # avoid setting attributes not supported by properties
def __init__(self, data_dict=None, **kw):
if data_dict is None:
data = kw
@ -39,7 +41,7 @@ class PropDict:
def as_dict(self):
"""return the internal dictionary"""
return self._dict # XXX use StableDict?
return StableDict(self._dict)
def _check_key(self, key):
"""make sure key is of type str and known"""
@ -108,6 +110,8 @@ class Item(PropDict):
VALID_KEYS = set(key.decode() for key in ITEM_KEYS) # we want str-typed keys
__slots__ = ("_dict", ) # avoid setting attributes not supported by properties
# properties statically defined, so that IDEs can know their names:
path = PropDict._make_property('path', str, 'surrogate-escaped str', encode=safe_encode, decode=safe_decode)

View file

@ -136,3 +136,12 @@ def test_item_dict_property():
item.xattrs['bar'] = 'baz'
assert item.xattrs == StableDict({'foo': 'bar', 'bar': 'baz'})
assert item.as_dict() == {'xattrs': {'foo': 'bar', 'bar': 'baz'}}
def test_unknown_property():
# we do not want the user to be able to set unknown attributes -
# they won't get into the .as_dict() result dictionary.
# also they might be just typos of known attributes.
item = Item()
with pytest.raises(AttributeError):
item.unknown_attribute = None