Skip to content
Browse files

Make xpybConn a base type

Signed-off-by: Julien Danjou <julien@danjou.info>
  • Loading branch information...
1 parent 38370d3 commit bb41ff214ddcace2a08bf998c6c7411cac1a30d3 @sigalrm sigalrm committed Jan 9, 2010
Showing with 87 additions and 84 deletions.
  1. +74 −15 src/conn.c
  2. +3 −0 src/conn.h
  3. +10 −69 src/module.c
View
89 src/conn.c
@@ -25,38 +25,96 @@ xpybConn_invalid(xpybConn *self)
return 0;
}
-xpybConn *
-xpybConn_create(PyObject *core_type)
+static int
+xpyb_parse_auth(const char *authstr, int authlen, xcb_auth_info_t *auth)
{
- xpybConn *self;
+ int i = 0;
- self = PyObject_New(xpybConn, &xpybConn_type);
- if (self == NULL)
- return NULL;
+ while (i < authlen && authstr[i] != ':')
+ i++;
+
+ if (i >= authlen) {
+ PyErr_SetString(xpybExcept_base, "Auth string must take the form '<name>:<data>'.");
+ return -1;
+ }
+ auth->name = (char *)authstr;
+ auth->namelen = i++;
+ auth->data = (char *)authstr + i;
+ auth->datalen = authlen - i;
+ return 0;
+}
+
+int
+xpybConn_init_struct(xpybConn *self, PyObject *core_type)
+{
self->core = PyObject_CallFunctionObjArgs(core_type, self, NULL);
if (self->core == NULL)
- goto err;
+ return -1;
self->dict = PyDict_New();
if (self->dict == NULL)
- goto err;
+ return -1;
- self->extcache = PyDict_New();
+ self->extcache = PyDict_New();
if (self->extcache == NULL)
- goto err;
+ return -1;
self->wrapped = 0;
self->setup = NULL;
self->events = NULL;
self->events_len = 0;
self->errors = NULL;
self->errors_len = 0;
- return self;
+ return 0;
+}
-err:
- Py_DECREF(self);
- return NULL;
+int
+xpybConn_init(xpybConn *self, PyObject *args, PyObject *kw)
+{
+ static char *kwlist[] = { "display", "fd", "auth", NULL };
+ const char *displayname = NULL, *authstr = NULL;
+ xcb_auth_info_t auth, *authptr = NULL;
+ int authlen, fd = -1;
+
+ /* Make sure core was set. */
+ if (xpybModule_core == NULL) {
+ PyErr_SetString(xpybExcept_base, "No core protocol object has been set. Did you import xcb.xproto?");
+ return -1;
+ }
+
+ /* Parse arguments and allocate new connection object */
+ if (!PyArg_ParseTupleAndKeywords(args, kw, "|ziz#", kwlist, &displayname,
+ &fd, &authstr, &authlen))
+ return -1;
+
+ /* Set up authorization */
+ if (authstr != NULL) {
+ if (xpyb_parse_auth(authstr, authlen, &auth) < 0)
+ return -1;
+ authptr = &auth;
+ }
+
+ /* Connect to display */
+ if (fd >= 0)
+ self->conn = xcb_connect_to_fd(fd, authptr);
+ else if (authptr)
+ self->conn = xcb_connect_to_display_with_auth_info(displayname, authptr, &self->pref_screen);
+ else
+ self->conn = xcb_connect(displayname, &self->pref_screen);
+
+ if (xcb_connection_has_error(self->conn)) {
+ PyErr_SetString(xpybExcept_conn, "Failed to connect to X server.");
+ return -1;
+ }
+
+ xpybConn_init_struct(self, (PyObject *)xpybModule_core);
+
+ /* Load extensions */
+ if (xpybConn_setup(self) < 0)
+ return -1;
+
+ return 0;
}
static xpybExt *
@@ -511,7 +569,8 @@ PyTypeObject xpybConn_type = {
.tp_basicsize = sizeof(xpybConn),
.tp_new = xpybConn_new,
.tp_dealloc = (destructor)xpybConn_dealloc,
- .tp_flags = Py_TPFLAGS_DEFAULT,
+ .tp_init = (initproc)xpybConn_init,
+ .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.tp_doc = "XCB connection object",
.tp_methods = xpybConn_methods,
.tp_members = xpybConn_members,
View
3 src/conn.h
@@ -11,4 +11,7 @@ int xpybConn_setup(xpybConn *self);
int xpybConn_modinit(PyObject *m);
+int xpybConn_init_struct(xpybConn *self, PyObject *core_type);
+int xpybConn_init(xpybConn *self, PyObject *args, PyObject *kw);
+
#endif
View
79 src/module.c
@@ -34,85 +34,21 @@ PyObject *xpybModule_ext_errors;
/*
- * Helpers
- */
-
-static int
-xpyb_parse_auth(const char *authstr, int authlen, xcb_auth_info_t *auth)
-{
- int i = 0;
-
- while (i < authlen && authstr[i] != ':')
- i++;
-
- if (i >= authlen) {
- PyErr_SetString(xpybExcept_base, "Auth string must take the form '<name>:<data>'.");
- return -1;
- }
-
- auth->name = (char *)authstr;
- auth->namelen = i++;
- auth->data = (char *)authstr + i;
- auth->datalen = authlen - i;
- return 0;
-}
-
-/*
* Module functions
*/
static PyObject *
xpyb_connect(PyObject *self, PyObject *args, PyObject *kw)
{
- static char *kwlist[] = { "display", "fd", "auth", NULL };
- const char *displayname = NULL, *authstr = NULL;
- xcb_auth_info_t auth, *authptr = NULL;
- xpybConn *conn;
- int authlen, fd = -1;
-
- /* Make sure core was set. */
- if (xpybModule_core == NULL) {
- PyErr_SetString(xpybExcept_base, "No core protocol object has been set. Did you import xcb.xproto?");
- return NULL;
- }
+ xpybConn *conn = PyObject_New(xpybConn, &xpybConn_type);
- /* Parse arguments and allocate new connection object */
- if (!PyArg_ParseTupleAndKeywords(args, kw, "|ziz#", kwlist, &displayname,
- &fd, &authstr, &authlen))
- return NULL;
-
- conn = xpybConn_create((PyObject *)xpybModule_core);
if (conn == NULL)
return NULL;
- /* Set up authorization */
- if (authstr != NULL) {
- if (xpyb_parse_auth(authstr, authlen, &auth) < 0)
- goto err;
- authptr = &auth;
- }
-
- /* Connect to display */
- if (fd >= 0)
- conn->conn = xcb_connect_to_fd(fd, authptr);
- else if (authptr)
- conn->conn = xcb_connect_to_display_with_auth_info(displayname, authptr, &conn->pref_screen);
- else
- conn->conn = xcb_connect(displayname, &conn->pref_screen);
+ if(xpybConn_init(conn, args, kw) < 0)
+ return NULL;
- if (xcb_connection_has_error(conn->conn)) {
- PyErr_SetString(xpybExcept_conn, "Failed to connect to X server.");
- goto err;
- }
-
- /* Load extensions */
- if (xpybConn_setup(conn) < 0)
- goto err;
-
- return (PyObject *)conn;
-err:
- Py_DECREF(conn);
- return NULL;
+ return (PyObject *) conn;
}
static PyObject *
@@ -132,10 +68,15 @@ xpyb_wrap(PyObject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "O", &obj))
return NULL;
- conn = xpybConn_create((PyObject *)xpybModule_core);
+ /* Create Python object */
+ conn = PyObject_New(xpybConn, &xpybConn_type);
if (conn == NULL)
return NULL;
+ /* Init struct of that Python object */
+ if (xpybConn_init_struct(conn, (PyObject *)xpybModule_core) < 0)
+ return NULL;
+
/* Get our pointer */
raw = PyLong_AsVoidPtr(obj);
if (!raw || PyErr_Occurred()) {

0 comments on commit bb41ff2

Please sign in to comment.
Something went wrong with that request. Please try again.