Permalink
Browse files

Implemented xcb.Protobj subclasses in python

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...
1 parent 0d98bc3 commit 033a35096b0345710bcb80131f33806477209d95 @dequis dequis committed Nov 28, 2010
Showing with 159 additions and 478 deletions.
  1. +2 −3 setup.py
  2. +7 −2 xcb/__init__.py
  3. +0 −2 xcb/conn.c
  4. +9 −4 xcb/cookie.c
  5. +2 −3 xcb/cookie.h
  6. +0 −69 xcb/error.c
  7. +0 −17 xcb/error.h
  8. +0 −57 xcb/event.c
  9. +0 −12 xcb/event.h
  10. +12 −10 xcb/ext.c
  11. +1 −19 xcb/module.c
  12. +97 −1 xcb/protobj.c
  13. +12 −0 xcb/protobj.h
  14. +17 −0 xcb/{response.py → protobj.py}
  15. +0 −39 xcb/reply.c
  16. +0 −16 xcb/reply.h
  17. +0 −73 xcb/request.c
  18. +0 −17 xcb/request.h
  19. +0 −14 xcb/response.h
  20. +0 −46 xcb/struct.c
  21. +0 −14 xcb/struct.h
  22. +0 −46 xcb/union.c
  23. +0 −14 xcb/union.h
View
@@ -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(
View
@@ -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' ]
View
@@ -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"
View
@@ -2,8 +2,7 @@
#include "except.h"
#include "conn.h"
#include "cookie.h"
-#include "error.h"
-#include "reply.h"
+#include "protobj.h"
/*
* Helpers
@@ -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;
}
@@ -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;
}
View
@@ -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;
View
@@ -1,69 +0,0 @@
-#include "module.h"
-#include "except.h"
-#include "response.h"
-#include "error.h"
-
-/*
- * Helpers
- */
-
-int
-xpybError_set(xpybConn *conn, xcb_generic_error_t *e)
-{
- unsigned char opcode;
- PyObject *shim, *error, *type, *except;
-
- type = (PyObject *)xpybError_type;
- except = xpybExcept_proto;
-
- if (e) {
- opcode = e->error_code;
- if (opcode < conn->errors_len && conn->errors[opcode] != NULL) {
- type = PyTuple_GET_ITEM(conn->errors[opcode], 0);
- except = PyTuple_GET_ITEM(conn->errors[opcode], 1);
- }
-
- shim = PyBuffer_FromMemory(e, sizeof(*e));
- if (shim == NULL)
- return 1;
-
- error = PyObject_CallFunctionObjArgs(type, shim, NULL);
- if (error != NULL)
- PyErr_SetObject(except, error);
- Py_DECREF(shim);
- return 1;
- }
- return 0;
-}
-
-
-/*
- * Infrastructure
- */
-
-
-/*
- * Members
- */
-
-
-/*
- * Definition
- */
-
-PyTypeObject *xpybError_type;
-
-/*
- * Module init
- */
-int xpybError_modinit(PyObject *m)
-{
- PyObject *module = PyImport_ImportModule("xcb.response");
- if (!module)
- return -1;
-
- xpybError_type = (PyTypeObject *) PyObject_GetAttrString(module, "Error");
- Py_INCREF(xpybError_type);
-
- return 0;
-}
View
@@ -1,17 +0,0 @@
-#ifndef XPYB_ERROR_H
-#define XPYB_ERROR_H
-
-#include "response.h"
-#include "conn.h"
-
-typedef struct {
- xpybResponse response;
-} xpybError;
-
-extern PyTypeObject *xpybError_type;
-
-int xpybError_set(xpybConn *conn, xcb_generic_error_t *e);
-
-int xpybError_modinit(PyObject *m);
-
-#endif
View
@@ -1,57 +0,0 @@
-#include "module.h"
-#include "except.h"
-#include "event.h"
-
-/*
- * Helpers
- */
-
-PyObject *
-xpybEvent_create(xpybConn *conn, xcb_generic_event_t *e)
-{
- unsigned char opcode = e->response_type;
- PyObject *shim, *event, *type = (PyObject *)xpybEvent_type;
-
- if (opcode < conn->events_len && conn->events[opcode] != NULL)
- type = conn->events[opcode];
-
- shim = PyBuffer_FromMemory(e, sizeof(*e));
- if (shim == NULL)
- return NULL;
-
- event = PyObject_CallFunctionObjArgs(type, shim, NULL);
- Py_DECREF(shim);
- return event;
-}
-
-
-/*
- * Infrastructure
- */
-
-
-/*
- * Members
- */
-
-
-/*
- * Definition
- */
-
-PyTypeObject *xpybEvent_type;
-
-/*
- * Module init
- */
-int xpybEvent_modinit(PyObject *m)
-{
- PyObject *module = PyImport_ImportModule("xcb.response");
- if (!module)
- return -1;
-
- xpybEvent_type = (PyTypeObject *) PyObject_GetAttrString(module, "Event");
- Py_INCREF(xpybEvent_type);
-
- return 0;
-}
View
@@ -1,12 +0,0 @@
-#ifndef XPYB_EVENT_H
-#define XPYB_EVENT_H
-
-#include "conn.h"
-
-extern PyTypeObject *xpybEvent_type;
-
-PyObject *xpybEvent_create(xpybConn *conn, xcb_generic_event_t *e);
-
-int xpybEvent_modinit(PyObject *m);
-
-#endif
View
@@ -4,8 +4,7 @@
#include "ext.h"
#include "conn.h"
#include "cookie.h"
-#include "reply.h"
-#include "request.h"
+#include "protobj.h"
/*
* Helpers
@@ -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;
@@ -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;
@@ -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)
@@ -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 */
View
@@ -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"
@@ -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;
}
@@ -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;
Oops, something went wrong.

0 comments on commit 033a350

Please sign in to comment.