From 40dcb628c6f00f56489ebc86f72e31d11d44bf4f Mon Sep 17 00:00:00 2001 From: lericson Date: Tue, 17 Nov 2009 17:52:39 +0100 Subject: [PATCH] Expose all per-error exception objects. --- _pylibmcmodule.c | 10 +++++++++- pylibmc.py | 8 ++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/_pylibmcmodule.c b/_pylibmcmodule.c index 40093c7..e61db10 100644 --- a/_pylibmcmodule.c +++ b/_pylibmcmodule.c @@ -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); diff --git a/pylibmc.py b/pylibmc.py index 441324d..9b3efe9 100644 --- a/pylibmc.py +++ b/pylibmc.py @@ -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.