summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSteve Chaplin <>2011-03-27 20:27:40 +0800
committerSteve Chaplin <>2011-03-27 20:27:40 +0800
commitc8e0d19ac0129b2e13741df6ca8b6d7aa495850f (patch)
tree5cf088f7d45c990d831e86f707cfd804bbbd1226 /src
parent4c8b539ef4643755f4aed5425b32f446024bb995 (diff)
Changes to support Python 3.2 PyCapsule.
Note: last commit should say "Update waf from 1.5 to 1.6.3".
Diffstat (limited to 'src')
-rw-r--r--src/cairomodule.c26
-rw-r--r--src/py3cairo.h20
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<module> function
+/* To access the Pycairo C API, the client module should call 'import_cairo()'
+ * from the init<module> 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_ */