29
29
import socket
30
30
31
31
class ClientException (Exception ):
32
+ '''
33
+ Raised when the server does something we don't expect
34
+
35
+ | This does not include `socket errors <http://docs.python.org/library/socket.html#socket.error>`_
36
+ | Note that ``ValidationException`` subclasses this so, technically, this is raised on any error
37
+ '''
32
38
33
39
def __init__ (self , msg , item = None ):
34
40
if item is not None :
35
41
msg = '%s: %r' % (msg , item ) # use repr() to better see special chars
36
42
super (ClientException , self ).__init__ (msg )
37
43
38
44
class ValidationException (ClientException ):
45
+ '''
46
+ Raised when an invalid parameter is passed to a ``Client`` function
47
+ '''
39
48
40
49
def __init__ (self , msg , item ):
41
50
super (ValidationException , self ).__init__ (msg , item )
@@ -70,12 +79,17 @@ def _set_timeout(self, timeout):
70
79
# b/c if socket fails, it will probably be closed/reopened
71
80
# and will want to use last intended value
72
81
self ._timeout = timeout
82
+ print 'socket' , self ._socket
73
83
if self ._socket :
74
84
self ._socket .settimeout (timeout )
75
85
76
86
timeout = property (_get_timeout , _set_timeout )
77
- ''' The timeout for reads and sends on the underlying socket
78
- (``connect_timeout`` cannot be changed once set) '''
87
+ '''
88
+ A float representing the timeout in seconds for reads and sends on the underlying socket
89
+ (``connect_timeout`` cannot be changed once init)
90
+
91
+ Setting a timeout can raise a ``TypeError`` (non-float) or a ``ValueError`` (negative)
92
+ '''
79
93
80
94
def _connect (self ):
81
95
# buffer needed since we always ask for 4096 bytes at a time
@@ -173,13 +187,18 @@ def close(self):
173
187
174
188
| Sockets are automatically closed when the ``Client`` object is garbage collected
175
189
| Sockets are opened the first time a command is run (such as ``get`` or ``set``)
190
+ | Raises socket errors
176
191
'''
177
192
if self ._socket :
178
193
self ._socket .close ()
179
194
self ._socket = None
180
195
181
196
def delete (self , key ):
182
- ''' Deletes a key/value pair from the server '''
197
+ '''
198
+ Deletes a key/value pair from the server
199
+
200
+ Raises ``ClientException`` and socket errors
201
+ '''
183
202
# req - delete <key> [noreply]\r\n
184
203
# resp - DELETED\r\n
185
204
# or
@@ -194,12 +213,16 @@ def delete(self, key):
194
213
def get (self , key ):
195
214
'''
196
215
Gets a single value from the server; returns None if there is no value
216
+
217
+ Raises ``ValidationException``, ``ClientException``, and socket errors
197
218
'''
198
219
return self .multi_get ([key ])[0 ]
199
220
200
221
def multi_get (self , keys ):
201
222
'''
202
223
Takes a list of keys and returns a list of values
224
+
225
+ Raises ``ValidationException``, ``ClientException``, and socket errors
203
226
'''
204
227
# req - get <key> [<key> ...]\r\n
205
228
# resp - VALUE <key> <flags> <bytes> [<cas unique>]\r\n
@@ -250,6 +273,8 @@ def multi_get(self, keys):
250
273
def set (self , key , val , exptime = 0 ):
251
274
'''
252
275
Sets a key to a value on the server with an optional exptime (0 means don't auto-expire)
276
+
277
+ Raises ``ValidationException``, ``ClientException``, and socket errors
253
278
'''
254
279
# req - set <key> <flags> <exptime> <bytes> [noreply]\r\n
255
280
# <data block>\r\n
@@ -282,10 +307,11 @@ def stats(self, additional_args=None):
282
307
'''
283
308
Runs a stats command on the server.
284
309
285
-
286
310
``additional_args`` are passed verbatim to the server.
287
311
See `the memcached wiki <http://code.google.com/p/memcached/wiki/NewCommands#Statistics>`_ for details
288
312
or `the spec <https://github.com/memcached/memcached/blob/master/doc/protocol.txt>`_ for even more details
313
+
314
+ Raises ``ClientException`` and socket errors
289
315
'''
290
316
# req - stats [additional args]\r\n
291
317
# resp - STAT <name> <value>\r\n (one per result)
0 commit comments