Skip to content

Commit d7390dd

Browse files
committed
Work around pybind11's segfault on MathTextParser deallocation.
1 parent d706c7a commit d7390dd

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

src/_mplcairo.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ namespace mplcairo {
1818

1919
using namespace pybind11::literals;
2020

21-
py::object GraphicsContextRenderer::mathtext_parser_{};
22-
2321
Region::Region(cairo_rectangle_int_t bbox, std::unique_ptr<uint8_t[]> buf) :
2422
bbox{bbox}, buf{std::move(buf)}
2523
{}
@@ -1235,7 +1233,8 @@ void GraphicsContextRenderer::draw_text(
12351233
}
12361234
auto const& ac = additional_context();
12371235
if (ismath) {
1238-
mathtext_parser_.attr("parse")(s, get_additional_state().dpi, prop)
1236+
py::module::import("mplcairo")
1237+
.attr("_mathtext_parse")(s, get_additional_state().dpi, prop)
12391238
.cast<MathtextBackend>()._draw(*this, x, y, angle);
12401239
} else {
12411240
// Need to set the current point (otherwise later texts will just follow,
@@ -1274,7 +1273,8 @@ GraphicsContextRenderer::get_text_width_height_descent(
12741273
if (ismath.cast<bool>()) {
12751274
// NOTE: Agg reports nonzero descents for seemingly zero-descent cases.
12761275
return
1277-
mathtext_parser_.attr("parse")(s, get_additional_state().dpi, prop)
1276+
py::module::import("mplcairo")
1277+
.attr("_mathtext_parse")(s, get_additional_state().dpi, prop)
12781278
.cast<MathtextBackend>().get_text_width_height_descent();
12791279
} else {
12801280
cairo_save(cr_);
@@ -1514,9 +1514,11 @@ PYBIND11_MODULE(_mplcairo, m)
15141514

15151515
detail::PIXEL_MARKER =
15161516
py::module::import("matplotlib.markers").attr("MarkerStyle")(",");
1517-
GraphicsContextRenderer::mathtext_parser_ =
1517+
// Making the mathtext parser live in a Python module works around
1518+
// FIXME[pybind11]'s failure to call the destructor (#1493).
1519+
py::module::import("mplcairo").attr("_mathtext_parse") =
15181520
py::module::import("matplotlib.mathtext")
1519-
.attr("MathTextParser")("mplcairo");
1521+
.attr("MathTextParser")("mplcairo").attr("parse");
15201522

15211523
py::module::import("atexit").attr("register")(
15221524
py::cpp_function{
@@ -1530,7 +1532,6 @@ PYBIND11_MODULE(_mplcairo, m)
15301532
// late in the shutdown sequence.)
15311533
detail::UNIT_CIRCLE = {};
15321534
detail::PIXEL_MARKER = {};
1533-
GraphicsContextRenderer::mathtext_parser_ = {};
15341535
}
15351536
}
15361537
);

src/_mplcairo.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ class GraphicsContextRenderer {
3535
AdditionalContext additional_context();
3636

3737
public:
38-
static py::object mathtext_parser_;
39-
4038
cairo_t* const cr_;
4139

4240
GraphicsContextRenderer(

0 commit comments

Comments
 (0)