Fix weird with Client.behaviors.update.

This would manifest itself in pylibmc setting seemingly random (but
consistently so) values for the updated behaviors.

Not sure how or why this works when the previous code didn't.
This commit is contained in:
lericson 2009-11-25 12:00:04 +01:00
parent 0f8c1e442c
commit a4e7e2cd75
2 changed files with 15 additions and 5 deletions

View File

@ -57,9 +57,9 @@ class BehaviorDict(dict):
super(BehaviorDict, self).__setitem__(name, value)
self.client.set_behaviors({name: value})
def update(self, *args, **kwds):
super(BehaviorDict, self).update(*args, **kwds)
self.client.set_behaviors(self.copy())
def update(self, d):
super(BehaviorDict, self).update(d)
self.client.set_behaviors(d.copy())
class Client(_pylibmc.client):
def __init__(self, servers, binary=False):
@ -115,9 +115,9 @@ class Client(_pylibmc.client):
This also happens for `distribution`.
"""
behaviors = behaviors.copy()
if behaviors.get("hash", None) in hashers:
if behaviors.get("hash") is not None:
behaviors["hash"] = hashers[behaviors["hash"]]
if behaviors.get("distribution") in distributions:
if behaviors.get("distribution") is not None:
behaviors["distribution"] = distributions[behaviors["distribution"]]
return super(Client, self).set_behaviors(behaviors)

View File

@ -1,9 +1,11 @@
"""Tests. They want YOU!!
Basic functionality.
>>> _pylibmc.__version__ == pylibmc.__version__
True
>>> c = _pylibmc.client([test_server])
>>> c.get("hello")
>>> c.set("test_key", 123)
True
>>> c.get("test_key")
@ -177,6 +179,14 @@ Empty server lists are bad for your health.
Traceback (most recent call last):
...
MemcachedError: empty server list
Python-wrapped behaviors dict
>>> pc = pylibmc.Client(["%s:%d" % test_server[1:]])
>>> (pc.behaviors["hash"], pc.behaviors["distribution"])
('default', 'modula')
>>> pc.behaviors.update({"hash": "fnv1a_32", "distribution": "consistent"})
>>> (pc.behaviors["hash"], pc.behaviors["distribution"])
('fnv1a_32', 'consistent')
"""
# Used to test pickling.