git » nmdb » commit 199492d

python: Fix key selection when autopickling

author Alberto Bertogli
2008-12-07 13:53:12 UTC
committer Alberto Bertogli
2008-12-07 13:53:12 UTC
parent 582a0a01afc29903253f24d98abdb015d92fe6e1

python: Fix key selection when autopickling

When autopickling is enabled, we select the key using str(hash(key)),
which is stupid (and non portable) because the hash() of some classes is
just the object's memory address, which is obviously not stable across
invocations.

The fix is to just use pickle to select the key.

Thanks to Kelvin (lbh1976@gmail.com) for the bug report.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>

bindings/python/nmdb.py +5 -5

diff --git a/bindings/python/nmdb.py b/bindings/python/nmdb.py
index 406c653..b3019e8 100644
--- a/bindings/python/nmdb.py
+++ b/bindings/python/nmdb.py
@@ -78,7 +78,7 @@ class GenericDB (object):
 	def generic_get(self, getf, key):
 		"d[k]   Returns the value associated with the key k."
 		if self.autopickle:
-			key = str(hash(key))
+			key = cPickle.dumps(key, protocol = -1)
 		try:
 			r = getf(key)
 		except:
@@ -101,7 +101,7 @@ class GenericDB (object):
 	def generic_set(self, setf, key, val):
 		"d[k] = v   Associates the value v to the key k."
 		if self.autopickle:
-			key = str(hash(key))
+			key = cPickle.dumps(key, protocol = -1)
 			val = cPickle.dumps(val, protocol = -1)
 		r = setf(key, val)
 		if r <= 0:
@@ -121,7 +121,7 @@ class GenericDB (object):
 	def generic_delete(self, delf, key):
 		"del d[k]   Deletes the key k."
 		if self.autopickle:
-			key = str(hash(key))
+			key = cPickle.dumps(key, protocol = -1)
 		r = delf(key)
 		if r < 0:
 			raise NetworkError
@@ -142,7 +142,7 @@ class GenericDB (object):
 	def generic_cas(self, casf, key, oldval, newval):
 		"Perform a compare-and-swap."
 		if self.autopickle:
-			key = str(hash(key))
+			key = cPickle.dumps(key, protocol = -1)
 			oldval = cPickle.dumps(oldval, protocol = -1)
 			newval = cPickle.dumps(newval, protocol = -1)
 		r = casf(key, oldval, newval)
@@ -171,7 +171,7 @@ class GenericDB (object):
 		"""Atomically increment the value associated with the given
 		key by the given increment."""
 		if self.autopickle:
-			key = str(hash(key))
+			key = cPickle.dumps(key, protocol = -1)
 		r, v = incrf(key, increment)
 		if r == 2:
 			# success