Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
Added response.py, implements Response, Event, Reply, Error
Browse files Browse the repository at this point in the history
  • Loading branch information
dequis committed Nov 23, 2010
1 parent ef3f707 commit ecbafe6
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 156 deletions.
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -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 = [
Expand Down
4 changes: 4 additions & 0 deletions 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' ]
1 change: 0 additions & 1 deletion 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"
Expand Down
38 changes: 7 additions & 31 deletions xcb/error.c
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion xcb/error.h
Expand Up @@ -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);

Expand Down
22 changes: 7 additions & 15 deletions xcb/event.c
@@ -1,6 +1,5 @@
#include "module.h"
#include "except.h"
#include "response.h"
#include "event.h"

/*
Expand All @@ -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];
Expand Down Expand Up @@ -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;
}
7 changes: 1 addition & 6 deletions 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);

Expand Down
2 changes: 1 addition & 1 deletion xcb/ext.c
Expand Up @@ -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;
}
Expand Down
3 changes: 0 additions & 3 deletions xcb/module.c
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
37 changes: 6 additions & 31 deletions xcb/reply.c
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion xcb/reply.h
Expand Up @@ -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);

Expand Down
65 changes: 0 additions & 65 deletions xcb/response.c

This file was deleted.

27 changes: 27 additions & 0 deletions 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)

0 comments on commit ecbafe6

Please sign in to comment.