Skip to content

Commit e22bba4

Browse files
author
raylu
committed
document exceptions
1 parent c70de5b commit e22bba4

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

doc/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ Contents:
1313
.. automodule:: memcache
1414
:members:
1515
:undoc-members:
16+
:show-inheritance:

memcache.py

+30-4
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,22 @@
2929
import socket
3030

3131
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+
'''
3238

3339
def __init__(self, msg, item=None):
3440
if item is not None:
3541
msg = '%s: %r' % (msg, item) # use repr() to better see special chars
3642
super(ClientException, self).__init__(msg)
3743

3844
class ValidationException(ClientException):
45+
'''
46+
Raised when an invalid parameter is passed to a ``Client`` function
47+
'''
3948

4049
def __init__(self, msg, item):
4150
super(ValidationException, self).__init__(msg, item)
@@ -70,12 +79,17 @@ def _set_timeout(self, timeout):
7079
# b/c if socket fails, it will probably be closed/reopened
7180
# and will want to use last intended value
7281
self._timeout = timeout
82+
print 'socket', self._socket
7383
if self._socket:
7484
self._socket.settimeout(timeout)
7585

7686
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+
'''
7993

8094
def _connect(self):
8195
# buffer needed since we always ask for 4096 bytes at a time
@@ -173,13 +187,18 @@ def close(self):
173187
174188
| Sockets are automatically closed when the ``Client`` object is garbage collected
175189
| Sockets are opened the first time a command is run (such as ``get`` or ``set``)
190+
| Raises socket errors
176191
'''
177192
if self._socket:
178193
self._socket.close()
179194
self._socket = None
180195

181196
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+
'''
183202
# req - delete <key> [noreply]\r\n
184203
# resp - DELETED\r\n
185204
# or
@@ -194,12 +213,16 @@ def delete(self, key):
194213
def get(self, key):
195214
'''
196215
Gets a single value from the server; returns None if there is no value
216+
217+
Raises ``ValidationException``, ``ClientException``, and socket errors
197218
'''
198219
return self.multi_get([key])[0]
199220

200221
def multi_get(self, keys):
201222
'''
202223
Takes a list of keys and returns a list of values
224+
225+
Raises ``ValidationException``, ``ClientException``, and socket errors
203226
'''
204227
# req - get <key> [<key> ...]\r\n
205228
# resp - VALUE <key> <flags> <bytes> [<cas unique>]\r\n
@@ -250,6 +273,8 @@ def multi_get(self, keys):
250273
def set(self, key, val, exptime=0):
251274
'''
252275
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
253278
'''
254279
# req - set <key> <flags> <exptime> <bytes> [noreply]\r\n
255280
# <data block>\r\n
@@ -282,10 +307,11 @@ def stats(self, additional_args=None):
282307
'''
283308
Runs a stats command on the server.
284309
285-
286310
``additional_args`` are passed verbatim to the server.
287311
See `the memcached wiki <http://code.google.com/p/memcached/wiki/NewCommands#Statistics>`_ for details
288312
or `the spec <https://github.com/memcached/memcached/blob/master/doc/protocol.txt>`_ for even more details
313+
314+
Raises ``ClientException`` and socket errors
289315
'''
290316
# req - stats [additional args]\r\n
291317
# resp - STAT <name> <value>\r\n (one per result)

0 commit comments

Comments
 (0)