From c8e0d19ac0129b2e13741df6ca8b6d7aa495850f Mon Sep 17 00:00:00 2001 From: Steve Chaplin <> Date: Sun, 27 Mar 2011 20:27:40 +0800 Subject: Changes to support Python 3.2 PyCapsule. Note: last commit should say "Update waf from 1.5 to 1.6.3". --- src/cairomodule.c | 26 ++++++++------------------ src/py3cairo.h | 20 ++++++++++++++------ 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/cairomodule.c b/src/cairomodule.c index 58e665c..09612b7 100644 --- a/src/cairomodule.c +++ b/src/cairomodule.c @@ -70,7 +70,7 @@ Pycairo_Check_Status (cairo_status_t status) { } -/* C API. Clients get at this via Pycairo_IMPORT, defined in pycairo.h. +/* C API. Clients get at this via import_cairo(), defined in pycairo.h. */ static Pycairo_CAPI_t CAPI = { &PycairoContext_Type, @@ -268,8 +268,6 @@ PyInit__cairo(void) PyObject *m = PyModule_Create(&cairomodule); - //PyObject *m; - //m = Py_InitModule("cairo._cairo", cairo_functions); if (m==NULL) return NULL; GETSTATE(m)->ErrorObject = PyErr_NewException("cairo.Error", NULL, NULL); @@ -277,18 +275,6 @@ PyInit__cairo(void) Py_DECREF(m); return NULL; } - /* Add 'cairo.Error' to the module */ - // if (CairoError == NULL) { - // CairoError = PyErr_NewException("cairo.Error", NULL, NULL); - // if (CairoError == NULL) - // return NULL; - //} - //Py_INCREF(CairoError); - // not needed ? - //if (PyModule_AddObject(m, "Error", CairoError) < 0) - // return NULL; - - PyModule_AddStringConstant(m, "version", VERSION); PyModule_AddObject(m, "version_info", @@ -377,9 +363,7 @@ PyInit__cairo(void) (PyObject *)&PycairoXlibSurface_Type); #endif - PyModule_AddObject(m, "CAPI", PyCObject_FromVoidPtr(&CAPI, NULL)); - - /* constants */ + /* constants */ #if CAIRO_HAS_ATSUI_FONT PyModule_AddIntConstant(m, "HAS_ATSUI_FONT", 1); #else @@ -541,5 +525,11 @@ PyInit__cairo(void) CONSTANT(SUBPIXEL_ORDER_VBGR); #undef CONSTANT + /* Create a Capsule containing the CAPI pointer */ + PyObject *T = PyCapsule_New((void *)(&CAPI), "cairo.CAPI", 0); + if (T != NULL) { + PyModule_AddObject(m, "CAPI", T); + } + return m; } diff --git a/src/py3cairo.h b/src/py3cairo.h index 35b4240..1aaf743 100644 --- a/src/py3cairo.h +++ b/src/py3cairo.h @@ -193,13 +193,21 @@ typedef struct { #define Pycairo_Check_Status (Pycairo_CAPI->Check_Status) -/* To access the Pycairo C API, edit the client module file to: - * 1) Add the following line to define a global variable for the C API - * static Pycairo_CAPI_t *Pycairo_CAPI; - * 2) Add 'Pycairo_IMPORT;' to the init function +/* To access the Pycairo C API, the client module should call 'import_cairo()' + * from the init function, and check the return value, < 0 means the + * import failed. */ -#define Pycairo_IMPORT \ - Pycairo_CAPI = (Pycairo_CAPI_t*) PyCObject_Import("cairo", "CAPI") +static Pycairo_CAPI_t *Pycairo_CAPI; + +/* Return -1 on error, 0 on success. + * PyCapsule_Import will set an exception if there's an error. + */ +static int +import_cairo(void) +{ + Pycairo_CAPI = (Pycairo_CAPI_t*) PyCapsule_Import("cairo.CAPI", 0); + return (Pycairo_CAPI != 0) ? 0 : -1; +} #endif /* ifndef _INSIDE_PYCAIRO_ */ -- cgit v1.2.1