0

I have a simple python ctypes wrapper around rsvg to be used with cairo. It seems to work on ubuntu 16 but when I try it on ubuntu 18 I get a Segmentation error. I have looked at the cairo docs for an example of how this can be done, so nothing fancy.(https://www.cairographics.org/cookbook/librsvgpython/).

from ctypes import CDLL, Structure, byref, c_byte, c_double, c_int, c_void_p
# CDLL: Instances of this class represent loaded shared libraries.
l = CDLL('librsvg-2.so.2')
class rsvgHandle():
    class RsvgDimensionData(Structure):
        _fields_ = [("width", c_int),
                    ("height", c_int),
                    ("em", c_double),
                    ("ex", c_double)]
    class PycairoContext(Structure):
        _fields_ = [("PyObject_HEAD", c_byte * object.__basicsize__),
                    ("ctx", c_void_p),
                    ("base", c_void_p)]
    def __init__(self, path):
        self.path = path
        error = ''
        self.handle = l.rsvg_handle_new_from_file(self.path, error)

    def get_dimension_data(self):
        svgDim = self.RsvgDimensionData()
        l.rsvg_handle_get_dimensions(self.handle, byref(svgDim))
        return (svgDim.width, svgDim.height)

    def render_cairo(self, ctx):
        ctx.save()
        z = self.PycairoContext.from_address(id(ctx))
        l.rsvg_handle_render_cairo(self.handle, z.ctx)
        ctx.restore()
class rsvgClass():
    def Handle(self, file):
        return rsvgHandle(file)

rsvg = rsvgClass()
svg_handle = rsvg.Handle(fpath)
svg_width, svg_height = svg_handle.get_dimension_data()[:2]
svg_handle.render_cairo(context)

The error occurs inside get_dimension_data at the line l.rsvg_handle_get_dimensions(self.handle, byref(svgDim))

The trace isn't too helpful it says Segmenation Fault and a bunch of these

/usr/lib/x86_64-linux-gnu/librsvg-2.so.2(rsvg_handle_get_dimensions+0) [0x7ff70b01ed50]

/usr/lib/x86_64-linux-gnu/libffi.so.6(ffi_call_unix64+0x4c) [0x7ff71f52adae]

/usr/lib/x86_64-linux-gnu/libffi.so.6(ffi_call+0x22f) [0x7ff71f52a71f]

/usr/lib/python2.7/lib-dynload/_ctypes.x86_64-linux-gnu.so(_ctypes_callproc+0x2a4) [0x7ff71f73dcc4]

/usr/lib/python2.7/lib-dynload/_ctypes.x86_64-linux-gnu.so(+0x106c5) [0x7ff71f73d6c5] /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x5bf6) [0x7ff7247eb366] /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x8b5b) [0x7ff7247ee2cb] /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyEval_EvalCodeEx+0x7d8) [0x7ff72492a908] /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x5bf6) [0x7ff7247eb366]

Does anyone know what could be going on in my ubuntu 18 environment to cause this issue ? Or alternatively how I could render an svg into an open cairo context (as shown in the last line of the code) on ubuntu 18 ? (the readymade python binding for rsvg is removed from ubuntu 18 btw which is why im trying ctypes to begin with)

morgen
  • 11
  • 1
  • Most likely the type of one or more of your struct fields went from int to long. – CJR Apr 28 '20 at 04:29
  • Do you mean that for some reason on ubuntu 18 a `long` would be required for `rsvg` instead of an `int` ? – morgen Apr 28 '20 at 04:34
  • Possible dupe of [\[SO\]: C function called from Python via ctypes returns incorrect value (@CristiFati's answer)](https://stackoverflow.com/questions/58610333/c-function-called-from-python-via-ctypes-returns-incorrect-value/58611011#58611011). – CristiFati Apr 28 '20 at 13:19

0 Answers0