Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
352 changes: 199 additions & 153 deletions cairo/cairomodule.c

Large diffs are not rendered by default.

30 changes: 21 additions & 9 deletions cairo/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -893,8 +893,15 @@ pycairo_set_dash (PycairoContext *o, PyObject *args) {
}

for (i = 0; i < num_dashes; i++) {
dashes[i] = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(py_dashes, i));
if (PyErr_Occurred()) {
PyObject *py_dash = PySequence_ITEM(py_dashes, i);
if (py_dash == NULL) {
PyMem_Free (dashes);
Py_DECREF(py_dashes);
return NULL;
}
dashes[i] = PyFloat_AsDouble(py_dash);
Py_DECREF(py_dash);
if (dashes[i] == -1.0 && PyErr_Occurred()) {
PyMem_Free (dashes);
Py_DECREF(py_dashes);
return NULL;
Expand Down Expand Up @@ -1328,9 +1335,12 @@ pycairo_show_text_glyphs (PycairoContext *o, PyObject *args) {
goto error;
}
for (i=0; i < glyphs_size; i++) {
py_item = PySequence_Fast_GET_ITEM (glyphs_seq, i);
if (py_item == 0 || _PyGlyph_AsGlyph (py_item, &(glyphs[i])) != 0)
goto error;
py_item = PySequence_ITEM (glyphs_seq, i);
if (py_item == NULL || _PyGlyph_AsGlyph (py_item, &(glyphs[i])) != 0) {
Py_XDECREF (py_item);
goto error;
}
Py_DECREF (py_item);
}
Py_CLEAR (glyphs_seq);

Expand All @@ -1348,10 +1358,12 @@ pycairo_show_text_glyphs (PycairoContext *o, PyObject *args) {
goto error;
}
for (i=0; i < clusters_size; i++) {
py_item = PySequence_Fast_GET_ITEM (clusters_seq, i);
if (py_item == NULL ||
_PyTextCluster_AsTextCluster (py_item, &(clusters[i])) != 0)
goto error;
py_item = PySequence_ITEM (clusters_seq, i);
if (py_item == NULL || _PyTextCluster_AsTextCluster (py_item, &(clusters[i])) != 0) {
Py_XDECREF (py_item);
goto error;
}
Py_DECREF (py_item);
}
Py_CLEAR (clusters_seq);

Expand Down
69 changes: 50 additions & 19 deletions cairo/enums.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,29 @@ enum_type_register_constant(PyTypeObject *type, const char* name, long value) {
PyObject *int_obj, *name_obj, *en;

/* Get/Create the int->name mapping */
value_map = PyDict_GetItemString(type->tp_dict, map_name);
if (PyDict_GetItemStringRef(type->tp_dict, map_name, &value_map) < 0) {
return NULL;
}
if (value_map == NULL) {
value_map = PyDict_New();
PyDict_SetItemString(type->tp_dict, map_name, value_map);
Py_DECREF(value_map);
if (value_map == NULL)
return NULL;
if (PyDict_SetItemString(type->tp_dict, map_name, value_map) < 0) {
Py_DECREF(value_map);
return NULL;
}
}

/* Add int->name pair to the mapping */
int_obj = PyLong_FromLong(value);
name_obj = PyUnicode_FromString (name);
if (PyDict_SetItem(value_map, int_obj, name_obj) < 0) {
Py_DECREF(value_map);
Py_DECREF(int_obj);
Py_DECREF(name_obj);
return NULL;
}
Py_DECREF(value_map);
Py_DECREF(int_obj);
Py_DECREF(name_obj);

Expand All @@ -112,29 +120,53 @@ enum_type_register_constant(PyTypeObject *type, const char* name, long value) {
return en;
}

/* If returns NULL no error is set */
static PyObject *
int_enum_get_name(PyObject *obj) {
/**
* Returns -1 on error with *result NULL, and 0 if successful.
* *result can be NULL if the name does not exist
*/
static int
int_enum_get_name(PyObject *obj, PyObject **result) {
PyObject *value_map, *name_obj;

value_map = PyDict_GetItemString(Py_TYPE(obj)->tp_dict, map_name);
if(value_map == NULL)
return NULL;
if (PyDict_GetItemStringRef(Py_TYPE(obj)->tp_dict, map_name, &value_map) < 0) {
*result = NULL;
return -1;
}
if (value_map == NULL) {
*result = NULL;
return 0;
}

name_obj = PyDict_GetItem(value_map, obj);
if(name_obj == NULL)
return NULL;
if (PyDict_GetItemRef(value_map, obj, &name_obj) < 0) {
Py_DECREF(value_map);
*result = NULL;
return -1;
}
Py_DECREF(value_map);

return PyUnicode_FromFormat ("%s.%s", Py_TYPE(obj)->tp_name,
PyUnicode_AsUTF8(name_obj));
if (name_obj == NULL) {
*result = NULL;
return 0;
}

*result = PyUnicode_FromFormat("%s.%S", Py_TYPE(obj)->tp_name, name_obj);
if (*result == NULL) {
Py_DECREF(name_obj);
return -1;
}
Py_DECREF(name_obj);

return 0;
}

static PyObject *
int_enum_repr(PyObject *obj)
{
PyObject *name_obj;

name_obj = int_enum_get_name(obj);
if (int_enum_get_name(obj, &name_obj) < 0) {
return NULL;
}
if(name_obj == NULL)
return PyLong_Type.tp_repr(obj);

Expand Down Expand Up @@ -219,8 +251,7 @@ init_enum_type (PyObject *module, const char *name, PyTypeObject *type) {
if (PyType_Ready(type) < 0)
return -1;

Py_INCREF(type);
if (PyModule_AddObject(module, name, (PyObject *)type) < 0)
if (PyModule_AddObjectRef(module, name, (PyObject *)type) < 0)
return -1;

return 0;
Expand All @@ -236,7 +267,7 @@ format_stride_for_width (PyObject *self, PyObject *args) {
return NULL;

value = PyLong_AsLong (self);
if (PyErr_Occurred())
if (value == -1 && PyErr_Occurred())
return NULL;
if (value > INT_MAX || value < INT_MIN) {
PyErr_SetString (PyExc_ValueError, "format value out of range");
Expand Down Expand Up @@ -274,7 +305,7 @@ init_enums (PyObject *module) {

#define CONSTANT(t, a, b) \
ev = enum_type_register_constant(&Pycairo_##t##_Type, #b, CAIRO_##a##_##b); \
if (ev == NULL || PyModule_AddObject(module, #a "_" #b, ev) < 0) \
if (ev == NULL || PyModule_Add(module, #a "_" #b, ev) < 0) \
return -1;

ENUM(Antialias);
Expand Down
8 changes: 2 additions & 6 deletions cairo/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,11 @@ init_error (PyObject *module) {

error = (PyObject*)&PycairoError_Type;

Py_INCREF(error);
if (PyModule_AddObject(module, "Error", error) < 0) {
Py_DECREF (error);
if (PyModule_AddObjectRef(module, "Error", error) < 0) {
return -1;
}

Py_INCREF(error);
if (PyModule_AddObject(module, "CairoError", error) < 0) {
Py_DECREF (error);
if (PyModule_AddObjectRef(module, "CairoError", error) < 0) {
return -1;
}

Expand Down
85 changes: 68 additions & 17 deletions cairo/glyph.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,52 @@ _PycairoGlyphs_AsGlyphs (PyObject *py_object, int *num_glyphs)
goto error;
}
for (i = 0, glyph = glyphs; i < *num_glyphs; i++, glyph++) {
long index;
PyObject *py_item = PySequence_Fast_GET_ITEM (py_glyphs, i);
unsigned long index;
double x, y;
PyObject *py_item = PySequence_ITEM (py_glyphs, i);
if (py_item == NULL)
goto error;
py_seq = PySequence_Fast (py_item, "glyph items must be a sequence");
Py_DECREF (py_item);
if (py_seq == NULL)
goto error;
if (PySequence_Fast_GET_SIZE (py_seq) != 3) {
PyErr_SetString (PyExc_ValueError,
"each glyph item must be an (i,x,y) sequence");
goto error;
}
index = PyLong_AsLong (PySequence_Fast_GET_ITEM (py_seq, 0));
if (PyErr_Occurred ())
PyObject *py_index = PySequence_ITEM (py_seq, 0);
if (py_index == NULL) {
goto error;
}
index = PyLong_AsUnsignedLong (py_index);
Py_DECREF (py_index);
if (index == (unsigned long)-1 && PyErr_Occurred ()) {
goto error;
glyph->index = (unsigned long)index;
glyph->x = PyFloat_AsDouble (PySequence_Fast_GET_ITEM (py_seq, 1));
glyph->y = PyFloat_AsDouble (PySequence_Fast_GET_ITEM (py_seq, 2));
if (PyErr_Occurred())
}
PyObject *py_x = PySequence_ITEM (py_seq, 1);
if (py_x == NULL) {
goto error;
}
x = PyFloat_AsDouble (py_x);
Py_DECREF (py_x);
if (x == 1.0 && PyErr_Occurred()) {
goto error;
}
PyObject *py_y = PySequence_ITEM (py_seq, 2);
if (py_y == NULL) {
goto error;
}
y = PyFloat_AsDouble (py_y);
Py_DECREF (py_y);
if (y == 1.0 && PyErr_Occurred()) {
goto error;
}

glyph->index = index;
glyph->x = x;
glyph->y = y;

Py_DECREF (py_seq);
}

Expand All @@ -99,23 +127,45 @@ _PycairoGlyphs_AsGlyphs (PyObject *py_object, int *num_glyphs)
/* 0 on success */
int
_PyGlyph_AsGlyph (PyObject *pyobj, cairo_glyph_t *glyph) {
long index;
unsigned long index;
double x, y;

if (!PyObject_TypeCheck (pyobj, &PycairoGlyph_Type)) {
PyErr_SetString (PyExc_TypeError, "item must be of type cairo.Glyph");
return -1;
}

index = PyLong_AsLong (PySequence_Fast_GET_ITEM (pyobj, 0));
if (PyErr_Occurred ())
PyObject *py_index = PySequence_ITEM (pyobj, 0);
if (py_index == NULL) {
return -1;
}
index = PyLong_AsUnsignedLong (py_index);
Py_DECREF (py_index);
if (index == (unsigned long)-1 && PyErr_Occurred ())
return -1;
PyObject *py_x = PySequence_ITEM (pyobj, 1);
if (py_x == NULL) {
return -1;
}
x = PyFloat_AsDouble (py_x);
Py_DECREF (py_x);
if (x == 1.0 && PyErr_Occurred()) {
return -1;
}
PyObject *py_y = PySequence_ITEM (pyobj, 2);
if (py_y == NULL) {
return -1;
if (index < 0) {
PyErr_SetString (PyExc_ValueError, "negative index");
}
y = PyFloat_AsDouble (py_y);
Py_DECREF (py_y);
if (y == 1.0 && PyErr_Occurred()) {
return -1;
}
glyph->index = (unsigned long) index;
glyph->x = PyFloat_AsDouble (PySequence_Fast_GET_ITEM (pyobj, 1));
glyph->y = PyFloat_AsDouble (PySequence_Fast_GET_ITEM (pyobj, 2));

glyph->index = index;
glyph->x = x;
glyph->y = y;

return 0;
}

Expand All @@ -131,7 +181,8 @@ glyph_new (PyTypeObject *type, PyObject *args, PyObject *kwds) {
KWDS, &pyindex, &x, &y))
return NULL;

if (_conv_pyobject_to_ulong (pyindex, &index) < 0)
index = PyLong_AsUnsignedLong (pyindex);
if (index == (unsigned long)-1 && PyErr_Occurred ())
return NULL;

tuple_args = Py_BuildValue ("((kdd))", index, x, y);
Expand Down
29 changes: 0 additions & 29 deletions cairo/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,32 +176,3 @@ Pycairo_richcompare (void* a, void *b, int op)
Py_INCREF (res);
return res;
}

/* NULL on error */
static PyObject *
_conv_pyobject_to_pylong (PyObject *pyobj) {
if (!PyLong_Check (pyobj)) {
PyErr_SetString (PyExc_TypeError, "not of type int");
return NULL;
}
Py_INCREF (pyobj);
return pyobj;
}

/* -1 on error */
int
_conv_pyobject_to_ulong (PyObject *pyobj, unsigned long *result) {
unsigned long temp;
PyObject *pylong;

pylong = _conv_pyobject_to_pylong (pyobj);
if (pylong == NULL)
return -1;

temp = PyLong_AsUnsignedLong (pylong);
if (PyErr_Occurred ())
return -1;

*result = temp;
return 0;
}
4 changes: 2 additions & 2 deletions cairo/private.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@

#include "py3cairo.h"

#include "pythoncapi_compat.h"

#define PYCAIRO_STRINGIFY(s) PYCAIRO_STRINGIFY_ARG(s)
#define PYCAIRO_STRINGIFY_ARG(s) #s

Expand All @@ -57,8 +59,6 @@ int _PyGlyph_AsGlyph (PyObject *pyobj, cairo_glyph_t *glyph);
int _PyTextCluster_AsTextCluster (PyObject *pyobj,
cairo_text_cluster_t *cluster);

int _conv_pyobject_to_ulong (PyObject *pyobj, unsigned long *result);

PyObject* Pycairo_richcompare (void* a, void *b, int op);

extern PyTypeObject PycairoContext_Type;
Expand Down
Loading
Loading