Error appropriately when putting invalid typed value in DHT (#48)

* Fixes #46

* Remove some ambiguity in the way IStorage works in comment

* Make documentation addition more clear

* Remove unneeded line

* Remove documentation example
This commit is contained in:
Brett Johnson 2018-04-18 12:55:23 -04:00 committed by Brian Muller
parent ecde2c9258
commit 3035876839
3 changed files with 23 additions and 1 deletions

2
.gitignore vendored
View File

@ -6,4 +6,4 @@ apidoc
build
dist
kademlia.egg-info
docs/_build
docs/_build

View File

@ -157,6 +157,10 @@ class Server(object):
"""
Set the given string key to the given value in the network.
"""
if not check_dht_value_type(value):
raise TypeError(
"Value must be of type int, float, bool, str, or bytes"
)
log.info("setting '%s' = '%s' on network", key, value)
dkey = digest(key)
return await self.set_digest(dkey, value)
@ -235,3 +239,20 @@ class Server(object):
self.saveStateRegularly,
fname,
frequency)
def check_dht_value_type(value):
"""
Checks to see if the type of the value is a valid type for
placing in the dht.
"""
typeset = set(
[
int,
float,
bool,
str,
bytes,
]
)
return type(value) in typeset

View File

@ -7,6 +7,7 @@ from collections import OrderedDict
class IStorage:
"""
Local storage for this node.
IStorage implementations of get must return the same type as put in by set
"""
def __setitem__(self, key, value):