Skip to content
Permalink
Browse files

Port to Python 3

  • Loading branch information...
lazka committed Apr 7, 2017
1 parent 22a7693 commit 727470e8a170bf355c9689826bb1db95f7c1389e
Showing with 303 additions and 141 deletions.
  1. +1 −1 cairo/__init__.py
  2. +85 −29 cairo/cairomodule.c
  3. +79 −0 cairo/compat.h
  4. +13 −15 cairo/context.c
  5. +16 −20 cairo/font.c
  6. +13 −6 cairo/matrix.c
  7. +9 −11 cairo/path.c
  8. +9 −15 cairo/pattern.c
  9. +1 −0 cairo/private.h
  10. +21 −0 cairo/pycairo.h
  11. +56 −44 cairo/surface.c
@@ -1 +1 @@
from _cairo import *
from ._cairo import *
@@ -82,7 +82,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 Pycairo_IMPORT or import_cairo(), defined in pycairo.h.
*/
static Pycairo_CAPI_t CAPI = {
&PycairoContext_Type,
@@ -162,12 +162,12 @@ static Pycairo_CAPI_t CAPI = {

static PyObject *
pycairo_cairo_version (PyObject *self) {
return PyInt_FromLong (cairo_version());
return PYCAIRO_PyLong_FromLong (cairo_version());
}

static PyObject *
pycairo_cairo_version_string (PyObject *self) {
return PyString_FromString (cairo_version_string());
return PYCAIRO_PyUnicode_FromString (cairo_version_string());
}

static PyMethodDef cairo_functions[] = {
@@ -177,82 +177,120 @@ static PyMethodDef cairo_functions[] = {
{NULL, NULL, 0, NULL},
};

#if PY_MAJOR_VERSION >= 3
// Module initialization
struct cairo_state {
PyObject *ErrorObject;
};

#define GETSTATE(m) ((struct cairo_state*)PyModule_GetState(m))

static int
cairo_traverse(PyObject *m, visitproc v, void *arg)
{
return 0;
}

static int
cairo_clear(PyObject *m)
{
Py_CLEAR(GETSTATE(m)->ErrorObject);
return 0;
}

static struct PyModuleDef cairomodule = {
PyModuleDef_HEAD_INIT,
"cairo",
NULL,
sizeof(struct cairo_state),
cairo_functions,
0, /* m_reload */
cairo_traverse,
cairo_clear,
0, /* m_free - not needed, since all is done in m_clear */
};
#endif

DL_EXPORT(void)
init_cairo(void)
PYCAIRO_MOD_INIT(_cairo)
{
PyObject *m;

if (PyType_Ready(&PycairoContext_Type) < 0)
return;
return PYCAIRO_MOD_ERROR_VAL;
if (PyType_Ready(&PycairoFontFace_Type) < 0)
return;
return PYCAIRO_MOD_ERROR_VAL;
if (PyType_Ready(&PycairoToyFontFace_Type) < 0)
return;
return PYCAIRO_MOD_ERROR_VAL;
if (PyType_Ready(&PycairoFontOptions_Type) < 0)
return;
return PYCAIRO_MOD_ERROR_VAL;
if (PyType_Ready(&PycairoMatrix_Type) < 0)
return;
return PYCAIRO_MOD_ERROR_VAL;
if (PyType_Ready(&PycairoPath_Type) < 0)
return;
return PYCAIRO_MOD_ERROR_VAL;
PycairoPathiter_Type.tp_iter=&PyObject_SelfIter;
if (PyType_Ready(&PycairoPathiter_Type) < 0)
return;
return PYCAIRO_MOD_ERROR_VAL;

if (PyType_Ready(&PycairoPattern_Type) < 0)
return;
return PYCAIRO_MOD_ERROR_VAL;
if (PyType_Ready(&PycairoSolidPattern_Type) < 0)
return;
return PYCAIRO_MOD_ERROR_VAL;
if (PyType_Ready(&PycairoSurfacePattern_Type) < 0)
return;
return PYCAIRO_MOD_ERROR_VAL;
if (PyType_Ready(&PycairoGradient_Type) < 0)
return;
return PYCAIRO_MOD_ERROR_VAL;
if (PyType_Ready(&PycairoLinearGradient_Type) < 0)
return;
return PYCAIRO_MOD_ERROR_VAL;
if (PyType_Ready(&PycairoRadialGradient_Type) < 0)
return;
return PYCAIRO_MOD_ERROR_VAL;

if (PyType_Ready(&PycairoScaledFont_Type) < 0)
return;
return PYCAIRO_MOD_ERROR_VAL;

if (PyType_Ready(&PycairoSurface_Type) < 0)
return;
return PYCAIRO_MOD_ERROR_VAL;
#ifdef CAIRO_HAS_IMAGE_SURFACE
if (PyType_Ready(&PycairoImageSurface_Type) < 0)
return;
return PYCAIRO_MOD_ERROR_VAL;
#endif
#ifdef CAIRO_HAS_PDF_SURFACE
if (PyType_Ready(&PycairoPDFSurface_Type) < 0)
return;
return PYCAIRO_MOD_ERROR_VAL;
#endif
#ifdef CAIRO_HAS_PS_SURFACE
if (PyType_Ready(&PycairoPSSurface_Type) < 0)
return;
return PYCAIRO_MOD_ERROR_VAL;
#endif
#ifdef CAIRO_HAS_RECORDING_SURFACE
if (PyType_Ready(&PycairoRecordingSurface_Type) < 0)
return;
return PYCAIRO_MOD_ERROR_VAL;
#endif
#ifdef CAIRO_HAS_SVG_SURFACE
if (PyType_Ready(&PycairoSVGSurface_Type) < 0)
return;
return PYCAIRO_MOD_ERROR_VAL;
#endif
#ifdef CAIRO_HAS_WIN32_SURFACE
if (PyType_Ready(&PycairoWin32Surface_Type) < 0)
return;
return PYCAIRO_MOD_ERROR_VAL;
if (PyType_Ready(&PycairoWin32PrintingSurface_Type) < 0)
return;
return PYCAIRO_MOD_ERROR_VAL;
#endif
#ifdef CAIRO_HAS_XCB_SURFACE
if (PyType_Ready(&PycairoXCBSurface_Type) < 0)
return;
return PYCAIRO_MOD_ERROR_VAL;
#endif
#ifdef CAIRO_HAS_XLIB_SURFACE
if (PyType_Ready(&PycairoXlibSurface_Type) < 0)
return;
return PYCAIRO_MOD_ERROR_VAL;
#endif

#if PY_MAJOR_VERSION < 3
m = Py_InitModule("cairo._cairo", cairo_functions);
#else
m = PyModule_Create(&cairomodule);
#endif
if (m == NULL)
return PYCAIRO_MOD_ERROR_VAL;

PyModule_AddStringConstant(m, "version", VERSION);
PyModule_AddObject(m, "version_info",
@@ -347,6 +385,7 @@ init_cairo(void)
(PyObject *)&PycairoXlibSurface_Type);
#endif

#if PY_MAJOR_VERSION < 3
PyModule_AddObject(m, "CAPI", PyCObject_FromVoidPtr(&CAPI, NULL));

/* Add 'cairo.Error' to the module */
@@ -358,6 +397,13 @@ init_cairo(void)
Py_INCREF(CairoError);
if (PyModule_AddObject(m, "Error", CairoError) < 0)
return;
#else
GETSTATE(m)->ErrorObject = PyErr_NewException("cairo.Error", NULL, NULL);
if (GETSTATE(m)->ErrorObject == NULL) {
Py_DECREF(m);
return PYCAIRO_MOD_ERROR_VAL;
}
#endif

/* constants */
#if CAIRO_HAS_ATSUI_FONT
@@ -526,4 +572,14 @@ init_cairo(void)
CONSTANT(SUBPIXEL_ORDER_VRGB);
CONSTANT(SUBPIXEL_ORDER_VBGR);
#undef CONSTANT

#if PY_MAJOR_VERSION >= 3
/* Create a Capsule containing the CAPI pointer */
PyObject *T = PyCapsule_New((void *)(&CAPI), "cairo.CAPI", 0);
if (T != NULL) {
PyModule_AddObject(m, "CAPI", T);
}
#endif

return PYCAIRO_MOD_SUCCESS_VAL(m);
}
@@ -0,0 +1,79 @@
/* -*- mode: C; c-basic-offset: 2 -*-
*
* Pycairo - Python bindings for cairo
*
* Copyright © 2017 Christoph Reiter
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* You should have received a copy of the MPL along with this library
* in the file COPYING-MPL-1.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
* the specific language governing rights and limitations.
*/

#ifndef __PYCAIRO_COMPAT_H__
#define __PYCAIRO_COMPAT_H__

#ifndef PyVarObject_HEAD_INIT
#define PyVarObject_HEAD_INIT(type, size) \
PyObject_HEAD_INIT(type) size,
#endif

#if PY_MAJOR_VERSION < 3

#define PYCAIRO_MOD_ERROR_VAL
#define PYCAIRO_MOD_SUCCESS_VAL(val)
#define PYCAIRO_MOD_INIT(name) void init##name(void)

#define PYCAIRO_PyUnicode_FromString PyString_FromString
#define PYCAIRO_PyUnicode_Join _PyString_Join

#define PYCAIRO_PyBytes_AsStringAndSize PyString_AsStringAndSize

#define PYCAIRO_PyLong_FromLong PyInt_FromLong
#define PYCAIRO_PyLong_AsLong PyInt_AsLong

#define PYCAIRO_ENC_TEXT_FORMAT "et"
#define PYCAIRO_DATA_FORMAT "s"

#define PYCAIRO_PyFilenameBase_Type PyBaseString_Type

#else

#define PYCAIRO_MOD_ERROR_VAL NULL
#define PYCAIRO_MOD_SUCCESS_VAL(val) val
#define PYCAIRO_MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)

#define PYCAIRO_PyUnicode_FromString PyUnicode_FromString
#define PYCAIRO_PyUnicode_Join PyUnicode_Join

#define PYCAIRO_PyBytes_AsStringAndSize PyBytes_AsStringAndSize

#define PYCAIRO_PyLong_FromLong PyLong_FromLong
#define PYCAIRO_PyLong_AsLong PyLong_AsLong

#define PYCAIRO_ENC_TEXT_FORMAT "es"
#define PYCAIRO_DATA_FORMAT "y"

#define PYCAIRO_PyFilenameBase_Type PyUnicode_Type

#endif

#endif /* __PYCAIRO_COMPAT_H__ */

0 comments on commit 727470e

Please sign in to comment.
You can’t perform that action at this time.