#NEWLINE# def _validate_encode_value( value , do_pickle ) : #NEWLINE# #INDENT# "Utility function to validate and encode server keys and values.\n\n  Args:\n    value: Value to store in memcache. If it's a string, it will get passed\n      along as-is. If it's a unicode string, it will be marked appropriately,\n      such that retrievals will yield a unicode value. If it's any other data\n      type, this function will attempt to pickle the data and then store the\n      serialized result, unpickling it upon retrieval.\n    do_pickle: Callable that takes an object and returns a non-unicode\n      string containing the pickled object.\n\n  Returns:\n    Tuple (stored_value, flags) where:\n      stored_value: The value as a non-unicode string that should be stored\n        in memcache.\n      flags: An integer with bits set from the FLAG_* constants in this file\n        to indicate the encoding of the key and value.\n\n  Raises:\n    ValueError: If the encoded value is too large.\n    pickle.PicklingError: If the value is not a string and could not be pickled.\n    RuntimeError: If a complicated data structure could not be pickled due to\n      too many levels of recursion in its composition.\n  " #NEWLINE# flags = 0 #NEWLINE# stored_value = value #NEWLINE# if isinstance ( value , str ) : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( value , unicode ) : #NEWLINE# #INDENT# stored_value = value . encode ( 'utf-8' ) #NEWLINE# flags |= TYPE_UNICODE #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( value , bool ) : #NEWLINE# #INDENT# stored_value = str ( int ( value ) ) #NEWLINE# flags |= TYPE_BOOL #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( value , int ) : #NEWLINE# #INDENT# stored_value = str ( value ) #NEWLINE# flags |= TYPE_INT #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( value , long ) : #NEWLINE# #INDENT# stored_value = str ( value ) #NEWLINE# flags |= TYPE_LONG #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# stored_value = do_pickle ( value ) #NEWLINE# flags |= TYPE_PICKLED #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if ( len ( stored_value ) > MAX_VALUE_SIZE ) : #NEWLINE# #INDENT# raise ValueError ( ( 'Values may not be more than %d bytes in length; received %d bytes' % ( MAX_VALUE_SIZE , len ( stored_value ) ) ) ) #NEWLINE# #UNINDENT# return ( stored_value , flags )