From 9ef23582d893a4af3aef176d0747336f1c7aa482 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Mon, 20 Oct 2014 16:57:08 -0400 Subject: [PATCH 1/3] Fix the building of various examples in the docs --- lib/matplotlib/backends/backend_pdf.py | 2 +- lib/matplotlib/sankey.py | 6 +- src/py_converters.cpp | 134 +++++++++++-------------- 3 files changed, 60 insertions(+), 82 deletions(-) diff --git a/lib/matplotlib/backends/backend_pdf.py b/lib/matplotlib/backends/backend_pdf.py index 329cde787f92..da970286e0cb 100644 --- a/lib/matplotlib/backends/backend_pdf.py +++ b/lib/matplotlib/backends/backend_pdf.py @@ -1865,7 +1865,7 @@ def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None): if elt[0] == 'font': self.file.output(elt[1], elt[2], Op.selectfont) elif elt[0] == 'text': - curx, cury = mytrans.transform((elt[1], elt[2])) + curx, cury = mytrans.transform_point((elt[1], elt[2])) self._setup_textpos(curx, cury, angle, oldx, oldy) oldx, oldy = curx, cury if len(elt[3]) == 1: diff --git a/lib/matplotlib/sankey.py b/lib/matplotlib/sankey.py index 653d6f377966..d915fa034ba8 100755 --- a/lib/matplotlib/sankey.py +++ b/lib/matplotlib/sankey.py @@ -728,7 +728,7 @@ def _get_angle(a, r): if prior is None: if rotation != 0: # By default, none of this is needed. angles = [_get_angle(angle, rotation) for angle in angles] - rotate = Affine2D().rotate_deg(rotation * 90).transform_point + rotate = Affine2D().rotate_deg(rotation * 90).transform_affine tips = rotate(tips) label_locations = rotate(label_locations) vertices = rotate(vertices) @@ -737,10 +737,10 @@ def _get_angle(a, r): rotation = (self.diagrams[prior].angles[connect[0]] - angles[connect[1]]) angles = [_get_angle(angle, rotation) for angle in angles] - rotate = Affine2D().rotate_deg(rotation * 90).transform_point + rotate = Affine2D().rotate_deg(rotation * 90).transform_affine tips = rotate(tips) offset = self.diagrams[prior].tips[connect[0]] - tips[connect[1]] - translate = Affine2D().translate(*offset).transform_point + translate = Affine2D().translate(*offset).transform_affine tips = translate(tips) label_locations = translate(rotate(label_locations)) vertices = translate(rotate(vertices)) diff --git a/src/py_converters.cpp b/src/py_converters.cpp index 81cbb1e849a9..fb1e9de3a550 100644 --- a/src/py_converters.cpp +++ b/src/py_converters.cpp @@ -9,6 +9,47 @@ extern "C" { +static int convert_string_enum(PyObject *obj, const char *name, const char **names, int *values, int *result) +{ + PyObject *bytesobj; + char *str; + + if (obj == NULL || obj == Py_None) { + return 1; + } + + if (PyUnicode_Check(obj)) { + bytesobj = PyUnicode_AsASCIIString(obj); + if (bytesobj == NULL) { + return 0; + } + } else if (PyBytes_Check(obj)) { + Py_INCREF(obj); + bytesobj = obj; + } else { + PyErr_Format(PyExc_TypeError, "%s must be bytes or unicode", name); + return 0; + } + + str = PyBytes_AsString(bytesobj); + if (str == NULL) { + Py_DECREF(bytesobj); + return 0; + } + + for ( ; *names != NULL; names++, values++) { + if (strncmp(str, *names, 64) == 0) { + *result = *values; + Py_DECREF(bytesobj); + return 1; + } + } + + PyErr_Format(PyExc_ValueError, "invalid %s value", name); + Py_DECREF(bytesobj); + return 0; +} + int convert_from_method(PyObject *obj, const char *name, converter func, void *p) { PyObject *value; @@ -76,76 +117,29 @@ int convert_bool(PyObject *obj, void *p) int convert_cap(PyObject *capobj, void *capp) { - PyObject *capstrobj; - char *capstr; - agg::line_cap_e *cap = (agg::line_cap_e *)capp; - - if (capobj == NULL || capobj == Py_None) { - return 1; - } - - capstrobj = PyUnicode_AsASCIIString(capobj); - if (capstrobj == NULL) { - return 0; - } + const char *names[] = {"butt", "round", "projecting", NULL}; + int values[] = {agg::butt_cap, agg::round_cap, agg::square_cap}; + int result; - capstr = PyBytes_AsString(capstrobj); - if (capstr == NULL) { - Py_DECREF(capstrobj); + if (!convert_string_enum(capobj, "capstyle", names, values, &result)) { return 0; } - if (strncmp(capstr, "butt", 5) == 0) { - *cap = agg::butt_cap; - } else if (strncmp(capstr, "round", 6) == 0) { - *cap = agg::round_cap; - } else if (strncmp(capstr, "projecting", 11) == 0) { - *cap = agg::square_cap; - } else { - PyErr_Format(PyExc_ValueError, "Unknown capstyle '%s'", capstr); - Py_DECREF(capstrobj); - return 0; - } - - Py_DECREF(capstrobj); - + *(agg::line_cap_e *)capp = (agg::line_cap_e)result; return 1; } int convert_join(PyObject *joinobj, void *joinp) { - PyObject *joinstrobj; - char *joinstr; - agg::line_join_e *join = (agg::line_join_e *)joinp; - - if (joinobj == NULL || joinobj == Py_None) { - return 1; - } + const char *names[] = {"miter", "round", "bevel", NULL}; + int values[] = {agg::miter_join_revert, agg::round_join, agg::bevel_join}; + int result; - joinstrobj = PyUnicode_AsASCIIString(joinobj); - if (joinstrobj == NULL) { + if (!convert_string_enum(joinobj, "joinstyle", names, values, &result)) { return 0; } - joinstr = PyBytes_AsString(joinstrobj); - if (joinstr == NULL) { - Py_DECREF(joinstrobj); - return 0; - } - - if (strncmp(joinstr, "miter", 6) == 0) { - *join = agg::miter_join_revert; - } else if (strncmp(joinstr, "round", 6) == 0) { - *join = agg::round_join; - } else if (strncmp(joinstr, "bevel", 6) == 0) { - *join = agg::bevel_join; - } else { - PyErr_Format(PyExc_ValueError, "Unknown joinstyle '%s'", joinstr); - Py_DECREF(joinstrobj); - return 0; - } - - Py_DECREF(joinstrobj); + *(agg::line_join_e *)joinp = (agg::line_join_e)result; return 1; } @@ -497,32 +491,16 @@ int convert_gcagg(PyObject *pygc, void *gcp) int convert_offset_position(PyObject *obj, void *offsetp) { e_offset_position *offset = (e_offset_position *)offsetp; - PyObject *offsetstrobj; - char *offsetstr; + const char *names[] = {"data", NULL}; + int values[] = {OFFSET_POSITION_DATA}; + int result; *offset = OFFSET_POSITION_FIGURE; - if (obj == NULL || obj == Py_None) { - return 1; - } - - offsetstrobj = PyUnicode_AsASCIIString(obj); - if (offsetstrobj == NULL) { - return 0; + if (convert_string_enum(obj, "offset_position", names, values, &result)) { + *offset = (e_offset_position)result; } - offsetstr = PyBytes_AsString(offsetstrobj); - if (offsetstr == NULL) { - Py_DECREF(offsetstrobj); - return 0; - } - - if (strncmp(offsetstr, "data", 5) == 0) { - *offset = OFFSET_POSITION_DATA; - } - - Py_DECREF(offsetstrobj); - return 1; } From b248596e65ca30250ed73e31c535fe3b71126135 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Mon, 20 Oct 2014 17:28:29 -0400 Subject: [PATCH 2/3] Handle default in offset position correctly --- src/py_converters.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/py_converters.cpp b/src/py_converters.cpp index fb1e9de3a550..a843911bd874 100644 --- a/src/py_converters.cpp +++ b/src/py_converters.cpp @@ -499,6 +499,8 @@ int convert_offset_position(PyObject *obj, void *offsetp) if (convert_string_enum(obj, "offset_position", names, values, &result)) { *offset = (e_offset_position)result; + } else { + PyErr_Clear(); } return 1; From c0995e51e9c71a9b912e736075481319796694bd Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Tue, 21 Oct 2014 08:07:50 -0400 Subject: [PATCH 3/3] Fix one more failing doc example --- doc/pyplots/annotate_transform.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/pyplots/annotate_transform.py b/doc/pyplots/annotate_transform.py index 1d39970e3f58..c3abd2369852 100644 --- a/doc/pyplots/annotate_transform.py +++ b/doc/pyplots/annotate_transform.py @@ -11,7 +11,7 @@ ax.set_ylim(-1, 1) xdata, ydata = 5, 0 -xdisplay, ydisplay = ax.transData.transform((xdata, ydata)) +xdisplay, ydisplay = ax.transData.transform_point((xdata, ydata)) bbox = dict(boxstyle="round", fc="0.8") arrowprops = dict(