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:
parent
ecde2c9258
commit
3035876839
2
.gitignore
vendored
2
.gitignore
vendored
@ -6,4 +6,4 @@ apidoc
|
||||
build
|
||||
dist
|
||||
kademlia.egg-info
|
||||
docs/_build
|
||||
docs/_build
|
||||
|
@ -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
|
||||
|
@ -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):
|
||||
|
Loading…
Reference in New Issue
Block a user