Python ctypes 和 librsvg 的错误
我正在尝试用ctypes把librsvg的基本功能封装成Python可以用的,但我遇到了段错误(segfault)。
C语言代码:
// pycairo excerpt
typedef struct {
PyObject_HEAD
cairo_t *ctx;
PyObject *base; /* base object used to create context, or NULL */
} PycairoContext;
// librsvg excerpt
RsvgHandle * rsvg_handle_new_from_file (const gchar * file_name, GError ** error);
// ...
gboolean rsvg_handle_render_cairo (RsvgHandle * handle, cairo_t * cr);
Python中的ctypes代码:
from ctypes import *
from ctypes import util
librsvg = cdll.LoadLibrary('/brew/lib/librsvg-2.2.dylib')
libgobject = cdll.LoadLibrary('/brew/lib/libgobject-2.0.dylib')
libgobject.g_type_init()
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)
)
class RSVGHandle(object):
def __init__(self, path):
self.path = path
self.error = ''
self.handle = librsvg.rsvg_handle_new_from_file(self.path, self.error)
def render_cairo(self, context):
context.save()
z = PycairoContext.from_address(id(context))
librsvg.rsvg_handle_render_cairo(self.handle, z.ctx)
context.restore()
import cairo
h = RSVGHandle('bank.svg')
s = cairo.ImageSurface(cairo.FORMAT_ARGB32, 100, 100)
ctx = cairo.Context(s)
# segmentation fault....
h.render_cairo(ctx)
错误发生在这一行:librsvg.rsvg_handle_render_cairo(self.handle, z.ctx)
你知道这可能是什么问题吗?
2 个回答
3
librsvg.rsvg_handle_render_cairo这个函数需要的是指针,但它却收到了整数。具体情况我也不太清楚,不过这个修改至少不会导致程序崩溃。
试试这个
librsvg.rsvg_handle_render_cairo(c_void_p(self.handle), c_void_p(z.ctx))
注意我把两个参数用c_void_p包裹起来,这样它们就变成了void *指针。虽然这不是最理想的做法,但看起来是有效的。
6
问题在于返回类型的定义不明确;单独使用 c_void_p
来处理结果并不能解决这个问题。你需要在合适的地方放入
librsvg.rsvg_handle_new_from_file.restype = c_void_p
。这样在OSX上,无论是32位还是64位都能正常工作。
不过,我发现增强基本的封装来处理从文件创建句柄时可能出现的错误更有帮助。下面是一个基本的封装示例,它可以做到这一点。它在很大程度上与标准的 rsvg
绑定的基本用法相同。
from ctypes import CDLL, POINTER, Structure, byref, util
from ctypes import c_bool, c_byte, c_void_p, c_int, c_double, c_uint32, c_char_p
class _PycairoContext(Structure):
_fields_ = [("PyObject_HEAD", c_byte * object.__basicsize__),
("ctx", c_void_p),
("base", c_void_p)]
class _RsvgProps(Structure):
_fields_ = [("width", c_int), ("height", c_int),
("em", c_double), ("ex", c_double)]
class _GError(Structure):
_fields_ = [("domain", c_uint32), ("code", c_int), ("message", c_char_p)]
def _load_rsvg(rsvg_lib_path=None, gobject_lib_path=None):
if rsvg_lib_path is None:
rsvg_lib_path = util.find_library('rsvg-2')
if gobject_lib_path is None:
gobject_lib_path = util.find_library('gobject-2.0')
l = CDLL(rsvg_lib_path)
g = CDLL(gobject_lib_path)
g.g_type_init()
l.rsvg_handle_new_from_file.argtypes = [c_char_p, POINTER(POINTER(_GError))]
l.rsvg_handle_new_from_file.restype = c_void_p
l.rsvg_handle_render_cairo.argtypes = [c_void_p, c_void_p]
l.rsvg_handle_render_cairo.restype = c_bool
l.rsvg_handle_get_dimensions.argtypes = [c_void_p, POINTER(_RsvgProps)]
return l
_librsvg = _load_rsvg()
class Handle(object):
def __init__(self, path):
lib = _librsvg
err = POINTER(_GError)()
self.handle = lib.rsvg_handle_new_from_file(path.encode(), byref(err))
if self.handle is None:
gerr = err.contents
raise Exception(gerr.message)
self.props = _RsvgProps()
lib.rsvg_handle_get_dimensions(self.handle, byref(self.props))
def render_cairo(self, ctx):
"""Returns True is drawing succeeded."""
z = _PycairoContext.from_address(id(ctx))
return _librsvg.rsvg_handle_render_cairo(self.handle, z.ctx)
你可以在这里查看示例用法:https://stackoverflow.com/a/14928770/1832154。