From ecbafe6419f02830293a9b599625e5905dad15e7 Mon Sep 17 00:00:00 2001 From: dequis Date: Tue, 23 Nov 2010 14:10:32 -0300 Subject: [PATCH] Added response.py, implements Response, Event, Reply, Error --- setup.py | 2 +- xcb/__init__.py | 4 +++ xcb/conn.c | 1 - xcb/error.c | 38 ++++++----------------------- xcb/error.h | 2 +- xcb/event.c | 22 ++++++----------- xcb/event.h | 7 +----- xcb/ext.c | 2 +- xcb/module.c | 3 --- xcb/reply.c | 37 +++++----------------------- xcb/reply.h | 2 +- xcb/response.c | 65 ------------------------------------------------- xcb/response.py | 27 ++++++++++++++++++++ 13 files changed, 56 insertions(+), 156 deletions(-) delete mode 100644 xcb/response.c create mode 100644 xcb/response.py diff --git a/setup.py b/setup.py index 942d315..fc97f69 100644 --- a/setup.py +++ b/setup.py @@ -17,7 +17,7 @@ extensions = [ "conn", "constant", "cookie", "error", "event", "except", "ext", "extkey", "list", "module", - "protobj", "reply", "request", "response", "struct", + "protobj", "reply", "request", "struct", "union", "void" ] ext_modules = [ diff --git a/xcb/__init__.py b/xcb/__init__.py index dcbd512..cfa7ba1 100644 --- a/xcb/__init__.py +++ b/xcb/__init__.py @@ -1,4 +1,8 @@ from xcb import * from iter import Iterator +from response import Response, Event, Reply, Error +xcb.Event = Event +xcb.Error = Error +xcb.Reply = Reply __all__ = [ 'xproto', 'bigreq', 'xc_misc' ] diff --git a/xcb/conn.c b/xcb/conn.c index 39833c8..ecbca72 100644 --- a/xcb/conn.c +++ b/xcb/conn.c @@ -1,7 +1,6 @@ #include "module.h" #include "except.h" #include "cookie.h" -#include "response.h" #include "event.h" #include "error.h" #include "extkey.h" diff --git a/xcb/error.c b/xcb/error.c index c51530b..6436ad5 100644 --- a/xcb/error.c +++ b/xcb/error.c @@ -13,7 +13,7 @@ xpybError_set(xpybConn *conn, xcb_generic_error_t *e) unsigned char opcode; PyObject *shim, *error, *type, *except; - type = (PyObject *)&xpybError_type; + type = (PyObject *)xpybError_type; except = xpybExcept_proto; if (e) { @@ -46,48 +46,24 @@ xpybError_set(xpybConn *conn, xcb_generic_error_t *e) * Members */ -static PyObject * -xpybError_getattro(PyObject *self, PyObject *obj) -{ - const char *name = PyString_AS_STRING(obj); - const xcb_generic_error_t *data; - Py_ssize_t size; - - if (PyObject_AsReadBuffer(self, (const void **)&data, &size) < 0) - return NULL; - - if (strcmp(name, "code") == 0) - return Py_BuildValue("B", data->error_code); - - return xpybError_type.tp_base->tp_getattro(self, obj); -} - /* * Definition */ -PyTypeObject xpybError_type = { - PyObject_HEAD_INIT(NULL) - .tp_name = "xcb.Error", - .tp_basicsize = sizeof(xpybError), - .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, - .tp_doc = "XCB generic error object", - .tp_base = &xpybResponse_type, - .tp_getattro = xpybError_getattro -}; - +PyTypeObject *xpybError_type; /* * Module init */ int xpybError_modinit(PyObject *m) { - if (PyType_Ready(&xpybError_type) < 0) + PyObject *module = PyImport_ImportModule("xcb.response"); + if (!module) return -1; - Py_INCREF(&xpybError_type); - if (PyModule_AddObject(m, "Error", (PyObject *)&xpybError_type) < 0) - return -1; + + xpybError_type = (PyTypeObject *) PyObject_GetAttrString(module, "Error"); + Py_INCREF(xpybError_type); return 0; } diff --git a/xcb/error.h b/xcb/error.h index 910b8b8..df281d9 100644 --- a/xcb/error.h +++ b/xcb/error.h @@ -8,7 +8,7 @@ typedef struct { xpybResponse response; } xpybError; -extern PyTypeObject xpybError_type; +extern PyTypeObject *xpybError_type; int xpybError_set(xpybConn *conn, xcb_generic_error_t *e); diff --git a/xcb/event.c b/xcb/event.c index 73acf1f..6317dd8 100644 --- a/xcb/event.c +++ b/xcb/event.c @@ -1,6 +1,5 @@ #include "module.h" #include "except.h" -#include "response.h" #include "event.h" /* @@ -11,7 +10,7 @@ PyObject * xpybEvent_create(xpybConn *conn, xcb_generic_event_t *e) { unsigned char opcode = e->response_type; - PyObject *shim, *event, *type = (PyObject *)&xpybEvent_type; + PyObject *shim, *event, *type = (PyObject *)xpybEvent_type; if (opcode < conn->events_len && conn->events[opcode] != NULL) type = conn->events[opcode]; @@ -40,26 +39,19 @@ xpybEvent_create(xpybConn *conn, xcb_generic_event_t *e) * Definition */ -PyTypeObject xpybEvent_type = { - PyObject_HEAD_INIT(NULL) - .tp_name = "xcb.Event", - .tp_basicsize = sizeof(xpybEvent), - .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, - .tp_doc = "XCB generic event object", - .tp_base = &xpybResponse_type -}; - +PyTypeObject *xpybEvent_type; /* * Module init */ int xpybEvent_modinit(PyObject *m) { - if (PyType_Ready(&xpybEvent_type) < 0) + PyObject *module = PyImport_ImportModule("xcb.response"); + if (!module) return -1; - Py_INCREF(&xpybEvent_type); - if (PyModule_AddObject(m, "Event", (PyObject *)&xpybEvent_type) < 0) - return -1; + + xpybEvent_type = (PyTypeObject *) PyObject_GetAttrString(module, "Event"); + Py_INCREF(xpybEvent_type); return 0; } diff --git a/xcb/event.h b/xcb/event.h index 6b05d12..66d9b51 100644 --- a/xcb/event.h +++ b/xcb/event.h @@ -1,14 +1,9 @@ #ifndef XPYB_EVENT_H #define XPYB_EVENT_H -#include "response.h" #include "conn.h" -typedef struct { - xpybResponse response; -} xpybEvent; - -extern PyTypeObject xpybEvent_type; +extern PyTypeObject *xpybEvent_type; PyObject *xpybEvent_create(xpybConn *conn, xcb_generic_event_t *e); diff --git a/xcb/ext.c b/xcb/ext.c index 2d622d9..91691fb 100644 --- a/xcb/ext.c +++ b/xcb/ext.c @@ -108,7 +108,7 @@ xpybExt_send_request(xpybExt *self, PyObject *args, PyObject *kw) return NULL; if (!request->is_void) - if (reply == NULL || !PyType_IsSubtype(reply, &xpybReply_type)) { + if (reply == NULL || !PyType_IsSubtype(reply, xpybReply_type)) { PyErr_SetString(xpybExcept_base, "Reply type missing or not derived from xcb.Reply."); return NULL; } diff --git a/xcb/module.c b/xcb/module.c index 9277536..74be4c3 100644 --- a/xcb/module.c +++ b/xcb/module.c @@ -4,7 +4,6 @@ #include "constant.h" #include "cookie.h" #include "protobj.h" -#include "response.h" #include "event.h" #include "error.h" #include "reply.h" @@ -274,8 +273,6 @@ initxcb(void) if (xpybProtobj_modinit(m) < 0) return; - if (xpybResponse_modinit(m) < 0) - return; if (xpybEvent_modinit(m) < 0) return; if (xpybError_modinit(m) < 0) diff --git a/xcb/reply.c b/xcb/reply.c index d87a135..01ec0b5 100644 --- a/xcb/reply.c +++ b/xcb/reply.c @@ -17,48 +17,23 @@ * Members */ -static PyObject * -xpybReply_getattro(PyObject *self, PyObject *obj) -{ - const char *name = PyString_AS_STRING(obj); - xcb_generic_reply_t *data; - Py_ssize_t size; - - if (PyObject_AsReadBuffer(self, (const void **)&data, &size) < 0) - return NULL; - - if (strcmp(name, "length") == 0) - return Py_BuildValue("I", data->length); - - return xpybReply_type.tp_base->tp_getattro(self, obj); -} - - /* * Definition */ -PyTypeObject xpybReply_type = { - PyObject_HEAD_INIT(NULL) - .tp_name = "xcb.Reply", - .tp_basicsize = sizeof(xpybReply), - .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, - .tp_doc = "XCB generic reply object", - .tp_base = &xpybResponse_type, - .tp_getattro = xpybReply_getattro -}; - +PyTypeObject *xpybReply_type; /* * Module init */ int xpybReply_modinit(PyObject *m) { - if (PyType_Ready(&xpybReply_type) < 0) + PyObject *module = PyImport_ImportModule("xcb.response"); + if (!module) return -1; - Py_INCREF(&xpybReply_type); - if (PyModule_AddObject(m, "Reply", (PyObject *)&xpybReply_type) < 0) - return -1; + + xpybReply_type = (PyTypeObject *) PyObject_GetAttrString(module, "Reply"); + Py_INCREF(xpybReply_type); return 0; } diff --git a/xcb/reply.h b/xcb/reply.h index a1a42af..34bbace 100644 --- a/xcb/reply.h +++ b/xcb/reply.h @@ -7,7 +7,7 @@ typedef struct { xpybResponse response; } xpybReply; -extern PyTypeObject xpybReply_type; +extern PyTypeObject *xpybReply_type; int xpybReply_populate(xpybReply *self, xcb_generic_reply_t *data); diff --git a/xcb/response.c b/xcb/response.c deleted file mode 100644 index abaad77..0000000 --- a/xcb/response.c +++ /dev/null @@ -1,65 +0,0 @@ -#include "module.h" -#include "except.h" -#include "response.h" - -/* - * Helpers - */ - - -/* - * Infrastructure - */ - - -/* - * Members - */ - -static PyObject * -xpybResponse_getattro(PyObject *self, PyObject *obj) -{ - const char *name = PyString_AS_STRING(obj); - const xcb_generic_event_t *data; - Py_ssize_t size; - - if (PyObject_AsReadBuffer(self, (const void **)&data, &size) < 0) - return NULL; - - if (strcmp(name, "response_type") == 0) - return Py_BuildValue("B", data->response_type); - if (strcmp(name, "sequence") == 0) - return Py_BuildValue("H", data->sequence); - - return xpybResponse_type.tp_base->tp_getattro(self, obj); -} - - -/* - * Definition - */ - -PyTypeObject xpybResponse_type = { - PyObject_HEAD_INIT(NULL) - .tp_name = "xcb.Response", - .tp_basicsize = sizeof(xpybResponse), - .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, - .tp_doc = "XCB generic response object", - .tp_base = &xpybProtobj_type, - .tp_getattro = xpybResponse_getattro -}; - - -/* - * Module init - */ -int xpybResponse_modinit(PyObject *m) -{ - if (PyType_Ready(&xpybResponse_type) < 0) - return -1; - Py_INCREF(&xpybResponse_type); - if (PyModule_AddObject(m, "Response", (PyObject *)&xpybResponse_type) < 0) - return -1; - - return 0; -} diff --git a/xcb/response.py b/xcb/response.py new file mode 100644 index 0000000..a771447 --- /dev/null +++ b/xcb/response.py @@ -0,0 +1,27 @@ +import xcb +import struct + +class Response(xcb.Protobj): + """XCB generic response object""" + def __init__(self, parent): + xcb.Protobj.__init__(self, parent) + # self is a xcb_generic_event_t + self.response_type, self.sequence = struct.unpack_from('BxH', self) + +class Event(Response): + """XCB generic event object""" + pass + +class Reply(Response): + """XCB generic reply object""" + def __init__(self, parent): + Response.__init__(self, parent) + # self is a xcb_generic_reply_t + (self.length, ) = struct.unpack_from('4xI', self) + +class Error(Response): + """XCB generic error object""" + def __init__(self, parent): + Response.__init__(self, parent) + # self is a xcb_generic_error_t + (self.code, ) = struct.unpack_from('xB', self)