Expose all per-error exception objects.

This commit is contained in:
lericson 2009-11-17 17:52:39 +01:00
parent 0e1c0b8204
commit 40dcb628c6
2 changed files with 17 additions and 1 deletions

View File

@ -984,7 +984,7 @@ static PyMethodDef PylibMC_functions[] = {
};
PyMODINIT_FUNC init_pylibmc(void) {
PyObject *module;
PyObject *module, *exc_objs;
PylibMC_Behavior *b;
PylibMC_McErr *err;
char name[128];
@ -1024,14 +1024,22 @@ Oh, and: plankton.\n");
PyModule_AddObject(module, "MemcachedError",
(PyObject *)PylibMCExc_MemcachedError);
exc_objs = PyList_New(0);
PyList_Append(exc_objs,
Py_BuildValue("sO", "Error", (PyObject *)PylibMCExc_MemcachedError));
for (err = PylibMCExc_mc_errs; err->name != NULL; err++) {
char excnam[64];
strncpy(excnam, "_pylibmc.", 64);
strncat(excnam, err->name, 64);
err->exc = PyErr_NewException(excnam, PylibMCExc_MemcachedError, NULL);
PyModule_AddObject(module, err->name, (PyObject *)err->exc);
PyList_Append(exc_objs,
Py_BuildValue("sO", err->name, (PyObject *)err->exc));
}
PyModule_AddObject(module, "exceptions", exc_objs);
Py_INCREF(&PylibMC_ClientType);
PyModule_AddObject(module, "client", (PyObject *)&PylibMC_ClientType);

View File

@ -27,6 +27,14 @@ import _pylibmc
__all__ = ["hashers", "distributions", "Client"]
__version__ = _pylibmc.__version__
errors = tuple(e for (n, e) in _pylibmc.exceptions)
# *Cough* Uhm, not the prettiest of things but this unpacks all exception
# objects and sets them on the very module object currently constructed.
import sys
modself = sys.modules[__name__]
for name, exc in _pylibmc.exceptions:
setattr(modself, name, exc)
hashers, hashers_rvs = {}, {}
distributions, distributions_rvs = {}, {}
# Not the prettiest way of doing things, but works well.