Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added response.py, implements Response, Event, Reply, Error

  • Loading branch information...
commit ecbafe6419f02830293a9b599625e5905dad15e7 1 parent ef3f707
@dequis dequis authored
View
2  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 = [
View
4 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' ]
View
1  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"
View
38 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;
}
View
2  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);
View
22 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;
}
View
7 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);
View
2  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;
}
View
3  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)
View
37 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;
}
View
2  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);
View
65 xcb/response.c
@@ -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;
-}
View
27 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)
Please sign in to comment.
Something went wrong with that request. Please try again.