diff options
author | Eamon Walsh <ewalsh@tycho.nsa.gov> | 2008-05-31 00:12:38 -0400 |
---|---|---|
committer | Eamon Walsh <ewalsh@moss-charon.epoch.ncsc.mil> | 2008-05-31 00:12:38 -0400 |
commit | 65ba91e8fb63a8570147b75b0e21e0bb73bcd3a6 (patch) | |
tree | bdb92e7619b3a0bd86e4017dce5acba181316c7c /src | |
parent | 0dc4dbce0853d592570a28c21dae80d41d351288 (diff) |
Support setting attributes on connection objects.
Diffstat (limited to 'src')
-rw-r--r-- | src/conn.c | 66 | ||||
-rw-r--r-- | src/conn.h | 1 |
2 files changed, 61 insertions, 6 deletions
@@ -36,11 +36,15 @@ xpybConn_create(PyObject *core_type) self->core = PyObject_CallFunctionObjArgs(core_type, self, NULL); if (self->core == NULL) - goto err1; + goto err; + + self->dict = PyDict_New(); + if (self->dict == NULL) + goto err; self->extcache = PyDict_New(); if (self->extcache == NULL) - goto err2; + goto err; self->setup = NULL; self->events = NULL; @@ -49,9 +53,7 @@ xpybConn_create(PyObject *core_type) self->errors_len = 0; return self; -err2: - Py_DECREF(self->core); -err1: +err: Py_DECREF(self); return NULL; } @@ -182,6 +184,7 @@ xpybConn_dealloc(xpybConn *self) { int i; + Py_CLEAR(self->dict); Py_CLEAR(self->core); Py_CLEAR(self->setup); Py_CLEAR(self->extcache); @@ -200,6 +203,49 @@ xpybConn_dealloc(xpybConn *self) } static PyObject * +xpybConn_getattro(xpybConn *self, PyObject *obj) +{ + const char *name = PyString_AS_STRING(obj); + PyMethodDef *mptr = xpybConn_type.tp_methods; + PyMemberDef *sptr = xpybConn_type.tp_members; + PyObject *result; + + while (mptr && mptr->ml_name) + if (strcmp(name, (mptr++)->ml_name) == 0) + goto out2; + while (sptr && sptr->name) + if (strcmp(name, (sptr++)->name) == 0) + goto out2; + + Py_XINCREF(result = PyDict_GetItem(self->dict, obj)); + if (result != NULL || PyErr_Occurred()) + return result; + + return xpybConn_type.tp_base->tp_getattro((PyObject *)self, obj); +out2: + return PyObject_GenericGetAttr((PyObject *)self, obj); +} + +static int +xpybConn_setattro(xpybConn *self, PyObject *obj, PyObject *val) +{ + const char *name = PyString_AS_STRING(obj); + PyMethodDef *mptr = xpybConn_type.tp_methods; + PyMemberDef *sptr = xpybConn_type.tp_members; + + while (mptr && mptr->ml_name) + if (strcmp(name, (mptr++)->ml_name) == 0) + goto out2; + while (sptr && sptr->name) + if (strcmp(name, (sptr++)->name) == 0) + goto out2; + + return val ? PyDict_SetItem(self->dict, obj, val) : PyDict_DelItem(self->dict, obj); +out2: + return PyObject_GenericSetAttr((PyObject *)self, obj, val); +} + +static PyObject * xpybConn_call(xpybConn *self, PyObject *args, PyObject *kw) { static char *kwlist[] = { "key", NULL }; @@ -241,6 +287,12 @@ static PyMemberDef xpybConn_members[] = { READONLY, "Core protocol object" }, + { "__dict__", + T_OBJECT, + offsetof(xpybConn, dict), + READONLY, + "Instance dictionary object" }, + { NULL } /* terminator */ }; @@ -462,7 +514,9 @@ PyTypeObject xpybConn_type = { .tp_doc = "XCB connection object", .tp_methods = xpybConn_methods, .tp_members = xpybConn_members, - .tp_call = (ternaryfunc)xpybConn_call + .tp_call = (ternaryfunc)xpybConn_call, + .tp_getattro = (getattrofunc)xpybConn_getattro, + .tp_setattro = (setattrofunc)xpybConn_setattro }; @@ -4,6 +4,7 @@ typedef struct { PyObject_HEAD xcb_connection_t *conn; + PyObject *dict; int pref_screen; PyObject *core; PyObject *setup; |