From 4b0ad1a9a0ecc34a14e2883a8c860a16ee7e0ff5 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 16 Jun 2015 17:48:11 -0400 Subject: [PATCH 1/2] ENH: prevent access to private key through [] --- history.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/history.py b/history.py index 45716b8..58e58fd 100644 --- a/history.py +++ b/history.py @@ -72,6 +72,9 @@ def __getitem__(self, key): return self._cache[key] def __setitem__(self, key, val): + if key == self.RESERVED_KEY_KEY: + raise ValueError("can not set internal keys through []") + return self.put(key, val) def __iter__(self): @@ -87,7 +90,7 @@ def __delitem__(self, key): cur_keys = list(self._cache) cur_keys.remove(key) # INSERT SQL QUERY TO DELETE ALL INFO ABOUT THIS KEY - self[self.RESERVED_KEY_KEY] = cur_keys + self.put(self.RESERVED_KEY_KEY, cur_keys) raise NotImplementedError() def __len__(self): From 1274236e12399efadee5f7eb801c1e9b926d4a0c Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 16 Jun 2015 17:48:29 -0400 Subject: [PATCH 2/2] TST: full test coverage --- test_history.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test_history.py b/test_history.py index 8ace8bb..bca8664 100644 --- a/test_history.py +++ b/test_history.py @@ -95,3 +95,10 @@ def test_get(): h.clear() b = h.get('b', 'aardvark') assert_equal(b, 'aardvark') + + +def test_protected_key(): + assert_raises(ValueError, h.__getitem__, + History.RESERVED_KEY_KEY) + assert_raises(ValueError, h.__setitem__, + History.RESERVED_KEY_KEY, 'aardvark')