4747
4848from __future__ import print_function
4949
50- import sys
51- import socket
52- import time
5350import binascii
5451import os
5552import pickle
5653import re
54+ import socket
55+ import sys
5756import threading
57+ import time
5858import zlib
59+
5960import six
6061
6162
@@ -514,7 +515,9 @@ def _deletetouch(self, expected, cmd, key, time=0):
514515 return 0
515516
516517 def incr (self , key , delta = 1 ):
517- """Sends a command to the server to atomically increment the
518+ """Increment value for C{key} by C{delta}
519+
520+ Sends a command to the server to atomically increment the
518521 value for C{key} by C{delta}, or by 1 if C{delta} is
519522 unspecified. Returns None if C{key} doesn't exist on server,
520523 otherwise it returns the new value after incrementing.
@@ -542,7 +545,9 @@ def incr(self, key, delta=1):
542545 return self ._incrdecr ("incr" , key , delta )
543546
544547 def decr (self , key , delta = 1 ):
545- """Like L{incr}, but decrements. Unlike L{incr}, underflow is
548+ """Decrement value for C{key} by C{delta}
549+
550+ Like L{incr}, but decrements. Unlike L{incr}, underflow is
546551 checked and new values are capped at 0. If server value is 1,
547552 a decrement of 2 returns 0, not -1.
548553
@@ -651,7 +656,9 @@ def set(self, key, val, time=0, min_compress_len=0):
651656 return self ._set ("set" , key , val , time , min_compress_len )
652657
653658 def cas (self , key , val , time = 0 , min_compress_len = 0 ):
654- '''Sets a key to a given value in the memcache if it hasn't been
659+ '''Check and set (CAS)
660+
661+ Sets a key to a given value in the memcache if it hasn't been
655662 altered since last fetched. (See L{gets}).
656663
657664 The C{key} can optionally be an tuple, with the first element
@@ -883,8 +890,8 @@ def _val_to_store_info(self, val, min_compress_len):
883890 val = comp_val
884891
885892 # silently do not store if value length exceeds maximum
886- if self .server_max_value_length != 0 and \
887- len (val ) > self .server_max_value_length :
893+ if ( self .server_max_value_length != 0 and
894+ len (val ) > self .server_max_value_length ) :
888895 return (0 )
889896
890897 return (flags , len (val ), val )
@@ -1153,7 +1160,10 @@ def _recv_value(self, server, flags, rlen):
11531160 return val
11541161
11551162 def check_key (self , key , key_extra_len = 0 ):
1156- """Checks sanity of key. Fails if:
1163+ """Checks sanity of key.
1164+
1165+ Fails if:
1166+
11571167 Key length is > SERVER_MAX_KEY_LENGTH (Raises MemcachedKeyLength).
11581168 Contains control characters (Raises MemcachedKeyCharacterError).
11591169 Is not a string (Raises MemcachedStringEncodingError)
@@ -1170,14 +1180,14 @@ def check_key(self, key, key_extra_len=0):
11701180 # it's a separate type.
11711181 if _has_unicode is True and isinstance (key , unicode ):
11721182 raise Client .MemcachedStringEncodingError (
1173- "Keys must be str()'s, not unicode. Convert your unicode "
1174- "strings using mystring.encode(charset)!" )
1183+ "Keys must be str()'s, not unicode. Convert your unicode "
1184+ "strings using mystring.encode(charset)!" )
11751185 if not isinstance (key , str ):
11761186 raise Client .MemcachedKeyTypeError ("Key must be str()'s" )
11771187
11781188 if isinstance (key , _str_cls ):
1179- if self .server_max_key_length != 0 and \
1180- len (key ) + key_extra_len > self .server_max_key_length :
1189+ if ( self .server_max_key_length != 0 and
1190+ len (key ) + key_extra_len > self .server_max_key_length ) :
11811191 raise Client .MemcachedKeyLengthError (
11821192 "Key length is > %s" % self .server_max_key_length
11831193 )
@@ -1385,7 +1395,8 @@ def to_s(val):
13851395
13861396 def test_setget (key , val ):
13871397 global failures
1388- print ("Testing set/get {'%s': %s} ..." % (to_s (key ), to_s (val )), end = " " )
1398+ print ("Testing set/get {'%s': %s} ..."
1399+ % (to_s (key ), to_s (val )), end = " " )
13891400 mc .set (key , val )
13901401 newval = mc .get (key )
13911402 if newval == val :
@@ -1411,15 +1422,15 @@ def __eq__(self, other):
14111422
14121423 test_setget ("a_string" , "some random string" )
14131424 test_setget ("an_integer" , 42 )
1414- if test_setget ("long" , long (1 << 30 )):
1425+ if test_setget ("long" , long (1 << 30 )):
14151426 print ("Testing delete ..." , end = " " )
14161427 if mc .delete ("long" ):
14171428 print ("OK" )
14181429 else :
14191430 print ("FAIL" )
14201431 failures += 1
14211432 print ("Checking results of delete ..." , end = " " )
1422- if mc .get ("long" ) == None :
1433+ if mc .get ("long" ) is None :
14231434 print ("OK" )
14241435 else :
14251436 print ("FAIL" )
@@ -1428,19 +1439,19 @@ def __eq__(self, other):
14281439 print (mc .get_multi (["a_string" , "an_integer" ]))
14291440
14301441 # removed from the protocol
1431- #if test_setget("timed_delete", 'foo'):
1432- # print "Testing timed delete ...",
1433- # if mc.delete("timed_delete", 1):
1434- # print("OK")
1435- # else:
1436- # print("FAIL")
1437- # failures += 1
1438- # print "Checking results of timed delete ..."
1439- # if mc.get("timed_delete") == None:
1440- # print("OK")
1441- # else:
1442- # print("FAIL")
1443- # failures += 1
1442+ # if test_setget("timed_delete", 'foo'):
1443+ # print "Testing timed delete ...",
1444+ # if mc.delete("timed_delete", 1):
1445+ # print("OK")
1446+ # else:
1447+ # print("FAIL")
1448+ # failures += 1
1449+ # print "Checking results of timed delete ..."
1450+ # if mc.get("timed_delete") is None:
1451+ # print("OK")
1452+ # else:
1453+ # print("FAIL")
1454+ # failures += 1
14441455
14451456 print ("Testing get(unknown value) ..." , end = " " )
14461457 print (to_s (mc .get ("unknown_value" )))
@@ -1511,12 +1522,11 @@ def __eq__(self, other):
15111522 failures += 1
15121523 try :
15131524 x = mc .set ((unicode ('a' )* SERVER_MAX_KEY_LENGTH ).encode ('utf-8' ), 1 )
1514- except :
1525+ except Client . MemcachedKeyError :
15151526 print ("FAIL" , end = " " )
15161527 failures += 1
15171528 else :
15181529 print ("OK" , end = " " )
1519- import pickle
15201530 s = pickle .loads ('V\\ u4f1a\n p0\n .' )
15211531 try :
15221532 x = mc .set ((s * SERVER_MAX_KEY_LENGTH ).encode ('utf-8' ), 1 )
@@ -1529,13 +1539,13 @@ def __eq__(self, other):
15291539 print ("Testing using a value larger than the memcached value limit..." )
15301540 print ('NOTE: "MemCached: while expecting[...]" is normal...' )
15311541 x = mc .set ('keyhere' , 'a' * SERVER_MAX_VALUE_LENGTH )
1532- if mc .get ('keyhere' ) == None :
1542+ if mc .get ('keyhere' ) is None :
15331543 print ("OK" , end = " " )
15341544 else :
15351545 print ("FAIL" , end = " " )
15361546 failures += 1
15371547 x = mc .set ('keyhere' , 'a' * SERVER_MAX_VALUE_LENGTH + 'aaa' )
1538- if mc .get ('keyhere' ) == None :
1548+ if mc .get ('keyhere' ) is None :
15391549 print ("OK" )
15401550 else :
15411551 print ("FAIL" )
0 commit comments