Skip to content

Commit

Permalink
Implemented xcb.Protobj subclasses in python
Browse files Browse the repository at this point in the history
This includes: Struct, Union, Request, Response, Event, Reply, Error.

The Protobj base class itself can't be translated to python because it
must expose a buffer interface for PyObject_AsReadBuffer, which is also
called outside of this project, in pycairo.
  • Loading branch information
dequis committed Nov 28, 2010
1 parent 0d98bc3 commit 033a350
Show file tree
Hide file tree
Showing 23 changed files with 159 additions and 478 deletions.
5 changes: 2 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ def find_xcb():
"xtest", "xvmc", "xv"
]
extensions = [
"conn", "constant", "cookie", "error", "event",
"conn", "constant", "cookie",
"except", "ext", "extkey", "list", "module",
"protobj", "reply", "request", "struct",
"union", "void"
"protobj", "void"
]
ext_modules = [
Extension(
Expand Down
9 changes: 7 additions & 2 deletions xcb/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from xcb import *
from iter import Iterator
from response import Response, Event, Reply, Error
from protobj import *

xcb.Struct = Struct
xcb.Union = Union
xcb.Request = Request
xcb.Response = Response
xcb.Event = Event
xcb.Error = Error
xcb.Reply = Reply
xcb.Error = Error

__all__ = [ 'xproto', 'bigreq', 'xc_misc' ]
2 changes: 0 additions & 2 deletions xcb/conn.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "module.h"
#include "except.h"
#include "cookie.h"
#include "event.h"
#include "error.h"
#include "extkey.h"
#include "ext.h"
#include "conn.h"
Expand Down
13 changes: 9 additions & 4 deletions xcb/cookie.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
#include "except.h"
#include "conn.h"
#include "cookie.h"
#include "error.h"
#include "reply.h"
#include "protobj.h"

/*
* Helpers
Expand Down Expand Up @@ -46,8 +45,11 @@ static PyObject *
xpybCookie_check(xpybCookie *self, PyObject *args)
{
xcb_generic_error_t *error;
int is_void, is_checked;

if (!(self->request->is_void && self->request->is_checked)) {
xpybRequest_get_attributes(self->request, &is_void, NULL, &is_checked);

if (!(is_void && is_checked)) {
PyErr_SetString(xpybExcept_base, "Request is not void and checked.");
return NULL;
}
Expand All @@ -67,9 +69,12 @@ xpybCookie_reply(xpybCookie *self, PyObject *args)
xcb_generic_error_t *error;
xcb_generic_reply_t *data;
PyObject *shim, *reply;
int is_void;

xpybRequest_get_attributes(self->request, &is_void, NULL, NULL);

/* Check arguments and connection. */
if (self->request->is_void) {
if (is_void) {
PyErr_SetString(xpybExcept_base, "Request has no reply.");
return NULL;
}
Expand Down
5 changes: 2 additions & 3 deletions xcb/cookie.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
#define XPYB_COOKIE_H

#include "conn.h"
#include "request.h"
#include "reply.h"
#include "protobj.h"

typedef struct {
PyObject_HEAD
xpybConn *conn;
xpybRequest *request;
PyObject *request;
PyTypeObject *reply_type;
xcb_void_cookie_t cookie;
} xpybCookie;
Expand Down
69 changes: 0 additions & 69 deletions xcb/error.c

This file was deleted.

17 changes: 0 additions & 17 deletions xcb/error.h

This file was deleted.

57 changes: 0 additions & 57 deletions xcb/event.c

This file was deleted.

12 changes: 0 additions & 12 deletions xcb/event.h

This file was deleted.

22 changes: 12 additions & 10 deletions xcb/ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
#include "ext.h"
#include "conn.h"
#include "cookie.h"
#include "reply.h"
#include "request.h"
#include "protobj.h"

/*
* Helpers
Expand Down Expand Up @@ -90,7 +89,7 @@ static PyObject *
xpybExt_send_request(xpybExt *self, PyObject *args, PyObject *kw)
{
static char *kwlist[] = { "request", "cookie", "reply", NULL };
xpybRequest *request;
PyObject *request;
xpybCookie *cookie;
PyTypeObject *reply = NULL;
xcb_protocol_request_t xcb_req;
Expand All @@ -99,15 +98,18 @@ xpybExt_send_request(xpybExt *self, PyObject *args, PyObject *kw)
int flags;
const void *data;
Py_ssize_t size;
int is_void, opcode, is_checked;

/* Parse and check arguments */
if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|O!", kwlist,
&xpybRequest_type, &request,
&xpybCookie_type, &cookie,
&PyType_Type, &reply))
xpybRequest_type, &request,
&xpybCookie_type, &cookie,
&PyType_Type, &reply))
return NULL;

if (!request->is_void)
xpybRequest_get_attributes(request, &is_void, &opcode, &is_checked);

if (!is_void)
if (reply == NULL || !PyType_IsSubtype(reply, xpybReply_type)) {
PyErr_SetString(xpybExcept_base, "Reply type missing or not derived from xcb.Reply.");
return NULL;
Expand All @@ -116,8 +118,8 @@ xpybExt_send_request(xpybExt *self, PyObject *args, PyObject *kw)
/* Set up request structure */
xcb_req.count = 2;
xcb_req.ext = (self->key != (xpybExtkey *)Py_None) ? &self->key->key : 0;
xcb_req.opcode = request->opcode;
xcb_req.isvoid = request->is_void;
xcb_req.opcode = opcode;
xcb_req.isvoid = is_void;

/* Allocate and fill in data strings */
if (PyObject_AsReadBuffer(((xpybProtobj *)request)->buf, &data, &size) < 0)
Expand All @@ -128,7 +130,7 @@ xpybExt_send_request(xpybExt *self, PyObject *args, PyObject *kw)
xcb_parts[3].iov_len = -xcb_parts[2].iov_len & 3;

/* Make request call */
flags = request->is_checked ? XCB_REQUEST_CHECKED : 0;
flags = is_checked ? XCB_REQUEST_CHECKED : 0;
seq = xcb_send_request(self->conn->conn, flags, xcb_parts + 2, &xcb_req);

/* Set up cookie */
Expand Down
20 changes: 1 addition & 19 deletions xcb/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
#include "constant.h"
#include "cookie.h"
#include "protobj.h"
#include "event.h"
#include "error.h"
#include "reply.h"
#include "request.h"
#include "struct.h"
#include "union.h"
#include "list.h"
#include "conn.h"
#include "extkey.h"
Expand Down Expand Up @@ -111,7 +105,7 @@ xpyb_add_core(PyObject *self, PyObject *args)
PyErr_SetString(xpybExcept_base, "Extension type not derived from xcb.Extension.");
return NULL;
}
if (!PyType_IsSubtype(setup, &xpybStruct_type)) {
if (!PyType_IsSubtype(setup, xpybStruct_type)) {
PyErr_SetString(xpybExcept_base, "Setup type not derived from xcb.Struct.");
return NULL;
}
Expand Down Expand Up @@ -273,18 +267,6 @@ initxcb(void)

if (xpybProtobj_modinit(m) < 0)
return;
if (xpybEvent_modinit(m) < 0)
return;
if (xpybError_modinit(m) < 0)
return;
if (xpybReply_modinit(m) < 0)
return;
if (xpybRequest_modinit(m) < 0)
return;
if (xpybStruct_modinit(m) < 0)
return;
if (xpybUnion_modinit(m) < 0)
return;

if (xpybList_modinit(m) < 0)
return;
Expand Down
Loading

0 comments on commit 033a350

Please sign in to comment.