Python ctypes和librsvg出错
|
我正在尝试使用ctypes来包装librsvg的基本功能,但是我遇到了段错误。
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)
该行发生错误::2ѭ
关于这有什么问题的任何想法吗?
没有找到相关结果
已邀请:
2 个回复
填盖
在适当的地方。然后,它(也)可以在OSX中以32位或64位工作。 但是,我发现增强基本包装更为有用,以便处理从文件创建句柄时可能出现的错误。以下是执行此操作的基本包装。它还以几乎相同的方式复制了标准“ 5”绑定的基本用法。
用法示例可以在https://stackoverflow.com/a/14928770/1832154上看到。
妒垮
注意,我将两个参数包装在c_void_p中,使它们成为void *指针。不理想,但似乎可行。